Add GameDataComponent which holds current game's data (score, lives, etc)

Now more like an actual game. Score, lives, and when you die, you get
respawned. If you die rnough times, you Game Over, and your lives and
score are reset. Each time you get 10000 points, you gain a life!

Add GameMode enum
Add GameDataSystem which manipulates the current game data based on events.
Add GameDataRenderSystem which renders the current game data.

Add AsteroidHitEvent and PlayerDeathEvent which are are fired when an
asteroid is destroyed or the player is killed, respectively.

Add SpriteBatch to Graphics
This commit is contained in:
2020-01-30 23:56:00 +04:00
parent c3b806f79f
commit fa809148a7
9 changed files with 274 additions and 5 deletions

View File

@ -0,0 +1,102 @@
package com.me.asteroids.systems;
import com.me.asteroids.Constants;
import com.me.asteroids.GameMode;
import com.me.asteroids.components.GameDataComponent;
import com.me.asteroids.components.PositionComponent;
import com.me.asteroids.components.VelocityComponent;
import com.me.asteroids.events.AsteroidHitEvent;
import com.me.asteroids.events.PlayerDeathEvent;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem;
import com.me.common.ecs.event.EventHandler;
import com.me.common.ecs.event.Listener;
public class GameDataSystem extends EntitySystem implements Listener {
private ComponentMapper<GameDataComponent> gameDataMapper;
private GameDataComponent gameData;
public GameDataSystem(Engine engine) {
super(engine, GameDataComponent.class);
gameDataMapper = engine.getComponentMapper(GameDataComponent.class);
}
@Override
public void processEntity(Entity entity, float dt) {
if(gameData == null) {
gameData = gameDataMapper.get(entity);
}
if (gameData.gameModeTimer > 0 && (gameData.gameModeTimer -= dt) < 0) {
switch (gameData.gameMode) {
case DIED:
if (--gameData.lives >= 0) {
resetPlayer();
setGameMode(GameMode.PLAYING);
} else {
setGameMode(GameMode.GAME_OVER);
}
break;
case GAME_OVER:
gameData.reset();
resetPlayer();
setGameMode(GameMode.PLAYING);
}
}
}
@EventHandler
public void onAsteroidHit(AsteroidHitEvent event) {
if (gameData == null) {
return;
}
switch (event.asteroid.generation) {
case 0:
gameData.score += 20;
gameData.newLifeScore += 20;
break;
case 1:
gameData.score += 50;
gameData.newLifeScore += 50;
break;
default:
gameData.score += 100;
gameData.newLifeScore += 100;
}
if (gameData.newLifeScore >= Constants.NEW_LIFE_SCORE) {
gameData.newLifeScore -= Constants.NEW_LIFE_SCORE;
gameData.lives++;
}
}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
if (gameData == null) {
return;
}
setGameMode(GameMode.DIED);
}
private void resetPlayer() {
Entity player = engine.getEntities().get(1);
PositionComponent position = player.getComponent(PositionComponent.class);
position.rotation = 90;
position.position.set(Constants.HALF_WIDTH, Constants.HALF_HEIGHT);
player.getComponent(VelocityComponent.class).velocity.set(0, 0);
player.activate();
}
private void setGameMode(GameMode mode) {
gameData.gameMode = mode;
gameData.gameModeTimer = mode.getTimer();
}
}