Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions core/assets/tmx/level-01.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.3.0" orientation="orthogonal" renderorder="left-up" compressionlevel="0" width="124" height="12" tilewidth="32" tileheight="32" infinite="0" nextlayerid="8" nextobjectid="40">
<map version="1.2" tiledversion="1.2.5" orientation="orthogonal" renderorder="left-up" width="124" height="12" tilewidth="32" tileheight="32" infinite="0" nextlayerid="8" nextobjectid="44">
<properties>
<property name="name" value="The Descent"/>
</properties>
Expand All @@ -8,12 +8,12 @@
</tileset>
<layer id="6" name="Tile Layer 1" width="124" height="12">
<data encoding="base64" compression="zlib">
eAHt17EVACAIQ8GsIM7mCI7g/mrPBMkvaCy5II+SNKmYHhTWMdZ/rvHO+t/wxpt97psB5tvXtptbvPHucsGbRy4W91jUPbbxxpsM2GbgPNtBxfTgAntXbrs=
eJzt0UENACAMALFZALQhAQn4BxOQJeuj/0uuR8SgjJ6gAb/xG7/xG7/xu6KZoIF/VoIG/OaNfTXKOHtXbrs=
</data>
</layer>
<layer id="7" name="Tile Layer 2" width="124" height="12" offsetx="0" offsety="16">
<data encoding="base64" compression="zlib">
eAHt0TENADAMA8FQCX+STRl4tq5SNy+535nZ4N/EKxBIWv+N1yGgd0fH9Aq9U6mOnd4dHdMr9E6lOnZ6d3R0BQECBAgQIECAQIfAA9aYCQc=
eJzt0UENADAMA7FQKX+So9Dn1NjSIbhJMou4YfPa7zv87uJ3F7+7+N3FbwAA+McD1pgJBw==
</data>
</layer>
<objectgroup id="2" name="collisions">
Expand Down Expand Up @@ -62,5 +62,29 @@
<property name="physics.enabled" type="bool" value="true"/>
</properties>
</object>
<object id="40" type="circuit_lamp" x="167.25" y="266.25">
<properties>
<property name="circuit_id" value="1"/>
</properties>
<point/>
</object>
<object id="41" type="circuit_flip_switch" x="220" y="331">
<properties>
<property name="circuit_id" value="1"/>
</properties>
<point/>
</object>
<object id="42" type="circuit_lamp" x="544.25" y="328.25">
<properties>
<property name="circuit_id" value="2"/>
</properties>
<point/>
</object>
<object id="43" type="circuit_flip_switch" x="606" y="325">
<properties>
<property name="circuit_id" value="2"/>
</properties>
<point/>
</object>
</objectgroup>
</map>
43 changes: 42 additions & 1 deletion core/src/com/punchbrain/awake/behavior/HoundBehavior.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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;

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;
Expand All @@ -27,15 +30,34 @@ public class HoundBehavior extends BehaviorAdapter {
private final Queue<Vector2> snapshots = new LinkedList<Vector2>();
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<GameObject, Model> which can be added to the context
// Map<Model.class,Map<GameObject, Model>>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very interesting idea. We might wanna add this to braingdx if it fulfills these criterias:

  • it saves a lot of time when creating new games
  • it does not introduce strange coupling at scale (checkout https://github.com/bitbrain/galacticum as a reference, this is a project of mine where I attempted to create an infinite 2D space game but it failed due to coupling issues. I want to avoid them in braingdx)

Let's see some code in action and see if it's useful!

// Or, class ModelMap<T, >

// 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();
Expand Down Expand Up @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you actually don't need that. Checkout the RayHandler class available through the LightingManager. I'd prefer this since we then can use different types of lights (like cone light for example)

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;
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/com/punchbrain/awake/bootstrap/LevelBootstrap.java
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this private


@Override
public void boostrap(final GameContext2D gameContext2D, final TiledMapContext tiledMapContext) {
// add hound
Expand All @@ -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");
Expand Down
12 changes: 10 additions & 2 deletions core/src/com/punchbrain/awake/model/Circuit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions core/src/com/punchbrain/awake/model/map/CircuitModelMap.java
Original file line number Diff line number Diff line change
@@ -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<GameObject, Circuit> lampMap;
private final Map<GameObject, Circuit> 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);
}

}
67 changes: 36 additions & 31 deletions core/src/com/punchbrain/awake/screens/LevelScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +49,7 @@ public class LevelScreen extends BrainGdxScreen2D<AwakeGame> {
private final String tiledMapFile;
private GameContext2D context;
private CircuitInitialiser circuitInitialiser;
private CircuitModelMap circuitModelMap;


public LevelScreen(AwakeGame game, String tiledMapFile, String targetSpawnId) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
}
9 changes: 8 additions & 1 deletion core/src/com/punchbrain/awake/tmx/CircuitInitialiser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,13 +38,15 @@ public class CircuitInitialiser implements GameEventListener<TiledMapEvents.OnLo

private final GameContext2D context;
Map<String, GameObject> 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<>();
}

Expand All @@ -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);
}
Expand All @@ -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);
}
}
Expand Down