/
room.js
178 lines (152 loc) · 5.96 KB
/
room.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var STD_ROOM_SIZE = 18; /* size of exterior rooms (interior may be of any size) */
/* ======================================================================== */
/* Room containing objects and terrain data.
Room objects are passed as 1st argument in StaticObject's constructor.
*/
var Room = function (width, height) {
this.objects = []; /* list of objects (StaticObject descendants) for layer1 */
this.width = width || STD_ROOM_SIZE; /* size in tiles */
this.height = height || STD_ROOM_SIZE;
this.quest = null; /* a Quest instance, if any */
this.entry_point = null; /* an {x,y} object; used when teleporting into this room
* as initial position for player */
this.is_interior = false; /* if true, player can't walk to neighboring rooms */
this.visited = false; /* is set to true when player enters the roomfor the first time */
};
/* call this to display this room on screen
* @param int scroll_dx: +1, 0 or -1 depending on where the player came from *
* +1 means 'player went right' (we need to scroll left)
* @param int scroll_dy: +1 means 'player went down' (scrolling up)
*/
Room.prototype.enter = function (enterCallback, scroll_dx, scroll_dy) {
this.visited = true;
this.old_layer0 = Game.layer0;
this.old_layer1 = Game.layer1;
this.old_layer2 = Game.layer2;
this.enterCallback = enterCallback;
Game.createLayerRoots();
this.prepareSprites();
if (scroll_dx !== undefined) {
/* transit animation */
Game.player.controls_enabled = false;
var room = this;
Game.positionLayers(room.old_layer0.left + STD_ROOM_SIZE*32*scroll_dx, room.old_layer0.top + STD_ROOM_SIZE*32*scroll_dy);
room.remainingAnimationSteps = VIEWPORT_SIDE;
var delta = (VIEWPORT_SIDE * 32) / room.remainingAnimationSteps;
var shiftLayers = function () {
if (room.remainingAnimationSteps > 0) {
room.old_layer0.setPositionDelta(-scroll_dx * delta, -scroll_dy * delta);
room.old_layer1.setPositionDelta(-scroll_dx * delta, -scroll_dy * delta);
room.old_layer2.setPositionDelta(-scroll_dx * delta, -scroll_dy * delta);
Game.positionLayers(Game.layer0.left - scroll_dx * delta, Game.layer0.top - scroll_dy * delta);
room.remainingAnimationSteps--;
} else {
room.enterFinish();
}
};
this.old_layer1.onDrawCallback = shiftLayers;
} else {
/* finalize operation directly */
this.enterFinish();
}
};
/* After transit animation is completed... */
Room.prototype.enterFinish = function () {
if (Game.currentroom)
Game.currentroom.leave();
if (Game.player) {
this.old_layer1.removeChild(Game.player.sprite);
Game.layer1.addChild(Game.player.sprite);
Game.player.controls_enabled = true;
}
this.old_layer0.remove();
this.old_layer1.remove();
this.old_layer2.remove();
//Game.layer2.remove();
Game.currentroom = this;
if (this.enterCallback)
this.enterCallback();
}
Room.prototype.prepareSprites = function () {
Game.grid = new jstile.Grid(this.width, this.height, 32, 32, jstile.ORTHOGONAL);
for (var i in this.objects) {
this.objects[i].enterRoom();
}
};
/* call this to clear the screen before entering next room */
Room.prototype.leave = function () {
for (var i in this.objects) {
this.objects[i].leaveRoom();
}
Game.currentroom = null;
};
/* is called in ActiveObject's constructor */
Room.prototype.addObj = function (obj) {
this.objects.push(obj);
};
/* is called in ActiveObject's destructor */
Room.prototype.removeObj = function (obj) {
var i = this.objects.indexOf(obj);
this.objects.splice(i, 1);
};
/* returns first object which is located at (x,y) grid coords
and is an obstacle.
*/
Room.prototype.get_obstacles = function (x,y) {
for (var i in this.objects) {
if (this.objects[i].cx == x && this.objects[i].cy == y && this.objects[i].obstacle)
return this.objects[i];
}
return null;
};
/* returns a list of {x, y} points --- empty cells in layer1 in the room */
Room.prototype.get_empty_cells = function () {
var grid = [];
for (var i = 0; i < this.width; i++) {
var row = [];
for (var j = 0; j < this.height; j++)
row.push(0); /* 0 means empty */
grid.push(row);
}
for (var i in this.objects) {
if (this.objects[i].obstacle)
grid[this.objects[i].cx][this.objects[i].cy] = 1; /* occupied */
}
var empty_cells = [];
for (var i = 0; i < this.width; i++) {
for (var j = 0; j < this.height; j++)
if (grid[i][j] == 0)
empty_cells.push({x: i, y: j});
}
return empty_cells;
};
/* ======================================================================== */
var SimpleRoom = function (width, height) {
SimpleRoom.superclass.constructor.apply(this, [width, height]);
};
SimpleRoom.inheritFrom(Room);
SimpleRoom.prototype.prepareSprites = function () {
SimpleRoom.superclass.prepareSprites.apply(this);
this.layer0tiles = [];
this.layer2tiles = [];
for(var y = 0; y < this.height; ++y) {
for(var x = 0; x < this.width; ++x) {
var cell;
var offset = Game.grid.offsets[x][y];
cell = jstile.Sprite.tileFactory(
Math.floor(Math.random() * 6),
offset.left,
offset.top,
x+y
);
this.layer0tiles.push(cell);
}
}
Game.layer0.addChild(this.layer0tiles);
Game.layer2.addChild(this.layer2tiles);
};
SimpleRoom.prototype.leave = function () {
Game.layer0.removeChild(this.layer0tiles);
Game.layer2.removeChild(this.layer2tiles);
SimpleRoom.superclass.leave.apply(this);
};