Skip to content

Commit

Permalink
single player mode working
Browse files Browse the repository at this point in the history
  • Loading branch information
gerad committed Sep 21, 2010
1 parent 1d7b070 commit dfb06a0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
15 changes: 6 additions & 9 deletions lazeroids.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Controller

setupKeys: ->
$(window).keydown (e) =>
ship: @universe.ship
ship = @universe.ship
switch e.which
when 32 # space bar = shoot
ship.shoot()
Expand Down Expand Up @@ -199,7 +199,7 @@ class Universe

connect: (ship) ->
return if @ships.find ship
@status "$ship.name connected"
@status "#{ship.name} connected"
@silently =>
@add ship
@send 'connect', ship
Expand All @@ -222,7 +222,7 @@ class Universe
@loop()

@injectAsteroids 5
setInterval __bind(this, @injectAsteroids, 3), 5000
setInterval (=> @injectAsteroids 3), 5000
setInterval __bind(@updateLeaderboard, this), 1000

play 'ambient', { loop: true }
Expand All @@ -231,7 +231,7 @@ class Universe
@network()
@step 1
@render()
setTimeout (@loop <- this), 1000/24
setTimeout __bind(@loop, this), 1000/24

step: (dt) ->
@tick += dt
Expand All @@ -246,8 +246,6 @@ class Universe
@io.flush()

perform: (method, data) ->
# console.log "Performing #{method} with:"
# console.dir data
this[method] data

status: (message) ->
Expand All @@ -270,8 +268,7 @@ class Universe

injectAsteroids: (howMany) ->
return if @masses.length > 80

for i in [1 .. howMany || 1]
for i in [1 .. (howMany || 1)]
b = @bounds
[w, h] = [@bounds.width, @bounds.height]
outside = new Vector w*Math.random()-w/2+b.l, h*Math.random()-h/2+b.t
Expand Down Expand Up @@ -582,7 +579,7 @@ class Asteroid extends Mass
@universe.add(new Explosion({ from: this }))

_render: (ctx) ->
p: @corners
p = @corners
ctx.beginPath()
ctx.moveTo p[0].x, p[0].y
ctx.lineTo p[i].x, p[i].y for i in [1 ... p.length]
Expand Down
32 changes: 15 additions & 17 deletions lazeroids.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@
};
Controller.prototype.setupKeys = function() {
$(window).keydown(__bind(function(e) {
var _d;
{
ship: this.universe.ship
};
var _d, ship;
ship = this.universe.ship;
if ((_d = e.which) === 32) {
return ship.shoot();
} else if (_d === 37) {
Expand Down Expand Up @@ -290,7 +288,7 @@
if (this.ships.find(ship)) {
return null;
}
this.status("$ship.name connected");
this.status(("" + (ship.name) + " connected"));
this.silently(__bind(function() {
return this.add(ship);
}, this));
Expand Down Expand Up @@ -318,7 +316,9 @@
this.requestSync();
this.loop();
this.injectAsteroids(5);
setInterval(__bind(this, this.injectAsteroids, 3), 5000);
setInterval((__bind(function() {
return this.injectAsteroids(3);
}, this)), 5000);
setInterval(__bind(this.updateLeaderboard, this), 1000);
return play('ambient', {
loop: true
Expand All @@ -328,7 +328,7 @@
this.network();
this.step(1);
this.render();
return setTimeout((this.loop < -this), 1000 / 24);
return setTimeout(__bind(this.loop, this), 1000 / 24);
};
Universe.prototype.step = function(dt) {
var _d, _e, id, mass;
Expand Down Expand Up @@ -392,17 +392,17 @@
return ctx.restore();
};
Universe.prototype.injectAsteroids = function(howMany) {
var _d, _e, b, centripetal, h, i, inside, outside, w;
var _d, _e, _f, b, centripetal, h, i, inside, outside, w;
if (this.masses.length > 80) {
return null;
}
_d = [];
for (i = 1; (1 <= howMany || 1 ? i <= howMany || 1 : i >= howMany || 1); (1 <= howMany || 1 ? i += 1 : i -= 1)) {
_d = []; _e = (howMany || 1);
for (i = 1; (1 <= _e ? i <= _e : i >= _e); (1 <= _e ? i += 1 : i -= 1)) {
_d.push((function() {
b = this.bounds;
_e = [this.bounds.width, this.bounds.height];
w = _e[0];
h = _e[1];
_f = [this.bounds.width, this.bounds.height];
w = _f[0];
h = _f[1];
outside = new Vector(w * Math.random() - w / 2 + b.l, h * Math.random() - h / 2 + b.t);
if (outside.x > b.l) {
outside.x += w;
Expand Down Expand Up @@ -812,10 +812,8 @@
}));
};
Asteroid.prototype._render = function(ctx) {
var _d, i;
{
p: this.corners
};
var _d, i, p;
p = this.corners;
ctx.beginPath();
ctx.moveTo(p[0].x, p[0].y);
_d = p.length;
Expand Down
2 changes: 1 addition & 1 deletion script/server
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#! /usr/bin/env bash
restartr node server.js
restartr node server.js *.js
3 changes: 2 additions & 1 deletion views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
| \
span.divider |
| \ objects:
| \
span.objects

table#score
Expand Down Expand Up @@ -64,7 +65,7 @@ canvas
| $('#splash').fadeIn();
| if (localStorage.name)
| $('input[name=name]').val(localStorage.name).select();
| //c = new Lz.Controller($('canvas').get(0));
| c = new Lz.Controller($('canvas').get(0));
| $('form').submit(function() {
| c.setName(localStorage.name = $('input[name=name]', this).val());
| c.setupInput();
Expand Down

0 comments on commit dfb06a0

Please sign in to comment.