-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwid-tiles-store.html
More file actions
162 lines (136 loc) · 4.27 KB
/
wid-tiles-store.html
File metadata and controls
162 lines (136 loc) · 4.27 KB
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="wid-tile.html">
<!--
An element that display store a set of hexagonal tiles.
The store is indexed twice. Once with the cube layout and once with the
even-q layout.
Example:
```
<wid-tiles-store id="store"></wid-tiles-store>
```
@group Wid
@element wid-tiles-store
@demo demo/index.html
@hero hero.svg
-->
<script>
(function() {
Polymer({
is: 'wid-tiles-store',
properties: {
type: {
type: String,
value: 'circle'
},
tilesXyz: {
type: Array,
notify: true,
value: {}
},
tilesIj: {
type: Array,
notify: true,
value: {}
}
},
/**
* Compute the position of a tile in the even-q and cube layouts.
* @param {Tile} tile A tile with at least one set of coordinates in one layout.
* @return {{i, j, x, y, z: Number}} The complete coordinates of the tile.
*/
_getPositions: function (tile) {
/*jslint bitwise: true */
var pos = {
x: tile.x || (tile.j - Math.floor((tile.i - (tile.i&1)) / 2)),
y: tile.y || (- tile.x - tile.z),
z: tile.z || tile.i,
i: tile.i || tile.z,
j: tile.j || (tile.x + Math.floor((tile.z - (tile.z&1)) / 2))
};
pos.x = (!pos.x) ? 0: pos.x;
pos.y = (!pos.y) ? 0: pos.y;
pos.z = (!pos.z) ? 0: pos.z;
pos.i = (!pos.i) ? 0: pos.i;
pos.j = (!pos.j) ? 0: pos.j;
return pos;
},
/**
* Add a tile to the store.
* @param {Tile} tile A tile.
*/
addTile: function (tile) {
this._addTileXYZ(tile);
this._addTileIJ(tile);
},
/**
* Add a tile in the tilesIj Map and notifies parents.
* @param {Tile} tile A tile.
*/
_addTileIJ: function (tile) {
var pos = this._getPositions(tile);
this.tilesIj[this.type] = this.tilesIj[this.type] || [];
this.tilesIj[this.type][pos.i] = this.tilesIj[this.type][pos.i] || [];
this.tilesIj[this.type][pos.i][pos.j] = this.tilesIj[this.type][pos.i][pos.j] || [];
this.set('tilesIj.' + this.type + '.' + pos.i + '.' + pos.j, tile);
},
/**
* Add a tile in the tilesXyz Map and notifies parents.
* @param {Tile} tile A tile.
*/
_addTileXYZ: function (tile) {
var pos = this._getPositions(tile);
this.tilesXyz[this.type] = this.tilesXyz[this.type] || [];
this.tilesXyz[this.type][pos.x] = this.tilesXyz[this.type][pos.x] || [];
this.tilesXyz[this.type][pos.x][pos.y] = this.tilesXyz[this.type][pos.x][pos.y] || [];
this.tilesXyz[this.type][pos.x][pos.y][pos.z] = this.tilesXyz[this.type][pos.x][pos.y][pos.z] || [];
this.set('tilesXyz.' + [this.type] + '.' + pos.x + '.' + pos.y + '.' + pos.z, tile);
},
/**
* Return a tile knowing the cube coordinates.
* If the tile doesn't exist. We create a fake tile and store it.
* This fake tile is usefull when the user get the neighbors of a tile.
* @param {Number} x X coordinate
* @param {Number} y Y coordinate
* @param {Number} z Z coordinate
* @return {Tile} The tile.
*/
getTileXYZ: function (x, y, z) {
try {
return this.tilesXyz[this.type][x][y][z];
} catch (e) {
var emptyTile = document.createElement('wid-tile');
emptyTile.x = x;
emptyTile.y = y;
emptyTile.z = z;
emptyTile.type = this.type;
emptyTile.isFake = true;
emptyTile._store = this;
this.addTile(emptyTile);
return emptyTile;
}
},
/**
* Return a tile knowing the even-q coordinates.
* If the tile doesn't exist. We create a fake tile and store it.
* This fake tile is usefull when the user get the neighbors of a tile.
* @param {Number} i I coordinate
* @param {Number} j J coordinate
* @return {Tile} The tile.
*/
getTileIJ: function (i, j) {
try {
return this.tilesIj[this.type][i][j];
} catch (e) {
var emptyTile = document.createElement('wid-tile');
emptyTile.i = i;
emptyTile.j = j;
emptyTile.isFake = true;
emptyTile.type = this.type;
emptyTile._store = this;
this.addTile(emptyTile);
return emptyTile;
}
}
});
})();
</script>