BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / transform.js

transform.js

engine optimisations
timfa Tim Falken · pushed by timfa · 3 months ago
74d8cdb3 History
transform.js 2.4 KiB Download Raw
1class Transform
2{
3 get position ()
4 {
5 if (!this.g_position)
6 {this.g_position = new vector(0, 0);}
7
8 return this.g_position;
9 }
10
11 set position (v)
12 {
13 if (!this.g_position)
14 {this.g_position = new vector(v.x, v.y);}
15
16 this.g_position.x = v.x;
17 this.g_position.y = v.y;
18 }
19
20 get scale ()
21 {
22 if (!this.g_scale)
23 {this.g_scale = new vector(0, 0);}
24
25 return this.g_scale;
26 }
27
28 set scale (v)
29 {
30 if (!this.g_scale)
31 {this.g_scale = new vector(v.x, v.y);}
32
33 this.g_scale.x = v.x;
34 this.g_scale.y = v.y;
35 }
36
37 get velocity ()
38 {
39 if (!this.g_velocity)
40 {this.g_velocity = new vector(0, 0);}
41
42 return this.g_velocity;
43 }
44
45 set velocity (v)
46 {
47 if (!this.g_velocity)
48 {this.g_velocity = new vector(v.x, v.y);}
49
50 this.g_velocity.x = v.x;
51 this.g_velocity.y = v.y;
52 }
53
54 g_position = null;
55
56 g_scale = null;
57 rotation = 0;
58 gameObject = null;
59
60 g_velocity = null;
61 angularVelocity = 0;
62
63 get size ()
64 {
65 const sprite = this.gameObject.renderer && this.gameObject.renderer.sprite;
66 const width = sprite ? sprite.width : 0;
67 const height = sprite ? sprite.height : 0;
68
69 if (!this.g_size)
70 { this.g_size = new vector(0, 0); }
71
72 this.g_size.x = this.scale.x * width;
73 this.g_size.y = this.scale.y * height;
74 return this.g_size;
75 }
76
77 set size (v)
78 {
79 const sprite = this.gameObject.renderer && this.gameObject.renderer.sprite;
80 const width = sprite && sprite.width ? sprite.width : 1;
81 const height = sprite && sprite.height ? sprite.height : 1;
82
83 this.scale.x = v.x / width;
84 this.scale.y = v.y / height;
85 }
86
87 constructor (gameObject, x, y, w, h, r)
88 {
89 this.gameObject = gameObject;
90 this.position = new vector(x, y);
91 this.scale = new vector(w, h);
92 this.rotation = r;
93
94 this.velocity = new vector(0, 0);
95 }
96
97 update (deltaTime)
98 {
99 const vel = this.velocity;
100 if (vel.x !== 0 || vel.y !== 0)
101 {
102 this.position.x += vel.x * deltaTime;
103 this.position.y += vel.y * deltaTime;
104 }
105
106 if (this.angularVelocity !== 0)
107 { this.rotation += this.angularVelocity * deltaTime; }
108 }
109
110 pointTowards (point)
111 {
112 var delta = point.subtract(this.position);
113
114 this.rotation = delta.toAngle();
115 }
116
117 get left ()
118 {
119 return this.g_dir(0);
120 }
121
122 get right ()
123 {
124 return this.g_dir(180);
125 }
126
127 get forward ()
128 {
129 return this.g_dir(90);
130 }
131
132 get back ()
133 {
134 return this.g_dir(270);
135 }
136
137 g_dir (offset)
138 {
139 var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180);
140
141 return new vector(-Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians));
142 }
143}