Add afterburner.

This commit is contained in:
Matt Low 2020-01-31 21:30:33 +04:00
parent 06d6a9630c
commit b389cb9c2b
4 changed files with 47 additions and 1 deletions

View File

@ -62,13 +62,31 @@ public class EntityFactory {
AccelerationComponent accel = new AccelerationComponent(); AccelerationComponent accel = new AccelerationComponent();
accel.acceleration = new Vector2(0, 1f); accel.acceleration = new Vector2(0, 1f);
ModelComponent afterBurnerModel = new ModelComponent();
afterBurnerModel.model = new LineModel(Color.BLUE);
afterBurnerModel.model.setVertices(new float[] {
-2/16f, -5/16f,
0f, -0.8f,
2/16f, -5/16f
});
PositionComponent afterBurnerPosition = new PositionComponent();
afterBurnerPosition.position = new Vector2(Constants.HALF_WIDTH, Constants.HALF_HEIGHT);
afterBurnerPosition.rotation = 90;
Entity player = createEntity(engine); Entity player = createEntity(engine);
Entity afterBurner = createEntity(engine);
afterBurner.addComponent(afterBurnerPosition);
afterBurner.addComponent(velocity);
afterBurner.addComponent(afterBurnerModel);
player.addComponent(position); player.addComponent(position);
player.addComponent(velocity); player.addComponent(velocity);
player.addComponent(model); player.addComponent(model);
player.addComponent(accel); player.addComponent(accel);
player.addComponent(COLLIDER); player.addComponent(COLLIDER);
player.addComponent(new PlayerComponent()); player.addComponent(new PlayerComponent(afterBurner));
return player; return player;
} }

View File

@ -1,7 +1,14 @@
package com.me.asteroids.components; package com.me.asteroids.components;
import com.me.common.ecs.Component; import com.me.common.ecs.Component;
import com.me.common.ecs.Entity;
public class PlayerComponent implements Component { public class PlayerComponent implements Component {
// TODO: implement engine feature for tagging entities (as player, for e.g.) // TODO: implement engine feature for tagging entities (as player, for e.g.)
public Entity afterBurner;
public PlayerComponent(Entity afterBurner) {
this.afterBurner = afterBurner;
}
} }

View File

