From a5ccba0326579068cf524eab9870821888e1f15f Mon Sep 17 00:00:00 2001 From: punchready Date: Mon, 3 May 2021 19:36:30 +0200 Subject: [PATCH] refactor: switch to EventBuilder --- .../gamelibrary/events/CharacterEvent.java | 30 ------ .../gamelibrary/events/EntityEvent.java | 15 --- .../gamelibrary/gamelogic/GameLogic.java | 94 +++++++++---------- 3 files changed, 47 insertions(+), 92 deletions(-) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/events/CharacterEvent.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/events/CharacterEvent.java index f1f3ba9..421b576 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/events/CharacterEvent.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/events/CharacterEvent.java @@ -17,36 +17,6 @@ public class CharacterEvent extends Event { public Integer amount = null; public StoneType stoneType = null; - public CharacterEvent setOriginEntity(EntityID originEntity) { - this.originEntity = originEntity; - return this; - } - - public CharacterEvent setTargetEntity(EntityID targetEntity) { - this.targetEntity = targetEntity; - return this; - } - - public CharacterEvent setOriginField(IntVector2 originField) { - this.originField = originField; - return this; - } - - public CharacterEvent setTargetField(IntVector2 targetField) { - this.targetField = targetField; - return this; - } - - public CharacterEvent setAmount(Integer amount) { - this.amount = amount; - return this; - } - - public CharacterEvent setStone(StoneType stoneType) { - this.stoneType = stoneType; - return this; - } - @Override public boolean check() { if(!super.check()) { diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/events/EntityEvent.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/events/EntityEvent.java index 3e229b6..58f8470 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/events/EntityEvent.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/events/EntityEvent.java @@ -13,21 +13,6 @@ public class EntityEvent extends Event { public Integer amount = null; public Entity entity = null; - public EntityEvent setTargetEntity(EntityID targetEntity) { - this.targetEntity = targetEntity; - return this; - } - - public EntityEvent setTargetField(IntVector2 targetField) { - this.targetField = targetField; - return this; - } - - public EntityEvent setAmount(Integer amount) { - this.amount = amount; - return this; - } - @Override public boolean check() { if(!super.check()) { 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 90ae8a7..d2f41e5 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -32,66 +32,66 @@ class GameLogic { switch(request.type) { case MeleeAttackRequest, RangedAttackRequest -> { CharacterRequest data = (CharacterRequest)request; - result.add(new CharacterEvent() - .setOriginEntity(data.originEntity) - .setTargetEntity(data.targetEntity) - .setOriginField(data.originField) - .setTargetField(data.targetField) - .setAmount(data.value) - .type(request.type == RequestType.MeleeAttackRequest ? EventType.MeleeAttackEvent : EventType.RangedAttackEvent)); - result.add(new EntityEvent() - .setTargetEntity(data.originEntity) - .setTargetField(data.originField) - .setAmount(1) - .type(EventType.ConsumedAPEvent)); - result.add(new EntityEvent() - .setTargetEntity(data.targetEntity) - .setTargetField(data.targetField) - .setAmount(data.value) - .type(EventType.TakenDamageEvent)); + result.add(new EventBuilder(request.type == RequestType.MeleeAttackRequest ? EventType.MeleeAttackEvent : EventType.RangedAttackEvent) + .withOriginEntity(data.originEntity) + .withTargetEntity(data.targetEntity) + .withOriginField(data.originField) + .withTargetField(data.targetField) + .withAmount(data.value) + .buildCharacterEvent()); + result.add(new EventBuilder(EventType.ConsumedAPEvent) + .withTargetEntity(data.originEntity) + .withTargetField(data.originField) + .withAmount(1) + .buildEntityEvent()); + result.add(new EventBuilder(EventType.TakenDamageEvent) + .withTargetEntity(data.targetEntity) + .withTargetField(data.targetField) + .withAmount(data.value) + .buildEntityEvent()); } case MoveRequest -> { CharacterRequest data = (CharacterRequest)request; - result.add(new CharacterEvent() - .setOriginEntity(data.originEntity) - .setOriginField(data.originField) - .setTargetField(data.targetField) - .type(EventType.MoveEvent)); - result.add(new EntityEvent() - .setTargetEntity(data.originEntity) - .setTargetField(data.targetField) //when this event gets handled, the character already moved to the target field - .setAmount(1) - .type(EventType.ConsumedMPEvent)); + result.add(new EventBuilder(EventType.MoveEvent) + .withOriginEntity(data.originEntity) + .withOriginField(data.originField) + .withTargetField(data.targetField) + .buildCharacterEvent()); + result.add(new EventBuilder(EventType.ConsumedMPEvent) + .withTargetEntity(data.originEntity) + .withTargetField(data.targetField) //when this event gets handled, the character already moved to the target field + .withAmount(1) + .buildEntityEvent()); for(Entity entity: state.entities.findByPosition(data.targetField)) { if(entity instanceof Character) { - result.add(new CharacterEvent() - .setOriginEntity(entity.id) - .setOriginField(data.targetField) - .setTargetField(data.originField) - .type(EventType.MoveEvent)); + result.add(new EventBuilder(EventType.MoveEvent) + .withOriginEntity(entity.id) + .withOriginField(data.targetField) + .withTargetField(data.originField) + .buildCharacterEvent()); break; //we should only have one character per field anyways } } } case ExchangeInfinityStoneRequest, UseInfinityStoneRequest -> { CharacterRequest data = (CharacterRequest)request; - result.add(new CharacterEvent() - .setOriginEntity(data.originEntity) - .setTargetEntity(data.targetEntity) - .setOriginField(data.originField) - .setTargetField(data.targetField) - .setStone(data.stoneType) - .type(request.type == RequestType.ExchangeInfinityStoneRequest ? EventType.ExchangeInfinityStoneEvent : EventType.UseInfinityStoneEvent)); - result.add(new EntityEvent() - .setTargetEntity(data.originEntity) - .setTargetField(data.originField) - .setAmount(1) - .type(EventType.ConsumedAPEvent)); + result.add(new EventBuilder(request.type == RequestType.ExchangeInfinityStoneRequest ? EventType.ExchangeInfinityStoneEvent : EventType.UseInfinityStoneEvent) + .withOriginEntity(data.originEntity) + .withOriginField(data.originField) + .withTargetEntity(data.targetEntity) + .withTargetField(data.targetField) + .withStoneType(data.stoneType) + .buildCharacterEvent()); + result.add(new EventBuilder(EventType.ConsumedAPEvent) + .withTargetEntity(data.originEntity) + .withTargetField(data.originField) + .withAmount(1) + .buildEntityEvent()); //TODO: add infinity stone usage effect in GameLogic.executeRequest } case DisconnectRequest -> { - result.add(new GameEvent() - .type(EventType.DisconnectEvent)); + result.add(new EventBuilder(EventType.DisconnectEvent) + .buildGameEvent()); } } @@ -249,7 +249,7 @@ class GameLogic { * Verifies that a {@link Character} has the required {@link StoneType}. */ private static void requireInfinityStone(Character entity, StoneType stone) throws InvalidRequestException { - if(!entity.inventory.hasStone(stone)) { + if(stone == null || !entity.inventory.hasStone(stone)) { throw new InvalidRequestException(); } }