File size: 3,163 Bytes
4ee4376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cell from '../cell/cell';
import * as CONST from '../constants';
import * as layers from './layers/';

export default class map {
  #scene;

  grid = [];
  cells = [];
  list = [];
  selectedCell = { x: 0, y: 0 };
  sprites = { all: [] };
  layers = [];

  constructor (options) {
    this.#scene        = options.scene;
  }


  create () {
    for (let i = 0; i < this.#scene.importedData.cells.length; i++) {
      let c = new cell({
        scene: this.#scene,
        data: this.#scene.importedData.cells[i],
        index: i
      });

      if (!this.cells[c.x])      this.cells[c.x]      = [];
      if (!this.cells[c.x][c.y]) this.cells[c.x][c.y] = [];

      this.cells[c.x][c.y] = c;
      this.list.push(this.cells[c.x][c.y]);
    }

    // create map cells
    for (let x = 0; x < CONST.MAP_SIZE; x++)
      for (let y = 0; y < CONST.MAP_SIZE; y++)
        this.cells[x][y].create();

    // create layers
    this.layers.terrain   = new layers.terrain({ scene: this.#scene });
    this.layers.water     = new layers.water({ scene: this.#scene });
    this.layers.edge      = new layers.edge({ scene: this.#scene });
    this.layers.heightmap = new layers.heightmap({ scene: this.#scene });
    this.layers.zone      = new layers.zone({ scene: this.#scene });
    this.layers.building  = new layers.building({ scene: this.#scene });
    this.layers.road      = new layers.road({ scene: this.#scene });
    this.layers.rail      = new layers.rail({ scene: this.#scene });
    this.layers.power     = new layers.power({ scene: this.#scene });
    this.layers.highway   = new layers.highway({ scene: this.#scene });
    this.layers.subway    = new layers.subway({ scene: this.#scene });
    this.layers.pipe      = new layers.pipe({ scene: this.#scene });

    // do an initial object cull after rendering map
    this.#scene.viewport.cullObjects();
  }


  rotateCW () {
    let cells = this.cells;
    let tempCells = [];

    for (let x = 0; x < CONST.MAP_SIZE; x++) {
      for (let y = 0; y < CONST.MAP_SIZE; y++) {
        if (!tempCells[x])    tempCells[x]    = [];
        if (!tempCells[x][y]) tempCells[x][y] = [];

        let newX = y;
        let newY = CONST.MAP_SIZE - x - 1;

        tempCells[newX][newY] = cells[x][y];
      }
    }

    this.cells = tempCells;
  }


  rotateCCW () {
    let cells = this.cells;
    let tempCells = [];

    for (let x = 0; x < CONST.MAP_SIZE; x++) {
      for (let y = 0; y < CONST.MAP_SIZE; y++) {
        if (!tempCells[x])    tempCells[x]    = [];
        if (!tempCells[x][y]) tempCells[x][y] = [];

        let newX = CONST.MAP_SIZE - y - 1;
        let newY = x;

        tempCells[newX][newY] = cells[x][y];
      }
    }

    this.cells = tempCells;
  }


  update () {

  }


  shutdown () {
    if (this.list.length > 0)
      this.list.forEach((cell) => {
        cell.shutdown();
      });

    if (this.sprites.all.length > 0)
      this.sprites.all.forEach((sprite) => {
        sprite.destroy();
      });
  }


  addSprite (sprite, type) {
    if (!this.sprites[type])
      this.sprites[type] = [];

    this.sprites[type].push(sprite);
    this.sprites.all.push(sprite);
  }
}