-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
74 lines (59 loc) · 1.82 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var Mustache = require('mustache');
var _ = require('underscore');
var events = require('events');
var util = require('util');
var Entity = require('./game/entity')
var Player = require('./game/player')
var Rock = require('./game/rock')
function Game() {
var self = this;
this.tickHandle;
this.entities = {}; // keyed on id
this.fps = 10;
this.frameDuration = 1000 / this.fps;
this.ticks = 0;
this.nextEntityId = 1;
this.getNewEntityId = function(){
return this.nextEntityId++;
};
this.run = function() {
if (this.tickHandle) { throw 'Already running'; }
this.tickHandle = setInterval(function(){ self.tick(); }, this.frameDuration);
var rock = new Rock(this);
return this;
}
this.tick = function() {
this.thisTickTime = new Date().getTime();
this.ticks += 1;
// if (!this.lastPrintTime || new Date() - this.lastPrintTime > 1000) {
// var msg = Mustache.render("{{ts}}: {{ticks}} ticks. ({{ms}} ms since last print).", {
// ts: this.thisTickTime / 1000,
// ticks: this.ticks,
// ms: this.thisTickTime - this.lastPrintTime
// });
// console.log(msg);
// this.lastPrintTime = this.thisTickTime;
// }
_.each(self.entities, function(p){
p.tick();
});
self.emit('tick');
// Let nothing come after this in this function
this.lastTickTime = this.thisTickTime;
}
this.addEntity = function(e){
this.entities[e.id] = e;
}
this.removeEntity = function(eid) {
console.log('removing entity', eid)
var entityPresent = !!this.entities[eid];
if (!entityPresent) { throw "Entity not present: " + eid; }
delete this.entities[eid];
this.emit('entity_removed', eid)
}
};
util.inherits(Game, events.EventEmitter);
Game.Entity = Entity;
Game.Player = Player;
Game.Rock = Rock;
module.exports = Game;