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 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()) {

View File

@ -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()) {

View File

@ -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();
}
}