Skip to content

Commit

Permalink
Detecting hitbox overlap. Level exit portal.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinSc committed Jul 10, 2016
1 parent 165d199 commit 3dc1e94
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
"texturesForParts": {
"all": "blockTiles/Dirt.png"
}
},
"CollidingObjectComponent": {
"translateX": 0,
"translateY": 0,
"width": 1,
"height": 1
}
}
14 changes: 14 additions & 0 deletions core/assets/prefabs/blocks/exitPortal.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"BlockComponent": {
"shape": "cube",
"texturesForParts": {
"front": "blockTiles/Dirt.png"
}
},
"BlockEntityComponent": {},
"RectangleHitboxComponent": {
"width": 1,
"height": 1
},
"LevelExitPortalComponent": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
"bottom": "blockTiles/Dirt.png",
"sides": "blockTiles/GrassSide.png"
}
},
"CollidingObjectComponent": {
"translateX": 0,
"translateY": 0,
"width": 1,
"height": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
"texturesForParts": {
"all": "blockTiles/Stone.png"
}
},
"CollidingObjectComponent": {
"translateX": 0,
"translateY": 0,
"width": 1,
"height": 1
}
}
1 change: 1 addition & 0 deletions core/assets/prefabs/levels/level-sample2.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"2,-2,0": "grass",
"0,2,0": "grass",
"1,2,0": "grass",
"1,3,-1": "exitPortal",
"2,2,0": "grass",
"-3.5,0,0": "stone",
"-4.5,0,0": "stone",
Expand Down
7 changes: 7 additions & 0 deletions core/assets/prefabs/player.prefab
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"PlayerComponent": {},
"LocationComponent": {
"x": 0,
"y": 0,
Expand Down Expand Up @@ -26,6 +27,12 @@
"width": 1,
"height": 1
},
"RectangleHitboxComponent": {
"translateX": -0.5,
"translateY": 0,
"width": 1,
"height": 1
},
"CameraFocusComponent": {
"focusWeight": 1
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package jgd.platformer.level;

import com.gempukku.secsy.entity.Component;

public interface BlockEntityComponent extends Component {
}
40 changes: 40 additions & 0 deletions core/src/main/java/jgd/platformer/level/LevelEntitySystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package jgd.platformer.level;

import com.gempukku.gaming.asset.prefab.PrefabManager;
import com.gempukku.secsy.context.annotation.Inject;
import com.gempukku.secsy.context.annotation.RegisterSystem;
import com.gempukku.secsy.entity.EntityManager;
import com.gempukku.secsy.entity.EntityRef;
import com.gempukku.secsy.entity.dispatch.ReceiveEvent;
import com.gempukku.secsy.entity.event.AfterComponentAdded;
import com.gempukku.secsy.entity.io.EntityData;
import jgd.platformer.component.LocationComponent;

import java.util.Map;

@RegisterSystem
public class LevelEntitySystem {
@Inject
private PrefabManager prefabManager;
@Inject
private EntityManager entityManager;

@ReceiveEvent
public void levelLoaded(AfterComponentAdded event, EntityRef entity, LevelComponent level) {
for (Map.Entry<String, String> blockCoordinates : level.getBlockCoordinates().entrySet()) {
String locationStr = blockCoordinates.getKey();
String prefabName = blockCoordinates.getValue();
EntityData entityData = prefabManager.getPrefabByName(prefabName);
EntityRef blockData = entityManager.wrapEntityData(entityData);
if (blockData.hasComponent(BlockEntityComponent.class)) {
EntityRef result = entityManager.createEntity(entityData);
LocationComponent location = result.createComponent(LocationComponent.class);
String[] locationSplit = locationStr.split(",");
location.setX(Float.parseFloat(locationSplit[0]));
location.setY(Float.parseFloat(locationSplit[1]));
location.setZ(Float.parseFloat(locationSplit[2]));
result.saveChanges();
}
}
}
}
6 changes: 6 additions & 0 deletions core/src/main/java/jgd/platformer/logic/PlayerComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package jgd.platformer.logic;

import com.gempukku.secsy.entity.Component;

public interface PlayerComponent extends Component {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jgd.platformer.logic.hitbox;

import com.gempukku.secsy.entity.EntityRef;
import com.gempukku.secsy.entity.event.Event;

public class HitboxOverlapEvent extends Event {
private EntityRef otherEntity;

public HitboxOverlapEvent(EntityRef otherEntity) {
this.otherEntity = otherEntity;
}

public EntityRef getOtherEntity() {
return otherEntity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package jgd.platformer.logic.hitbox;

import com.gempukku.secsy.context.annotation.Inject;
import com.gempukku.secsy.context.annotation.RegisterSystem;
import com.gempukku.secsy.context.system.LifeCycleSystem;
import com.gempukku.secsy.entity.EntityRef;
import com.gempukku.secsy.entity.dispatch.ReceiveEvent;
import com.gempukku.secsy.entity.event.AfterComponentAdded;
import com.gempukku.secsy.entity.event.AfterComponentUpdated;
import com.gempukku.secsy.entity.event.BeforeComponentRemoved;
import com.gempukku.secsy.entity.game.GameLoop;
import com.gempukku.secsy.entity.game.GameLoopListener;
import jgd.platformer.component.LocationComponent;

import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RegisterSystem
public class HitboxOverlapSystem implements GameLoopListener, LifeCycleSystem {
@Inject
private GameLoop gameLoop;

private Map<EntityRef, Rectangle2D> hitboxEntities = new HashMap<>();

@Override
public void initialize() {
gameLoop.addGameLoopListener(this);
}

@ReceiveEvent
public void entityWithHitboxAdded(AfterComponentAdded event, EntityRef entity, RectangleHitboxComponent rectangleHitbox, LocationComponent location) {
Rectangle2D shape = createShape(rectangleHitbox, location);
hitboxEntities.put(entity, shape);
}

@ReceiveEvent
public void entityWithHitboxModified(AfterComponentUpdated event, EntityRef entity, RectangleHitboxComponent rectangleHitbox, LocationComponent location) {
Rectangle2D shape = createShape(rectangleHitbox, location);
hitboxEntities.put(entity, shape);
}

@ReceiveEvent
public void entityWithHitboxRemoved(BeforeComponentRemoved event, EntityRef entity, RectangleHitboxComponent rectangleHitbox, LocationComponent location) {
hitboxEntities.remove(entity);
}

@Override
public void update() {
List<OverlapEventToFire> eventsToFire = new LinkedList<>();

for (Map.Entry<EntityRef, Rectangle2D> hitboxEntity : hitboxEntities.entrySet()) {
EntityRef entity = hitboxEntity.getKey();
Rectangle2D shape = hitboxEntity.getValue();

for (Map.Entry<EntityRef, Rectangle2D> otherHitboxEntity : hitboxEntities.entrySet()) {
EntityRef otherEntity = otherHitboxEntity.getKey();
if (!entity.equals(otherEntity)) {
Rectangle2D otherShape = otherHitboxEntity.getValue();

if (shape.intersects(otherShape)) {
eventsToFire.add(new OverlapEventToFire(entity, new HitboxOverlapEvent(otherEntity)));
}
}
}
}

for (OverlapEventToFire overlapEventToFire : eventsToFire) {
overlapEventToFire.entity.send(overlapEventToFire.hitboxOverlapEvent);
}
}

private Rectangle2D createShape(RectangleHitboxComponent rectangleHitbox, LocationComponent location) {
return new Rectangle2D.Float(location.getX() + rectangleHitbox.getTranslateX(), location.getY() + rectangleHitbox.getTranslateY(),
rectangleHitbox.getWidth(), rectangleHitbox.getHeight());
}

private static class OverlapEventToFire {
private EntityRef entity;
private HitboxOverlapEvent hitboxOverlapEvent;

public OverlapEventToFire(EntityRef entity, HitboxOverlapEvent hitboxOverlapEvent) {
this.entity = entity;
this.hitboxOverlapEvent = hitboxOverlapEvent;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jgd.platformer.logic.hitbox;

import com.gempukku.secsy.entity.Component;

public interface RectangleHitboxComponent extends Component {
float getTranslateX();

float getTranslateY();

float getWidth();

float getHeight();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package jgd.platformer.logic.physics;

import com.gempukku.gaming.asset.prefab.PrefabManager;
import com.gempukku.secsy.context.annotation.Inject;
import com.gempukku.secsy.context.annotation.RegisterSystem;
import com.gempukku.secsy.entity.EntityManager;
import com.gempukku.secsy.entity.EntityRef;
import com.gempukku.secsy.entity.dispatch.ReceiveEvent;
import com.gempukku.secsy.entity.event.AfterComponentAdded;
Expand All @@ -10,19 +13,33 @@
import java.awt.geom.Rectangle2D;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RegisterSystem
public class PlatformCollider {
@Inject
private PrefabManager prefabManager;
@Inject
private EntityManager entityManager;

private List<Rectangle2D> platformBlocks = new LinkedList<>();

@ReceiveEvent
public void levelLoaded(AfterComponentAdded event, EntityRef entity, LevelComponent level) {
for (String blockCoordinates : level.getBlockCoordinates().keySet()) {
String[] split = blockCoordinates.split(",");
float x = Float.parseFloat(split[0]);
float y = Float.parseFloat(split[1]);

platformBlocks.add(new Rectangle2D.Float(x, y, 1, 1));
Map<String, String> blockCoordinates = level.getBlockCoordinates();
for (Map.Entry<String, String> blockCoordinate : blockCoordinates.entrySet()) {
String location = blockCoordinate.getKey();
String prefab = blockCoordinate.getValue();
EntityRef prefabData = entityManager.wrapEntityData(prefabManager.getPrefabByName(prefab));
CollidingObjectComponent collidingObject = prefabData.getComponent(CollidingObjectComponent.class);
if (collidingObject != null) {
String[] locationSplit = location.split(",");
float x = Float.parseFloat(locationSplit[0]);
float y = Float.parseFloat(locationSplit[1]);
platformBlocks.add(new Rectangle2D.Float(
x + collidingObject.getTranslateX(), y + collidingObject.getTranslateY(),
collidingObject.getWidth(), collidingObject.getHeight()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package jgd.platformer.logic.portal;

import com.gempukku.secsy.entity.Component;

public interface LevelExitPortalComponent extends Component {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package jgd.platformer.logic.portal;

import com.gempukku.secsy.context.annotation.RegisterSystem;
import com.gempukku.secsy.entity.EntityRef;
import com.gempukku.secsy.entity.dispatch.ReceiveEvent;
import jgd.platformer.logic.PlayerComponent;
import jgd.platformer.logic.hitbox.HitboxOverlapEvent;

@RegisterSystem
public class LevelExitSystem {
@ReceiveEvent
public void playerEnteredPortal(HitboxOverlapEvent event, EntityRef entity, PlayerComponent player) {
if (event.getOtherEntity().hasComponent(LevelExitPortalComponent.class)) {
System.out.println("Player exited");
}
}
}

0 comments on commit 3dc1e94

Please sign in to comment.