-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
86 lines (70 loc) · 2.13 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
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
require('./Helpers/Override');
require('setimmediate');
var EE = require('events').EventEmitter;
var Random = require('mersenne-twister');
var Tower = require('./Dungeon/Tower');
var Player = require('./Player');
var Camera = require('./General/Camera');
var contactListener = require('./General/Collision/ContactListener');
var assets = require('./Assets');
console.log(assets);
var B2d = require('Box2D');
var Vec2 = B2d.b2Vec2;
var World = B2d.b2World;
var Game = module.exports = function(seed, draw, controller, size){
EE.call(this);
this.timeListeners = [];
this.nextTime = this.nextTime.bind(this);
this.on('newListener', function(type){
console.trace(type);
});
var r = new Random(seed);
this.rng = r.random.bind(r);
this.world = new World(new Vec2());
this.world.SetContactListener(contactListener);
this.draw = draw;
this.camera = new Camera(draw.context, {
max: 10, cur: size, min: .5
}, .1, controller);
this.world.SetDebugDraw(draw);
this.tower = new Tower(this, 8, assets.Rooms);
this.player = new Player(this, controller, {
hp: 30,
damageableRadius: 3,
personalBubbleRadius: 10,
runSpeed: 20,
walkSpeed: 10
});
};
Game.prototype = Object.create(EE.prototype);
Game.prototype.constructor = Game;
Game.prototype.loop = function(){
var timeStep = 1.0 / 60.0;
var l = this.timeListeners.length;
while(l--) this.once('time', this.timeListeners.pop());
this.emit('time', timeStep);
var iterations = 10;
this.world.Step(timeStep, iterations);
this.render(timeStep);
setImmediate(this.loop.bind(this));
};
Game.prototype.render = function(timeStep){
this.draw.clear();
this.camera.step(timeStep);
this.emit('pre-debugDraw', this.draw);
this.world.DrawDebugData();
this.emit('post-debugDraw', this.draw);
this.draw.flush();
};
Game.prototype.start = function(){
console.log('starting');
this.tower.setFloor(0);
this.player.spawn(this.world, this.tower.getStart());
this.camera.follow(this.player.body);
console.log('spawned');
setImmediate(this.loop.bind(this));
};
Game.prototype.nextTime = function(fn){
this.timeListeners.push(fn);
};