refactor: reduce the type casting hell

This commit is contained in:
punchready 2021-06-03 04:18:16 +02:00
parent 5a90056cd8
commit d4a929878d

View File

@ -493,28 +493,36 @@ public class GameLogic {
state.entities.removeEntity(((EntityEvent)event).targetEntity);
}
case TakenDamageEvent -> {
Character target = (Character)state.entities.findEntity(((CharacterEvent)event).targetEntity);
target.hp.decreaseValue(((CharacterEvent)event).amount);
CharacterEvent data = (CharacterEvent)event;
Character target = (Character)state.entities.findEntity(data.targetEntity);
target.hp.decreaseValue(data.amount);
EntityType opposing = target.id.type == EntityType.P1 ? EntityType.P2 : EntityType.P1;
state.winConditions.increaseValue(opposing, WinCondition.TotalDamage, ((CharacterEvent)event).amount);
state.winConditions.increaseValue(opposing, WinCondition.TotalDamage, data.amount);
if(!target.isAlive()) {
state.winConditions.increaseValue(opposing, WinCondition.TotalKnockouts, 1);
}
}
case ConsumedAPEvent -> {
((Character)state.entities.findEntity(((CharacterEvent)event).targetEntity)).ap.decreaseValue(((CharacterEvent)event).amount);
CharacterEvent data = (CharacterEvent)event;
((Character)state.entities.findEntity(data.targetEntity)).ap.decreaseValue(data.amount);
}
case ConsumedMPEvent -> {
((NPC)state.entities.findEntity(((CharacterEvent)event).targetEntity)).mp.decreaseValue(((CharacterEvent)event).amount);
CharacterEvent data = (CharacterEvent)event;
((NPC)state.entities.findEntity(data.targetEntity)).mp.decreaseValue(data.amount);
}
case TurnEvent -> {
if(((GameEvent)event).nextCharacter.type != EntityType.NPC) {
Character target = (Character)state.entities.findEntity(((GameEvent)event).nextCharacter);
GameEvent data = (GameEvent)event;
if(data.nextCharacter.type != EntityType.NPC) {
Character target = (Character)state.entities.findEntity(data.nextCharacter);
target.ap.setValue(target.ap.getMax());
target.mp.setValue(target.mp.getMax());
}else if(((GameEvent)event).nextCharacter.id == NPCType.Thanos.getID()) {
NPC target = (NPC)state.entities.findEntity(((GameEvent)event).nextCharacter);
}else if(data.nextCharacter.id == NPCType.Thanos.getID()) {
NPC target = (NPC)state.entities.findEntity(data.nextCharacter);
target.mp.setValue(target.mp.getMax());
}
}
@ -522,26 +530,32 @@ public class GameLogic {
state.entities.addEntity(((EntityEvent)event).entity);
}
case HealedEvent -> {
((Character)state.entities.findEntity(((CharacterEvent)event).targetEntity)).hp.increaseValue(((CharacterEvent)event).amount);
CharacterEvent data = (CharacterEvent)event;
((Character)state.entities.findEntity(data.targetEntity)).hp.increaseValue(data.amount);
}
case MoveEvent -> {
NPC target = (NPC)state.entities.findEntity(((CharacterEvent)event).originEntity);
for(Entity entity: state.entities.findByPosition(((CharacterEvent)event).targetField)) {
CharacterEvent data = (CharacterEvent)event;
NPC target = (NPC)state.entities.findEntity(data.originEntity);
for(Entity entity: state.entities.findByPosition(data.targetField)) {
if(entity instanceof InfinityStone) {
target.inventory.addStone(((InfinityStone)entity).type);
state.winConditions.updateValue(target.id.type, WinCondition.MaxStones, target.inventory.getSize());
}
}
target.setPosition(((CharacterEvent)event).targetField);
target.setPosition(data.targetField);
}
case UseInfinityStoneEvent -> {
state.stoneCooldown.setCooldown(((CharacterEvent)event).stoneType);
}
case ExchangeInfinityStoneEvent -> {
((Character)state.entities.findEntity(((CharacterEvent)event).originEntity)).inventory.removeStone(((CharacterEvent)event).stoneType);
Character target = (Character)state.entities.findEntity(((CharacterEvent)event).targetEntity);
target.inventory.addStone(((CharacterEvent)event).stoneType);
CharacterEvent data = (CharacterEvent)event;
((Character)state.entities.findEntity(data.originEntity)).inventory.removeStone(data.stoneType);
Character target = (Character)state.entities.findEntity(data.targetEntity);
target.inventory.addStone(data.stoneType);
state.winConditions.updateValue(target.id.type, WinCondition.MaxStones, target.inventory.getSize());
}
@ -550,15 +564,10 @@ public class GameLogic {
state.entities.clear();
state.entities.addEntities(data.entities);
state.mapSize.set(data.mapSize);
state.turnOrder = ArrayTools.toArrayList(data.turnOrder);
state.activeCharacter = data.activeCharacter;
state.stoneCooldown.import_(data.stoneCooldowns);
state.won = data.winCondition;
}
}