fix: unify death handling and add it to all cases of damage taking

This commit is contained in:
punchready 2021-08-11 09:30:01 +02:00
parent cf035dedac
commit fed9f8d077

View File

@ -396,36 +396,7 @@ public class GameLogic {
.buildEntityEvent());
Entity targetEntity = state.entities.findEntity(data.targetEntity);
if(targetEntity instanceof Character) {
Character target = (Character)targetEntity;
if(target.hp.getValue() <= data.value) {
List<StoneType> stones = Arrays.asList(target.inventory.getStonesAsArray());
Collections.shuffle(stones); // required by documents
ArrayList<IntVector2> used = new ArrayList<>();
for(StoneType stone: stones) {
ArrayList<IntVector2> options = getFreeNeighbour(state, target.getPosition(), used);
IntVector2 picked = options.get(rand.nextInt(options.size()));
used.add(picked);
result.add(new EventBuilder(EventType.SpawnEntityEvent)
.withEntity(new InfinityStone(
new EntityID(EntityType.InfinityStones, stone.getID()),
picked,
stone
))
.buildEntityEvent());
}
}
}else if(targetEntity instanceof Rock) {
Rock target = (Rock)targetEntity;
if(target.getHp() <= data.value) {
result.add(new EventBuilder(EventType.DestroyedEntityEvent)
.withTargetField(data.targetField)
.withTargetEntity(target.id)
.buildEntityEvent());
}
}
result.addAll(checkDeath(state, targetEntity, data.value));
}
case MoveRequest -> {
CharacterRequest data = (CharacterRequest)request;
@ -522,36 +493,7 @@ public class GameLogic {
.withAmount(state.partyConfig.mindStoneDMG)
.buildEntityEvent());
if(targetEntity instanceof Character) {
Character target = (Character)targetEntity;
if(target.hp.getValue() <= state.partyConfig.mindStoneDMG) {
List<StoneType> stones = Arrays.asList(target.inventory.getStonesAsArray());
Collections.shuffle(stones); // required by documents
ArrayList<IntVector2> used = new ArrayList<>();
for(StoneType stone: stones) {
ArrayList<IntVector2> options = getFreeNeighbour(state, target.getPosition(), used);
IntVector2 picked = options.get(rand.nextInt(options.size()));
used.add(picked);
result.add(new EventBuilder(EventType.SpawnEntityEvent)
.withEntity(new InfinityStone(
new EntityID(EntityType.InfinityStones, stone.getID()),
picked,
stone
))
.buildEntityEvent());
}
}
}else if(targetEntity instanceof Rock) {
Rock target = (Rock)targetEntity;
if(target.getHp() <= state.partyConfig.mindStoneDMG) {
result.add(new EventBuilder(EventType.DestroyedEntityEvent)
.withTargetField(data.targetField)
.withTargetEntity(target.id)
.buildEntityEvent());
}
}
result.addAll(checkDeath(state, targetEntity, state.partyConfig.mindStoneDMG));
}
case RealityStone -> {
EntityID target = null;
@ -595,6 +537,8 @@ public class GameLogic {
.withTargetField(data.targetField)
.withAmount(origin.meleeDamage * 2)
.buildEntityEvent());
result.addAll(checkDeath(state, targetEntity, origin.meleeDamage * 2));
}
case TimeStone -> {
Character origin = (Character)state.entities.findEntity(data.originEntity);
@ -639,6 +583,49 @@ public class GameLogic {
return result;
}
/**
* Checks for death of a Character or Rock and returns the resulting {@link Event Events}.
* @param state The game state to apply to
* @param targetEntity The entity to check
* @param damage The damage taken
* @return The resulting events
*/
private static ArrayList<Event> checkDeath(GameState state, Entity targetEntity, int damage) {
ArrayList<Event> result = new ArrayList<>();
if(targetEntity instanceof Character) {
Character target = (Character)targetEntity;
if(target.hp.getValue() <= damage) {
List<StoneType> stones = Arrays.asList(target.inventory.getStonesAsArray());
Collections.shuffle(stones); // required by documents
ArrayList<IntVector2> used = new ArrayList<>();
for(StoneType stone: stones) {
ArrayList<IntVector2> options = getFreeNeighbour(state, target.getPosition(), used);
IntVector2 picked = options.get(rand.nextInt(options.size()));
used.add(picked);
result.add(new EventBuilder(EventType.SpawnEntityEvent)
.withEntity(new InfinityStone(
new EntityID(EntityType.InfinityStones, stone.getID()),
picked,
stone
))
.buildEntityEvent());
}
}
}else if(targetEntity instanceof Rock) {
Rock target = (Rock)targetEntity;
if(target.getHp() <= damage) {
result.add(new EventBuilder(EventType.DestroyedEntityEvent)
.withTargetField(target.getPosition())
.withTargetEntity(target.id)
.buildEntityEvent());
}
}
return result;
}
/**
* Applies an {@link Event} to a {@link GameState}.