E.W.O. Next / All files / src/engine/Commands/CommandsTemplate moveCommand.ts

21.43% Statements 6/28
0% Branches 0/10
0% Functions 0/3
21.43% Lines 6/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92          5x 5x 5x   5x                         5x   5x                                                                                                                                      
/**
 * @module Engine.Commands.Templates
 * Move
 */
 
import { move } from '@engine/Characters/actions';
import { CharactersTools } from '@engine/Characters/CharacterTools';
import { MapsTools } from '@engine/Maps/MapsTools';
import { Character, Direction } from '@models';
import { Command, CommandList, CommandStatus } from '../Command';
 
export interface MovePayload {
  mat: number;
  Direction: Direction;
}
 
export interface MoveMeta {
  Character: Character;
  x: number;
  y: number;
}
 
const costMove = 1;
 
export class MoveCommand implements Command {
  public readonly command = CommandList.move;
  public readonly payload: MovePayload;
  public status: CommandStatus;
 
  constructor(payload: MovePayload) {
    this.payload = payload;
  }
 
  public eligible(payload: MovePayload, store) {
 
    // get the current character
    const currentCharacter = CharactersTools.currentCharacter(payload.mat, store);
    console.log({ currentCharacter });
 
    if (currentCharacter === null) {
      return false;
    }
 
    if (!currentCharacter.position) {
      // the character is not incarned
      return false;
    }
 
    if (currentCharacter.currentSpeed <= 0) {
      // the character cannot move
      return false;
    }
 
    // get the current position
    const coord = currentCharacter.position.coord;
 
    console.log({ position: coord });
 
    if (coord === null) {
      return false;
    }
 
    // get the new position
    const { x, y } = MapsTools.getRelativePosition(coord.x, coord.y, payload.Direction);
 
    console.log({ x, y });
 
    // check if the new position is free
    const newCoord = MapsTools.getCoordFromPosition(currentCharacter.position.plan, x, y, store);
 
    console.log({ newCoord });
 
    if (newCoord === null) {
      return {
        result: true,
        meta: {
          Character: currentCharacter,
          x,
          y,
        },
      };
    }
 
    return false;
  }
 
  public execute(meta: MoveMeta) {
    return [move(meta.Character, meta.x, meta.y, costMove)];
  }
 
}