From 84a3c92ac1a23085208f6a6cce9a34a35f8082d9 Mon Sep 17 00:00:00 2001 From: Nathan Kunicki Date: Fri, 4 Mar 2016 11:31:31 -0500 Subject: [PATCH] Added camera positioning --- examples/pong/pong.js | 16 ++++++++-------- src/classes/game.js | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/examples/pong/pong.js b/examples/pong/pong.js index 1c47886..ced457b 100644 --- a/examples/pong/pong.js +++ b/examples/pong/pong.js @@ -66,11 +66,11 @@ window.onload = function () { leftPaddle.update = function (delta) { if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_Q) || pong.inputs.keyboard.isPressed(KeyConsts.UP)) { - leftPaddle.pos.y -= (0.5 * delta); + leftPaddle.top -= (0.5 * delta); } if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_A) || pong.inputs.keyboard.isPressed(KeyConsts.DOWN)) { - leftPaddle.pos.y += (0.5 * delta); + leftPaddle.top += (0.5 * delta); } if (leftPaddle.top > height - (baseSize * 8)) { @@ -92,19 +92,19 @@ window.onload = function () { rightPaddle.update = function (delta) { if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_O)) { - rightPaddle.pos.y -= (0.5 * delta); + rightPaddle.top -= (0.5 * delta); } if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_L)) { - rightPaddle.pos.y += (0.5 * delta); + rightPaddle.top += (0.5 * delta); } - if (rightPaddle.pos.top > height - (baseSize * 8)) { - rightPaddle.pos.y = height - (baseSize * 8); + if (rightPaddle.top > height - (baseSize * 8)) { + rightPaddle.top = height - (baseSize * 8); } - if (rightPaddle.pos.top < baseSize) { - rightPaddle.pos.y = baseSize; + if (rightPaddle.top < baseSize) { + rightPaddle.top = baseSize; } if (rightPaddle.isCollidingWith(ball) && ball.state.speed.x > 0) { diff --git a/src/classes/game.js b/src/classes/game.js index 976c7b3..4d85701 100644 --- a/src/classes/game.js +++ b/src/classes/game.js @@ -1,6 +1,7 @@ "use strict"; import Entity from "./entity.js"; +import Vector2D from "./vector2d.js"; import KeyboardInput from "./keyboardinput.js"; class Game extends Entity { @@ -101,6 +102,26 @@ class Game extends Entity { } + setCamera (x, y) { + + if (x instanceof Vector2D) { + + let pos = x.clone(); + pos.x = -pos.x; + pos.y = -pos.y; + + this.pos = pos; + + } else { + + this.pos.x = -x; + this.pos.y = -y; + + } + + } + + step (delta) { this.frameCounter++;