Skip to content

Commit 39a4525

Browse files
committed
Added a Static Physicss Group. A Group that contains only Static Bodies.
1 parent 5485279 commit 39a4525

2 files changed

Lines changed: 84 additions & 5 deletions

File tree

v3/src/physics/arcade/Factory.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
var ArcadeImage = require('./ArcadeImage');
22
var ArcadeSprite = require('./ArcadeSprite');
33
var Class = require('../../utils/Class');
4-
var PhysicsGroup = require('./PhysicsGroup');
54
var CONST = require('./const');
5+
var PhysicsGroup = require('./PhysicsGroup');
6+
var StaticPhysicsGroup = require('./StaticPhysicsGroup');
67

78
var Factory = new Class({
89

@@ -19,42 +20,55 @@ var Factory = new Class({
1920

2021
staticImage: function (x, y, key, frame)
2122
{
22-
var image = new ArcadeImage(this.scene, CONST.STATIC_BODY, x, y, key, frame);
23+
var image = new ArcadeImage(this.scene, x, y, key, frame);
2324

2425
this.sys.displayList.add(image);
2526

27+
this.world.enableBody(image, CONST.STATIC_BODY);
28+
2629
return image;
2730
},
2831

2932
image: function (x, y, key, frame)
3033
{
31-
var image = new ArcadeImage(this.scene, CONST.DYNAMIC_BODY, x, y, key, frame);
34+
var image = new ArcadeImage(this.scene, x, y, key, frame);
3235

3336
this.sys.displayList.add(image);
3437

38+
this.world.enableBody(image, CONST.DYNAMIC_BODY);
39+
3540
return image;
3641
},
3742

3843
staticSprite: function (x, y, key, frame)
3944
{
40-
var sprite = new ArcadeSprite(this.scene, CONST.STATIC_BODY, x, y, key, frame);
45+
var sprite = new ArcadeSprite(this.scene, x, y, key, frame);
4146

4247
this.sys.displayList.add(sprite);
4348
this.sys.updateList.add(sprite);
4449

50+
this.world.enableBody(sprite, CONST.STATIC_BODY);
51+
4552
return sprite;
4653
},
4754

4855
sprite: function (x, y, key, frame)
4956
{
50-
var sprite = new ArcadeSprite(this.scene, CONST.DYNAMIC_BODY, x, y, key, frame);
57+
var sprite = new ArcadeSprite(this.scene, x, y, key, frame);
5158

5259
this.sys.displayList.add(sprite);
5360
this.sys.updateList.add(sprite);
5461

62+
this.world.enableBody(sprite, CONST.DYNAMIC_BODY);
63+
5564
return sprite;
5665
},
5766

67+
staticGroup: function (children, config)
68+
{
69+
return new StaticPhysicsGroup(this.world, this.world.scene, children, config);
70+
},
71+
5872
group: function (children, config)
5973
{
6074
return new PhysicsGroup(this.world, this.world.scene, children, config);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Phaser.Physics.Arcade.StaticPhysicsGroup
2+
3+
var ArcadeSprite = require('./ArcadeSprite');
4+
var Class = require('../../utils/Class');
5+
var CONST = require('./const');
6+
var Group = require('../../gameobjects/group/Group');
7+
8+
var StaticPhysicsGroup = new Class({
9+
10+
Extends: Group,
11+
12+
initialize:
13+
14+
function StaticPhysicsGroup (world, scene, children, config)
15+
{
16+
if (config === undefined && !Array.isArray(children) && typeof children === 'object')
17+
{
18+
config = children;
19+
children = null;
20+
}
21+
else if (config === undefined)
22+
{
23+
config = {};
24+
}
25+
26+
this.world = world;
27+
28+
config.createCallback = this.createCallback;
29+
config.removeCallback = this.removeCallback;
30+
config.classType = ArcadeSprite;
31+
32+
this.physicsType = CONST.STATIC_BODY;
33+
34+
Group.call(this, scene, children, config);
35+
},
36+
37+
createCallback: function (child)
38+
{
39+
if (!child.body)
40+
{
41+
this.world.enableBody(child, CONST.STATIC_BODY);
42+
}
43+
},
44+
45+
removeCallback: function (child)
46+
{
47+
if (child.body)
48+
{
49+
this.world.disableBody(child);
50+
}
51+
},
52+
53+
refresh: function ()
54+
{
55+
var children = this.children.entries;
56+
57+
for (var i = 0; i < children.length; i++)
58+
{
59+
children[i].body.reset();
60+
}
61+
}
62+
63+
});
64+
65+
module.exports = StaticPhysicsGroup;

0 commit comments

Comments
 (0)