Skip to content

Commit

Permalink
split the world update method into a separate step for clearer code w…
Browse files Browse the repository at this point in the history
…hen using custom update logic.
  • Loading branch information
obiot committed Aug 11, 2023
1 parent fea7185 commit 9e3de6a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
### Added
- Input: new `hasActiveEvents` returning true if there are any pending events in the queue
- Input: new `hasRegisteredEvents` returning true if there are registered pointer event listeners
- Physic: new `WORLD_STEP` event emmitted after the builtin physic world has been updated (if enabled)
- Renderer: fix/enable batching for all ellipse & arc(To) WebGL stroke operations

### Changed
- Physic: split the world update method into a separate step for clearer code when using custom update logic.

## [15.8.0] (melonJS 2) - _2023-07-29_

### Added
Expand Down
25 changes: 16 additions & 9 deletions src/physics/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,9 @@ export default class World extends Container {
/**
* update the game world
* @param {number} dt - the time passed since the last frame update
* @returns {boolean} true if the word is dirty
* @returns {boolean} true if the world is dirty
*/
update(dt) {
let isPaused = state.isPaused();

// only update the quadtree if necessary
if (this.physic === "builtin" || hasRegisteredEvents() === true) {
// clear the quadtree
Expand All @@ -177,15 +175,26 @@ export default class World extends Container {
this.broadphase.insertContainer(this);
}

// only iterate through object is builtin physic is enabled
// update the builtin physic simulation
this.step(dt);

// call the super constructor
return super.update(dt);
}

/**
* update the builtin physic simulation by one step (called by the game world update method)
* @param {number} dt - the time passed since the last frame update
*/
step(dt) {
if (this.physic === "builtin") {
let isPaused = state.isPaused();
// iterate through all bodies
this.bodies.forEach((body) => {
if (!body.isStatic) {
let ancestor = body.ancestor;
// if the game is not paused, and ancestor can be updated
if (!(isPaused && (!ancestor.updateWhenPaused)) &&
(ancestor.inViewport || ancestor.alwaysUpdate)) {
if (!(isPaused && (!ancestor.updateWhenPaused)) && (ancestor.inViewport || ancestor.alwaysUpdate)) {
// apply gravity to this body
this.bodyApplyGravity(body);
// body update function (this moves it)
Expand All @@ -201,9 +210,7 @@ export default class World extends Container {
}
});
}

// call the super constructor
return super.update(dt);
event.emit(event.WORLD_STEP, dt);
}
}

13 changes: 13 additions & 0 deletions src/system/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ export const GAME_BEFORE_DRAW = "me.game.beforeDraw";
*/
export const GAME_AFTER_DRAW = "me.game.afterDraw";


/**
* Event for when the physic world is updated
* Data passed : {number} time the current time stamp
* @public
* @constant
* @type {string}
* @name WORLD_STEP
* @memberof event
* @see event.on
*/
export const WORLD_STEP = "me.world.step";

/**
* Event for when a level is loaded <br>
* Data passed : {string} Level Name
Expand Down

0 comments on commit 9e3de6a

Please sign in to comment.