From adb0a497112f842434b2fe1adf62a5ae5874a929 Mon Sep 17 00:00:00 2001 From: punchready Date: Thu, 5 Aug 2021 12:16:29 +0200 Subject: [PATCH] feat: make rocks attackable --- .../gamelibrary/gamelogic/GameLogic.java | 99 +++++++++++++------ 1 file changed, 69 insertions(+), 30 deletions(-) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java index c4801f8..feee29d 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -27,7 +27,7 @@ public class GameLogic { * Checks a {@link Request} for validity for a {@link GameState}. * @param state The game state to check on * @param request The request to validate - * @return Whether or not the request is valid + * @return Whether the request is valid */ protected static boolean checkRequest(GameState state, Request request) { try { @@ -36,15 +36,20 @@ public class GameLogic { CharacterRequest data = (CharacterRequest)request; Character origin = getCharacter(state, data.originField, data.originEntity); - Character target = getCharacter(state, data.targetField, data.targetEntity); + Entity target = getAttackable(state, data.targetField, data.targetEntity); requireTurn(state, origin); - requireOppositeTeam(origin, target); requireAlive(origin); - requireAlive(target); requireAP(origin, 1); + if(target instanceof Character) { + Character targetCharacter = (Character)target; + + requireOppositeTeam(origin, targetCharacter); + requireAlive(targetCharacter); + } + if(request.type == RequestType.MeleeAttackRequest) { if(origin.meleeDamage != data.value) { throw new InvalidRequestException("Invalid melee damage"); @@ -183,6 +188,22 @@ public class GameLogic { return false; } + /** + * Retrieves an attack-able Entity ({@link Character} or {@link Rock}) for a {@link Request}. + * @param state The game state to use + * @param position The requested position + * @param entityID The requested {@link EntityID} + * @return The found entity + * @throws InvalidRequestException if the entity is invalid or not found + */ + private static Entity getAttackable(GameState state, IntVector2 position, EntityID entityID) throws InvalidRequestException { + Entity entity = state.entities.findEntity(entityID); + if(entity == null || !entity.getPosition().equals(position) || (!(entity instanceof Character) && !(entity instanceof Rock)) || entity.id.type == EntityType.NPC) { + throw new InvalidRequestException("Invalid target character or rock"); + } + return entity; + } + /** * Retrieves a {@link Character} for a {@link Request}. * @param state The game state to use @@ -315,23 +336,34 @@ public class GameLogic { .withAmount(data.value) .buildEntityEvent()); - Character target = (Character)state.entities.findEntity(data.targetEntity); - if(target.hp.getValue() <= data.value) { + Entity targetEntity = state.entities.findEntity(data.targetEntity); + if(targetEntity instanceof Character) { + Character target = (Character)targetEntity; + if(target.hp.getValue() <= data.value) { - List stones = Arrays.asList(target.inventory.getStonesAsArray()); - Collections.shuffle(stones); // required by documents + List stones = Arrays.asList(target.inventory.getStonesAsArray()); + Collections.shuffle(stones); // required by documents - ArrayList used = new ArrayList<>(); - for(StoneType stone: stones) { - ArrayList 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 - )) + ArrayList used = new ArrayList<>(); + for(StoneType stone: stones) { + ArrayList 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()); } } @@ -526,19 +558,26 @@ public class GameLogic { case TakenDamageEvent -> { EntityEvent data = (EntityEvent)event; - Character target = (Character)state.entities.findEntity(data.targetEntity); - target.hp.decreaseValue(data.amount); + Entity targetEntity = state.entities.findEntity(data.targetEntity); - EntityType opposing = target.id.type == EntityType.P1 ? EntityType.P2 : EntityType.P1; - state.winConditions.increaseValue(opposing, WinCondition.TotalDamage, data.amount); - if(!target.isAlive()) { - target.inventory.clear(); - state.winConditions.increaseValue(opposing, WinCondition.TotalKnockouts, 1); - } + if(targetEntity instanceof Character) { + Character target = (Character)targetEntity; + target.hp.decreaseValue(data.amount); - if(state.activeCharacter != null && state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == NPCType.Thanos.getID()) { - NPC thanos = (NPC)state.entities.findEntity(state.activeCharacter); - target.inventory.transfer(thanos.inventory); + EntityType opposing = target.id.type == EntityType.P1 ? EntityType.P2 : EntityType.P1; + state.winConditions.increaseValue(opposing, WinCondition.TotalDamage, data.amount); + if(!target.isAlive()) { + target.inventory.clear(); + state.winConditions.increaseValue(opposing, WinCondition.TotalKnockouts, 1); + } + + if(state.activeCharacter != null && state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == NPCType.Thanos.getID()) { + NPC thanos = (NPC)state.entities.findEntity(state.activeCharacter); + target.inventory.transfer(thanos.inventory); + } + }else if(targetEntity instanceof Rock) { + Rock target = (Rock)targetEntity; + target.decreaseHp(data.amount); } } case ConsumedAPEvent -> {