-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfireLevel.js
294 lines (259 loc) · 9.93 KB
/
fireLevel.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
function generateFireBasicLevel(width, height) {
var startX = 0;
var startY = ~~(height * 2 / 3);
var level = [];
for (var y = 0; y < height; y++) {
var columns = [];
for (var x = 0; x < width; x++) {
if (y > startY)
columns.push("x");
else
columns.push(" ");
}
level.push(columns);
}
level[startY - 1][1] = "@";
return level;
}
var fireLevelConstants = {
"fireBirdMaxWidth": 11,
"fireBirdMinHeight": 5
}
function generateFireLevelWithObstacles(level) {
var width = level[0].length;
var height = level.length;
var startX = 0;
var startY = ~~(height * 2 / 3);
var initialOffset = 5,
flatRegion = 8,
troughMinDeapth = 4,
troughMinSize = 10;
var isFlat = true;
startX = startX + initialOffset;
var actorList = { "0": "FIREBIRD", "1": "VOLCANO", "2": "TREE" };
while (startX < width - 12) {
var enemySelect = actorList[String(~~(Math.random() * Object.keys(actorList).length))];
var droppingEnemyHolderSize = 5;
if (enemySelect == "FIREBIRD" && isFlat == true) {
var fireBirdHeight = ~~(fireLevelConstants.fireBirdMinHeight + 6 * Math.random());
level[startY - fireBirdHeight][startX + ~~(fireLevelConstants.fireBirdMaxWidth / 2)] = "f";
startX = startX + fireLevelConstants.fireBirdMaxWidth;
isFlat = false;
} else if (enemySelect == "VOLCANO" && isFlat == true) {
var volcanoSize = getRandomElement([13, 17, 21]);
var volcanoDeapth = Math.floor(volcanoSize / 4);
var volcanoHeight = volcanoDeapth;
var margin = volcanoDeapth + volcanoHeight - 1;
for (var y = startY - volcanoHeight + 1; y < (startY + volcanoDeapth); y++) {
for (var x = startX; x < startX + volcanoSize; x++) {
if (x > startX + margin && x < startX + (volcanoSize - margin)) {
level[y][x] = "!";
if (y == startY - volcanoHeight + 1) {
level[y - 2][x] = "v";
}
}
if (x == startX + margin || x == startX + (volcanoSize - margin))
level[y][x] = "n";
}
margin = margin - 1;
}
startX = startX + volcanoSize;
isFlat = false;
} else if (enemySelect == "TREE" && isFlat == true) {
var treeSize = 5;
level[startY][startX + 2] = "t";
startX = startX + treeSize;
isFlat = false;
} else if (isFlat == false) {
startX += flatRegion;
isFlat = true;
}
}
level[startY - 2][width - 3] = "g";
return level;
}
var fireLevelMap = generateFireLevelWithObstacles(generateFireBasicLevel(300, 50));
function FireBolt(pos, character) {
this.pos = pos;
this.size = new Vector(0.5, 1);
this.speed = new Vector(0, 20);
}
FireBolt.prototype.type = "fireBolt";
FireBolt.prototype.act = function(step, level) {
var newPos = this.pos.plus(this.speed.times(step));
var actor = this;
if (!level.collisionWith(newPos, this.size, "obstacle")) { // If no obstacle set newPos
this.pos = newPos
} else {
level.actors = level.actors.filter(function(inDivActor) {
return inDivActor != actor;
});
}
};
FireBolt.prototype.draw = function(cx, x, y) {
cx.save();
cx.fillStyle = "yellow";
cx.fillRect(x, y, this.size.x * Game.scale, this.size.y * Game.scale + 4);
cx.fillStyle = "red";
cx.fillRect(x + 2, y + 2, this.size.x * Game.scale - 4, this.size.y * Game.scale - 4);
cx.restore();
}
function FireBird(pos, character) {
this.pos = pos;
this.size = new Vector(3, 1);
this.speed = Math.random() > 0.5 ? new Vector(2, 0) : new Vector(-2, 0);
this.origSpeed = new Vector(2, 0);
this.maxLeft = this.pos.x - ~~(fireLevelConstants.fireBirdMaxWidth / 2);
this.maxRight = this.pos.x + ~~(fireLevelConstants.fireBirdMaxWidth / 2);
}
FireBird.prototype.type = "fireBird";
FireBird.prototype.act = function(step, level) {
var newPos = this.pos.plus(this.speed.times(step));
if (newPos.x > this.maxRight)
this.speed.x = -1 * this.origSpeed.x;
else if (newPos.x < this.maxLeft)
this.speed.x = this.origSpeed.x;
this.pos = newPos;
if (~~(newPos.x) % 3 == 0)
level.actors.push(new FireBolt(this.pos.plus(new Vector(0, 1)), "b"));
};
FireBird.prototype.draw = function(cx, x, y) {
cx.save();
cx.fillStyle = "red";
cx.fillRect(x, y, this.size.x * Game.scale, this.size.y * Game.scale);
cx.fillStyle = "yellow";
cx.fillRect(x + 4, y + 4, this.size.x * Game.scale - 8, this.size.y * Game.scale - 8);
cx.restore();
}
function FireTree(pos, character) {
this.pos = pos;
this.size = new Vector(3, 12);
this.horizBranchNos = 3 + getRandomElement([2, 4])
this.vertBranchNos = 4 + ~~(Math.random() * 4);
this.treeColor = "green";
this.addFruits = false;
}
FireTree.prototype.type = "tree";
FireTree.prototype.act = function(step, level) {
if (!this.addFruits) {
var numberOfBerries = 4 + ~~(Math.random() * 4);
for (var i = 0; i < numberOfBerries; i++) {
var xOffset = getRandomElement([-(this.horizBranchNos - 1), -1, 0, 1, 2, this.horizBranchNos + 1]);
var yOffset = -1 * (7 + ~~(Math.random() * this.vertBranchNos));
var berry = new Berry(new Vector(this.pos.x + xOffset, this.pos.y + yOffset));
berry.drawLast = true;
level.actors.push(berry);
}
this.addFruits = true;
}
};
FireTree.prototype.draw = function(cx, x, y) {
cx.save();
drawTree(cx, x, y + (1 * Game.scale), this.horizBranchNos, this.vertBranchNos, this.treeColor);
cx.restore();
}
function VolcanoLava(pos, character) {
this.pos = pos;
this.origPos = pos;
this.size = new Vector(1, 1);
this.getSpeed();
this.gravity = 0.005;
this.gravity = 30;
}
VolcanoLava.prototype.getSpeed = function() {
var speedX = Math.random() > 0.5 ? ~~(Math.random() * 10) : -1 * ~~(Math.random() * 10);
var speedY = -15 + -1 * ~~(Math.random() * 10);
this.speed = new Vector(speedX, speedY);
}
VolcanoLava.prototype.type = "volcanoLava";
VolcanoLava.prototype.act = function(step, level) {
this.speed.y += step * this.gravity;
var motion = new Vector(this.speed.x * step, this.speed.y * step);
var newPos = this.pos.plus(motion);
var actor = this;
if (!level.collisionWith(newPos, this.size, "obstacle")) { // If no obstacle set newPos
this.pos = newPos
} else {
this.pos = this.origPos;
this.getSpeed();
}
};
VolcanoLava.prototype.draw = function(cx, x, y) {
cx.save();
cx.fillStyle = "yellow";
cx.fillRect(x, y, this.size.x * Game.scale, this.size.y * Game.scale);
cx.fillStyle = "red";
cx.fillRect(x + 2, y + 2, this.size.x * Game.scale - 4, this.size.y * Game.scale - 4);
cx.restore();
}
function FirePlayer(pos) {
PlayerPlatformer.call(this, pos);
this.drawLast = true;
}
FirePlayer.prototype = Object.create(PlayerPlatformer.prototype);
function FireStoneGem(pos) {
Gem.call(this, pos, "FireStoneGem", "#FE7777", "#FE2222", "fire gem.");
}
FireStoneGem.prototype = Object.create(Gem.prototype);
var fireLevelBackgroundChars = {
"x": "wall",
"!": "lava",
"n": "volcanoWall"
};
var fireLevelActorChars = {
"@": FirePlayer,
"v": VolcanoLava,
"f": FireBird,
"b": FireBolt,
"t": FireTree,
"g": FireStoneGem
};
var fireLevel = new LevelInfo(LEVEL_TYPE.PLATFORMER, fireLevelMap, fireLevelBackgroundChars, fireLevelActorChars);
fireLevel.platformerType = "horizontal";
fireLevel.generateLevel = function() {
this.level = generateFireLevelWithObstacles(generateFireBasicLevel(300, 50));
}
fireLevel.drawBackground = function(backgroundChar, cx, x, y) {
if (backgroundChar == "wall") {
cx.fillStyle = "rgba(170, 170, 50, 255)";
cx.fillRect(x, y, Game.scale + 1, Game.scale + 1);
} else if (backgroundChar == "lava") {
cx.fillStyle = "red";
cx.fillRect(x, y, Game.scale + 1, Game.scale + 1);
} else if (backgroundChar == "volcanoWall") {
cx.fillStyle = "brown";
cx.fillRect(x, y, Game.scale + 1, Game.scale + 1);
}
};
fireLevel.playerTouched = function(type, actor, level) {
if (type == "lava" && level.status == null) {
Game.hud.setGameMessage("Lava killed you.");
return "lost";
} else if (type == "fireBolt" && level.status == null) {
reducePlayerHealth(50, level, "Beware of the Fire Bolt.");
} else if (type == "volcanoLava" && level.status == null) {
reducePlayerHealth(50, level, "Beware of the Volcanic Lava.");
} else if (type == "berry") { //Filter the berry from actor list as it is picked
level.actors = level.actors.filter(function(inDivActor) {
return inDivActor != actor;
});
level.player.health += 10;
Game.hud.setGameMessage("Gained health");
} else if (type == "FireStoneGem" && level.status == null) {
level.actors = level.actors.filter(function(inDivActor) {
return inDivActor != actor;
});
Game.level = waterLevel;
Game.numberOfGemsCollected++;
Game.gemsCollected["fire"] = true;
Game.hud.setGameMessage(actor.winMessage);
return "won";
} else if (type == "tree") {
level.player.gravity = 20;
} else {
level.player.gravity = level.player.gravityConst;
}
if (level.player.health <= 0 && level.status == null) {
return "lost"
}
}