-
Notifications
You must be signed in to change notification settings - Fork 34
/
entities.js
77 lines (66 loc) · 1.87 KB
/
entities.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
Entity.MT_PORTABLE = 0;
Entity.MT_LIFTABLE = 1;
Entity.MT_STATIC = 2;
Entity.templates = {};
Entity.init = function(data) {
data.forEach(function(props) {
var e = new Entity();
e.sync(props)
Entity.templates[e.Type] = e;
});
}
Entity.sync = function(data, remove) {
var containers = []; //to update
var i = 0;
for (var id in data) {
var edata = data[id];
var entity = game.entities.get(id);
if(!entity) {
entity = new Entity(id, edata.Type);
game.addEntity(entity);
}
entity.sync(edata);
if (entity.MoveType == Entity.MT_STATIC) {
game.map.updateObject(entity);
}
if(game.containers[entity.Id]) {
containers.push(game.containers[entity.Id]);
} else if (entity.Container == 0 && game.containers[0]) {
game.containers[0].update();
}
i++;
}
remove && remove.forEach(game.removeEntityById);
containers.forEach(function(container) {
container.update();
})
},
Entity.collides = function(entity) {
return game.filter("Entity").some(function(e) {
return e.collides(entity.X, entity.Y, entity.Radius);
});
};
Entity.get = function(id) {
return game.entities.get(parseInt(id));
};
Entity.getPreview = function(group) {
var image = new Image();
for (var type in Entity.templates) {
var template = Entity.templates[type];
if (Entity.prototype.is.call(template, group)) {
image = template.icon();
break;
}
}
image.className = "item-preview";
return image;
}
Entity.find = function(pattern) {
var regex = new RegExp(pattern);
return game.entities.filter(function(e) {
return regex.test(e.Type)
})
}
Entity.books = {
$intro: "Именем Императора и Его Синода",
}