Delta is now passed as param to update methods rather than game property, tidied up sprite and entity
This commit is contained in:
parent
5446079fe6
commit
e620ff9c94
2
dist/es5.js
vendored
2
dist/es5.js
vendored
File diff suppressed because one or more lines are too long
2
dist/es5.js.map
vendored
2
dist/es5.js.map
vendored
File diff suppressed because one or more lines are too long
@ -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)) {
|
||||
|
@ -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();
|
||||
|
||||
|
||||
};
|
@ -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);
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ class Sprite extends Entity {
|
||||
}
|
||||
|
||||
|
||||
isReady () { // Overrides super isReady
|
||||
isReady () {
|
||||
return (this._image.isLoaded() && !this._image.isError());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user