-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
72 lines (69 loc) · 2.07 KB
/
model.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
window.Model = {
create: function() {
// TODO: make the structure private vars and have methods on the return object to deal with them.
var entities = [],
camera = null,
view = {
width: 1600,
height: 900
},
roomNum = 0;
return {
getEntities: function() {
return entities;
},
getCamera: function() {
return camera;
},
getView: function() {
return view;
},
getRoomNum: function() {
return roomNum;
},
loadRoom: function(destinationRoomNum) {
if(destinationRoomNum >= window.Rooms.length) {
console.log("error: room " + destinationRoomNum + " not found");
return;
}
roomNum = destinationRoomNum;
entities = [];
window.Rooms[roomNum].forEach(function(entity) {
// copy the entities so we don't change the contents of window.Levels
// this makes the level 'reset' next time the player comes back to it.
entities.push(Entity.clone(entity));
});
camera = entities.filter(function(entity) {
return entity.camera;
})[0];
},
restartLevel: function() {
this.loadRoom(this.getRoomNum());
},
getExtraEntities: function() {
var allEntities;
allEntities = [].concat(entities);
entities.filter(function(entity) {
return Entity.getLeft(entity) < camera.x + view.width / 2 && Entity.getRight(entity) > camera.x - view.width / 2; // on screen any amount
}).forEach(function(entity) {
var newEntity,
direction,
distance;
newEntity = Entity.clone(entity);
direction = Entity.getLeft(newEntity) < camera.x - view.width / 2 ? -1 : 1;
distance = Math.max(0, Math.max(-(Entity.getLeft(newEntity) - camera.x + view.width / 2), Entity.getRight(newEntity) - camera.x - view.width / 2));
newEntity.width -= distance;
newEntity.x -= direction * distance / 2;
// left copy
newEntity.x -= view.width;
allEntities.push(newEntity);
// right copy
newEntity = Entity.clone(newEntity);
newEntity.x += 2 * view.width;
allEntities.push(newEntity);
});
return allEntities;
}
}
}
};