| 1 | class 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 | return new vector( |
| 66 | this.scale.x * this.gameObject.renderer.sprite.width, |
| 67 | this.scale.y * this.gameObject.renderer.sprite.height); |
| 68 | } |
| 69 | |
| 70 | constructor (gameObject, x, y, w, h, r) |
| 71 | { |
| 72 | this.gameObject = gameObject; |
| 73 | this.position = new vector(x, y); |
| 74 | this.scale = new vector(w, h); |
| 75 | this.rotation = r; |
| 76 | |
| 77 | this.velocity = new vector(0, 0); |
| 78 | } |
| 79 | |
| 80 | update (deltaTime) |
| 81 | { |
| 82 | this.position = this.position.add(this.velocity.stretch(deltaTime)); |
| 83 | this.rotation += this.angularVelocity * deltaTime; |
| 84 | } |
| 85 | |
| 86 | pointTowards (point) |
| 87 | { |
| 88 | var delta = point.subtract(this.position); |
| 89 | |
| 90 | this.rotation = delta.toAngle(); |
| 91 | } |
| 92 | |
| 93 | get left () |
| 94 | { |
| 95 | return this.g_dir(0); |
| 96 | } |
| 97 | |
| 98 | get right () |
| 99 | { |
| 100 | return this.g_dir(180); |
| 101 | } |
| 102 | |
| 103 | get forward () |
| 104 | { |
| 105 | return this.g_dir(90); |
| 106 | } |
| 107 | |
| 108 | get back () |
| 109 | { |
| 110 | return this.g_dir(270); |
| 111 | } |
| 112 | |
| 113 | g_dir (offset) |
| 114 | { |
| 115 | var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180); |
| 116 | |
| 117 | return new vector(-Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians)); |
| 118 | } |
| 119 | } |