-
Notifications
You must be signed in to change notification settings - Fork 1
/
airLevel.js
180 lines (158 loc) · 5.55 KB
/
airLevel.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
function generateSteps(level, xMargin, treeHeight) {
var width = level[0].length;
var height = level.length;
var leftX = xMargin;
var rightX = width - xMargin;
var isLeft = true;
var minPlatWidth = ~~(width / 5);
var maxPlatWidth = ~~(width / 4);
var minYDist = treeHeight; // Holds where to generate the next step.
var maxYDist = ~~(1.5 * treeHeight);
var startY = height - 2;
var stepWidth, startX, yStep;
var lastPlaced = 0;
var cloudDistance = 10;
while (startY > 5) { // Add standing steps at regular intervals
// Place platform
stepWidth = ~~(minPlatWidth + Math.random() * (maxPlatWidth - minPlatWidth));
if (isLeft) {
startX = leftX;
} else {
startX = rightX - stepWidth;
}
for (var i = 0; i < stepWidth; i++) {
level[startY][startX + i] = "x";
}
isLeft = !isLeft;
level[startY - 1][startX + 1] = "t";
if ((startY < .75 * height) && lastPlaced > cloudDistance) {
level[startY][~~(width / 2)] = "C";
lastPlaced = 0;
}
nextY = ~~(minYDist + Math.random() * (maxYDist - minYDist));
startY = startY - nextY - 1;
lastPlaced = lastPlaced + nextY + 1;
}
// Set the winning goal character.
level[2][~~(width / 2)] = "g";
level[height - 5][~~(width / 2)] = "@"; // Place the player at the bottom.
firstTime = false;
return level;
}
function generateAirBasicLevel(width, height, xMargin, treeHeight) {
var startX = xMargin;
var endX = width - 2 * xMargin;
var level = [];
for (var y = 0; y < height; y++) {
var columns = [];
for (var x = 0; x < width; x++) {
if ((x > endX) || (x < startX)) { // Mountain left and right
columns.push("x");
} else if (y == height - 1) { // Ground
columns.push("x");
} else {
columns.push(" "); // Everything else is initially empty.
}
}
level.push(columns);
}
level = generateSteps(level, xMargin, treeHeight);
return level;
}
function AirPlayer(pos) {
PlayerPlatformer.call(this, pos);
this.drawLast = true;
this.flyPower = 9000;
this.fly = true;
}
AirPlayer.prototype = Object.create(PlayerPlatformer.prototype);
function AirGem(pos) {
Gem.call(this, pos, "AirGem", "#DCDCDC", "#ECECEC", "air gem.");
}
AirGem.prototype = Object.create(Gem.prototype);
function EvilCloud(pos) {
var speedX = ~~~(5 + Math.random() * 10);
this.speed = new Vector(speedX, 0);
this.pos = pos;
var radius = Game.scale * 2;
var minRadius = Game.scale;
this.r1 = minRadius + Math.random() * (radius - minRadius);
this.r2 = minRadius + Math.random() * (radius - minRadius);
this.size = new Vector(2 * (this.r1 / Game.scale) + 2 * (this.r2 / Game.scale), Math.min(this.r1 / Game.scale, this.r2 / Game.scale));
this.type = "EvilCloud";
}
// Called at every step of the animate.
EvilCloud.prototype.act = function(step, level) {
var newPos = this.pos.plus(this.speed.times(step)); // Calculate newPos
if (!level.collisionWith(newPos, this.size, "obstacle")) { // If no obstacle set newPos
this.pos = newPos
} else {
this.speed = this.speed.times(-1); // Reverse direction if obstacle.
}
};
EvilCloud.prototype.draw = function(cx, x, y) {
cx.save();
drawCloud(cx, x, y, this.r1, this.r2, "grey");
cx.restore();
}
function Tree(pos, character) {
this.pos = pos;
this.size = new Vector(3, 12);
this.horizBranchNos = 1 + getRandomElement([2, 4])
this.vertBranchNos = 2 + ~~(Math.random() * 4);
this.treeColor = "green";
}
Tree.prototype.type = "tree";
Tree.prototype.act = function(step, level) {
};
Tree.prototype.draw = function(cx, x, y) {
cx.save();
//console.log(this.pos.x * Game.scale + " " + this.pos.y * Game.scale + " " + x + " " + y);
drawTree(cx, x, y + (1 * Game.scale), this.horizBranchNos, this.vertBranchNos, this.treeColor);
cx.restore();
}
var airLevelBackgroundChars = {
"x": "wall"
};
var airLevelActorChars = {
"@": AirPlayer,
"t": Tree,
"C": EvilCloud,
"g": AirGem
};
var xMargin = 1;
var treeHeight = 5;
var airLevelMap = generateAirBasicLevel(40, 200, xMargin, treeHeight);
var airLevel = new LevelInfo(LEVEL_TYPE.PLATFORMER, airLevelMap, airLevelBackgroundChars, airLevelActorChars);
airLevel.platformerType = "vertical";
airLevel.generateLevel = function() {
this.level = generateAirBasicLevel(40, 200, xMargin, treeHeight);
}
airLevel.drawBackground = function(backgroundChar, cx, x, y) {
if (backgroundChar == "wall") {
cx.fillStyle = "gold";
cx.fillRect(x, y, Game.scale + 1, Game.scale + 1);
} else if (backgroundChar == "fierceRiver") {
cx.fillStyle = "green";
cx.fillRect(x, y, Game.scale + 1, Game.scale + 1);
}
};
airLevel.playerTouched = function(type, actor, level) {
if (type == "AirGem" && level.status == null) {
level.player.gravity = 0;
level.player.speed.y = -level.player.jumpSpeed;
level.actors = level.actors.filter(function(inDivActor) {
return inDivActor != actor;
});
Game.level = waterLevel;
Game.numberOfGemsCollected++;
Game.gemsCollected["air"] = true;
Game.hud.setGameMessage(actor.winMessage);
return "won";
} else if (type == "EvilCloud") {
reducePlayerHealth(50, level, "Beware of the Evil Cloud.");
}
if (level.player.health <= 0 && level.status == null) {
return "lost"
}
}