From 7dbeda0dc94f8844f7f78004c692c7fe6ee58a7e Mon Sep 17 00:00:00 2001 From: Nathan Kunicki Date: Thu, 24 Dec 2015 18:29:00 +0000 Subject: [PATCH] Delta is now passed as param to update methods rather than game property, tidied up sprite and entity --- examples/pong/pong.js | 16 ++--- examples/snowflakes/pong.js | 116 ------------------------------ examples/snowflakes/snowflakes.js | 4 +- src/classes/entity.js | 16 ++--- src/classes/game.js | 4 +- src/classes/sprite.js | 2 +- 6 files changed, 17 insertions(+), 141 deletions(-) delete mode 100644 examples/snowflakes/pong.js diff --git a/examples/pong/pong.js b/examples/pong/pong.js index 7e646e9..ac9451c 100644 --- a/examples/pong/pong.js +++ b/examples/pong/pong.js @@ -45,9 +45,9 @@ window.onload = function () { // Update the ball position ball.state.speed = new MomentumEngine.Classes.Vector2D(0.1, 0.05); // Current ball speed - ball.update = function () { + ball.update = function (delta) { - this.pos.add(this.state.speed.clone().multiply(pong.lastFrameDelta)); + this.pos.add(this.state.speed.clone().multiply(delta)); if ((this.pos.x + baseSize > width && this.state.speed.x > 0) || (this.pos.x < 0 && this.state.speed.x < 0)) { this.state.speed.x = -this.state.speed.x; @@ -61,14 +61,14 @@ window.onload = function () { // Update the left paddle according to keyboard input - leftPaddle.update = function () { + leftPaddle.update = function (delta) { if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_Q) || pong.inputs.keyboard.isPressed(KeyConsts.UP)) { - leftPaddle.pos.y -= (0.5 * pong.lastFrameDelta); + leftPaddle.pos.y -= (0.5 * delta); } if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_A) || pong.inputs.keyboard.isPressed(KeyConsts.DOWN)) { - leftPaddle.pos.y += (0.5 * pong.lastFrameDelta); + leftPaddle.pos.y += (0.5 * delta); } if (leftPaddle.pos.y > height - (baseSize * 8)) { @@ -87,14 +87,14 @@ window.onload = function () { // Update the right paddle according to keyboard input - rightPaddle.update = function () { + rightPaddle.update = function (delta) { if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_O)) { - rightPaddle.pos.y -= (0.5 * pong.lastFrameDelta); + rightPaddle.pos.y -= (0.5 * delta); } if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_L)) { - rightPaddle.pos.y += (0.5 * pong.lastFrameDelta); + rightPaddle.pos.y += (0.5 * delta); } if (rightPaddle.pos.y > height - (baseSize * 8)) { diff --git a/examples/snowflakes/pong.js b/examples/snowflakes/pong.js deleted file mode 100644 index c9f27b5..0000000 --- a/examples/snowflakes/pong.js +++ /dev/null @@ -1,116 +0,0 @@ -"use strict"; - -window.onload = function () { - - - var KeyConsts = MomentumEngine.Consts.Input.Keys; - - - var width = 640, - height = 360, - baseSize = width / 64; - - - var pong = new MomentumEngine.Classes.Game({ - canvas: document.getElementById("canvas"), - width: width, - height: height, - fixRatio: true, - desiredFps: 60, - inputs: { - keyboard: true - } - }); - - - // Colors - var white = new MomentumEngine.Classes.Color(255, 255, 255), - black = new MomentumEngine.Classes.Color(0, 0, 0); - - - // All of these are instances of MomentumEngine.Entity; - var mainScene = new MomentumEngine.Classes.Rect(0, 0, width, height, black), - ball = new MomentumEngine.Classes.Rect((width / 2) - (baseSize / 2), (height / 2) - (baseSize / 2), baseSize, baseSize, white), - leftPaddle = new MomentumEngine.Classes.Rect(baseSize, baseSize, baseSize, baseSize * 7, white), - rightPaddle = new MomentumEngine.Classes.Rect(width - (baseSize * 2), baseSize, baseSize, baseSize * 7, white); - - - // Create scene graph - pong.addChildEntity(mainScene); - mainScene.addChildEntity(ball); - mainScene.addChildEntity(leftPaddle); - mainScene.addChildEntity(rightPaddle); - - - // Update the ball position - ball.state.speed = new MomentumEngine.Classes.Vector2D(0.1, 0.05); // Current ball speed - - ball.update = function () { - - this.pos.add(this.state.speed.clone().multiply(pong.lastFrameDelta)); - - if ((this.pos.x + baseSize > width && this.state.speed.x > 0) || (this.pos.x < 0 && this.state.speed.x < 0)) { - this.state.speed.x = -this.state.speed.x; - } - - if ((this.pos.y + baseSize > height && this.state.speed.y > 0) || (this.pos.y < 0 && this.state.speed.y < 0)) { - this.state.speed.y = -this.state.speed.y; - } - - }; - - - // Update the left paddle according to keyboard input - leftPaddle.update = function () { - - if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_Q) || pong.inputs.keyboard.isPressed(KeyConsts.UP)) { - leftPaddle.pos.y -= (0.5 * pong.lastFrameDelta); - } - - if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_A) || pong.inputs.keyboard.isPressed(KeyConsts.DOWN)) { - leftPaddle.pos.y += (0.5 * pong.lastFrameDelta); - } - - if (leftPaddle.pos.y > height - (baseSize * 8)) { - leftPaddle.pos.y = height - (baseSize * 8); - } - - if (leftPaddle.pos.y < baseSize) { - leftPaddle.pos.y = baseSize; - } - - if (leftPaddle.isCollidingWith(ball) && ball.state.speed.x < 0) { - ball.state.speed.x = -ball.state.speed.x; - } - - }; - - - // Update the right paddle according to keyboard input - rightPaddle.update = function () { - - if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_O)) { - rightPaddle.pos.y -= (0.5 * pong.lastFrameDelta); - } - - if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_L)) { - rightPaddle.pos.y += (0.5 * pong.lastFrameDelta); - } - - if (rightPaddle.pos.y > height - (baseSize * 8)) { - rightPaddle.pos.y = height - (baseSize * 8); - } - - if (rightPaddle.pos.y < baseSize) { - rightPaddle.pos.y = baseSize; - } - - if (rightPaddle.isCollidingWith(ball) && ball.state.speed.x > 0) { - ball.state.speed.x = -ball.state.speed.x; - } - - }; - pong.start(); - - -}; \ No newline at end of file diff --git a/examples/snowflakes/snowflakes.js b/examples/snowflakes/snowflakes.js index 0c2de98..8a35266 100644 --- a/examples/snowflakes/snowflakes.js +++ b/examples/snowflakes/snowflakes.js @@ -34,7 +34,7 @@ window.onload = function () { // Create scene graph snowflakes.addChildEntity(mainScene); - mainScene.update = function () { + mainScene.update = function (delta) { if ((snowflakes.frameCounter % 120) == 0) { // Every two seconds or so, add a new snowflake @@ -43,7 +43,7 @@ window.onload = function () { var newSnowflake = new MomentumEngine.Classes.Sprite(startPos, -100, 100, 100, snowflakeImg); newSnowflake.update = function () { - this.pos.y = this.pos.y + (snowflakes.lastFrameDelta * 0.06); + this.pos.y = this.pos.y + (delta * 0.06); }; mainScene.addChildEntity(newSnowflake); diff --git a/src/classes/entity.js b/src/classes/entity.js index e572646..629cb15 100644 --- a/src/classes/entity.js +++ b/src/classes/entity.js @@ -9,7 +9,6 @@ class Entity { this.pos = new Vector2D(x || 0, y || 0); - this._ready = true; this.state = {}; this.children = []; @@ -61,11 +60,6 @@ class Entity { } - isReady () { - return this._ready; - } - - _preprocess () { // NK: The purpose of this function is to calculate the true position of the entity relative to all its parents. It does this recursively, calling the _preprocess method all the way back up the tree and continuously adding the results together. @@ -108,14 +102,14 @@ class Entity { } - _updateEntity () { + _updateEntity (delta) { - var updated = this.update && this.update(); + var updated = this.update && this.update(delta); - if (this.isReady() && (updated || (typeof updated == "undefined") || (typeof this.update === "undefined"))) { + if (updated || (typeof updated == "undefined") || (typeof this.update === "undefined")) { this.children.forEach((child) => { - child._updateEntity(); + child._updateEntity(delta); }); } @@ -129,7 +123,7 @@ class Entity { var rendered = this.render && this.render(); - if (this.isReady() && (rendered || (typeof rendered == "undefined") || (typeof this.render === "undefined"))) { + if (rendered || (typeof rendered == "undefined") || (typeof this.render === "undefined")) { this.children.forEach((child) => { child._renderEntity(); diff --git a/src/classes/game.js b/src/classes/game.js index 12b3cf2..501dc2f 100644 --- a/src/classes/game.js +++ b/src/classes/game.js @@ -86,7 +86,6 @@ class Game extends Entity { // Initialize defaults - this.lastFrameDelta = 0; this.frameCounter = 0; this.inputs = {}; @@ -103,10 +102,9 @@ class Game extends Entity { step (delta) { - this.lastFrameDelta = delta; this.frameCounter++; - this._updateEntity(); + this._updateEntity(delta); this._renderEntity(); } diff --git a/src/classes/sprite.js b/src/classes/sprite.js index aee1760..4a83ca9 100644 --- a/src/classes/sprite.js +++ b/src/classes/sprite.js @@ -37,7 +37,7 @@ class Sprite extends Entity { } - isReady () { // Overrides super isReady + isReady () { return (this._image.isLoaded() && !this._image.isError()); }