Browse Source

better handling of input

master
mortie 7 years ago
parent
commit
4f81b826fc
3 changed files with 28 additions and 7 deletions
  1. 7
    2
      js/entities.js
  2. 20
    4
      js/game.js
  3. 1
    1
      js/worldgen.js

+ 7
- 2
js/entities.js View File

@@ -6,6 +6,8 @@
function Player(game) {
makeEnt(this, game, 100);
this.moves = true;
this.inputListener = true;

this.invincible = false;
this.invincibleTimeout = null;
this.started = false;
@@ -51,12 +53,15 @@ Player.prototype.setInvincible = function(time) {
this.invincible = false;
}.bind(this), time);
}
Player.prototype.update = function() {
Player.prototype.onInput = function(name, down) {

// Jump
if (this.game.presses.jump) {
if (name === "jump" && down) {
this.started = true;
this.vel.y = -1.3;
}
}
Player.prototype.update = function() {

// Gravity and movement
if (this.started) {

+ 20
- 4
js/game.js View File

@@ -118,7 +118,8 @@ function makeEnt(obj, game, mass) {
obj.game = game;
obj.shape = new Shape(obj);
obj.moves = false;
this.dead = false;
obj.dead = false;
obj.inputListener = false;

obj.mass = mass || 0;
obj.forceScalar = 1 / obj.mass;
@@ -138,9 +139,9 @@ function Game(canvas) {
this.worldgen = null;

this.entities = [];
this.inputListeners = [];

this.keys = {};
this.presses = {};
this.onkey = function onkey(evt) {
var down = (evt.type === "keydown" || evt.type === "touchstart");
var code = evt.keyCode || "touch";
@@ -148,8 +149,18 @@ function Game(canvas) {
var name = keymap[code];
if (name) {
this.keys[name] = down;
if (down)
this.presses[name] = true;

for (var i = 0; i < this.inputListeners.length; ++i) {
var ent = this.inputListeners[i];
if (ent === null) {
continue;
} else if (ent.dead) {
delete this.inputListeners[i];
continue;
}

this.inputListeners[i].onInput(name, down);
}
}
}.bind(this);
}
@@ -165,6 +176,11 @@ Game.prototype.start = function(worldgen) {

this.update();
}
Game.prototype.spawn = function(ent) {
this.entities.push(ent);
if (ent.inputListener)
this.inputListeners.push(ent);
}
Game.prototype.update = function() {
var time = new Date().getTime();
var dt = time - this.prevTime;

+ 1
- 1
js/worldgen.js View File

@@ -7,7 +7,7 @@ function WorldGen(game) {
this.powerupCounter = randInt(WorldGen.range[0], WorldGen.range[1]);

// Spawn player
game.entities.push(new Player(game));
game.spawn(new Player(game));
}
WorldGen.range = [5, 12];


Loading…
Cancel
Save