@@ -7,6 +7,7 @@ import {Color} from './Color'
7
7
import { AnchorType } from './AnchorType'
8
8
import { Texture } from './Texture'
9
9
import { Bound } from './Bound'
10
+ import { Rectangle } from 'Geom/Rectangle'
10
11
11
12
export class Sprite extends Container {
12
13
public alpha : number = 1
@@ -27,8 +28,8 @@ export class Sprite extends Container {
27
28
protected _mask : HTMLImageElement
28
29
29
30
// TODO Dynamic allocation
30
- private _width : number = 100
31
- private _height : number = 100
31
+ private _width : number
32
+ private _height : number
32
33
33
34
protected _isDirty : boolean = true
34
35
@@ -52,14 +53,7 @@ export class Sprite extends Container {
52
53
this . graphics = this . canvas . getContext ( '2d' )
53
54
54
55
if ( texture ) {
55
- //TODO [GJP] Tidy all this up
56
- this . _texture = texture
57
- this . _targetCanvas . width = texture . width
58
- this . canvas . width = texture . width
59
- this . _targetCanvas . height = texture . height
60
- this . canvas . height = texture . height
61
- this . _width = texture . width
62
- this . _height = texture . height
56
+ this . texture = texture
63
57
} else {
64
58
this . _texture = new Texture ( )
65
59
}
@@ -74,7 +68,7 @@ export class Sprite extends Container {
74
68
}
75
69
76
70
public dispose ( ) : void {
77
- this . _modelBound . dispose ( )
71
+ // this._modelBound.dispose()
78
72
}
79
73
80
74
public get texture ( ) : Texture {
@@ -222,28 +216,145 @@ export class Sprite extends Container {
222
216
}
223
217
224
218
public get width ( ) : number {
225
- return this . _targetCanvas . width
219
+ return this . _worldBound . aabb . width
226
220
}
227
221
228
222
public set width ( value : number ) {
229
- if ( this . _isDirty ) {
230
- this . redraw ( )
231
- }
232
223
this . scaleX = value / this . _targetCanvas . width
233
224
}
234
225
235
226
public get height ( ) : number {
236
- return this . _targetCanvas . height
227
+ return this . _worldBound . aabb . height
237
228
}
238
229
239
230
public set height ( value : number ) {
240
- if ( this . _isDirty ) {
241
- this . redraw ( )
242
- }
243
231
this . scaleY = value / this . _targetCanvas . height
244
232
}
245
233
246
- public set anchor ( value : AnchorType ) {
234
+ public get left ( ) : number {
235
+ return this . _worldBound . aabb . x
236
+ }
237
+
238
+ public get right ( ) : number {
239
+ return this . left + this . width
240
+ }
241
+
242
+ public get top ( ) : number {
243
+ return this . _worldBound . aabb . y
244
+ }
245
+
246
+ public get bottom ( ) : number {
247
+ return this . top + this . height
248
+ }
249
+
250
+ public get center ( ) : Point {
251
+ return new Point (
252
+ ( this . left + this . width ) / 2 ,
253
+ ( this . top + this . height ) / 2
254
+ )
255
+ }
256
+
257
+ public intersectsXY ( x : number , y : number ) : boolean {
258
+ if ( this . bottom < y ) {
259
+ return false
260
+ }
261
+
262
+ if ( this . top > y ) {
263
+ return false
264
+ }
265
+
266
+ if ( this . right < x ) {
267
+ return false
268
+ }
269
+
270
+ if ( this . left > x ) {
271
+ return false
272
+ }
273
+
274
+ return true
275
+ }
276
+
277
+ public intersectsPoint ( point : Point ) : boolean {
278
+ return this . intersectsXY ( point . x , point . y )
279
+ }
280
+
281
+ /**
282
+ * TODO [GJP] This is fast and dirty! Fix this up
283
+ * by implementing a full physics system
284
+ */
285
+ public intersects ( sprite : Sprite ) : boolean {
286
+ if ( this . bottom < sprite . top ) {
287
+ return false
288
+ }
289
+
290
+ if ( this . top > sprite . bottom ) {
291
+ return false
292
+ }
293
+
294
+ if ( this . right < sprite . left ) {
295
+ return false
296
+ }
297
+
298
+ if ( this . left > sprite . right ) {
299
+ return false
300
+ }
301
+
302
+ return true
303
+ }
304
+
305
+ /**
306
+ * TODO [GJP] This is fast and dirty! Fix this up
307
+ * by implementing a full physics system
308
+ */
309
+ public intersectsRectangle ( rect : Rectangle ) : boolean {
310
+ if ( this . bottom < rect . top ) {
311
+ return false
312
+ }
313
+
314
+ if ( this . top > rect . bottom ) {
315
+ return false
316
+ }
317
+
318
+ if ( this . right < rect . left ) {
319
+ return false
320
+ }
321
+
322
+ if ( this . left > rect . right ) {
323
+ return false
324
+ }
325
+ }
326
+
327
+ public intersectsRadiusAdvanced (
328
+ sprite : Sprite ,
329
+ radius : number ,
330
+ radiusOther : number ,
331
+ tolerance : number
332
+ ) : boolean {
333
+ let center1 : Point = this . center
334
+ let center2 : Point = sprite . center
335
+ let xdiff : number = center2 . x - center1 . x
336
+ let ydiff : number = center2 . y - center1 . y
337
+ let dCentreSq : number = ydiff * ydiff + xdiff * xdiff
338
+ let rSumSq : number = radius + radiusOther
339
+ rSumSq *= rSumSq
340
+
341
+ return dCentreSq - rSumSq <= tolerance * tolerance
342
+ }
343
+
344
+ public intersectsRadius ( sprite : Sprite , radius ?: number ) {
345
+ if ( radius ) {
346
+ return this . intersectsRadiusAdvanced ( sprite , radius , radius , 0 )
347
+ } else {
348
+ return this . intersectsRadiusAdvanced (
349
+ sprite ,
350
+ ( this . width + this . height ) / 4 ,
351
+ ( sprite . width + sprite . height ) / 4 ,
352
+ 0
353
+ )
354
+ }
355
+ }
356
+
357
+ public set anchorType ( value : AnchorType ) {
247
358
let width : number = this . _width
248
359
let height : number = this . _height
249
360
let halfWidth : number = this . _width / 2
0 commit comments