Sound effects working.
This commit is contained in:
BIN
android/assets/data/gfx/textures/bckg.png
Normal file
BIN
android/assets/data/gfx/textures/bckg.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 345 KiB |
BIN
android/assets/data/sfx/BounceYoFrankie.ogg
Normal file
BIN
android/assets/data/sfx/BounceYoFrankie.ogg
Normal file
Binary file not shown.
BIN
android/assets/data/sfx/atari_boom.ogg
Normal file
BIN
android/assets/data/sfx/atari_boom.ogg
Normal file
Binary file not shown.
BIN
android/assets/data/sfx/oh_yeah_wav_cut.ogg
Normal file
BIN
android/assets/data/sfx/oh_yeah_wav_cut.ogg
Normal file
Binary file not shown.
@@ -20,10 +20,10 @@ import com.badlogic.ashley.core.ComponentMapper;
|
||||
public abstract class Mappers {
|
||||
public static final ComponentMapper<PositionComponent> positionMapper = ComponentMapper.getFor(PositionComponent.class);
|
||||
public static final ComponentMapper<VelocityComponent> velocityMapper = ComponentMapper.getFor(VelocityComponent.class);
|
||||
public static final ComponentMapper<SoundEffectComponent> soundEffectMapper = ComponentMapper.getFor(SoundEffectComponent.class);
|
||||
public static final ComponentMapper<TextureComponent> textureMapper = ComponentMapper.getFor(TextureComponent.class);
|
||||
public static final ComponentMapper<SpriteComponent> spriteMapper = ComponentMapper.getFor(SpriteComponent.class);
|
||||
public static final ComponentMapper<BoundingBoxComponent> bboxMapper = ComponentMapper.getFor(BoundingBoxComponent.class);
|
||||
public static final ComponentMapper<ScoreComponent> scoreMapper = ComponentMapper.getFor(ScoreComponent.class);
|
||||
public static final ComponentMapper<PlayerComponent> playerMapper = ComponentMapper.getFor(PlayerComponent.class);
|
||||
public static final ComponentMapper<SoundComponent> soundMapper = ComponentMapper.getFor(SoundComponent.class);
|
||||
}
|
||||
|
@@ -19,6 +19,9 @@ import com.badlogic.ashley.core.Component;
|
||||
import com.badlogic.gdx.utils.Pool.Poolable;
|
||||
|
||||
public class PlayerComponent extends Component implements Poolable {
|
||||
public static int HUMAN_PLAYER = 0;
|
||||
public static int COMPUTER_PLAYER = 1;
|
||||
|
||||
public int id = -1;
|
||||
|
||||
@Override
|
||||
|
@@ -0,0 +1,13 @@
|
||||
package com.gamejolt.mikykr5.poukemon.ecs.components;
|
||||
|
||||
import com.badlogic.ashley.core.Component;
|
||||
import com.badlogic.gdx.utils.Pool.Poolable;
|
||||
|
||||
public class SoundComponent extends Component implements Poolable {
|
||||
public String path = "";
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
this.path = "";
|
||||
}
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Miguel Angel Astor Romero
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Read the LICENSE file for more details.
|
||||
*/
|
||||
package com.gamejolt.mikykr5.poukemon.ecs.components;
|
||||
|
||||
import com.badlogic.ashley.core.Component;
|
||||
import com.badlogic.gdx.utils.Pool.Poolable;
|
||||
|
||||
public class SoundEffectComponent extends Component implements Poolable {
|
||||
int soundEffectID = -1;
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
soundEffectID = -1;
|
||||
}
|
||||
}
|
@@ -17,22 +17,31 @@ package com.gamejolt.mikykr5.poukemon.ecs.entities;
|
||||
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.ashley.core.PooledEngine;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.Sprite;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.gamejolt.mikykr5.poukemon.ProjectConstants;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.BoundingBoxComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.Mappers;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.PlayerComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.PositionComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.ScoreComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.SoundComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.SpriteComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.VelocityComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.utils.AsyncAssetLoader;
|
||||
import com.gamejolt.mikykr5.poukemon.utils.managers.CachedSoundManager;
|
||||
|
||||
public class PongEntityInitializer extends EntityInitializerBase {
|
||||
private AsyncAssetLoader loader;
|
||||
private Entity victorySound;
|
||||
private Entity defeatSound;
|
||||
private Entity ball;
|
||||
private Entity paddleUser;
|
||||
private Entity paddleComp;
|
||||
private Entity background;
|
||||
private boolean entitiesCreated;
|
||||
private boolean assetsLoaded;
|
||||
|
||||
@@ -44,17 +53,34 @@ public class PongEntityInitializer extends EntityInitializerBase {
|
||||
@Override
|
||||
public void createAllEntities(PooledEngine engine){
|
||||
loader = AsyncAssetLoader.getInstance();
|
||||
CachedSoundManager soundManager = CachedSoundManager.getInstance();
|
||||
|
||||
loader.addAssetToLoad("data/gfx/textures/pong_atlas.atlas", TextureAtlas.class);
|
||||
loader.addAssetToLoad("data/gfx/textures/bckg.png", Texture.class);
|
||||
soundManager.loadSound("data/sfx/BounceYoFrankie.ogg");
|
||||
soundManager.loadSound("data/sfx/oh_yeah_wav_cut.ogg");
|
||||
soundManager.loadSound("data/sfx/atari_boom.ogg");
|
||||
|
||||
victorySound = engine.createEntity();
|
||||
victorySound.add(engine.createComponent(SoundComponent.class));
|
||||
|
||||
defeatSound = engine.createEntity();
|
||||
defeatSound.add(engine.createComponent(SoundComponent.class));
|
||||
|
||||
background = engine.createEntity();
|
||||
background.add(engine.createComponent(PositionComponent.class));
|
||||
background.add(engine.createComponent(SpriteComponent.class));
|
||||
|
||||
ball = engine.createEntity();
|
||||
ball.add(engine.createComponent(PositionComponent.class));
|
||||
ball.add(engine.createComponent(VelocityComponent.class));
|
||||
ball.add(engine.createComponent(SpriteComponent.class));
|
||||
ball.add(engine.createComponent(BoundingBoxComponent.class));
|
||||
ball.add(engine.createComponent(SoundComponent.class));
|
||||
|
||||
paddleUser = engine.createEntity();
|
||||
paddleUser.add(engine.createComponent(PositionComponent.class));
|
||||
paddleUser.add(engine.createComponent(VelocityComponent.class));
|
||||
paddleUser.add(engine.createComponent(SpriteComponent.class));
|
||||
paddleUser.add(engine.createComponent(BoundingBoxComponent.class));
|
||||
paddleUser.add(engine.createComponent(ScoreComponent.class));
|
||||
@@ -68,6 +94,9 @@ public class PongEntityInitializer extends EntityInitializerBase {
|
||||
paddleComp.add(engine.createComponent(ScoreComponent.class));
|
||||
paddleComp.add(engine.createComponent(PlayerComponent.class));
|
||||
|
||||
engine.addEntity(victorySound);
|
||||
engine.addEntity(defeatSound);
|
||||
engine.addEntity(background);
|
||||
engine.addEntity(ball);
|
||||
engine.addEntity(paddleUser);
|
||||
engine.addEntity(paddleComp);
|
||||
@@ -80,22 +109,31 @@ public class PongEntityInitializer extends EntityInitializerBase {
|
||||
if(!entitiesCreated)
|
||||
throw new IllegalStateException("Entities have not been created before setting assets.");
|
||||
|
||||
Vector2 randomVector = new Vector2().set(Vector2.X).setAngle(MathUtils.random(0, 360));
|
||||
TextureAtlas atlas = loader.getAsset("data/gfx/textures/pong_atlas.atlas", TextureAtlas.class);
|
||||
Texture bckg = loader.getAsset("data/gfx/textures/bckg.png", Texture.class);
|
||||
|
||||
Mappers.soundMapper.get(victorySound).path = "data/sfx/oh_yeah_wav_cut.ogg";
|
||||
Mappers.soundMapper.get(defeatSound).path = "data/sfx/atari_boom.ogg";
|
||||
|
||||
Mappers.spriteMapper.get(background).sprite = new Sprite(bckg);
|
||||
Mappers.positionMapper.get(background).setXY(-(ProjectConstants.FB_WIDTH / 2.0f), -(ProjectConstants.FB_HEIGHT / 2.0f));
|
||||
|
||||
Mappers.spriteMapper.get(ball).sprite = atlas.createSprite("ball");
|
||||
Mappers.positionMapper.get(ball).setXY(-(Mappers.spriteMapper.get(ball).sprite.getWidth() / 2), -(Mappers.spriteMapper.get(ball).sprite.getHeight() / 2));
|
||||
Mappers.velocityMapper.get(ball).setXY(173, -475);
|
||||
Mappers.velocityMapper.get(ball).setXY(randomVector.x * 475.0f, randomVector.y * 475.0f);
|
||||
Mappers.bboxMapper.get(ball).bbox.set(Mappers.spriteMapper.get(ball).sprite.getBoundingRectangle());
|
||||
Mappers.soundMapper.get(ball).path = "data/sfx/BounceYoFrankie.ogg";
|
||||
|
||||
Mappers.spriteMapper.get(paddleUser).sprite = atlas.createSprite("glasspaddle2");
|
||||
Mappers.positionMapper.get(paddleUser).setXY(-(ProjectConstants.FB_WIDTH / 2) + 100, -(Mappers.spriteMapper.get(paddleUser).sprite.getHeight() / 2));
|
||||
Mappers.bboxMapper.get(paddleUser).bbox.set(Mappers.spriteMapper.get(paddleUser).sprite.getBoundingRectangle());
|
||||
Mappers.playerMapper.get(paddleUser).id = 0;
|
||||
Mappers.playerMapper.get(paddleUser).id = PlayerComponent.HUMAN_PLAYER;
|
||||
|
||||
Mappers.spriteMapper.get(paddleComp).sprite = atlas.createSprite("paddle");
|
||||
Mappers.positionMapper.get(paddleComp).setXY(((ProjectConstants.FB_WIDTH / 2) - 1) - 100, -(Mappers.spriteMapper.get(paddleComp).sprite.getHeight() / 2));
|
||||
Mappers.positionMapper.get(paddleComp).setXY(((ProjectConstants.FB_WIDTH / 2) - 1) - 100 - Mappers.spriteMapper.get(paddleComp).sprite.getWidth(), -(Mappers.spriteMapper.get(paddleComp).sprite.getHeight() / 2));
|
||||
Mappers.bboxMapper.get(paddleComp).bbox.set(Mappers.spriteMapper.get(paddleComp).sprite.getBoundingRectangle());
|
||||
Mappers.playerMapper.get(paddleComp).id = 1;
|
||||
Mappers.playerMapper.get(paddleComp).id = PlayerComponent.COMPUTER_PLAYER;
|
||||
|
||||
AsyncAssetLoader.freeInstance();
|
||||
assetsLoaded = true;
|
||||
@@ -108,5 +146,7 @@ public class PongEntityInitializer extends EntityInitializerBase {
|
||||
|
||||
if(!assetsLoaded)
|
||||
throw new IllegalStateException("Assets have not been loaded before disposing.");
|
||||
|
||||
CachedSoundManager.freeInstance();
|
||||
}
|
||||
}
|
||||
|
@@ -15,15 +15,20 @@
|
||||
*/
|
||||
package com.gamejolt.mikykr5.poukemon.ecs.systems;
|
||||
|
||||
import com.badlogic.ashley.core.ComponentType;
|
||||
import com.badlogic.ashley.core.Engine;
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.ashley.systems.IteratingSystem;
|
||||
import com.badlogic.ashley.utils.ImmutableArray;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.gamejolt.mikykr5.poukemon.ProjectConstants;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.BoundingBoxComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.Mappers;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.PlayerComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.PositionComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.SoundComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.SpriteComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.VelocityComponent;
|
||||
|
||||
@@ -33,10 +38,12 @@ public class CollisionDetectionSystem extends IteratingSystem {
|
||||
private final float screenRightBorder;
|
||||
private final float screenTopBorder;
|
||||
private final float screenBottomBorder;
|
||||
private final Vector2 randomVector = new Vector2();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public CollisionDetectionSystem(Engine engine){
|
||||
super(Family.getFor(PositionComponent.class, BoundingBoxComponent.class, VelocityComponent.class));
|
||||
//super(Family.getFor(PositionComponent.class, BoundingBoxComponent.class, VelocityComponent.class));
|
||||
super(Family.getFor(ComponentType.getBitsFor(PositionComponent.class, BoundingBoxComponent.class, VelocityComponent.class), ComponentType.getBitsFor(), ComponentType.getBitsFor(PlayerComponent.class)));
|
||||
|
||||
collidables = engine.getEntitiesFor(Family.getFor(BoundingBoxComponent.class));
|
||||
screenLeftBorder = -((float)ProjectConstants.FB_WIDTH / 2.0f);
|
||||
@@ -51,6 +58,7 @@ public class CollisionDetectionSystem extends IteratingSystem {
|
||||
PositionComponent position = Mappers.positionMapper.get(entity);
|
||||
BoundingBoxComponent bounds = Mappers.bboxMapper.get(entity);
|
||||
VelocityComponent velocity = Mappers.velocityMapper.get(entity);
|
||||
SoundComponent sound = Mappers.soundMapper.get(entity);
|
||||
|
||||
// Check if this entity is within the screen.
|
||||
// If the entity collides with any of the borders then bounce or score as needed.
|
||||
@@ -62,6 +70,10 @@ public class CollisionDetectionSystem extends IteratingSystem {
|
||||
message = new InterSystemMessage(ScoringSystem.class.getCanonicalName());
|
||||
message.data.put("SCORE", 1);
|
||||
InterSystemMessagingQueue.pushMessage(message);
|
||||
|
||||
message = new InterSystemMessage(SoundSystem.class.getCanonicalName());
|
||||
message.data.put("PLAY", "data/sfx/atari_boom.ogg");
|
||||
InterSystemMessagingQueue.pushMessage(message);
|
||||
}
|
||||
|
||||
if(position.x + bounds.bbox.getWidth() >= screenRightBorder){
|
||||
@@ -72,18 +84,32 @@ public class CollisionDetectionSystem extends IteratingSystem {
|
||||
message = new InterSystemMessage(ScoringSystem.class.getCanonicalName());
|
||||
message.data.put("SCORE", 0);
|
||||
InterSystemMessagingQueue.pushMessage(message);
|
||||
|
||||
message = new InterSystemMessage(SoundSystem.class.getCanonicalName());
|
||||
message.data.put("PLAY", "data/sfx/oh_yeah_wav_cut.ogg");
|
||||
InterSystemMessagingQueue.pushMessage(message);
|
||||
}
|
||||
|
||||
if(position.y < screenBottomBorder){
|
||||
position.y = screenBottomBorder;
|
||||
velocity.vy = velocity.vy < 0.0f ? -velocity.vy : velocity.vy;
|
||||
accelerate(velocity);
|
||||
if(sound != null){
|
||||
message = new InterSystemMessage(SoundSystem.class.getCanonicalName());
|
||||
message.data.put("PLAY", sound.path);
|
||||
InterSystemMessagingQueue.pushMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
if(position.y + bounds.bbox.getHeight() >= screenTopBorder){
|
||||
position.y = screenTopBorder - bounds.bbox.getHeight();
|
||||
velocity.vy = velocity.vy > 0.0f ? -velocity.vy : velocity.vy;
|
||||
accelerate(velocity);
|
||||
if(sound != null){
|
||||
message = new InterSystemMessage(SoundSystem.class.getCanonicalName());
|
||||
message.data.put("PLAY", sound.path);
|
||||
InterSystemMessagingQueue.pushMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < collidables.size(); i++){
|
||||
@@ -108,7 +134,8 @@ public class CollisionDetectionSystem extends IteratingSystem {
|
||||
SpriteComponent sprite = Mappers.spriteMapper.get(entity);
|
||||
VelocityComponent velocity = Mappers.velocityMapper.get(entity);
|
||||
|
||||
velocity.setXY(173, -475);
|
||||
randomVector.set(Vector2.X).setAngle(MathUtils.random(0, 360));
|
||||
velocity.setXY(randomVector.x * -475, randomVector.y * 475);
|
||||
|
||||
if(position != null){
|
||||
if(sprite != null){
|
||||
|
@@ -18,15 +18,23 @@ package com.gamejolt.mikykr5.poukemon.ecs.systems;
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.ashley.systems.IteratingSystem;
|
||||
import com.gamejolt.mikykr5.poukemon.ProjectConstants;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.BoundingBoxComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.Mappers;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.PlayerComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.PositionComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.VelocityComponent;
|
||||
|
||||
public class ComputerPlayerPositioningSystem extends IteratingSystem {
|
||||
private final float screenTopBorder;
|
||||
private final float screenBottomBorder;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ComputerPlayerPositioningSystem() {
|
||||
super(Family.getFor(PlayerComponent.class, VelocityComponent.class, PositionComponent.class));
|
||||
super(Family.getFor(PlayerComponent.class, VelocityComponent.class, PositionComponent.class, BoundingBoxComponent.class));
|
||||
|
||||
screenTopBorder = ((float)ProjectConstants.FB_HEIGHT / 2.0f) - 1.0f;
|
||||
screenBottomBorder = -((float)ProjectConstants.FB_HEIGHT / 2.0f);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -35,19 +43,26 @@ public class ComputerPlayerPositioningSystem extends IteratingSystem {
|
||||
VelocityComponent velocity = Mappers.velocityMapper.get(entity);
|
||||
PositionComponent position = Mappers.positionMapper.get(entity);
|
||||
PlayerComponent player = Mappers.playerMapper.get(entity);
|
||||
BoundingBoxComponent bounds = Mappers.bboxMapper.get(entity);
|
||||
|
||||
if(player.id == 1){
|
||||
if(player.id == PlayerComponent.COMPUTER_PLAYER){
|
||||
while((message = InterSystemMessagingQueue.popMessage(ComputerPlayerPositioningSystem.class.getCanonicalName())) != null){
|
||||
float ballY;
|
||||
|
||||
if(message.data.containsKey("BALL_Y")){
|
||||
ballY = (Float) message.data.get("BALL_Y");
|
||||
|
||||
if(ballY > position.y){
|
||||
if(ballY > position.y + (bounds.bbox.height / 2.0f)){
|
||||
velocity.vy = 550.0f;
|
||||
}else if (ballY < position.y){
|
||||
}else if (ballY < position.y + (bounds.bbox.height / 2.0f)){
|
||||
velocity.vy = -550.0f;
|
||||
}
|
||||
|
||||
if(position.y < screenBottomBorder)
|
||||
position.y = screenBottomBorder;
|
||||
|
||||
if(position.y + bounds.bbox.getHeight() >= screenTopBorder)
|
||||
position.y = screenTopBorder - bounds.bbox.getHeight();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -18,14 +18,22 @@ package com.gamejolt.mikykr5.poukemon.ecs.systems;
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.ashley.systems.IteratingSystem;
|
||||
import com.gamejolt.mikykr5.poukemon.ProjectConstants;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.BoundingBoxComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.Mappers;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.PlayerComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.PositionComponent;
|
||||
|
||||
public class HumanPlayerPositioningSystem extends IteratingSystem {
|
||||
private final float screenTopBorder;
|
||||
private final float screenBottomBorder;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public HumanPlayerPositioningSystem() {
|
||||
super(Family.getFor(PlayerComponent.class, PositionComponent.class));
|
||||
super(Family.getFor(PlayerComponent.class, PositionComponent.class, BoundingBoxComponent.class));
|
||||
|
||||
screenTopBorder = ((float)ProjectConstants.FB_HEIGHT / 2.0f) - 1.0f;
|
||||
screenBottomBorder = -((float)ProjectConstants.FB_HEIGHT / 2.0f);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,14 +41,21 @@ public class HumanPlayerPositioningSystem extends IteratingSystem {
|
||||
InterSystemMessage message;
|
||||
PositionComponent position = Mappers.positionMapper.get(entity);
|
||||
PlayerComponent player = Mappers.playerMapper.get(entity);
|
||||
BoundingBoxComponent bounds = Mappers.bboxMapper.get(entity);
|
||||
|
||||
if(player.id == 0){
|
||||
if(player.id == PlayerComponent.HUMAN_PLAYER){
|
||||
while((message = InterSystemMessagingQueue.popMessage(HumanPlayerPositioningSystem.class.getCanonicalName())) != null){
|
||||
float playerY;
|
||||
|
||||
if(message.data.containsKey("INPUT_Y")){
|
||||
playerY = (Float) message.data.get("INPUT_Y");
|
||||
position.y = playerY;
|
||||
|
||||
if(position.y < screenBottomBorder)
|
||||
position.y = screenBottomBorder;
|
||||
|
||||
if(position.y + bounds.bbox.getHeight() >= screenTopBorder)
|
||||
position.y = screenTopBorder - bounds.bbox.getHeight();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package com.gamejolt.mikykr5.poukemon.ecs.systems;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.ashley.systems.IteratingSystem;
|
||||
@@ -31,6 +34,7 @@ import com.gamejolt.mikykr5.poukemon.utils.managers.CachedFontManager;
|
||||
public class ScoringSystem extends IteratingSystem {
|
||||
private final SpriteBatch batch;
|
||||
private BitmapFont font;
|
||||
private Queue<InterSystemMessage> ignoredMessages;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ScoringSystem(final SpriteBatch batch){
|
||||
@@ -38,6 +42,7 @@ public class ScoringSystem extends IteratingSystem {
|
||||
this.batch = batch;
|
||||
this.font = CachedFontManager.getInstance().loadFont("data/fonts/CRYSTAL-Regular.ttf", 180);
|
||||
CachedFontManager.freeInstance();
|
||||
ignoredMessages = new LinkedList<InterSystemMessage>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -58,14 +63,16 @@ public class ScoringSystem extends IteratingSystem {
|
||||
if(playerId == player.id){
|
||||
score.score++;
|
||||
}else{
|
||||
InterSystemMessagingQueue.pushMessage(message);
|
||||
break;
|
||||
ignoredMessages.add(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(InterSystemMessage msg : ignoredMessages)
|
||||
InterSystemMessagingQueue.pushMessage(msg);
|
||||
ignoredMessages.clear();
|
||||
|
||||
bounds = font.getBounds(String.format("%02d", score.score));
|
||||
|
||||
y = (ProjectConstants.FB_HEIGHT / 2.0f) - (bounds.height / 2.0f) - 20;
|
||||
if(player.id == 0){
|
||||
x = -(ProjectConstants.FB_WIDTH / 4.0f) - (bounds.width / 2.0f);
|
||||
|
@@ -0,0 +1,48 @@
|
||||
package com.gamejolt.mikykr5.poukemon.ecs.systems;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.ashley.systems.IteratingSystem;
|
||||
import com.badlogic.gdx.audio.Sound;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.Mappers;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.SoundComponent;
|
||||
import com.gamejolt.mikykr5.poukemon.utils.managers.CachedSoundManager;
|
||||
|
||||
public class SoundSystem extends IteratingSystem {
|
||||
private Queue<InterSystemMessage> ignoredMessages;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public SoundSystem() {
|
||||
super(Family.getFor(SoundComponent.class));
|
||||
ignoredMessages = new LinkedList<InterSystemMessage>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processEntity(Entity entity, float deltaTime) {
|
||||
InterSystemMessage message;
|
||||
String path;
|
||||
Sound sfx;
|
||||
SoundComponent sound = Mappers.soundMapper.get(entity);
|
||||
|
||||
while((message = InterSystemMessagingQueue.popMessage(SoundSystem.class.getCanonicalName())) != null){
|
||||
if(message.data.containsKey("PLAY")){
|
||||
path = (String) message.data.get("PLAY");
|
||||
|
||||
if(sound.path.compareTo(path) == 0){
|
||||
sfx = CachedSoundManager.getInstance().loadSound(path);
|
||||
sfx.play();
|
||||
CachedSoundManager.freeInstance();
|
||||
}else{
|
||||
ignoredMessages.add(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(InterSystemMessage msg : ignoredMessages)
|
||||
InterSystemMessagingQueue.pushMessage(msg);
|
||||
ignoredMessages.clear();
|
||||
}
|
||||
}
|
@@ -22,9 +22,8 @@ import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.Pixmap.Format;
|
||||
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.gamejolt.mikykr5.poukemon.GameCore;
|
||||
import com.gamejolt.mikykr5.poukemon.GameCore.game_states_t;
|
||||
import com.gamejolt.mikykr5.poukemon.ProjectConstants;
|
||||
@@ -38,6 +37,7 @@ import com.gamejolt.mikykr5.poukemon.ecs.systems.InterSystemMessagingQueue;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.systems.PositioningSystem;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.systems.RenderingSystem;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.systems.ScoringSystem;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.systems.SoundSystem;
|
||||
import com.gamejolt.mikykr5.poukemon.interfaces.AssetsLoadedListener;
|
||||
|
||||
public class InGameState extends BaseState implements AssetsLoadedListener{
|
||||
@@ -50,7 +50,6 @@ public class InGameState extends BaseState implements AssetsLoadedListener{
|
||||
private boolean assetsLoaded;
|
||||
private OrthographicCamera fbCamera;
|
||||
private Rectangle fbBounds;
|
||||
private Vector2 fbWoorldCoords;
|
||||
|
||||
public InGameState(final GameCore core) throws IllegalArgumentException{
|
||||
super(core);
|
||||
@@ -63,19 +62,19 @@ public class InGameState extends BaseState implements AssetsLoadedListener{
|
||||
oldRatio = aspectRatio(ProjectConstants.FB_WIDTH, ProjectConstants.FB_HEIGHT);
|
||||
assetsLoaded = false;
|
||||
fbCamera = new OrthographicCamera(ProjectConstants.FB_WIDTH, ProjectConstants.FB_HEIGHT);
|
||||
fbWoorldCoords = new Vector2();
|
||||
|
||||
// Create all entities.
|
||||
entityInitializer = new PongEntityInitializer();
|
||||
entityInitializer.createAllEntities(engine);
|
||||
|
||||
// Add systems in the order they will be processed.
|
||||
engine.addSystem(new PositioningSystem());
|
||||
engine.addSystem(new CollisionDetectionSystem(engine));
|
||||
engine.addSystem(new HumanPlayerPositioningSystem());
|
||||
engine.addSystem(new ComputerPlayerPositioningSystem());
|
||||
engine.addSystem(new ScoringSystem(core.batch));
|
||||
engine.addSystem(new PositioningSystem());
|
||||
engine.addSystem(new CollisionDetectionSystem(engine));
|
||||
engine.addSystem(new SoundSystem());
|
||||
engine.addSystem(new RenderingSystem(core.batch));
|
||||
engine.addSystem(new ScoringSystem(core.batch));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -147,7 +146,7 @@ public class InGameState extends BaseState implements AssetsLoadedListener{
|
||||
|
||||
if(touchInsideFrameBuffer(screenX, screenY)){
|
||||
message = new InterSystemMessage(HumanPlayerPositioningSystem.class.getCanonicalName());
|
||||
message.data.put("INPUT_Y", convertWorldHeightToFrameBufferHeight(touchPointWorldCoords.y));
|
||||
message.data.put("INPUT_Y", convertWorldYToFrameBufferY(screenY));
|
||||
InterSystemMessagingQueue.pushMessage(message);
|
||||
}
|
||||
|
||||
@@ -160,7 +159,7 @@ public class InGameState extends BaseState implements AssetsLoadedListener{
|
||||
|
||||
if(touchInsideFrameBuffer(screenX, screenY)){
|
||||
message = new InterSystemMessage(HumanPlayerPositioningSystem.class.getCanonicalName());
|
||||
message.data.put("INPUT_Y", convertWorldHeightToFrameBufferHeight(touchPointWorldCoords.y));
|
||||
message.data.put("INPUT_Y", convertWorldYToFrameBufferY(screenY));
|
||||
InterSystemMessagingQueue.pushMessage(message);
|
||||
}
|
||||
|
||||
@@ -188,15 +187,16 @@ public class InGameState extends BaseState implements AssetsLoadedListener{
|
||||
}
|
||||
}
|
||||
|
||||
private float convertWorldHeightToFrameBufferHeight(float height){
|
||||
float newHeight, oldHeight, b = (float)ProjectConstants.FB_HEIGHT / (float)h;
|
||||
private float convertWorldYToFrameBufferY(float y){
|
||||
Vector3 vec3 = new Vector3(0, y - Math.abs(fbBounds.y - (-(h/2.0f))), 0);
|
||||
|
||||
oldHeight = height + ((ProjectConstants.FB_HEIGHT / 2.0f) - 1.0f);
|
||||
oldHeight /= (float)h;
|
||||
newHeight = (oldHeight * b) * ProjectConstants.FB_HEIGHT;
|
||||
newHeight -= ProjectConstants.FB_HEIGHT;
|
||||
Gdx.app.log("IN_GAME", "Y before: " + Float.toString(vec3.y));
|
||||
|
||||
return newHeight;
|
||||
fbCamera.unproject(vec3, 0, 0, w, h / oldRatio);
|
||||
|
||||
Gdx.app.log("IN_GAME", "Y after: " + Float.toString(vec3.y));
|
||||
|
||||
return vec3.y;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -58,13 +58,13 @@ public class CachedFontManager{
|
||||
}
|
||||
|
||||
public BitmapFont loadFont(String path, int size){
|
||||
if(fonts.containsKey(path))
|
||||
return fonts.get(path);
|
||||
|
||||
FreeTypeFontGenerator fontGenerator;
|
||||
FreeTypeFontParameter fontParameters;
|
||||
BitmapFont font;
|
||||
|
||||
if(fonts.containsKey(path))
|
||||
return fonts.get(path);
|
||||
|
||||
fontParameters = new FreeTypeFontParameter();
|
||||
fontParameters.characters = FONT_CHARS;
|
||||
fontParameters.size = size;
|
||||
@@ -72,7 +72,6 @@ public class CachedFontManager{
|
||||
|
||||
fontGenerator = new FreeTypeFontGenerator(Gdx.files.internal(path));
|
||||
font = fontGenerator.generateFont(fontParameters);
|
||||
|
||||
fonts.put(path, font);
|
||||
|
||||
fontGenerator.dispose();
|
||||
@@ -92,7 +91,6 @@ public class CachedFontManager{
|
||||
|
||||
for(BitmapFont font : fonts.values())
|
||||
font.dispose();
|
||||
|
||||
fonts.clear();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Miguel Angel Astor Romero
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Read the LICENSE file for more details.
|
||||
*/
|
||||
package com.gamejolt.mikykr5.poukemon.utils.managers;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.audio.Sound;
|
||||
|
||||
public class CachedSoundManager {
|
||||
private Map<String, Sound> sounds;
|
||||
|
||||
private CachedSoundManager(){
|
||||
sounds = new HashMap<String, Sound>();
|
||||
}
|
||||
|
||||
private static final class SingletonHolder{
|
||||
public static int REF_COUNT = 0;
|
||||
public static CachedSoundManager INSTANCE;
|
||||
}
|
||||
|
||||
public static CachedSoundManager getInstance(){
|
||||
if(SingletonHolder.REF_COUNT == 0)
|
||||
SingletonHolder.INSTANCE = new CachedSoundManager();
|
||||
SingletonHolder.REF_COUNT++;
|
||||
return SingletonHolder.INSTANCE;
|
||||
}
|
||||
|
||||
public static void freeInstance(){
|
||||
SingletonHolder.REF_COUNT--;
|
||||
if(SingletonHolder.REF_COUNT <= 0){
|
||||
SingletonHolder.INSTANCE.dispose();
|
||||
SingletonHolder.INSTANCE = null;
|
||||
}
|
||||
}
|
||||
|
||||
public Sound loadSound(String path){
|
||||
if(sounds.containsKey(path))
|
||||
return sounds.get(path);
|
||||
|
||||
Sound s = Gdx.audio.newSound(Gdx.files.internal(path));
|
||||
sounds.put(path, s);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public void unloadSound(String path){
|
||||
if(sounds.containsKey(path)){
|
||||
sounds.get(path).dispose();
|
||||
sounds.remove(path);
|
||||
}
|
||||
}
|
||||
|
||||
private void dispose(){
|
||||
Gdx.app.log("SOUND_MANAGER", "Disposing sounds.");
|
||||
|
||||
for(Sound sound : sounds.values())
|
||||
sound.dispose();
|
||||
sounds.clear();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user