Skip to content

Commit 4f00477

Browse files
committed
MatterImage and MatterSprite allow you to easily create Game Objects with embedded Matter physics bodies.
1 parent 32766ca commit 4f00477

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var Bodies = require('./lib/factory/Bodies');
2+
var Class = require('../../utils/Class');
3+
var Components = require('./components');
4+
var GameObject = require('../../gameobjects/GameObject');
5+
var GetFastValue = require('../../utils/object/GetFastValue');
6+
var Image = require('../../gameobjects/image/Image');
7+
var Vector2 = require('../../math/Vector2');
8+
9+
var MatterImage = new Class({
10+
11+
Extends: Image,
12+
13+
Mixins: [
14+
Components.Bounce,
15+
Components.Force,
16+
Components.Friction,
17+
Components.Mass,
18+
Components.Static,
19+
Components.Transform,
20+
Components.Velocity
21+
],
22+
23+
initialize:
24+
25+
// x/y is the center of the Image / Body, just like other default Game Objects
26+
function MatterImage (world, x, y, texture, frame, options)
27+
{
28+
GameObject.call(this, world.scene, 'Image');
29+
30+
this.setTexture(texture, frame);
31+
this.setSizeToFrame();
32+
this.setOrigin();
33+
34+
this._tempVec2 = new Vector2();
35+
36+
var isCircle = GetFastValue(options, 'isCircle', false);
37+
38+
if (isCircle)
39+
{
40+
var radius = GetFastValue(options, 'radius', Math.max(this.width, this.height) / 2);
41+
42+
this.body = Bodies.circle(x, y, radius, options);
43+
}
44+
else
45+
{
46+
this.body = Bodies.rectangle(x, y, this.width, this.height, options);
47+
}
48+
49+
world.add(this.body);
50+
51+
this.setPosition(x, y);
52+
}
53+
54+
});
55+
56+
module.exports = MatterImage;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var AnimationComponent = require('../../gameobjects/components/Animation');
2+
var Bodies = require('./lib/factory/Bodies');
3+
var Class = require('../../utils/Class');
4+
var Components = require('./components');
5+
var GameObject = require('../../gameobjects/GameObject');
6+
var GetFastValue = require('../../utils/object/GetFastValue');
7+
var Sprite = require('../../gameobjects/sprite/Sprite');
8+
var Vector2 = require('../../math/Vector2');
9+
10+
var MatterSprite = new Class({
11+
12+
Extends: Sprite,
13+
14+
Mixins: [
15+
Components.Bounce,
16+
Components.Force,
17+
Components.Friction,
18+
Components.Mass,
19+
Components.Static,
20+
Components.Transform,
21+
Components.Velocity
22+
],
23+
24+
initialize:
25+
26+
// x/y is the center of the Sprite / Body, just like other default Game Objects
27+
function MatterSprite (world, x, y, texture, frame, options)
28+
{
29+
GameObject.call(this, world.scene, 'Image');
30+
31+
this.anims = new AnimationComponent(this);
32+
33+
this.setTexture(texture, frame);
34+
this.setSizeToFrame();
35+
this.setOrigin();
36+
37+
this._tempVec2 = new Vector2();
38+
39+
var isCircle = GetFastValue(options, 'isCircle', false);
40+
41+
if (isCircle)
42+
{
43+
var radius = GetFastValue(options, 'radius', Math.max(this.width, this.height) / 2);
44+
45+
this.body = Bodies.circle(x, y, radius, options);
46+
}
47+
else
48+
{
49+
this.body = Bodies.rectangle(x, y, this.width, this.height, options);
50+
}
51+
52+
world.add(this.body);
53+
54+
this.setPosition(x, y);
55+
}
56+
57+
});
58+
59+
module.exports = MatterSprite;

0 commit comments

Comments
 (0)