-
Notifications
You must be signed in to change notification settings - Fork 5
/
entity.ts
93 lines (78 loc) · 1.82 KB
/
entity.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
import { Matrix3, Vector2 } from "./math.js";
import { Node } from "./aabbtree.js";
export class Entity
{
protected _position: Vector2;
protected _rotation: number;
protected _scale: Vector2;
// Pointer to the node in the AABB tree
public node?: Node;
constructor()
{
this._position = new Vector2(0, 0);
this._rotation = 0;
this._scale = new Vector2(1, 1);
}
get position(): Vector2
{
return this._position;
}
set position(p: Vector2)
{
this._position.x = p.x;
this._position.y = p.y;
}
get rotation(): number
{
return this._rotation;
}
set rotation(r: number)
{
this._rotation = r;
}
get scale(): Vector2
{
return this._scale;
}
set scale(s: Vector2)
{
this._scale.x = s.x;
this._scale.y = s.y;
}
resetTransform(): void
{
this._position.clear();
this._rotation = 0;
this._scale = new Vector2(1, 1);
}
translate(t: Vector2): void
{
this._position.x += t.x;
this._position.y += t.y;
}
rotate(r: number): void
{
this._rotation += r;
}
doScale(s: Vector2): void
{
this._scale.x *= s.x;
this._scale.y *= s.y;
}
// Returns local to global transform
get localToGlobal(): Matrix3
{
return new Matrix3()
.translate(this._position.x, this._position.y)
.rotate(this._rotation)
.scale(this._scale.x, this._scale.y);
}
// Returns global to local transform
get globalToLocal(): Matrix3
{
return new Matrix3()
.scale(1.0 / this._scale.x, 1.0 / this._scale.y)
.rotate(-this._rotation)
.translate(-this._position.x, -this._position.y);
}
}