-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardSystem.ts
123 lines (94 loc) · 2.27 KB
/
BoardSystem.ts
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
import {WorldParent} from '../WorldParent'
import {
Vec2
} from '../Types'
import {
System,
World,
Component,
Entity,
EntityRef,
EntitySet,
EntityObject,
Query,
} from 'ape-ecs';
const ApeECS = {
World,
System,
Component,
};
class BoardSystem extends ApeECS.System {
wp: WorldParent;
constructor(world, worldParent) {
super(world);
this.wp = worldParent;
}
init() {
}
update(tick) {
}
initBoard(x: number, y: number): Entity {
const e = this.world.createEntity({
id: 'gboard',
components: [
{
type: 'GameBoard',
key: 'board',
tiles: [x,y],
}
]
});
return e;
}
getClickBounds(): [Vec2,Vec2] {
const e = this.world.getEntity('gboard');
const tiles = e.c.board.tiles;
// console.log(e);
// const tiles = e.c.gboard.tiles;
const ul = e.c.board.tileOffset;
const br = this.tileToPixel([tiles[0],tiles[1]]);
return [ul, br];
}
// returns the upper left pixel of a square
// drawing a rectangle here must be able to fill the entire grid correctly
tileToPixel(tile: Vec2): Vec2 {
const e = this.world.getEntity('gboard');
// console.log(e);
// const tiles = e.c.gboard.tiles;
const offset = e.c.board.tileOffset;
const sz = e.c.board.gsize;
const x = offset[0] + tile[0] * sz[0];
const y = 1+offset[1] + tile[1] * sz[1]; // not sure what this fudge is
return [x,y];
}
// istanbul ignore next
pixelToTile(px: Vec2): Vec2 {
const e = this.world.getEntity('gboard');
// console.log(e);
// const tiles = e.c.gboard.tiles;
const offset = e.c.board.tileOffset;
const sz = e.c.board.gsize;
let raw: Vec2 = [0,0];
const [x,y] = px;
// x = o + (t * s)
// x - o = (t * s)
// (x - o)/s = t
// y = 1+o + (t*s)
// y - 1 - o
raw[0] = (x - offset[0] ) / sz[0];
raw[1] = (y - offset[1] - 1) / sz[1];
// due to the rounding, we snap to the wrong grid
// offset by 50% of a tile
const t: Vec2 = [Math.round(raw[0]-0.5), Math.round(raw[1]-0.5)];
// const t = raw;
return t;
}
getBoardSize(): Vec2 {
const gboard = this.world.getEntity('gboard');
const sz: Vec2 = gboard.c.board.tiles;
return sz;
}
}
export {
BoardSystem,
}