| 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 | 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 | this.position = this.position.add(this.velocity.stretch(deltaTime)); |
| 100 | this.rotation += this.angularVelocity * deltaTime; |
| 101 | } |
| 102 | |
| 103 | pointTowards (point) |
| 104 | { |
| 105 | var delta = point.subtract(this.position); |
| 106 | |
| 107 | this.rotation = delta.toAngle(); |
| 108 | } |
| 109 | |
| 110 | get left () |
| 111 | { |
| 112 | return this.g_dir(0); |
| 113 | } |
| 114 | |
| 115 | get right () |
| 116 | { |
| 117 | return this.g_dir(180); |
| 118 | } |
| 119 | |
| 120 | get forward () |
| 121 | { |
| 122 | return this.g_dir(90); |
| 123 | } |
| 124 | |
| 125 | get back () |
| 126 | { |
| 127 | return this.g_dir(270); |
| 128 | } |
| 129 | |
| 130 | g_dir (offset) |
| 131 | { |
| 132 | var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180); |
| 133 | |
| 134 | return new vector(-Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians)); |
| 135 | } |
| 136 | } |