--world js var default_prefs boundingWalls true size width 100 height

 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
--world.js
var default_prefs = { boundingWalls: true, size: { width: 100, height: 200}, default_player: {defaultSize: 0.01}};
module.exports = function make_world(preferences) {
preferences = _.extend(default_prefs, preferences);
var units = [];
// bla-bla-bla
var listeners = {};
var trigger = function (eventType) {
if (listeners[eventType]) {
listeners[eventType].forEach(function (item) {
item ? item() : 0;
});
}
}
var obj = {
prefs: preferences
make_player: function (opts) {
var p = make_player.apply(this, [units.length, _.extend(preferences.default_player, opts)]);
units.push(p);
return p;
},
bind: function (eventType, listener) {
if (!listeners[eventType]) {
listeners[eventType] = [];
}
listeners[eventType].push(listener);
},
unbind: function (eventType, listener) {
if (listeners[eventType]) {
delete listeners[eventType][listeners[eventType].indexOf(listener)];
}
}
// bla-bla-bla
};
return obj;
}
--player.js
module.exports = function make_player(id, preferences) {
// bla-bla-bla
var size = this.prefs.size.width * preferences.defaultSize;
var that = {
id: id,
type: 'player',
size: size
}
return that;
}