Skip to content

Commit

Permalink
very basic game logic
Browse files Browse the repository at this point in the history
  • Loading branch information
javisantana committed Aug 18, 2011
1 parent 4632a19 commit ac358b1
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 24 deletions.
45 changes: 45 additions & 0 deletions public/game1/bullet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


var Bullet = function(pos, vel) {
this.pos = pos;
this.vel = vel;
};

Bullet.prototype.update = function(dt) {
var p = this;
p.time += dt;

p.pos = vec2.add(p.pos, vec2.mul(dt, p.vel));
//p.pos.x = 100*Math.cos(p.time*p.vel);
//p.pos.y = 100*Math.sin(p.time*p.vel);
};

Bullet.prototype.render = function(ctx) {
var p = this.pos;
ctx.fillRect(p.x, p.y, 5, 5);
};

var Bullets = function() {
this.bullets = [];
}

Bullets.prototype.fire = function(pos, vel) {
this.bullets.push(new Bullet(pos, vel));
console.log('bullets ' + this.bullets.length);
}

Bullets.prototype.update = function(dt) {
var b = this.bullets;
for(var i = 0; i < b.length; ++i) {
b[i].update(dt);
}
};

Bullets.prototype.render = function(ctx) {
var b = this.bullets;
for(var i = 0; i < b.length; ++i) {
b[i].render(ctx);
}
};

window.bullets = new Bullets();
20 changes: 20 additions & 0 deletions public/game1/js/vec2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

function vec2(x, y) {

this.x = x || 0;
this.y = y || 0;

this.lengthSq = function() {
var v = this;
return v.x*v.x + v.y*v.y;
}
}

vec2.add = function(a, b) {
return new vec2(a.x + b.x, a.y + b.y);
};

vec2.mul = function(scalar, b) {
return new vec2(scalar*b.x, scalar*b.y);
}

66 changes: 42 additions & 24 deletions public/game1/lines.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,14 @@

<script src="js/underscore.js"> </script>
<script src="js/backbone.js"> </script>
<script src="js/vec2.js"> </script>
<script src="js/game.js"> </script>

<script>
var Player = function(x, y) {
this.x = x || 0;
this.y = y || 0;
this.srcx = Math.random()*100;
this.srcy = Math.random()*100;
this.vel = Math.random()*0.001;
this.time = 0;
};

Player.prototype.update = function(dt) {
var p = this;
p.time += dt;
p.x = 100*Math.cos(p.time*p.vel);
p.y = 100*Math.sin(p.time*p.vel);
};

Player.prototype.render = function(ctx) {
var p = this;
ctx.fillRect(p.x, p.y, 10, 10);
};
<!-- game specific -->
<script src="bullet.js"> </script>
<script src="player.js"> </script>

<script>
var TestGame = Game.extend({

players: [],
Expand All @@ -43,24 +27,58 @@
this.bind('newplayer', this.on_new_player);
},

on_new_player: function() {
this.players.push(new Player());
on_new_player: function(controller) {
this.players.push(new Player(new vec2(0, 0), controller));
},

update: function(dt) {
_.each(this.players,function(p) { p.update(dt) });
bullets.update(dt);
},

render: function() {
// clear layer
var game = this;
game.layer.clearRect(-this.widthHalf, -this.heightHalf, 2*this.widthHalf, 2*this.heightHalf);
_.each(this.players, function(p) { p.render(game.layer) });
bullets.render(game.layer);
}
});
var g = new TestGame();
g.start();
g.on_new_player();

function Controller() {
this.controls = {
left: false,
right: false,
fire: false
};
};

var c = new Controller();
local_controller(c);
g.on_new_player(c);

function local_controller(c) {
window.addEventListener('keyup', function(e) {
if(e.keyCode == 37) { //left
c.controls.left = false;
} else if(e.keyCode == 39) { // right
c.controls.right= false;
} else if(e.keyCode == 32) {
c.controls.fire = false;
}
});
window.addEventListener('keydown', function(e) {
if(e.keyCode == 37) { //left
c.controls.left = true;
} else if(e.keyCode == 39) { // right
c.controls.right= true;
} else if(e.keyCode == 32) {
c.controls.fire = true;
}
});
}
</script>
</body>
</html>
Expand Down
25 changes: 25 additions & 0 deletions public/game1/player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

var Player = function(pos, controller) {
this.pos = pos;
this.controller = controller;
};

Player.prototype.update = function(dt) {
var p = this;
p.time += dt;

if(this.controller.controls.left) {
p.pos = vec2.add(p.pos, new vec2(1, 0));
}
if(this.controller.controls.fire) {
window.bullets.fire(p.pos, new vec2(0, -1));
}
//p.pos.x = 100*Math.cos(p.time*p.vel);
//p.pos.y = 100*Math.sin(p.time*p.vel);
};

Player.prototype.render = function(ctx) {
var p = this.pos;
ctx.fillRect(p.x, p.y, 10, 10);
};

0 comments on commit ac358b1

Please sign in to comment.