From b389cb9c2bae6f7d450a72c0470cc231d20ae478 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Fri, 31 Jan 2020 21:30:33 +0400 Subject: [PATCH] Add afterburner. --- core/src/com/me/asteroids/EntityFactory.java | 20 ++++++++++++++++++- .../asteroids/components/PlayerComponent.java | 7 +++++++ .../com/me/asteroids/screens/GameScreen.java | 3 +++ .../asteroids/systems/PlayerInputSystem.java | 18 +++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/core/src/com/me/asteroids/EntityFactory.java b/core/src/com/me/asteroids/EntityFactory.java index 384f941..5a05906 100644 --- a/core/src/com/me/asteroids/EntityFactory.java +++ b/core/src/com/me/asteroids/EntityFactory.java @@ -62,13 +62,31 @@ public class EntityFactory { AccelerationComponent accel = new AccelerationComponent(); 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 afterBurner = createEntity(engine); + afterBurner.addComponent(afterBurnerPosition); + afterBurner.addComponent(velocity); + afterBurner.addComponent(afterBurnerModel); + player.addComponent(position); player.addComponent(velocity); player.addComponent(model); player.addComponent(accel); player.addComponent(COLLIDER); - player.addComponent(new PlayerComponent()); + player.addComponent(new PlayerComponent(afterBurner)); + return player; } diff --git a/core/src/com/me/asteroids/components/PlayerComponent.java b/core/src/com/me/asteroids/components/PlayerComponent.java index 708db85..89b073f 100644 --- a/core/src/com/me/asteroids/components/PlayerComponent.java +++ b/core/src/com/me/asteroids/components/PlayerComponent.java @@ -1,7 +1,14 @@ package com.me.asteroids.components; import com.me.common.ecs.Component; +import com.me.common.ecs.Entity; public class PlayerComponent implements Component { // TODO: implement engine feature for tagging entities (as player, for e.g.) + public Entity afterBurner; + + public PlayerComponent(Entity afterBurner) { + this.afterBurner = afterBurner; + } + } diff --git a/core/src/com/me/asteroids/screens/GameScreen.java b/core/src/com/me/asteroids/screens/GameScreen.java index 01bd4cd..8b7da44 100644 --- a/core/src/com/me/asteroids/screens/GameScreen.java +++ b/core/src/com/me/asteroids/screens/GameScreen.java @@ -123,6 +123,7 @@ public class GameScreen extends Screen implements Listener { ComponentMapper bulletMapper; ComponentMapper debrisMapper; ComponentMapper asteroidMapper; + ComponentMapper playerMapper; EventListener(Engine engine) { this.positionMapper = engine.getComponentMapper(PositionComponent.class); @@ -130,6 +131,7 @@ public class GameScreen extends Screen implements Listener { this.bulletMapper = engine.getComponentMapper(BulletComponent.class); this.debrisMapper = engine.getComponentMapper(DebrisComponent.class); this.asteroidMapper = engine.getComponentMapper(AsteroidComponent.class); + this.playerMapper = engine.getComponentMapper(PlayerComponent.class); } @EventHandler @@ -172,6 +174,7 @@ public class GameScreen extends Screen implements Listener { if (asteroid.contains(player.getVertices()) || player.contains(asteroid.getVertices())) { engine.callEvent(new PlayerDeathEvent(event.getPlayer())); + playerMapper.get(event.getPlayer()).afterBurner.deactivate(); event.getPlayer().deactivate(); for (Entity debris : EntityFactory.createDebris(engine, event.getPlayer())) { debris.activate(); diff --git a/core/src/com/me/asteroids/systems/PlayerInputSystem.java b/core/src/com/me/asteroids/systems/PlayerInputSystem.java index 8b7147d..8a7f568 100644 --- a/core/src/com/me/asteroids/systems/PlayerInputSystem.java +++ b/core/src/com/me/asteroids/systems/PlayerInputSystem.java @@ -6,6 +6,7 @@ import com.badlogic.gdx.math.Vector2; import com.me.asteroids.EntityFactory; import com.me.asteroids.Utils; import com.me.asteroids.components.AccelerationComponent; +import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.PlayerComponent; import com.me.asteroids.components.PositionComponent; 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.EntitySystem; +import static com.me.asteroids.Constants.rand; + public class PlayerInputSystem extends EntitySystem { public Vector2 tmp = new Vector2(0, 1); @@ -21,6 +24,7 @@ public class PlayerInputSystem extends EntitySystem { private ComponentMapper positionMapper; private ComponentMapper velocityMapper; private ComponentMapper accelMapper; + private ComponentMapper playerMapper; public PlayerInputSystem(Engine engine) { super( @@ -33,6 +37,7 @@ public class PlayerInputSystem extends EntitySystem { positionMapper = engine.getComponentMapper(PositionComponent.class); velocityMapper = engine.getComponentMapper(VelocityComponent.class); accelMapper = engine.getComponentMapper(AccelerationComponent.class); + playerMapper = engine.getComponentMapper(PlayerComponent.class); } @Override @@ -40,6 +45,7 @@ public class PlayerInputSystem extends EntitySystem { PositionComponent positionComponent = positionMapper.get(entity); VelocityComponent velocityComponent = velocityMapper.get(entity); AccelerationComponent accelComponent = accelMapper.get(entity); + PlayerComponent playerComponent = playerMapper.get(entity); boolean isLeftPressed = Gdx.input.isKeyPressed(Input.Keys.A); boolean isRightPressed = Gdx.input.isKeyPressed(Input.Keys.D); @@ -54,9 +60,21 @@ public class PlayerInputSystem extends EntitySystem { Vector2 acceleration = accelComponent.acceleration; Vector2 velocity = velocityComponent.velocity; + playerComponent.afterBurner.getComponent(ModelComponent.class).model.setPosition(positionComponent.position); + if (Gdx.input.isKeyPressed(Input.Keys.W)) { 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 { + playerComponent.afterBurner.deactivate(); if (!velocity.isZero(1f)) { acceleration.set(Utils.setUnitVectorAngleRad(tmp, velocity.angleRad()).scl(-2.5f)); } else {