From 0b87485bdf51d33892bdf56fef65400151079d3a Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 4 May 2023 21:28:45 -0400 Subject: [PATCH] Clean up entity regions --- .../grasscutter/game/entity/EntityRegion.java | 31 +++++++++---------- .../scripts/SceneScriptManager.java | 22 +++++++------ 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/main/java/emu/grasscutter/game/entity/EntityRegion.java b/src/main/java/emu/grasscutter/game/entity/EntityRegion.java index 5fe55ce63..6a52297b2 100644 --- a/src/main/java/emu/grasscutter/game/entity/EntityRegion.java +++ b/src/main/java/emu/grasscutter/game/entity/EntityRegion.java @@ -15,7 +15,7 @@ public class EntityRegion extends GameEntity { private final Position position; private final Set entities; // Ids of entities inside this region private final SceneRegion metaRegion; - private boolean hasNewEntities; + private boolean entityEnter; private boolean entityLeave; public EntityRegion(Scene scene, SceneRegion region) { @@ -35,20 +35,18 @@ public class EntityRegion extends GameEntity { return this.metaRegion.config_id; } + public void resetNewEntities() { + this.entityEnter = false; + this.entityLeave = false; + } + public void addEntity(GameEntity entity) { if (this.getEntities().contains(entity.getId())) { return; } + this.getEntities().add(entity.getId()); - this.hasNewEntities = true; - } - - public boolean hasNewEntities() { - return hasNewEntities; - } - - public void resetNewEntities() { - hasNewEntities = false; + this.entityEnter = true; } public void removeEntity(int entityId) { @@ -57,18 +55,17 @@ public class EntityRegion extends GameEntity { } public void removeEntity(GameEntity entity) { - this.getEntities().remove(entity.getId()); - this.entityLeave = true; + this.removeEntity(entity.getId()); } - public boolean entityLeave() { + public boolean entityHasEntered() { + return this.entityEnter; + } + + public boolean entityHasLeft() { return this.entityLeave; } - public void resetEntityLeave() { - this.entityLeave = false; - } - @Override public Int2FloatMap getFightProperties() { return null; diff --git a/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java b/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java index 1a0d59138..d77237911 100644 --- a/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java +++ b/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java @@ -643,30 +643,32 @@ public class SceneScriptManager { .toList(); entities.forEach(region::addEntity); - int targetID = 0; + var targetId = 0; if (entities.size() > 0) { - targetID = entities.get(0).getId(); + targetId = entities.get(0).getId(); } - if (region.hasNewEntities()) { + if (region.entityHasEntered()) { Grasscutter.getLogger() .trace("Call EVENT_ENTER_REGION_{}", region.getMetaRegion().config_id); - callEvent( + this.callEvent( new ScriptArgs(region.getGroupId(), EventType.EVENT_ENTER_REGION, region.getConfigId()) .setSourceEntityId(region.getId()) - .setTargetEntityId(targetID)); + .setTargetEntityId(targetId)); region.resetNewEntities(); } - for (int entityId : region.getEntities()) { - if (getScene().getEntityById(entityId) == null - || !region.getMetaRegion().contains(getScene().getEntityById(entityId).getPosition())) { + for (var entityId : region.getEntities()) { + var entity = this.getScene().getEntityById(entityId); + if (entity == null || !region.getMetaRegion() + .contains(entity.getPosition())) { region.removeEntity(entityId); } } - if (region.entityLeave()) { - callEvent( + + if (region.entityHasLeft()) { + this.callEvent( new ScriptArgs(region.getGroupId(), EventType.EVENT_LEAVE_REGION, region.getConfigId()) .setSourceEntityId(region.getId()) .setTargetEntityId(region.getFirstEntityId()));