From 04abe1d28076e27ed5f2dd48204932c485ac0362 Mon Sep 17 00:00:00 2001 From: Luca Fruzza Date: Tue, 24 Dec 2019 08:17:11 -0600 Subject: [PATCH] :sparkles: add hound lamp freeze functionality --- core/assets/tmx/level-01.tmx | 30 ++++++++- .../awake/behavior/HoundBehavior.java | 43 +++++++++++- .../awake/bootstrap/LevelBootstrap.java | 3 + .../awake/bootstrap/LevelStageBootstrap.java | 9 ++- .../com/punchbrain/awake/model/Circuit.java | 12 +++- .../awake/model/map/CircuitModelMap.java | 38 +++++++++++ .../punchbrain/awake/screens/LevelScreen.java | 67 ++++++++++--------- .../awake/tmx/CircuitInitialiser.java | 9 ++- 8 files changed, 172 insertions(+), 39 deletions(-) create mode 100644 core/src/com/punchbrain/awake/model/map/CircuitModelMap.java diff --git a/core/assets/tmx/level-01.tmx b/core/assets/tmx/level-01.tmx index df868b6..8a34cc7 100644 --- a/core/assets/tmx/level-01.tmx +++ b/core/assets/tmx/level-01.tmx @@ -1,5 +1,5 @@ - + @@ -8,12 +8,12 @@ - eAHt17EVACAIQ8GsIM7mCI7g/mrPBMkvaCy5II+SNKmYHhTWMdZ/rvHO+t/wxpt97psB5tvXtptbvPHucsGbRy4W91jUPbbxxpsM2GbgPNtBxfTgAntXbrs= + eJzt0UENACAMALFZALQhAQn4BxOQJeuj/0uuR8SgjJ6gAb/xG7/xG7/xu6KZoIF/VoIG/OaNfTXKOHtXbrs= - eAHt0TENADAMA8FQCX+STRl4tq5SNy+535nZ4N/EKxBIWv+N1yGgd0fH9Aq9U6mOnd4dHdMr9E6lOnZ6d3R0BQECBAgQIECAQIfAA9aYCQc= + eJzt0UENADAMA7FQKX+So9Dn1NjSIbhJMou4YfPa7zv87uJ3F7+7+N3FbwAA+McD1pgJBw== @@ -62,5 +62,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/core/src/com/punchbrain/awake/behavior/HoundBehavior.java b/core/src/com/punchbrain/awake/behavior/HoundBehavior.java index 1cbce71..6d729c0 100644 --- a/core/src/com/punchbrain/awake/behavior/HoundBehavior.java +++ b/core/src/com/punchbrain/awake/behavior/HoundBehavior.java @@ -1,6 +1,7 @@ package com.punchbrain.awake.behavior; import static com.badlogic.gdx.math.MathUtils.clamp; +import static com.punchbrain.awake.GameObjectType.CIRCUIT_LAMP; import java.util.LinkedList; import java.util.Queue; @@ -8,6 +9,8 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.math.Vector2; import com.punchbrain.awake.event.GameOverEvent; +import com.punchbrain.awake.model.Circuit; +import com.punchbrain.awake.model.map.CircuitModelMap; import de.bitbrain.braingdx.behavior.BehaviorAdapter; import de.bitbrain.braingdx.event.GameEventManager; import de.bitbrain.braingdx.graphics.lighting.LightingManager; @@ -27,15 +30,34 @@ public class HoundBehavior extends BehaviorAdapter { private final Queue snapshots = new LinkedList(); private final GameEventManager eventManager; private final LightingManager lightingManager; + private final CircuitModelMap circuitModelMap; + private boolean inLight; - public HoundBehavior(GameObject player, GameEventManager eventManager, LightingManager lightingManager) { + public HoundBehavior(GameObject player, GameEventManager eventManager, LightingManager lightingManager, CircuitModelMap circuitModelMap) { this.player = player; this.eventManager = eventManager; this.lightingManager = lightingManager; + this.circuitModelMap = circuitModelMap; } @Override public void update(final GameObject hound, final float delta) { + + // TODO: inject a lookup map to find circuit by game object + // TODO: introduce object to model stereotype Map which can be added to the context + // Map> + // Or, class ModelMap + + // TODO: Add a radial resolver + // TODO: difference in space, plus radius + // TODO: util method: radialColision(GameObject, radius1, GameObject, radius2) + + // TODO: talk about making this more of a controller class, with resolvers on the models + if(inLight){ + // this bool needs to be updated before it's always returned so that it must be checked every time. + inLight = false; + return; + } timer.update(delta); if (timer.reached(SNAPSHOT_INTERVAL)) { timer.reset(); @@ -68,6 +90,25 @@ public void update(final GameObject hound, final float delta) { lightingManager.setAmbientLight(Colors.darken(com.punchbrain.awake.Colors.BACKGROUND, ratio)); } + @Override + public void update(GameObject hound, GameObject object, float delta){ + if(CIRCUIT_LAMP.isTypeOf(object)){ + Circuit circuit = circuitModelMap.getFromLamp(object); + if(circuit.getState() == Circuit.State.ON){ + double xSep = hound.getLeft() - object.getLeft(); + double ySep = hound.getTop() - object.getTop(); + double rSep = Math.pow(Math.pow(xSep, 2) + Math.pow(ySep, 2), 0.5); + if(rSep < circuit.getLightRadius()){ + System.out.println("Contact!"); + inLight = true; + System.out.println(inLight); + return; + } + System.out.println(inLight); + } + } + } + private boolean readyToPoll(GameObject hound) { return target == null || target.cpy().sub(hound.getPosition()).len() < 396f; } diff --git a/core/src/com/punchbrain/awake/bootstrap/LevelBootstrap.java b/core/src/com/punchbrain/awake/bootstrap/LevelBootstrap.java index 0143c95..42c3ee2 100644 --- a/core/src/com/punchbrain/awake/bootstrap/LevelBootstrap.java +++ b/core/src/com/punchbrain/awake/bootstrap/LevelBootstrap.java @@ -1,9 +1,12 @@ package com.punchbrain.awake.bootstrap; +import com.punchbrain.awake.model.map.CircuitModelMap; import de.bitbrain.braingdx.context.GameContext2D; import de.bitbrain.braingdx.tmx.TiledMapContext; public interface LevelBootstrap { void boostrap(GameContext2D gameContext2D, TiledMapContext tiledMapContext); + //TODO: get rid of once a generic game context collection exists. + void setCircuitModelMap(CircuitModelMap circuitModelMap); boolean isApplicable(String tiledMapPath); } diff --git a/core/src/com/punchbrain/awake/bootstrap/LevelStageBootstrap.java b/core/src/com/punchbrain/awake/bootstrap/LevelStageBootstrap.java index 837932b..63f2243 100644 --- a/core/src/com/punchbrain/awake/bootstrap/LevelStageBootstrap.java +++ b/core/src/com/punchbrain/awake/bootstrap/LevelStageBootstrap.java @@ -7,12 +7,15 @@ import com.badlogic.gdx.utils.GdxRuntimeException; import com.punchbrain.awake.GameObjectType; import com.punchbrain.awake.behavior.HoundBehavior; +import com.punchbrain.awake.model.map.CircuitModelMap; import de.bitbrain.braingdx.context.GameContext2D; import de.bitbrain.braingdx.tmx.TiledMapContext; import de.bitbrain.braingdx.world.GameObject; public class LevelStageBootstrap implements LevelBootstrap { + CircuitModelMap circuitModelMap; + @Override public void boostrap(final GameContext2D gameContext2D, final TiledMapContext tiledMapContext) { // add hound @@ -32,12 +35,16 @@ public void boostrap(final GameContext2D gameContext2D, final TiledMapContext ti } } if (player != null) { - gameContext2D.getBehaviorManager().apply(new HoundBehavior(player, gameContext2D.getEventManager(), gameContext2D.getLightingManager()), hound); + gameContext2D.getBehaviorManager().apply(new HoundBehavior(player, gameContext2D.getEventManager(), gameContext2D.getLightingManager(), circuitModelMap), hound); } else { throw new GdxRuntimeException("Unable to initialise hound! Player not found"); } } + public void setCircuitModelMap(CircuitModelMap circuitModelMap) { + this.circuitModelMap = circuitModelMap; + } + @Override public boolean isApplicable(final String tiledMapPath) { return tiledMapPath.contains("level"); diff --git a/core/src/com/punchbrain/awake/model/Circuit.java b/core/src/com/punchbrain/awake/model/Circuit.java index a098949..d8d995a 100644 --- a/core/src/com/punchbrain/awake/model/Circuit.java +++ b/core/src/com/punchbrain/awake/model/Circuit.java @@ -9,7 +9,7 @@ import de.bitbrain.braingdx.world.GameObject; public class Circuit { - enum State {OFF, ON, BROKEN} + public enum State {OFF, ON, BROKEN} private final float lightRadius = 200; @@ -18,7 +18,7 @@ enum State {OFF, ON, BROKEN} private final Light lightObject; private final AudioManager audioManager; private float deltaAccumulator = 0; - private float deltaLimit = 30; + private float deltaLimit = 10; private State state = State.OFF; @@ -29,6 +29,9 @@ public Circuit(GameObject lamp, GameObject flipSwitch, Light lightObject, AudioM this.audioManager = audioManager; } + public float getLightRadius(){ + return this.state == State.ON ? this.lightRadius : 0; + } public Circuit updatePassiveBehaviour(float delta) { updateState(delta); @@ -47,6 +50,11 @@ public Circuit resolvePlayerCollision() { return this; } + public State getState(){ + return this.state; + } + + //TODO: make this a more general state? private void updateAnimationState() { if (this.state == State.ON) { this.flipSwitch.setAttribute(LampState.class, LampState.ON); diff --git a/core/src/com/punchbrain/awake/model/map/CircuitModelMap.java b/core/src/com/punchbrain/awake/model/map/CircuitModelMap.java new file mode 100644 index 0000000..5f0de8b --- /dev/null +++ b/core/src/com/punchbrain/awake/model/map/CircuitModelMap.java @@ -0,0 +1,38 @@ +package com.punchbrain.awake.model.map; + +import com.punchbrain.awake.model.Circuit; +import de.bitbrain.braingdx.world.GameObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * Maps circuit time game objects to their {@link com.punchbrain.awake.model.Circuit} models. + */ +public class CircuitModelMap { + private final Map lampMap; + private final Map flipSwitchMap; + + public CircuitModelMap(){ + this.lampMap = new HashMap<>(); + this.flipSwitchMap = new HashMap<>(); + } + + public void registerLamp(GameObject gameObject, Circuit circuit) { + lampMap.put(gameObject, circuit); + } + + public void registerFlipSwitch(GameObject gameObject, Circuit circuit) { + lampMap.put(gameObject, circuit); + } + + public Circuit getFromLamp(GameObject gameObject){ + return lampMap.get(gameObject); + } + + public Circuit getFromFlipSwitch(GameObject gameObject){ + return flipSwitchMap.get(gameObject); + } + +} diff --git a/core/src/com/punchbrain/awake/screens/LevelScreen.java b/core/src/com/punchbrain/awake/screens/LevelScreen.java index cbf592a..67e0c3a 100644 --- a/core/src/com/punchbrain/awake/screens/LevelScreen.java +++ b/core/src/com/punchbrain/awake/screens/LevelScreen.java @@ -25,6 +25,7 @@ import com.punchbrain.awake.input.LevelControllerInputAdapter; import com.punchbrain.awake.input.LevelKeyboardInputAdapter; import com.punchbrain.awake.model.Circuit; +import com.punchbrain.awake.model.map.CircuitModelMap; import com.punchbrain.awake.tmx.CircuitInitialiser; import com.punchbrain.awake.tmx.PlayerInitialiser; import com.punchbrain.awake.ui.Toast; @@ -48,6 +49,7 @@ public class LevelScreen extends BrainGdxScreen2D { private final String tiledMapFile; private GameContext2D context; private CircuitInitialiser circuitInitialiser; + private CircuitModelMap circuitModelMap; public LevelScreen(AwakeGame game, String tiledMapFile, String targetSpawnId) { @@ -67,32 +69,34 @@ public void reset() { } - @Override - protected void onCreate(GameContext2D context) { - Toast.getInstance().init(context.getStage()); - this.context = context; - context.getScreenTransitions().in(0.5f); - context.setBackgroundColor(Colors.BACKGROUND); - setupGraphics(context); - setupEvents(context); - TiledMapContext tmxContext = setupTiled(context); - setupPhysics(context); - setupInput(context.getInputManager()); + @Override + protected void onCreate(GameContext2D context) { + Toast.getInstance().init(context.getStage()); + this.context = context; + context.getScreenTransitions().in(0.5f); + context.setBackgroundColor(Colors.BACKGROUND); + setupGraphics(context); + setupEvents(context); + TiledMapContext tmxContext = setupTiled(context); + setupPhysics(context); + setupInput(context.getInputManager()); - bootstrap(context, tmxContext); + bootstrap(context, tmxContext); - Toast.getInstance().doToast(tmxContext.getTiledMap().getProperties().get("name", "", String.class)); - } + Toast.getInstance().doToast(tmxContext.getTiledMap().getProperties().get("name", "", String.class)); + } - private TiledMapContext setupTiled(GameContext2D context) { - TiledMapContext tmxContext = loadTiledMap(tiledMapFile, context); - tmxContext.setEventFactory(new AwakeEventFactory()); - return tmxContext; - } + private TiledMapContext setupTiled(GameContext2D context) { + TiledMapContext tmxContext = loadTiledMap(tiledMapFile, context); + tmxContext.setEventFactory(new AwakeEventFactory()); + return tmxContext; + } private void setupEvents(GameContext2D context) { this.playerInitialiser = new PlayerInitialiser(context, targetSpawnId); - this.circuitInitialiser = new CircuitInitialiser(context); + circuitModelMap = new CircuitModelMap(); + this.circuitInitialiser = new CircuitInitialiser(context, circuitModelMap); + //TODO: add a misc map in context of class to object for stuff like object to model map context.getEventManager().register(playerInitialiser, OnLoadGameObjectEvent.class); context.getEventManager().register(circuitInitialiser, OnLoadGameObjectEvent.class); context.getEventManager().register(new TeleportEventListener(context, this), TeleportEvent.class); @@ -142,15 +146,16 @@ public Object getAnimationType(GameObject object) { } - private void setupPhysics(GameContext2D context) { - context.getPhysicsManager().setGravity(0f, -98); - } - - private void bootstrap(final GameContext2D context, final TiledMapContext tmxContext) { - for (LevelBootstrap bootstrap : BootstrapFactory.getBoostraps()) { - if (bootstrap.isApplicable(tiledMapFile)) { - bootstrap.boostrap(context, tmxContext); - } - } - } + private void setupPhysics(GameContext2D context) { + context.getPhysicsManager().setGravity(0f, -98); + } + + private void bootstrap(final GameContext2D context, final TiledMapContext tmxContext) { + for (LevelBootstrap bootstrap : BootstrapFactory.getBoostraps()) { + if (bootstrap.isApplicable(tiledMapFile)) { + bootstrap.setCircuitModelMap(circuitModelMap); + bootstrap.boostrap(context, tmxContext); + } + } + } } diff --git a/core/src/com/punchbrain/awake/tmx/CircuitInitialiser.java b/core/src/com/punchbrain/awake/tmx/CircuitInitialiser.java index f03d6d4..f3785fa 100644 --- a/core/src/com/punchbrain/awake/tmx/CircuitInitialiser.java +++ b/core/src/com/punchbrain/awake/tmx/CircuitInitialiser.java @@ -20,6 +20,7 @@ import com.punchbrain.awake.behavior.PlayerUpdateBehavior; import com.punchbrain.awake.model.Circuit; import com.punchbrain.awake.model.Player; +import com.punchbrain.awake.model.map.CircuitModelMap; import de.bitbrain.braingdx.context.GameContext2D; import de.bitbrain.braingdx.event.GameEventListener; import de.bitbrain.braingdx.tmx.TiledMapEvents; @@ -37,13 +38,15 @@ public class CircuitInitialiser implements GameEventListener circuitPairMap; + CircuitModelMap circuitModelMap; private GameObject targetTeleport; private GameObject playerObject; - public CircuitInitialiser(GameContext2D context) { + public CircuitInitialiser(GameContext2D context, CircuitModelMap modelMap) { this.context = context; + this.circuitModelMap = modelMap; this.circuitPairMap = new HashMap<>(); } @@ -62,6 +65,8 @@ public void onEvent(TiledMapEvents.OnLoadGameObjectEvent event) { object.setAttribute(LampState.class, LampState.ON); circuitFlipSwitch.setAttribute(LampState.class, LampState.ON); Circuit circuit = new Circuit(object, circuitFlipSwitch, light, context.getAudioManager()); + circuitModelMap.registerLamp(object, circuit); + circuitModelMap.registerFlipSwitch(circuitFlipSwitch, circuit); CircuitBehaviour behavior = new CircuitBehaviour(circuit, context); context.getBehaviorManager().apply(behavior, circuitFlipSwitch); } @@ -86,6 +91,8 @@ public void onEvent(TiledMapEvents.OnLoadGameObjectEvent event) { circuitLamp.setAttribute(LampState.class, LampState.ON); Circuit circuit = new Circuit(circuitLamp, object, light, context.getAudioManager()); CircuitBehaviour behavior = new CircuitBehaviour(circuit, context); + circuitModelMap.registerLamp(circuitLamp, circuit); + circuitModelMap.registerFlipSwitch(object, circuit); context.getBehaviorManager().apply(behavior, object); } }