File size: 4,129 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
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
import Phaser from 'phaser';
import * as CONST from '../../constants';

export default class tile {
  #debug = {};

  id;
  cell;
  map;
  tile;
  depth;
  rotation;
  sprite;
  type;
  x;
  y;

  props = {
    draw: false,
    flip: false,
    offsetX: 0,
    offsetY: 0,
    hitbox: null
  };


  constructor (options) {
    this.cell      = options.cell;
    this.map       = options.cell.scene.city.map;
    this.rotation  = options.cell.scene.city.rotation;
    this.id        = options.id;
    this.type      = options.type;
    this.tile      = this.get(options.id);
    this.x         = options.x || 0;
    this.y         = options.y || 0;
    this.depth     = {
      cell:       options.cell.depth,
      layer:      options.layerDepth || 0,
      tile:       options.tileDepth || 0,
      additional: options.additionalDepth || 0,
    };

    if (!this.check()) return;

    this.props.draw = true;
  }


  get (id) {
    id = this.cell?.scene?.tiles?.[id]?.rotate[this.rotation];

    if (!id) return false;

    return this.cell.scene.tiles[id];
  }


  check () {
    if (!this.cell) return false;
    if (!this.tile) return false;

    return true;
  }


  hide () {
    if (this.sprite) this.sprite.setVisible(false);
  }


  show () {
    if (this.sprite) this.sprite.setVisible(true);
  }


  refresh () {
    this.hide();
    this.show();
  }


  position () {
    this.x = this.cell.position.topLeft.x + this.props.offsetX;
    this.y = this.cell.position.topLeft.y + this.props.offsetY;
  }


  create () {
    if (!this.props.draw) return;

    this.logic();
    this.position();

    if (this.tile.baseLayer) {
      let tile = this.get(this.tile.baseLayer);
      this.cell.tiles.set(this.tile.subtype, tile.id);
      this.cell.tiles[this.tile.subtype].depthAdjustment = -2;
      this.cell.tiles[this.tile.subtype].create();
    }

    if (this.tile.frames > 1)
      this.sprite = this.cell.scene.add.sprite(this.x, this.y, CONST.TILE_ATLAS).play(this.tile.image);
    else
      this.sprite = this.cell.scene.add.sprite(this.x, this.y, CONST.TILE_ATLAS, this.tile.textures[0]);

    this.sprite.cell = this.cell;
    this.sprite.type = this.type;
    this.sprite.setScale(CONST.SCALE);
    this.sprite.setOrigin(CONST.ORIGIN_X, CONST.ORIGIN_Y);
    this.sprite.setDepth(this.depth.cell + this.depth.layer + this.depth.tile + this.depth.additional);

    this.cell.tiles.addSprite(this.sprite, this.type);
    this.cell.tiles.addTile(this);

    this.events();
    //this.debugBox();
    //this.debugHitbox();
  }

  events () {
    if (!this.sprite) return;

    this.props.hitbox = this.tile.hitbox;

    this.sprite.setInteractive(this.props.hitbox, Phaser.Geom.Polygon.Contains);
    this.sprite.on(CONST.E_POINTER_OVER, this.cell.onPointerOver, this.cell);
    this.sprite.on(CONST.E_POINTER_OUT,  this.cell.onPointerOut,  this.cell);
    this.sprite.on(CONST.E_POINTER_MOVE, this.cell.onPointerMove, this.cell);
    this.sprite.on(CONST.E_POINTER_DOWN, this.cell.onPointerDown, this.cell);
    this.sprite.on(CONST.E_POINTER_UP,   this.cell.onPointerUp,   this.cell);
  }

  logic () {
    return;
  }

  simulation () {
    return;
  }

  debugBox () {
    let bounds = this.sprite.getBounds();
    let center = this.sprite.getCenter();

    this.#debug.box = this.cell.scene.add.rectangle(bounds.x, bounds.y, bounds.width, bounds.height, 0x00ffff, 0);
    this.#debug.box.setOrigin(CONST.ORIGIN_X, CONST.ORIGIN_Y);
    this.#debug.box.setDepth(this.depth + 2);
    this.#debug.box.setStrokeStyle(1, 0x00ffff, 0.5);

    this.#debug.center = this.cell.scene.add.circle(center.x, center.y, 1, 0x00ffff, 0.75);
    this.#debug.center.setOrigin(CONST.ORIGIN_X, CONST.ORIGIN_Y);
    this.#debug.center.setDepth(this.sprite.depth + 2);
  }

  debugHitbox () {
    this.#debug.hitbox = this.cell.scene.add.polygon(this.x, this.y, this.props.hitbox.points, 0xff00ff, 0);
    this.#debug.hitbox.setDepth(this.sprite.depth + 16);
    this.#debug.hitbox.setScale(CONST.SCALE);
    this.#debug.hitbox.setOrigin(CONST.ORIGIN_X, CONST.ORIGIN_Y);
    this.#debug.hitbox.setStrokeStyle(1, 0xff00ff, 0.5);
  }
}