@ -123,6 +123,7 @@ public class GameScreen extends Screen implements Listener {
ComponentMapper<BulletComponent> bulletMapper; ComponentMapper<BulletComponent> bulletMapper;
ComponentMapper<DebrisComponent> debrisMapper; ComponentMapper<DebrisComponent> debrisMapper;
ComponentMapper<AsteroidComponent> asteroidMapper; ComponentMapper<AsteroidComponent> asteroidMapper;
ComponentMapper<PlayerComponent> playerMapper;
EventListener(Engine engine) { EventListener(Engine engine) {
this.positionMapper = engine.getComponentMapper(PositionComponent.class); this.positionMapper = engine.getComponentMapper(PositionComponent.class);
@ -130,6 +131,7 @@ public class GameScreen extends Screen implements Listener {
this.bulletMapper = engine.getComponentMapper(BulletComponent.class); this.bulletMapper = engine.getComponentMapper(BulletComponent.class);
this.debrisMapper = engine.getComponentMapper(DebrisComponent.class); this.debrisMapper = engine.getComponentMapper(DebrisComponent.class);
this.asteroidMapper = engine.getComponentMapper(AsteroidComponent.class); this.asteroidMapper = engine.getComponentMapper(AsteroidComponent.class);
this.playerMapper = engine.getComponentMapper(PlayerComponent.class);
} }
@EventHandler @EventHandler
@ -172,6 +174,7 @@ public class GameScreen extends Screen implements Listener {
if (asteroid.contains(player.getVertices()) || player.contains(asteroid.getVertices())) { if (asteroid.contains(player.getVertices()) || player.contains(asteroid.getVertices())) {
engine.callEvent(new PlayerDeathEvent(event.getPlayer())); engine.callEvent(new PlayerDeathEvent(event.getPlayer()));
playerMapper.get(event.getPlayer()).afterBurner.deactivate();
event.getPlayer().deactivate(); event.getPlayer().deactivate();
for (Entity debris : EntityFactory.createDebris(engine, event.getPlayer())) { for (Entity debris : EntityFactory.createDebris(engine, event.getPlayer())) {
debris.activate(); debris.activate();

View File

@ -6,6 +6,7 @@ import com.badlogic.gdx.math.Vector2;
import com.me.asteroids.EntityFactory; import com.me.asteroids.EntityFactory;
import com.me.asteroids.Utils; import com.me.asteroids.Utils;
import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.AccelerationComponent;
import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PlayerComponent; import com.me.asteroids.components.PlayerComponent;
import com.me.asteroids.components.PositionComponent; import com.me.asteroids.components.PositionComponent;
import com.me.asteroids.components.VelocityComponent; import com.me.asteroids.components.VelocityComponent;
@ -14,6 +15,8 @@ import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem; import com.me.common.ecs.EntitySystem;
import static com.me.asteroids.Constants.rand;
public class PlayerInputSystem extends EntitySystem { public class PlayerInputSystem extends EntitySystem {
public Vector2 tmp = new Vector2(0, 1); public Vector2 tmp = new Vector2(0, 1);
@ -21,6 +24,7 @@ public class PlayerInputSystem extends EntitySystem {
private ComponentMapper<PositionComponent> positionMapper; private ComponentMapper<PositionComponent> positionMapper;
private ComponentMapper<VelocityComponent> velocityMapper; private ComponentMapper<VelocityComponent> velocityMapper;
private ComponentMapper<AccelerationComponent> accelMapper; private ComponentMapper<AccelerationComponent> accelMapper;
private ComponentMapper<PlayerComponent> playerMapper;
public PlayerInputSystem(Engine engine) { public PlayerInputSystem(Engine engine) {
super( super(
@ -33,6 +37,7 @@ public class PlayerInputSystem extends EntitySystem {
positionMapper = engine.getComponentMapper(PositionComponent.class); positionMapper = engine.getComponentMapper(PositionComponent.class);
velocityMapper = engine.getComponentMapper(VelocityComponent.class); velocityMapper = engine.getComponentMapper(VelocityComponent.class);
accelMapper = engine.getComponentMapper(AccelerationComponent.class); accelMapper = engine.getComponentMapper(AccelerationComponent.class);
playerMapper = engine.getComponentMapper(PlayerComponent.class);
} }
@Override @Override
@ -40,6 +45,7 @@ public class PlayerInputSystem extends EntitySystem {
PositionComponent positionComponent = positionMapper.get(entity); PositionComponent positionComponent = positionMapper.get(entity);
VelocityComponent velocityComponent = velocityMapper.get(entity); VelocityComponent velocityComponent = velocityMapper.get(entity);
AccelerationComponent accelComponent = accelMapper.get(entity); AccelerationComponent accelComponent = accelMapper.get(entity);
PlayerComponent playerComponent = playerMapper.get(entity);
boolean isLeftPressed = Gdx.input.isKeyPressed(Input.Keys.A); boolean isLeftPressed = Gdx.input.isKeyPressed(Input.Keys.A);
boolean isRightPressed = Gdx.input.isKeyPressed(Input.Keys.D); boolean isRightPressed = Gdx.input.isKeyPressed(Input.Keys.D);
@ -54,9 +60,21 @@ public class PlayerInputSystem extends EntitySystem {
Vector2 acceleration = accelComponent.acceleration; Vector2 acceleration = accelComponent.acceleration;
Vector2 velocity = velocityComponent.velocity; Vector2 velocity = velocityComponent.velocity;
playerComponent.afterBurner.getComponent(ModelComponent.class).model.setPosition(positionComponent.position);
if (Gdx.input.isKeyPressed(Input.Keys.W)) { if (Gdx.input.isKeyPressed(Input.Keys.W)) {
acceleration.set(Utils.setUnitVectorAngle(tmp, positionComponent.rotation).scl(12.5f)); acceleration.set(Utils.setUnitVectorAngle(tmp, positionComponent.rotation).scl(12.5f));
if (rand.nextFloat() < 0.85) {
PositionComponent afterBurnerPosition = positionMapper.get(playerComponent.afterBurner);
afterBurnerPosition.position.set(positionComponent.position);
afterBurnerPosition.rotation = positionComponent.rotation;
playerComponent.afterBurner.activate();
} else {
playerComponent.afterBurner.deactivate();
}
} else { } else {
playerComponent.afterBurner.deactivate();
if (!velocity.isZero(1f)) { if (!velocity.isZero(1f)) {
acceleration.set(Utils.setUnitVectorAngleRad(tmp, velocity.angleRad()).scl(-2.5f)); acceleration.set(Utils.setUnitVectorAngleRad(tmp, velocity.angleRad()).scl(-2.5f));
} else { } else {