-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniverse.js
67 lines (55 loc) · 1.46 KB
/
universe.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
var events = require('events');
var util = require('util');
var _ = require('underscore');
function Universe(){
events.EventEmitter.call(this);
this.objects = {};
}
util.inherits(Universe, events.EventEmitter);
_.extend(Universe.prototype, {
add: function(uObject){
this.objects[uObject.id] = uObject;
this.emit('new', uObject);
},
getObjectByID: function(objectID){
return this.objects[objectID];
},
newObject: function(){
var newUniverseObject = UniverseObject.newObject.apply(this, arguments);
this.add(newUniverseObject);
return newUniverseObject;
},
serialize: function(){
var universeData = {};
_.each(this.objects, function(universeObject, id){
universeData[id] = universeObject.serialize();
});
return universeData;
}
});
var UniverseObject = function(id, type, x, y, props){
this.id = id;
this.type = type;
this.x = x;
this.y = y;
this.props = props;
};
UniverseObject.idSequence = 0;
UniverseObject.newObject = function(type, x, y, props){
return new UniverseObject(UniverseObject.idSequence++, type, x, y, props);
};
UniverseObject.prototype = {
serialize: function(){
return {
id: this.id,
type: this.type,
x: this.x,
y: this.y,
props: this.props
};
}
};
module.exports = {
Universe: Universe,
UniverseObject: UniverseObject
}