feat: properly handle movement requests

This commit is contained in:
punchready 2021-05-02 14:38:03 +02:00
parent d06ac91ef8
commit 05f4751a2c
2 changed files with 17 additions and 4 deletions

View File

@ -13,12 +13,15 @@ import java.util.ArrayList;
/** Contains game logic handling. */
class GameLogic {
/**
* Produces resulting {@link Event}s from a given {@link Request} independently of any {@link GameState}.
* Produces resulting {@link Event}s from a given {@link Request}.
* @param state The game state to execute on
* @param request The request to execute
* @return The list of resulting events
*/
public static ArrayList<Event> executeRequest(Request request) {
public static ArrayList<Event> executeRequest(GameState state, Request request) {
ArrayList<Event> result = new ArrayList<>();
//TODO: refactor for EventBuilder in GameLogic.executeRequest
switch(request.type) {
case MeleeAttackRequest, RangedAttackRequest -> {
CharacterRequest data = (CharacterRequest)request;
@ -52,7 +55,16 @@ class GameLogic {
.setTargetField(data.targetField) //when this event gets handled, the character already moved to the target field
.setAmount(1)
.type(EventType.ConsumedMPEvent));
//TODO: execute character swap in GameLogic.executeRequest
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));
break; //we should only have one character per field anyways
}
}
}
case ExchangeInfinityStoneRequest, UseInfinityStoneRequest -> {
CharacterRequest data = (CharacterRequest)request;
@ -68,6 +80,7 @@ class GameLogic {
.setTargetField(data.originField)
.setAmount(1)
.type(EventType.ConsumedAPEvent));
//TODO: add infinity stone use effect in GameLogic.executeRequest
}
case DisconnectRequest -> {

View File

@ -32,7 +32,7 @@ class GameStateManager {
for(Request request: requests) {
if (GameLogic.checkRequest(snapshot, request)) {
ArrayList<Event> result = GameLogic.executeRequest(request);
ArrayList<Event> result = GameLogic.executeRequest(snapshot, request);
if(apply) {
queue.addAll(result);
}