瀏覽代碼

better handling of input

master
mortie 7 年之前
父節點
當前提交
4f81b826fc
共有 3 個檔案被更改,包括 28 行新增7 行删除
  1. 7
    2
      js/entities.js
  2. 20
    4
      js/game.js
  3. 1
    1
      js/worldgen.js

+ 7
- 2
js/entities.js 查看文件

function Player(game) { function Player(game) {
makeEnt(this, game, 100); makeEnt(this, game, 100);
this.moves = true; this.moves = true;
this.inputListener = true;

this.invincible = false; this.invincible = false;
this.invincibleTimeout = null; this.invincibleTimeout = null;
this.started = false; this.started = false;
this.invincible = false; this.invincible = false;
}.bind(this), time); }.bind(this), time);
} }
Player.prototype.update = function() {
Player.prototype.onInput = function(name, down) {

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


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

+ 20
- 4
js/game.js 查看文件

obj.game = game; obj.game = game;
obj.shape = new Shape(obj); obj.shape = new Shape(obj);
obj.moves = false; obj.moves = false;
this.dead = false;
obj.dead = false;
obj.inputListener = false;


obj.mass = mass || 0; obj.mass = mass || 0;
obj.forceScalar = 1 / obj.mass; obj.forceScalar = 1 / obj.mass;
this.worldgen = null; this.worldgen = null;


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


this.keys = {}; this.keys = {};
this.presses = {};
this.onkey = function onkey(evt) { this.onkey = function onkey(evt) {
var down = (evt.type === "keydown" || evt.type === "touchstart"); var down = (evt.type === "keydown" || evt.type === "touchstart");
var code = evt.keyCode || "touch"; var code = evt.keyCode || "touch";
var name = keymap[code]; var name = keymap[code];
if (name) { if (name) {
this.keys[name] = down; 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); }.bind(this);
} }


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

+ 1
- 1
js/worldgen.js 查看文件

this.powerupCounter = randInt(WorldGen.range[0], WorldGen.range[1]); this.powerupCounter = randInt(WorldGen.range[0], WorldGen.range[1]);


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



Loading…
取消
儲存