Skip to content

Commit b909f70

Browse files
committed
Added bounds and bounce support and fixed velocity math
1 parent 916482b commit b909f70

2 files changed

Lines changed: 77 additions & 12 deletions

File tree

v3/src/gameobjects/particles/Particle.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ var Particle = new Class({
137137
this.angle = emitter.rotate.onEmit(this, 'rotate');
138138
this.rotation = DegToRad(this.angle);
139139

140+
this.bounce = emitter.bounce.onEmit(this, 'bounce');
141+
140142
this.alpha = emitter.alpha.onEmit(this, 'alpha');
141143

142144
this.color = (this.color & 0x00FFFFFF) | (((this.alpha * 0xFF) | 0) << 24);
@@ -186,8 +188,36 @@ var Particle = new Class({
186188
vy = -my;
187189
}
188190

189-
this.velocityX = vx * step;
190-
this.velocityY = vy * step;
191+
this.velocityX = vx;
192+
this.velocityY = vy;
193+
},
194+
195+
checkBounds: function (emitter)
196+
{
197+
var bounds = emitter.bounds;
198+
var bounce = -this.bounce;
199+
200+
if (this.x < bounds.x && emitter.collideLeft)
201+
{
202+
this.x = bounds.x;
203+
this.velocityX *= bounce;
204+
}
205+
else if (this.x > bounds.right && emitter.collideRight)
206+
{
207+
this.x = bounds.right;
208+
this.velocityX *= bounce;
209+
}
210+
211+
if (this.y < bounds.y && emitter.collideTop)
212+
{
213+
this.y = bounds.y;
214+
this.velocityY *= bounce;
215+
}
216+
else if (this.y > bounds.bottom && emitter.collideBottom)
217+
{
218+
this.y = bounds.bottom;
219+
this.velocityY *= bounce;
220+
}
191221
},
192222

193223
// delta = ms, step = delta / 1000
@@ -200,14 +230,13 @@ var Particle = new Class({
200230

201231
this.computeVelocity(emitter, step);
202232

203-
// this.velocityX += (emitter.gravityX * step);
204-
// this.velocityY += (emitter.gravityY * step);
205-
206-
// this.x += this.velocityX * step;
207-
// this.y += this.velocityY * step;
233+
this.x += this.velocityX * step;
234+
this.y += this.velocityY * step;
208235

209-
this.x += this.velocityX;
210-
this.y += this.velocityY;
236+
if (emitter.bounds)
237+
{
238+
this.checkBounds(emitter);
239+
}
211240

212241
this.scaleX = emitter.scaleX.onUpdate(this, 'scaleX', t, this.scaleX);
213242

v3/src/gameobjects/particles/ParticleEmitter.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
var BlendModes = require('../../renderer/BlendModes');
22
var Class = require('../../utils/Class');
33
var Components = require('../components');
4+
var EmitterOp = require('./EmitterOp');
5+
var GetFastValue = require('../../utils/object/GetFastValue');
46
var GetRandomElement = require('../../utils/array/GetRandomElement');
57
var GetValue = require('../../utils/object/GetValue');
6-
var GetFastValue = require('../../utils/object/GetFastValue');
78
var Particle = require('./Particle');
9+
var Rectangle = require('../../geom/rectangle/Rectangle');
810
var StableSort = require('../../utils/array/StableSort');
911
var Vector2 = require('../../math/Vector2');
10-
var EmitterOp = require('./EmitterOp');
1112

1213
var ParticleEmitter = new Class({
1314

@@ -136,7 +137,18 @@ var ParticleEmitter = new Class({
136137

137138
this.zone = GetFastValue(config, 'zone', null);
138139

139-
// bounds
140+
// bounds rectangle
141+
this.bounds = null;
142+
143+
if (config.hasOwnProperty('bounds'))
144+
{
145+
this.setBounds(config.bounds);
146+
}
147+
148+
this.collideLeft = GetFastValue(config, 'collideLeft', true);
149+
this.collideRight = GetFastValue(config, 'collideRight', true);
150+
this.collideTop = GetFastValue(config, 'collideTop', true);
151+
this.collideBottom = GetFastValue(config, 'collideBottom', true);
140152

141153
// Optional Particle emission zone - must be an object that supports a `getRandomPoint` function, such as a Rectangle, Circle, Path, etc.
142154
this.zone = GetFastValue(config, 'zone', null);
@@ -227,6 +239,30 @@ var ParticleEmitter = new Class({
227239
return this;
228240
},
229241

242+
setBounds: function (x, y, width, height)
243+
{
244+
if (typeof x === 'object')
245+
{
246+
var obj = x;
247+
248+
var x = obj.x;
249+
var y = obj.y;
250+
var width = (obj.hasOwnProperty('w')) ? obj.w : obj.width;
251+
var height = (obj.hasOwnProperty('h')) ? obj.h : obj.height;
252+
}
253+
254+
if (this.bounds)
255+
{
256+
this.bounds.setTo(x, y, width, height);
257+
}
258+
else
259+
{
260+
this.bounds = new Rectangle(x, y, width, height);
261+
}
262+
263+
return this;
264+
},
265+
230266
// Particle Emission
231267

232268
setSpeedX: function (value)

0 commit comments

Comments
 (0)