Skip to content

Commit 5485279

Browse files
committed
Refactored to add in the Utils methods and new static and dynamic body splits.
1 parent 9292603 commit 5485279

1 file changed

Lines changed: 61 additions & 11 deletions

File tree

v3/src/physics/arcade/World.js

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
var Body = require('./Body');
44
var Class = require('../../utils/Class');
5+
var CONST = require('./const');
56
var GetValue = require('../../utils/object/GetValue');
67
var Rectangle = require('../../geom/rectangle/Rectangle');
78
var RTree = require('../../structs/RTree');
89
var Set = require('../../structs/Set');
10+
var StaticBody = require('./StaticBody');
911
var Vector2 = require('../../math/Vector2');
1012

1113
var World = new Class({
@@ -18,8 +20,12 @@ var World = new Class({
1820

1921
this.events = scene.sys.events;
2022

23+
// Dynamic Bodies
2124
this.bodies = new Set();
2225

26+
// Static Bodies
27+
this.staticBodies = new Set();
28+
2329
this.gravity = new Vector2(GetValue(config, 'gravity.x', 0), GetValue(config, 'gravity.y', 0));
2430

2531
this.bounds = new Rectangle(
@@ -58,6 +64,7 @@ var World = new Class({
5864
this.maxEntries = GetValue(config, 'maxEntries', 16);
5965

6066
this.tree = new RTree(this.maxEntries, [ '.left', '.top', '.right', '.bottom' ]);
67+
this.staticTree = new RTree(this.maxEntries, [ '.left', '.top', '.right', '.bottom' ]);
6168

6269
this.treeMinMax = { minX: 0, minY: 0, maxX: 0, maxY: 0 };
6370

@@ -67,8 +74,10 @@ var World = new Class({
6774
}
6875
},
6976

70-
enable: function (object)
77+
enable: function (object, type)
7178
{
79+
if (type === undefined) { type = CONST.DYNAMIC_BODY; }
80+
7281
var i = 1;
7382

7483
if (Array.isArray(object))
@@ -80,32 +89,43 @@ var World = new Class({
8089
if (object[i].hasOwnProperty('children'))
8190
{
8291
// If it's a Group then we do it on the children regardless
83-
this.enable(object[i].children.entries);
92+
this.enable(object[i].children.entries, type);
8493
}
8594
else
8695
{
87-
this.enableBody(object[i]);
96+
this.enableBody(object[i], type);
8897
}
8998
}
9099
}
91100
else if (object.hasOwnProperty('children'))
92101
{
93102
// If it's a Group then we do it on the children regardless
94-
this.enable(object.children.entries);
103+
this.enable(object.children.entries, type);
95104
}
96105
else
97106
{
98-
this.enableBody(object);
107+
this.enableBody(object, type);
99108
}
100109
},
101110

102-
enableBody: function (object)
111+
enableBody: function (object, type)
103112
{
104113
if (object.body === null)
105114
{
106-
object.body = new Body(this, object);
115+
if (type === CONST.DYNAMIC_BODY)
116+
{
117+
object.body = new Body(this, object);
118+
119+
this.bodies.set(object.body);
120+
}
121+
else if (type === CONST.STATIC_BODY)
122+
{
123+
object.body = new StaticBody(this, object);
124+
125+
this.staticBodies.set(object.body);
107126

108-
this.bodies.set(object.body);
127+
this.staticTree.insert(object.body);
128+
}
109129
}
110130

111131
return object;
@@ -147,10 +167,17 @@ var World = new Class({
147167
{
148168
if (object.body)
149169
{
150-
this.bodies.delete(object.body);
170+
if (object.body.physicsType === CONST.DYNAMIC_BODY)
171+
{
172+
this.bodies.delete(object.body);
173+
}
174+
else if (object.body.physicsType === CONST.STATIC_BODY)
175+
{
176+
this.staticBodies.delete(object.body);
177+
this.staticTree.remove(object.body);
178+
}
151179

152180
object.body.destroy();
153-
154181
object.body = null;
155182
}
156183

@@ -220,7 +247,7 @@ var World = new Class({
220247
}
221248
}
222249

223-
// Populate our collision tree
250+
// Populate our dynamic collision tree
224251
this.tree.clear();
225252
this.tree.load(bodies);
226253
},
@@ -257,6 +284,19 @@ var World = new Class({
257284
body.drawDebug(graphics);
258285
}
259286
}
287+
288+
bodies = this.staticBodies.entries;
289+
len = bodies.length;
290+
291+
for (i = 0; i < len; i++)
292+
{
293+
body = bodies[i];
294+
295+
if (body.willDrawDebug())
296+
{
297+
body.drawDebug(graphics);
298+
}
299+
}
260300
}
261301
},
262302

@@ -273,6 +313,16 @@ var World = new Class({
273313
collideSpriteVsSprite: require('./inc/CollideSpriteVsSprite'),
274314
collideSpriteVsGroup: require('./inc/CollideSpriteVsGroup'),
275315

316+
// Utils
317+
accelerateTo: require('./utils/AccelerateTo'),
318+
accelerateToObject: require('./utils/AccelerateToObject'),
319+
closest: require('./utils/Closest'),
320+
furthest: require('./utils/Furthest'),
321+
moveTo: require('./utils/MoveTo'),
322+
moveToObject: require('./utils/MoveToObject'),
323+
velocityFromAngle: require('./utils/VelocityFromAngle'),
324+
velocityFromRotation: require('./utils/VelocityFromRotation'),
325+
276326
// TODO
277327
collideGroupVsGroup: function (group1, group2, collideCallback, processCallback, callbackContext, overlapOnly)
278328
{

0 commit comments

Comments
 (0)