Clean up entity regions

This commit is contained in:
KingRainbow44 2023-05-04 21:28:45 -04:00
parent 8ee3dec5df
commit 0b87485bdf
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
2 changed files with 26 additions and 27 deletions

View File

@ -15,7 +15,7 @@ public class EntityRegion extends GameEntity {
private final Position position;
private final Set<Integer> 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;

View File

@ -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()));