Skip to content

Commit ec67d02

Browse files
committed
Blockers is now a face array and added hardBlocked support
1 parent 18333f9 commit ec67d02

1 file changed

Lines changed: 115 additions & 46 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 115 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -728,15 +728,15 @@ var Body = new Class({
728728
this.worldBlocked = { none: true, up: false, down: false, left: false, right: false };
729729

730730
/**
731-
* The world blocked state of this body in the previous frame.
731+
*
732732
*
733-
* @name Phaser.Physics.Arcade.Body#wasBlocked
733+
* @name Phaser.Physics.Arcade.Body#hardBlocked
734734
* @type {Phaser.Physics.Arcade.Types.ArcadeBodyCollision}
735735
* @since 3.17.0
736736
*/
737-
this.wasBlocked = { up: false, down: false, left: false, right: false };
737+
this.hardBlocked = { none: true, up: false, down: false, left: false, right: false };
738738

739-
this.blockers = [];
739+
this.blockers = { up: [], down: [], left: [], right: [] };
740740

741741
/**
742742
* Whether to automatically synchronize this Body's dimensions to the dimensions of its Game Object's visual bounds.
@@ -956,6 +956,7 @@ var Body = new Class({
956956
var touching = this.touching;
957957
var blocked = this.blocked;
958958
var worldBlocked = this.worldBlocked;
959+
var hardBlocked = this.hardBlocked;
959960

960961
touching.none = true;
961962
touching.up = false;
@@ -975,6 +976,13 @@ var Body = new Class({
975976
worldBlocked.up = false;
976977
worldBlocked.down = false;
977978

979+
hardBlocked.none = true;
980+
hardBlocked.left = false;
981+
hardBlocked.right = false;
982+
hardBlocked.up = false;
983+
hardBlocked.down = false;
984+
985+
// Remove?
978986
this.overlapR = 0;
979987
this.overlapX = 0;
980988
this.overlapY = 0;
@@ -1167,9 +1175,6 @@ var Body = new Class({
11671175
this.checkSleep(dx, dy);
11681176

11691177
// Store collision flags
1170-
var wasBlocked = this.wasBlocked;
1171-
var worldBlocked = this.worldBlocked;
1172-
11731178
var wasTouching = this.wasTouching;
11741179
var touching = this.touching;
11751180

@@ -1179,18 +1184,8 @@ var Body = new Class({
11791184
wasTouching.left = touching.left;
11801185
wasTouching.right = touching.right;
11811186

1182-
wasBlocked.up = worldBlocked.up;
1183-
wasBlocked.down = worldBlocked.down;
1184-
wasBlocked.left = worldBlocked.left;
1185-
wasBlocked.right = worldBlocked.right;
1186-
11871187
this.prevVelocity.x = this.velocity.x;
11881188
this.prevVelocity.y = this.velocity.y;
1189-
1190-
if (!this.sleeping)
1191-
{
1192-
// console.log('frame', this.world._frame, this.gameObject.name, 'vy', this.velocity.y, 'sleep', this._sleep, 'y', this.y, 'gy', this.gameObject.y, 'blocked', this.blocked.down);
1193-
}
11941189
},
11951190

11961191
sleep: function (forceY)
@@ -1227,12 +1222,22 @@ var Body = new Class({
12271222
}
12281223
else if (forceY && !blocked.none)
12291224
{
1230-
console.log(this.gameObject.name, 'sleeping and fixed to blocker bounds');
1225+
console.log(this.gameObject.name, 'sleeping and fixed to blocker bounds scanning ...');
1226+
1227+
var body2;
12311228

12321229
if (blocked.down)
12331230
{
1234-
// this.bottom = worldBounds.bottom;
1235-
this.forcePosition = 2;
1231+
body2 = this.getBlocker(this.blockers.down);
1232+
1233+
if (body2)
1234+
{
1235+
console.log('blocker bounds found', body2.y);
1236+
1237+
this.bottom = body2.y;
1238+
1239+
this.forcePosition = 2;
1240+
}
12361241
}
12371242
else if (blocked.up)
12381243
{
@@ -1243,6 +1248,25 @@ var Body = new Class({
12431248
}
12441249
},
12451250

1251+
getBlocker: function (blockers)
1252+
{
1253+
for (var i = 0; i < blockers.length; i++)
1254+
{
1255+
var collisionInfo = blockers[i];
1256+
1257+
console.log('CI', collisionInfo.body1.gameObject.name, collisionInfo.body2.gameObject.name);
1258+
1259+
if (collisionInfo.body1 === this)
1260+
{
1261+
return collisionInfo.body2;
1262+
}
1263+
else if (collisionInfo.body2 === this)
1264+
{
1265+
return collisionInfo.body1;
1266+
}
1267+
}
1268+
},
1269+
12461270
wake: function ()
12471271
{
12481272
if (this.sleeping)
@@ -1258,20 +1282,23 @@ var Body = new Class({
12581282
{
12591283
// Iterate through the list of previous frame blockers and see if they are still there
12601284

1261-
var currentBlockers = [];
1262-
var prevBlockers = this.blockers;
1263-
1264-
for (var i = 0; i < prevBlockers.length; i++)
1285+
for (var face in this.blockers)
12651286
{
1266-
var data = prevBlockers[i];
1287+
var currentBlockers = [];
1288+
var prevBlockers = this.blockers[face];
12671289

1268-
if (CheckOverlapY(this, data))
1290+
for (var i = 0; i < prevBlockers.length; i++)
12691291
{
1270-
currentBlockers.push(data);
1292+
var data = prevBlockers[i];
1293+
1294+
if (CheckOverlapY(this, data))
1295+
{
1296+
currentBlockers.push(data);
1297+
}
12711298
}
1299+
1300+
this.blockers[face] = currentBlockers;
12721301
}
1273-
1274-
this.blockers = currentBlockers;
12751302
},
12761303

12771304
// Is this body moving OR can it be made to move?
@@ -1315,7 +1342,7 @@ var Body = new Class({
13151342
if (!this.collideWorldBounds || worldBlocked.none || velocity.equals(0) || (bx === 0 && by === 0))
13161343
{
13171344
// Nothing to do
1318-
console.log('CWB abort', this.collideWorldBounds, worldBlocked.none, velocity.equals(0));
1345+
// console.log('CWB abort', this.collideWorldBounds, worldBlocked.none, velocity.equals(0));
13191346

13201347
return true;
13211348
}
@@ -1441,7 +1468,7 @@ var Body = new Class({
14411468
return (gy === 0 || (gy < 0 && this.isBlockedUp()) || (gy > 0 && this.isBlockedDown()));
14421469
},
14431470

1444-
// Check for sleeping state
1471+
// Check for sleeping state (called during postUpdate AFTER positioning)
14451472
checkSleep: function (dx, dy)
14461473
{
14471474
// Can't sleep if not blocked in the opposite direction somehow
@@ -1463,8 +1490,14 @@ var Body = new Class({
14631490

14641491
if (this._sleep >= this.sleepIterations)
14651492
{
1466-
console.log('slept by checkSleep');
1467-
this.sleep();
1493+
console.log(this.world._frame, 'slept by checkSleep');
1494+
1495+
this.sleep(true);
1496+
1497+
var gameObject = this.gameObject;
1498+
1499+
gameObject.x = this.x;
1500+
gameObject.y = this.y;
14681501
}
14691502
}
14701503
}
@@ -1904,6 +1937,7 @@ var Body = new Class({
19041937

19051938
var blocked = this.blocked;
19061939
var worldBlocked = this.worldBlocked;
1940+
var hardBlocked = this.hardBlocked;
19071941

19081942
var color;
19091943

@@ -1912,19 +1946,19 @@ var Body = new Class({
19121946
// Top
19131947
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
19141948

1915-
if (blocked.up || worldBlocked.up)
1949+
if (blocked.up || worldBlocked.up || hardBlocked.up)
19161950
{
1917-
color = (worldBlocked.up) ? worldBlockedColor : blockedColor;
1951+
color = (worldBlocked.up || hardBlocked.up) ? worldBlockedColor : blockedColor;
19181952
}
19191953

19201954
graphic.lineStyle(thickness, color).lineBetween(x1, y1 + halfThickness, x2, y2 + halfThickness);
19211955

19221956
// Bottom
19231957
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
19241958

1925-
if (blocked.down || worldBlocked.down)
1959+
if (blocked.down || worldBlocked.down || hardBlocked.down)
19261960
{
1927-
color = (worldBlocked.down) ? worldBlockedColor : blockedColor;
1961+
color = (worldBlocked.down || hardBlocked.down) ? worldBlockedColor : blockedColor;
19281962
}
19291963

19301964
graphic.lineStyle(thickness, color).lineBetween(x3, y3 - halfThickness, x4, y4 - halfThickness);
@@ -2185,6 +2219,26 @@ var Body = new Class({
21852219
return this;
21862220
},
21872221

2222+
setHardBlockedUp: function ()
2223+
{
2224+
var hardBlocked = this.hardBlocked;
2225+
2226+
hardBlocked.none = false;
2227+
hardBlocked.up = true;
2228+
2229+
return this;
2230+
},
2231+
2232+
setHardBlockedDown: function ()
2233+
{
2234+
var hardBlocked = this.hardBlocked;
2235+
2236+
hardBlocked.none = false;
2237+
hardBlocked.down = true;
2238+
2239+
return this;
2240+
},
2241+
21882242
setBlockedUp: function (collisionInfo, body2)
21892243
{
21902244
var blocked = this.blocked;
@@ -2196,7 +2250,14 @@ var Body = new Class({
21962250
{
21972251
if (!body2)
21982252
{
2199-
ArrayAdd(this.blockers, collisionInfo);
2253+
ArrayAdd(this.blockers.up, collisionInfo);
2254+
}
2255+
else
2256+
{
2257+
if (body2.isWorldBlockedUp())
2258+
{
2259+
this.setHardBlockedUp();
2260+
}
22002261
}
22012262

22022263
// We don't reposition this body if it's already blocked on a face
@@ -2230,7 +2291,14 @@ var Body = new Class({
22302291
{
22312292
if (!body2)
22322293
{
2233-
ArrayAdd(this.blockers, collisionInfo);
2294+
ArrayAdd(this.blockers.down, collisionInfo);
2295+
}
2296+
else
2297+
{
2298+
if (body2.isWorldBlockedDown())
2299+
{
2300+
this.setHardBlockedDown();
2301+
}
22342302
}
22352303

22362304
// We don't reposition this body if it's already blocked on a face
@@ -2392,38 +2460,39 @@ var Body = new Class({
23922460

23932461
isBlocked: function ()
23942462
{
2395-
return (!this.blocked.none || !this.worldBlocked.none);
2463+
return (!this.blocked.none || !this.worldBlocked.none || !this.hardBlocked.none);
23962464
},
23972465

23982466
isBlockedUp: function ()
23992467
{
2400-
return (this.blocked.up || this.worldBlocked.up);
2468+
return (this.blocked.up || this.worldBlocked.up || this.hardBlocked.up);
24012469
},
24022470

24032471
isBlockedDown: function ()
24042472
{
2405-
return (this.blocked.down || this.worldBlocked.down);
2473+
return (this.blocked.down || this.worldBlocked.down || this.hardBlocked.down);
24062474
},
24072475

24082476
isWorldBlockedDown: function ()
24092477
{
2410-
return this.worldBlocked.down;
2478+
return (this.worldBlocked.down || this.hardBlocked.down);
24112479
},
24122480

24132481
isWorldBlockedUp: function ()
24142482
{
2415-
return this.worldBlocked.up;
2483+
return (this.worldBlocked.up || this.hardBlocked.up);
24162484
},
24172485

24182486
// Is this body world blocked AND blocked on the opposite face?
24192487
isBlockedY: function ()
24202488
{
24212489
var blocked = this.blocked;
24222490
var worldBlocked = this.worldBlocked;
2491+
var hardBlocked = this.hardBlocked;
24232492

24242493
return (
2425-
(worldBlocked.down && blocked.up) ||
2426-
(worldBlocked.up && blocked.down)
2494+
((worldBlocked.down || hardBlocked.down) && blocked.up) ||
2495+
((worldBlocked.up || hardBlocked.up) && blocked.down)
24272496
);
24282497
},
24292498

0 commit comments

Comments
 (0)