Pong example uses sticks
This commit is contained in:
parent
3406c28a80
commit
5336f0d4bf
@ -74,9 +74,9 @@ class Paddle extends MomentumEngine.Classes.Rect {
|
||||
|
||||
update (delta) {
|
||||
|
||||
if (this._game.inputs.keyboard.isPressed(this.keyUp)) {
|
||||
if (this.keyUp(this, delta)) {
|
||||
this.top -= (0.5 * delta);
|
||||
} else if (this._game.inputs.keyboard.isPressed(this.keyDown)) {
|
||||
} else if (this.keyDown(this, delta)) {
|
||||
this.top += (0.5 * delta);
|
||||
}
|
||||
|
||||
@ -129,7 +129,8 @@ class Pong extends MomentumEngine.Classes.Game {
|
||||
fixRatio: true,
|
||||
desiredFps: 60,
|
||||
inputs: {
|
||||
keyboard: true
|
||||
keyboard: true,
|
||||
gamepad: true
|
||||
}
|
||||
});
|
||||
|
||||
@ -170,6 +171,117 @@ class Pong extends MomentumEngine.Classes.Game {
|
||||
}
|
||||
|
||||
|
||||
var leftPaddleUpCondition = function (paddle, delta) {
|
||||
|
||||
let gamepadInput = paddle._game.inputs.gamepad;
|
||||
|
||||
let gamepadConnected = (gamepadInput.numGamepads >= 1),
|
||||
axisMoved = false;
|
||||
|
||||
if (gamepadConnected) {
|
||||
|
||||
let upDownAxes = gamepadInput.getGamepadById(0).getAxis(1);
|
||||
|
||||
if (upDownAxes < -0.1) {
|
||||
this.top += (0.5 * delta) * upDownAxes;
|
||||
axisMoved = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!axisMoved) {
|
||||
if (paddle._game.inputs.keyboard.isPressed(KeyConsts.CHAR_Q)) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
var leftPaddleDownCondition = function (paddle, delta) {
|
||||
|
||||
let gamepadInput = paddle._game.inputs.gamepad;
|
||||
|
||||
let gamepadConnected = (gamepadInput.numGamepads >= 1),
|
||||
axisMoved = false;
|
||||
|
||||
if (gamepadConnected) {
|
||||
|
||||
let upDownAxes = gamepadInput.getGamepadById(0).getAxis(1);
|
||||
|
||||
if (upDownAxes > 0.1) {
|
||||
this.top += (0.5 * delta) * upDownAxes;
|
||||
axisMoved = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!axisMoved) {
|
||||
if (paddle._game.inputs.keyboard.isPressed(KeyConsts.CHAR_A)) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
var rightPaddleUpCondition = function (paddle, delta) {
|
||||
|
||||
let gamepadInput = paddle._game.inputs.gamepad;
|
||||
|
||||
let gamepadConnected = (gamepadInput.numGamepads >= 1),
|
||||
axisMoved = false;
|
||||
|
||||
if (gamepadConnected) {
|
||||
|
||||
let upDownAxes = gamepadInput.getGamepadById(0).getAxis(4);
|
||||
|
||||
if (upDownAxes < -0.1) {
|
||||
this.top += (0.5 * delta) * upDownAxes;
|
||||
axisMoved = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!axisMoved) {
|
||||
if (paddle._game.inputs.keyboard.isPressed(KeyConsts.CHAR_O)) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
var rightPaddleDownCondition = function (paddle, delta) {
|
||||
|
||||
let gamepadInput = paddle._game.inputs.gamepad;
|
||||
|
||||
let gamepadConnected = (gamepadInput.numGamepads >= 1),
|
||||
axisMoved = false;
|
||||
|
||||
if (gamepadConnected) {
|
||||
|
||||
let upDownAxes = gamepadInput.getGamepadById(0).getAxis(4);
|
||||
|
||||
if (upDownAxes > 0.1) {
|
||||
this.top += (0.5 * delta) * upDownAxes;
|
||||
axisMoved = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!axisMoved) {
|
||||
if (paddle._game.inputs.keyboard.isPressed(KeyConsts.CHAR_L)) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
window.onload = function () {
|
||||
|
||||
var pong = new Pong(document.getElementById("canvas"), width, height);
|
||||
@ -177,13 +289,13 @@ window.onload = function () {
|
||||
var ball = new Ball((width / 2) - (baseSize / 2), (height / 2) - (baseSize / 2));
|
||||
|
||||
var leftPaddle = new Paddle(baseSize, {
|
||||
up: KeyConsts.CHAR_Q,
|
||||
down: KeyConsts.CHAR_A
|
||||
up: leftPaddleUpCondition,
|
||||
down: leftPaddleDownCondition
|
||||
});
|
||||
|
||||
var rightPaddle = new Paddle(width - (baseSize * 2), {
|
||||
up: KeyConsts.CHAR_O,
|
||||
down: KeyConsts.CHAR_L
|
||||
up: rightPaddleUpCondition,
|
||||
down: rightPaddleDownCondition
|
||||
});
|
||||
|
||||
var leftScoreboard = new Scoreboard(baseSize),
|
||||
|
@ -57,7 +57,7 @@ class Emitter extends Entity {
|
||||
|
||||
if (this.emitting) {
|
||||
|
||||
let currentTime = +(new Date());
|
||||
let currentTime = Date.now();
|
||||
|
||||
if (!this._wasEmitting) {
|
||||
this._wasEmitting = true;
|
||||
|
@ -207,7 +207,7 @@ class Entity {
|
||||
_updateEntity (delta) {
|
||||
|
||||
if (this.timeToLive) {
|
||||
if (+(new Date()) - this._creationTime > this.timeToLive) {
|
||||
if (Date.now() - this._creationTime > this.timeToLive) {
|
||||
this._parent.detachChildEntity(this);
|
||||
}
|
||||
}
|
||||
@ -216,7 +216,9 @@ class Entity {
|
||||
if (this.velocity && (this.velocity.x !== 0 || this.velocity.y !== 0)) {
|
||||
|
||||
this.velocity.add(this._calculateFields(delta));
|
||||
this.pos.add(this.velocity.clone().multiply(delta));
|
||||
//this.pos.add(this.velocity.clone().multiply(delta));
|
||||
this.pos.x += (this.velocity.x * delta);
|
||||
this.pos.y += (this.velocity.y * delta);
|
||||
|
||||
}
|
||||
|
||||
|
@ -160,6 +160,7 @@ class Game extends Entity {
|
||||
window.webkitRequestAnimationFrame ||
|
||||
window.mozRequestAnimationFrame ||
|
||||
function (callback) {
|
||||
console.log("THIS");
|
||||
window.setTimeout(callback, 1000 / self.desiredFps);
|
||||
});
|
||||
|
||||
|
@ -1,6 +1,43 @@
|
||||
"use strict";
|
||||
|
||||
|
||||
class Gamepad {
|
||||
|
||||
constructor (gamepadObj) {
|
||||
this._gamepadObj = gamepadObj;
|
||||
}
|
||||
|
||||
get numButtons () {
|
||||
return this._gamepadObj.buttons.length;
|
||||
}
|
||||
|
||||
get numAxis () {
|
||||
return this._gamepadObj.axes.length;
|
||||
}
|
||||
|
||||
isPressed (buttonId) {
|
||||
|
||||
if (this._gamepadObj.buttons[buttonId]) {
|
||||
return !!this._gamepadObj.buttons[buttonId].pressed;
|
||||
} else {
|
||||
throw new Error(`Button ${buttonId} not found on gamepad`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
getAxis (axisId) {
|
||||
|
||||
if (this._gamepadObj.axes[axisId]) {
|
||||
return this._gamepadObj.axes[axisId];
|
||||
} else {
|
||||
throw new Error(`Axis ${axisId} not found on gamepad`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class GamepadInput {
|
||||
|
||||
|
||||
@ -9,21 +46,41 @@ class GamepadInput {
|
||||
var self = this;
|
||||
|
||||
self._gamepadState = {};
|
||||
self._gamepadList = [];
|
||||
self.gamepadIds = [];
|
||||
|
||||
window.addEventListener("gamepadconnected", (event) => {
|
||||
self._gamepadState[event.gamepad.index] = event.gamepad;
|
||||
self._gamepadList.push(event.gamepad.index);
|
||||
self._gamepadState[event.gamepad.index] = new Gamepad(event.gamepad);
|
||||
self.gamepadIds.push(event.gamepad.index);
|
||||
console.log("Connected");
|
||||
console.log(event.gamepad);
|
||||
console.log(event.gamepad.buttons[0]);
|
||||
});
|
||||
|
||||
window.addEventListener("gamepaddisconnected", (event) => {
|
||||
delete self._gamepadState[event.gamepad.index];
|
||||
self._gamepadList.splice(self._gamepadList.indexOf(event.gamepad.index));
|
||||
self.gamepadIds.splice(self.gamepadIds.indexOf(event.gamepad.index));
|
||||
console.log("Disconnected");
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
get numGamepads () {
|
||||
return this.gamepadIds.length;
|
||||
}
|
||||
|
||||
|
||||
getGamepadById (gamepadId) {
|
||||
|
||||
if (this._gamepadState[gamepadId]) {
|
||||
return this._gamepadState[gamepadId];
|
||||
} else {
|
||||
throw new Error(`Gamepad ${buttonId} is not connected`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user