refactor: switch to EventBuilder

This commit is contained in:
punchready 2021-05-03 19:36:30 +02:00
parent 80c77b7956
commit a5ccba0326
3 changed files with 47 additions and 92 deletions

View File

@ -17,36 +17,6 @@ public class CharacterEvent extends Event {
public Integer amount = null; public Integer amount = null;
public StoneType stoneType = 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 @Override
public boolean check() { public boolean check() {
if(!super.check()) { if(!super.check()) {

View File

@ -13,21 +13,6 @@ public class EntityEvent extends Event {
public Integer amount = null; public Integer amount = null;
public Entity entity = 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 @Override
public boolean check() { public boolean check() {
if(!super.check()) { if(!super.check()) {

View File

@ -32,66 +32,66 @@ class GameLogic {
switch(request.type) { switch(request.type) {
case MeleeAttackRequest, RangedAttackRequest -> { case MeleeAttackRequest, RangedAttackRequest -> {
CharacterRequest data = (CharacterRequest)request; CharacterRequest data = (CharacterRequest)request;
result.add(new CharacterEvent() result.add(new EventBuilder(request.type == RequestType.MeleeAttackRequest ? EventType.MeleeAttackEvent : EventType.RangedAttackEvent)
.setOriginEntity(data.originEntity) .withOriginEntity(data.originEntity)
.setTargetEntity(data.targetEntity) .withTargetEntity(data.targetEntity)
.setOriginField(data.originField) .withOriginField(data.originField)
.setTargetField(data.targetField) .withTargetField(data.targetField)
.setAmount(data.value) .withAmount(data.value)
.type(request.type == RequestType.MeleeAttackRequest ? EventType.MeleeAttackEvent : EventType.RangedAttackEvent)); .buildCharacterEvent());
result.add(new EntityEvent() result.add(new EventBuilder(EventType.ConsumedAPEvent)
.setTargetEntity(data.originEntity) .withTargetEntity(data.originEntity)
.setTargetField(data.originField) .withTargetField(data.originField)
.setAmount(1) .withAmount(1)
.type(EventType.ConsumedAPEvent)); .buildEntityEvent());
result.add(new EntityEvent() result.add(new EventBuilder(EventType.TakenDamageEvent)
.setTargetEntity(data.targetEntity) .withTargetEntity(data.targetEntity)
.setTargetField(data.targetField) .withTargetField(data.targetField)
.setAmount(data.value) .withAmount(data.value)
.type(EventType.TakenDamageEvent)); .buildEntityEvent());
} }
case MoveRequest -> { case MoveRequest -> {
CharacterRequest data = (CharacterRequest)request; CharacterRequest data = (CharacterRequest)request;
result.add(new CharacterEvent() result.add(new EventBuilder(EventType.MoveEvent)
.setOriginEntity(data.originEntity) .withOriginEntity(data.originEntity)
.setOriginField(data.originField) .withOriginField(data.originField)
.setTargetField(data.targetField) .withTargetField(data.targetField)
.type(EventType.MoveEvent)); .buildCharacterEvent());
result.add(new EntityEvent() result.add(new EventBuilder(EventType.ConsumedMPEvent)
.setTargetEntity(data.originEntity) .withTargetEntity(data.originEntity)
.setTargetField(data.targetField) //when this event gets handled, the character already moved to the target field .withTargetField(data.targetField) //when this event gets handled, the character already moved to the target field
.setAmount(1) .withAmount(1)
.type(EventType.ConsumedMPEvent)); .buildEntityEvent());
for(Entity entity: state.entities.findByPosition(data.targetField)) { for(Entity entity: state.entities.findByPosition(data.targetField)) {
if(entity instanceof Character) { if(entity instanceof Character) {
result.add(new CharacterEvent() result.add(new EventBuilder(EventType.MoveEvent)
.setOriginEntity(entity.id) .withOriginEntity(entity.id)
.setOriginField(data.targetField) .withOriginField(data.targetField)
.setTargetField(data.originField) .withTargetField(data.originField)
.type(EventType.MoveEvent)); .buildCharacterEvent());
break; //we should only have one character per field anyways break; //we should only have one character per field anyways
} }
} }
} }
case ExchangeInfinityStoneRequest, UseInfinityStoneRequest -> { case ExchangeInfinityStoneRequest, UseInfinityStoneRequest -> {
CharacterRequest data = (CharacterRequest)request; CharacterRequest data = (CharacterRequest)request;
result.add(new CharacterEvent() result.add(new EventBuilder(request.type == RequestType.ExchangeInfinityStoneRequest ? EventType.ExchangeInfinityStoneEvent : EventType.UseInfinityStoneEvent)
.setOriginEntity(data.originEntity) .withOriginEntity(data.originEntity)
.setTargetEntity(data.targetEntity) .withOriginField(data.originField)
.setOriginField(data.originField) .withTargetEntity(data.targetEntity)
.setTargetField(data.targetField) .withTargetField(data.targetField)
.setStone(data.stoneType) .withStoneType(data.stoneType)
.type(request.type == RequestType.ExchangeInfinityStoneRequest ? EventType.ExchangeInfinityStoneEvent : EventType.UseInfinityStoneEvent)); .buildCharacterEvent());
result.add(new EntityEvent() result.add(new EventBuilder(EventType.ConsumedAPEvent)
.setTargetEntity(data.originEntity) .withTargetEntity(data.originEntity)
.setTargetField(data.originField) .withTargetField(data.originField)
.setAmount(1) .withAmount(1)
.type(EventType.ConsumedAPEvent)); .buildEntityEvent());
//TODO: add infinity stone usage effect in GameLogic.executeRequest //TODO: add infinity stone usage effect in GameLogic.executeRequest
} }
case DisconnectRequest -> { case DisconnectRequest -> {
result.add(new GameEvent() result.add(new EventBuilder(EventType.DisconnectEvent)
.type(EventType.DisconnectEvent)); .buildGameEvent());
} }
} }
@ -249,7 +249,7 @@ class GameLogic {
* Verifies that a {@link Character} has the required {@link StoneType}. * Verifies that a {@link Character} has the required {@link StoneType}.
*/ */
private static void requireInfinityStone(Character entity, StoneType stone) throws InvalidRequestException { 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(); throw new InvalidRequestException();
} }
} }