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