@@ -17,18 +17,18 @@ Phaser.Plugin.ArcadeSlopes.Overrides = {};
17
17
* Collide a sprite against a single tile.
18
18
*
19
19
* @method Phaser.Plugin.ArcadeSlopes.Overrides#collideSpriteVsTile
20
- * @param {integer } i - The tile index.
21
- * @param {Phaser.Sprite } sprite - The sprite to check.
22
- * @param {Phaser.Tile } tile - The tile to check.
23
- * @param {Phaser.TilemapLayer } tilemapLayer - The tilemap layer the tile belongs to.
24
- * @param {function } collideCallback - An optional collision callback.
25
- * @param {function } processCallback - An optional overlap processing callback.
26
- * @param {object } callbackContext - The context in which to run the callbacks.
27
- * @param {boolean } overlapOnly - Whether to only check for an overlap.
28
- * @return {boolean } - Whether a collision occurred.
20
+ * @param {integer } i - The tile index.
21
+ * @param {Phaser.Sprite } sprite - The sprite to check.
22
+ * @param {Phaser.Tile } tile - The tile to check.
23
+ * @param {Phaser.TilemapLayer } tilemapLayer - The tilemap layer the tile belongs to.
24
+ * @param {function } [ collideCallback] - An optional collision callback.
25
+ * @param {function } [ processCallback] - An optional overlap processing callback.
26
+ * @param {object } [ callbackContext] - The context in which to run the callbacks.
27
+ * @param {boolean } [ overlapOnly] - Whether to only check for an overlap.
28
+ * @return {boolean } - Whether a collision occurred.
29
29
*/
30
30
Phaser . Plugin . ArcadeSlopes . Overrides . collideSpriteVsTile = function ( i , sprite , tile , tilemapLayer , collideCallback , processCallback , callbackContext , overlapOnly ) {
31
- if ( ! sprite . body ) {
31
+ if ( ! sprite . body || ! tile || ! tilemapLayer ) {
32
32
return false ;
33
33
}
34
34
@@ -59,22 +59,22 @@ Phaser.Plugin.ArcadeSlopes.Overrides.collideSpriteVsTile = function (i, sprite,
59
59
* Collide a sprite against a set of tiles.
60
60
*
61
61
* @method Phaser.Plugin.ArcadeSlopes.Overrides#collideSpriteVsTiles
62
- * @param {Phaser.Sprite } sprite - The sprite to check.
63
- * @param {Phaser.Tile[] } tiles - The tiles to check.
64
- * @param {Phaser.TilemapLayer } tilemapLayer - The tilemap layer the tiles belong to.
65
- * @param {function } collideCallback - An optional collision callback.
66
- * @param {function } processCallback - An optional overlap processing callback.
67
- * @param {object } callbackContext - The context in which to run the callbacks.
68
- * @param {boolean } overlapOnly - Whether to only check for an overlap.
69
- * @return {boolean } - Whether a collision occurred.
62
+ * @param {Phaser.Sprite } sprite - The sprite to check.
63
+ * @param {Phaser.Tile[] } tiles - The tiles to check.
64
+ * @param {Phaser.TilemapLayer } tilemapLayer - The tilemap layer the tiles belong to.
65
+ * @param {function } [ collideCallback] - An optional collision callback.
66
+ * @param {function } [ processCallback] - An optional overlap processing callback.
67
+ * @param {object } [ callbackContext] - The context in which to run the callbacks.
68
+ * @param {boolean } [ overlapOnly] - Whether to only check for an overlap.
69
+ * @return {boolean } - Whether a collision occurred.
70
70
*/
71
71
Phaser . Plugin . ArcadeSlopes . Overrides . collideSpriteVsTiles = function ( sprite , tiles , tilemapLayer , collideCallback , processCallback , callbackContext , overlapOnly ) {
72
- var collided = false ;
73
-
74
- if ( ! sprite . body ) {
75
- return collided ;
72
+ if ( ! sprite . body || ! tiles || ! tiles . length || ! tilemapLayer ) {
73
+ return false ;
76
74
}
77
75
76
+ var collided = false ;
77
+
78
78
for ( var i = 0 ; i < tiles . length ; i ++ ) {
79
79
if ( processCallback ) {
80
80
if ( processCallback . call ( callbackContext , sprite , tiles [ i ] ) ) {
@@ -104,7 +104,7 @@ Phaser.Plugin.ArcadeSlopes.Overrides.collideSpriteVsTiles = function (sprite, ti
104
104
* @return {boolean } - Whether a collision occurred.
105
105
*/
106
106
Phaser . Plugin . ArcadeSlopes . Overrides . collideSpriteVsTilemapLayer = function ( sprite , tilemapLayer , collideCallback , processCallback , callbackContext , overlapOnly ) {
107
- if ( ! sprite . body ) {
107
+ if ( ! sprite . body || ! tilemapLayer ) {
108
108
return false ;
109
109
}
110
110
@@ -113,20 +113,17 @@ Phaser.Plugin.ArcadeSlopes.Overrides.collideSpriteVsTilemapLayer = function (spr
113
113
sprite . body . position . y - sprite . body . tilePadding . y - tilemapLayer . getCollisionOffsetY ( ) ,
114
114
sprite . body . width + sprite . body . tilePadding . x ,
115
115
sprite . body . height + sprite . body . tilePadding . y ,
116
- false ,
116
+ true ,
117
117
false
118
118
) ;
119
119
120
120
if ( tiles . length === 0 ) {
121
121
return false ;
122
122
}
123
123
124
- var collided = this . collideSpriteVsTiles ( sprite , tiles , tilemapLayer , collideCallback , processCallback , callbackContext , overlapOnly ) ;
124
+ // TODO: Sort by distance from body center to tile center?
125
125
126
- if ( ! collided && ! overlapOnly ) {
127
- // TODO: This call is too hacky and solver-specific
128
- this . game . slopes . solvers . sat . snap ( sprite . body , tiles , tilemapLayer ) ;
129
- }
126
+ var collided = this . collideSpriteVsTiles ( sprite , tiles , tilemapLayer , collideCallback , processCallback , callbackContext , overlapOnly ) ;
130
127
131
128
return collided ;
132
129
} ;
@@ -254,8 +251,13 @@ Phaser.Plugin.ArcadeSlopes.Overrides.renderDebug = function () {
254
251
var height = this . layer . height ;
255
252
var tw = this . _mc . tileWidth * scaleX ;
256
253
var th = this . _mc . tileHeight * scaleY ;
254
+ var htw = tw / 2 ;
255
+ var hth = th / 2 ;
256
+ var qtw = tw / 4 ;
257
+ var qth = th / 4 ;
257
258
var cw = this . _mc . cw * scaleX ;
258
259
var ch = this . _mc . ch * scaleY ;
260
+ var m = this . _mc . edgeMidpoint ;
259
261
260
262
var left = Math . floor ( scrollX / tw ) ;
261
263
var right = Math . floor ( ( renderW - 1 + scrollX ) / tw ) ;
@@ -281,7 +283,7 @@ Phaser.Plugin.ArcadeSlopes.Overrides.renderDebug = function () {
281
283
var normStartX = ( left + ( ( 1 << 20 ) * width ) ) % width ;
282
284
var normStartY = ( top + ( ( 1 << 20 ) * height ) ) % height ;
283
285
284
- var tx , ty , x , y , xmax , ymax , polygon , i , j ;
286
+ var tx , ty , x , y , xmax , ymax , polygon , i , j , a , b , norm , gx , gy ;
285
287
286
288
for ( y = normStartY , ymax = bottom - top , ty = baseY ; ymax >= 0 ; y ++ , ymax -- , ty += th ) {
287
289
if ( y >= height ) {
@@ -369,8 +371,9 @@ Phaser.Plugin.ArcadeSlopes.Overrides.renderDebug = function () {
369
371
}
370
372
}
371
373
372
- // Stroke the colliding edges
374
+ // Stroke the colliding edges and edge normals
373
375
if ( this . debugSettings . slopeCollidingEdgeStroke ) {
376
+ // Colliding edges
374
377
context . beginPath ( ) ;
375
378
376
379
context . lineWidth = this . debugSettings . slopeCollidingEdgeStrokeWidth || 1 ;
@@ -379,11 +382,12 @@ Phaser.Plugin.ArcadeSlopes.Overrides.renderDebug = function () {
379
382
polygon = tile . slope . polygon ;
380
383
381
384
for ( i = 0 ; i < polygon . points . length ; i ++ ) {
382
- j = ( i + 1 ) % polygon . points . length ;
383
-
384
- // Skip internal edges
385
- if ( polygon . points [ i ] . internal )
385
+ // Skip the edges with ignored normals
386
+ if ( polygon . normals [ i ] . ignore ) {
386
387
continue ;
388
+ }
389
+
390
+ j = ( i + 1 ) % polygon . points . length ;
387
391
388
392
context . moveTo ( tx + polygon . points [ i ] . x * scaleX , ty + polygon . points [ i ] . y * scaleY ) ;
389
393
context . lineTo ( tx + polygon . points [ j ] . x * scaleX , ty + polygon . points [ j ] . y * scaleY ) ;
@@ -392,6 +396,56 @@ Phaser.Plugin.ArcadeSlopes.Overrides.renderDebug = function () {
392
396
context . closePath ( ) ;
393
397
394
398
context . stroke ( ) ;
399
+
400
+ // Edge normals
401
+ for ( i = 0 ; i < polygon . points . length ; i ++ ) {
402
+ context . beginPath ( ) ;
403
+
404
+ if ( polygon . normals [ i ] . ignore ) {
405
+ context . lineWidth = this . debugSettings . slopeNormalStrokeWidth ;
406
+ context . strokeStyle = this . debugSettings . slopeNormalStroke ;
407
+ } else {
408
+ context . lineWidth = this . debugSettings . slopeCollidingNormalStrokeWidth ;
409
+ context . strokeStyle = this . debugSettings . slopeCollidingNormalStroke ;
410
+ }
411
+
412
+
413
+ j = ( i + 1 ) % polygon . points . length ;
414
+
415
+ a = polygon . points [ i ] ;
416
+ b = polygon . points [ j ] ;
417
+ norm = polygon . normals [ i ] ;
418
+
419
+ // Midpoint of the edge
420
+ m . x = ( a . x + b . x ) / 2 ;
421
+ m . y = ( a . y + b . y ) / 2 ;
422
+
423
+ // Draw from the midpoint outwards using the normal
424
+ context . moveTo ( tx + m . x * scaleX , ty + m . y * scaleY ) ;
425
+ context . lineTo ( tx + m . x * scaleX + norm . x * qtw , ty + m . y * scaleY + norm . y * qth ) ;
426
+
427
+ context . closePath ( ) ;
428
+ context . stroke ( ) ;
429
+ }
430
+
431
+ // Ignormals
432
+ if ( tile . slope . ignormals ) {
433
+ for ( i = 0 ; i < tile . slope . ignormals . length ; i ++ ) {
434
+ context . beginPath ( ) ;
435
+
436
+ context . lineWidth = 1 ;
437
+ context . strokeStyle = 'rgba(255, 0, 0, 1)' ;
438
+
439
+ gx = tile . slope . ignormals [ i ] . x ;
440
+ gy = tile . slope . ignormals [ i ] . y ;
441
+
442
+ context . moveTo ( tx + htw , ty + hth ) ;
443
+ context . lineTo ( tx + htw + gx * qtw , ty + hth + gy * qth ) ;
444
+
445
+ context . closePath ( ) ;
446
+ context . stroke ( ) ;
447
+ }
448
+ }
395
449
}
396
450
}
397
451
}
0 commit comments