BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / transform.js

transform.js

autoformat all
timfa Tim · pushed by timfa · 4 years ago
fc3febb8 History
transform.js 1.3 KiB Download Raw
1class Transform
2{
3 position = null;
4 scale = null;
5 rotation = 0;
6 gameObject = null;
7
8 velocity = null;
9 angularVelocity = 0;
10
11 get size ()
12 {
13 return new vector(
14 this.scale.x * this.gameObject.renderer.sprite.width,
15 this.scale.y * this.gameObject.renderer.sprite.height);
16 }
17
18 constructor (gameObject, x, y, w, h, r)
19 {
20 this.gameObject = gameObject;
21 this.position = new vector(x, y);
22 this.scale = new vector(w, h);
23 this.rotation = r;
24
25 this.velocity = new vector(0, 0);
26 }
27
28 update (deltaTime)
29 {
30 this.position = this.position.add(this.velocity.stretch(deltaTime));
31 this.rotation += this.angularVelocity * deltaTime;
32 }
33
34 pointTowards (point)
35 {
36 var delta = point.subtract(this.position);
37
38 this.rotation = delta.toAngle();
39 }
40
41 left ()
42 {
43 return this.g_dir(0);
44 }
45
46 right ()
47 {
48 return this.g_dir(180);
49 }
50
51 forward ()
52 {
53 return this.g_dir(90);
54 }
55
56 back ()
57 {
58 return this.g_dir(270);
59 }
60
61 g_dir (offset)
62 {
63 var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180);
64
65 return angleVector = new vector(-Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians));
66 }
67}