forked from netology-code/js-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
327 lines (269 loc) · 6.64 KB
/
game.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
'use strict';
class Vector {
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
plus(vector) {
if(!(vector instanceof Vector)) {
throw new Error('Можно прибавлять к вектору только вектор типа Vector');
}
return new Vector(this.x + vector.x, this.y + vector.y);
}
times(multiplier) {
return new Vector(multiplier * this.x, multiplier * this.y);
}
}
class Actor {
constructor(pos = new Vector(0, 0), size = new Vector(1, 1), speed = new Vector(0, 0)) {
if (!(pos instanceof Vector) || !(size instanceof Vector) || !(speed instanceof Vector)) {
throw new Error('Параметры должны быть векторами типа Vector');
}
this.pos = pos;
this.size = size;
this.speed = speed;
}
act() {
}
get left() {
return this.pos.x;
}
get top() {
return this.pos.y;
}
get right() {
return this.pos.x + this.size.x;
}
get bottom() {
return this.pos.y + this.size.y;
}
get type() {
return 'actor';
}
isIntersect(obj) {
if (typeof(obj) === 'undefined' || !(obj instanceof Actor)) {
throw new Error('Аргумент должен быть типа Actor');
}
if (obj === this) {
return false;
}
if (this.left >= obj.right || this.right <= obj.left || this.bottom <= obj.top || this.top >= obj.bottom) {
return false;
} else {
return true;
}
}
}
class Level {
constructor(grid, actors = []) {
this.grid = grid;
this.actors = actors;
this.player = this.actors.find(function(actor) {
return actor.type === 'player';
});
if (typeof(grid) !== 'undefined') {
this.height = grid.length;
this.width = Math.max(...grid.map(function(arr) {
return arr.length;
}));
} else {
this.height = 0;
this.width = 0;
}
this.status = null;
this.finishDelay = 1;
}
isFinished() {
if (this.status !== null && this.finishDelay < 0) {
return true;
} else {
return false;
}
}
actorAt(obj) {
if (typeof(obj) === 'undefined' || !(obj instanceof Actor)) {
throw new Error('Аргумент должен быть типа Actor');
}
return this.actors.find(function(actor) {
return actor.isIntersect(obj);
})
}
obstacleAt(pos, size) {
if (!(pos instanceof Vector) || !(size instanceof Vector)) {
throw new Error('Аргументы должны быть типа Vector');
}
let actor = new Actor(pos, size);
if (actor.bottom >= this.height) {
return 'lava';
}
if (actor.left < 0 || actor.top < 0 || actor.right >= this.width) {
return 'wall';
}
if (typeof(this.grid) === 'undefined') {
return;
}
for (let y = Math.floor(actor.top); y < Math.ceil(actor.bottom); y++) {
for (let x = Math.floor(actor.left); x < Math.ceil(actor.right); x++) {
if (typeof(this.grid[x][y] !== 'undefined')) {
return (this.grid)[x][y];
}
}
}
}
removeActor(actor) {
this.actors = this.actors.filter(function(obj) {
return obj !== actor;
});
}
noMoreActors(type) {
let actorsOfType = this.actors.filter(function(actor) {
return actor.type === type;
});
return actorsOfType.length === 0;
}
playerTouched(obstacleType, actor) {
if (this.status !== null) {
return;
}
if (obstacleType === 'lava' || obstacleType === 'fireball') {
this.status = 'lost';
return;
}
if (obstacleType === 'coin' && actor && actor.type === 'coin') {
this.removeActor(actor);
if (this.noMoreActors('coin')) {
this.status = 'won';
}
}
}
}
class LevelParser {
constructor(dict = {}) {
this.dict = dict;
}
actorFromSymbol(symbol) {
if (typeof(symbol) === 'undefined') {
return undefined;
}
return this.dict[symbol];
}
obstacleFromSymbol(symbol) {
switch (symbol) {
case 'x':
return 'wall';
case '!':
return 'lava';
}
}
createGrid(strings) {
return strings.map(str => {
return str.split('').map(char => this.obstacleFromSymbol(char));
});
}
createActors(strings) {
let actors = [];
for (let x in strings) {
for (let y in strings[x]) {
let symbol = strings[x][y];
let construc = this.dict[symbol];
if (typeof(construc) !== 'function') {
continue;
}
let obj = new construc(new Vector(parseInt(y), parseInt(x)));
if (obj instanceof Actor) {
actors.push(obj);
}
}
}
return actors;
}
parse(strings) {
let grid = this.createGrid(strings);
let actors = this.createActors(strings);
return new Level(grid, actors);
}
}
class Fireball extends Actor {
constructor(pos, speed) {
super(pos, undefined, speed);
}
get type() {
return 'fireball';
}
getNextPosition(time = 1) {
return this.pos.plus(this.speed.times(time));
}
handleObstacle() {
this.speed = this.speed.times(-1);
}
act(time, level) {
let nextPosition = this.getNextPosition(time);
if (level.obstacleAt(nextPosition, this.size)) {
this.handleObstacle();
} else {
this.pos = nextPosition;
}
}
}
class HorizontalFireball extends Fireball {
constructor(pos) {
let speed = new Vector(2, 0);
super(pos, speed);
}
}
class VerticalFireball extends Fireball {
constructor(pos) {
let speed = new Vector(0, 2);
super(pos, speed);
}
}
class FireRain extends Fireball {
constructor(pos) {
let speed = new Vector(0, 3);
super(pos, speed);
this.startPos = pos;
}
handleObstacle() {
this.pos = this.startPos;
}
}
class Coin extends Actor {
constructor(pos) {
let size = new Vector(0.6, 0.6);
let delta = new Vector(0.2, 0.1);
super(pos, size);
this.pos = this.pos.plus(delta);
this.startPos = new Vector(this.pos.x, this.pos.y);
this.springSpeed = 8;
this.springDist = 0.07;
this.spring = 2 * Math.PI * Math.random();
}
get type() {
return 'coin';
}
updateSpring(time = 1) {
this.spring += this.springSpeed * time;
}
getSpringVector() {
let y = Math.sin(this.spring) * this.springDist;
return new Vector(0, y);
}
getNextPosition(time = 1) {
this.updateSpring(time);
return this.startPos.plus(this.getSpringVector());
}
act(time) {
this.pos = this.getNextPosition(time);
}
}
class Player extends Actor {
constructor(pos) {
let size = new Vector(0.8, 1.5);
let delta = new Vector(0, -0.5);
super(pos, size);
this.pos = this.pos.plus(delta);
}
get type() {
return 'player';
}
}