E.W.O. Next / All files / src/engine/Maps MapsTools.ts

59.76% Statements 49/82
68.97% Branches 20/29
55.56% Functions 5/9
60.76% Lines 48/79

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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185              7x 7x     7x   7x 4x   4x 1x     5x   3x 2x     1x     7x   49x 49x   49x   6x 6x   6x 6x   6x 6x   6x 6x   6x 6x 6x   6x 6x 6x   6x 6x 6x   6x 6x 6x   1x     49x     7x                             7x 1x           1x   1x 1x 6x                 7x                                   7x                                                                                     7x                               7x    
/**
 * @module Engine.Maps
 * Maps & Coords
 * @preferred
 */
 
import { IStateServer } from '@engine/reducers';
import { Coord, CoordEnvironmentFrontend, Direction, Plan, RawMap } from '@models';
import { List } from 'immutable';
import { Store } from 'redux';
 
export class MapsTools {
 
  public static getCoordFromPosition = (plan: Plan, posX: number, posY: number, store: Store<IStateServer>): Coord | null => {
    const map = store.getState().Maps.get(plan.id);
 
    if (map === undefined) {
      return null;
    }
 
    const coord = map.find(m => m.x === posX && m.y === posY);
 
    if (coord !== undefined) {
      return coord;
    }
 
    return null;
  }
 
  public static getRelativePosition = (posX: number, posY: number, direction: Direction) => {
 
    let newX = posX; // east-west position
    let newY = posY; // north-south
 
    switch (direction) {
      case Direction.North:
        newY -= 1;
        break;
      case Direction.South:
        newY += 1;
        break;
      case Direction.West:
        newX -= 1;
        break;
      case Direction.East:
        newX += 1;
        break;
      case Direction.NorthWest:
        newY -= 1;
        newX -= 1;
        break;
      case Direction.NorthEast:
        newY -= 1;
        newX += 1;
        break;
      case Direction.SouthWest:
        newY += 1;
        newX -= 1;
        break;
      case Direction.SouthEast:
        newY += 1;
        newX += 1;
        break;
      default:
        break;
    }
 
    return { x: newX, y: newY };
  }
 
  public static getPositionsFromAroundPosition = (centerX: number, centerY: number, insight: number) => {
    const xMin = centerX - insight;
    const xMax = centerX + insight;
 
    const yMin = centerY - insight;
    const yMax = centerY + insight;
 
    return {
      xMin,
      xMax,
      yMin,
      yMax,
    };
  }
 
  public static getCoordsFromAroundPosition = (plan: Plan, positions: {xMin: number, yMin: number, xMax: number, yMax: number}, store: Store<IStateServer>) => {
    const getCoords = store.getState().Maps.get(plan.id);
    const {
      xMin,
      xMax,
      yMin,
      yMax,
    } = positions;
 
    Eif (getCoords !== undefined) {
      return getCoords.filter(c =>
        c.x >= xMin &&
        c.x <= xMax &&
        c.y >= yMin &&
        c.y <= yMax);
    }
 
    return List();
  }
 
  public static getCoordMeta = (plan: Plan, posX: number, posY: number) => {
    let rawMap: RawMap;
    if (!MapsTools.loadedMaps.has(plan.rawMapName)) {
      // tslint:disable-next-line: non-literal-require
      rawMap = require(`../resources/maps/${plan.rawMapName}`);
      // import * from '../resources/maps/'
      MapsTools.loadedMaps = MapsTools.loadedMaps.set(plan.rawMapName, rawMap);
    } else {
      rawMap = MapsTools.loadedMaps.get(plan.rawMapName);
    }
 
    const meta = rawMap.meta[posX][posY] !== null ? rawMap.meta[posX][posY] : {};
 
    meta.block = rawMap.block[posX][posY] === 1;
 
    return meta;
  }
 
  public static getCoordsEnvironment = (plan: Plan, positions: {xMin: number, yMin: number, xMax: number, yMax: number}) => {
    let rawMap: RawMap;
    const {
      xMin,
      xMax,
      yMin,
      yMax,
    } = positions;
 
    if (!MapsTools.loadedMaps.has(plan.rawMapName)) {
      // tslint:disable-next-line: non-literal-require
      rawMap = require(`../resources/maps/${plan.rawMapName}`);
      // import * from '../resources/maps/'
      MapsTools.loadedMaps = MapsTools.loadedMaps.set(plan.rawMapName, rawMap);
    } else {
      rawMap = MapsTools.loadedMaps.get(plan.rawMapName);
    }
 
    const metas: CoordEnvironmentFrontend[] = [];
 
    for (let posX = xMin; posX <= xMax; posX += 1) {
      for (let posY = yMin; posY <= yMax; posY += 1) {
 
        const tiles = rawMap.tiles[posX][posY];
 
        // tslint:disable-next-line: no-for-in forin
        for (const layer in tiles) {
          const meta: CoordEnvironmentFrontend = {
            x: posX,
            y: posY,
            type: 'env',
            tile: tiles[layer],
            layer: Number.parseInt(layer, 10),
          };
 
          metas.push(meta);
        }
      }
    }
 
    return metas;
  }
 
  public static getMapsInfo = (plan: Plan) => {
    if (!MapsTools.loadedMaps.has(plan.rawMapName)) {
      // tslint:disable-next-line: non-literal-require
      const rawMap = require(`../resources/maps/${plan.rawMapName}`);
      // import * from '../resources/maps/'
      MapsTools.loadedMaps = MapsTools.loadedMaps.set(plan.rawMapName, rawMap);
 
      return rawMap.image;
    }
 
      return MapsTools.loadedMaps.get(plan.rawMapName).image;
 
 
 
  };
 
  private static loadedMaps: Map<string, RawMap> = new Map();
}