Added camera positioning

This commit is contained in:
Nathan Kunicki 2016-03-04 11:31:31 -05:00
parent 73ee61cb00
commit 84a3c92ac1
2 changed files with 29 additions and 8 deletions

View File

@ -66,11 +66,11 @@ window.onload = function () {
leftPaddle.update = function (delta) { leftPaddle.update = function (delta) {
if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_Q) || pong.inputs.keyboard.isPressed(KeyConsts.UP)) { 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)) { 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)) { if (leftPaddle.top > height - (baseSize * 8)) {
@ -92,19 +92,19 @@ window.onload = function () {
rightPaddle.update = function (delta) { rightPaddle.update = function (delta) {
if (pong.inputs.keyboard.isPressed(KeyConsts.CHAR_O)) { 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)) { 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)) { if (rightPaddle.top > height - (baseSize * 8)) {
rightPaddle.pos.y = height - (baseSize * 8); rightPaddle.top = height - (baseSize * 8);
} }
if (rightPaddle.pos.top < baseSize) { if (rightPaddle.top < baseSize) {
rightPaddle.pos.y = baseSize; rightPaddle.top = baseSize;
} }
if (rightPaddle.isCollidingWith(ball) && ball.state.speed.x > 0) { if (rightPaddle.isCollidingWith(ball) && ball.state.speed.x > 0) {

View File

@ -1,6 +1,7 @@
"use strict"; "use strict";
import Entity from "./entity.js"; import Entity from "./entity.js";
import Vector2D from "./vector2d.js";
import KeyboardInput from "./keyboardinput.js"; import KeyboardInput from "./keyboardinput.js";
class Game extends Entity { 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) { step (delta) {
this.frameCounter++; this.frameCounter++;