-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detecting hitbox overlap. Level exit portal.
- Loading branch information
Showing
15 changed files
with
256 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
core/src/main/java/jgd/platformer/level/BlockEntityComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
core/src/main/java/jgd/platformer/level/LevelEntitySystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
16 changes: 16 additions & 0 deletions
16
core/src/main/java/jgd/platformer/logic/hitbox/HitboxOverlapEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
core/src/main/java/jgd/platformer/logic/hitbox/HitboxOverlapSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
core/src/main/java/jgd/platformer/logic/hitbox/RectangleHitboxComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
core/src/main/java/jgd/platformer/logic/portal/LevelExitPortalComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
17 changes: 17 additions & 0 deletions
17
core/src/main/java/jgd/platformer/logic/portal/LevelExitSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |