-
Notifications
You must be signed in to change notification settings - Fork 1
/
riverLevel.js
246 lines (212 loc) · 7.24 KB
/
riverLevel.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
function generateRiverBasicLevel(width, height, xMargin) {
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)) { // River bank.
columns.push("x");
} else if (y == height - 1) { // River Bank
columns.push("x");
} else {
columns.push(" "); // Everything else is initially empty.
}
}
level.push(columns);
}
return level;
}
function generateRiverLevelWithObstacles(level) {
var width = level[0].length;
var height = level.length;
var xMargin = 1;
var endX = width - xMargin;
var startY = height - 2;
var startX;
var firstTime = true;
while (startY > 4) { // Add standing pfs at regular intervals
// Place platform
startX = ~~(width / 2 + Math.random() * (endX - width / 2 - 10)); // bw center and right wall
level[startY][startX] = "n";
startX = ~~(xMargin + Math.random() * (width / 2 - 10)); //bw left all and center
level[startY][startX] = "n";
if (firstTime) {
level[startY][startX + 1] = "@"; // Place the player at the bottom most board.
firstTime = false;
}
startY = startY - 2;
// Place a moving log on this row.
if (startY < 2) {
break;
}
startX = ~~(xMargin + Math.random() * (endX - xMargin - 10));
level[startY][startX] = "|";
startY = startY - 2;
}
level[1][~~(width / 2) - 1] = "n";
level[2][~~(width / 2) - 1] = "n";
level[3][~~(width / 2) - 1] = "n";
level[4][~~(width / 2) - 1] = "|";
// Set the winning goal character.
level[1][~~(width / 2) - 1] = "g";
return level;
}
var xMargin = 1;
var riverLevelMap = generateRiverLevelWithObstacles(generateRiverBasicLevel(40, 90, xMargin));
function RiverPlayer(pos) {
PlayerNonPlatformer.call(this, pos);
this.health = 100;
this.drawLast = true;
}
RiverPlayer.prototype = Object.create(PlayerNonPlatformer.prototype);
RiverPlayer.prototype.move = function(step, level, keys) {
if (!this.inCollision) {
this.speed.x = 0;
this.speed.y = 0;
}
var PLAYER_NON_PLATFORM_SPEED = 4;
if (keys.left) {
if (this.inCollision) {
this.speed.x += -1 * PLAYER_NON_PLATFORM_SPEED;
} else {
this.speed.x = -1 * PLAYER_NON_PLATFORM_SPEED;
}
this.facingRight = false;
}
if (keys.right) {
if (this.inCollision) {
this.speed.x += PLAYER_NON_PLATFORM_SPEED;
} else {
this.speed.x = PLAYER_NON_PLATFORM_SPEED;
}
this.facingRight = true;
}
if (keys.up)
this.speed.y = -1 * PLAYER_NON_PLATFORM_SPEED;
if (keys.down)
this.speed.y = PLAYER_NON_PLATFORM_SPEED;
var motion = new Vector(this.speed.x * step, this.speed.y * step);
var newPos = this.pos.plus(motion);
this.checkCollision(newPos, level);
};
RiverPlayer.prototype.checkCollision = function(newPos, level) {
var obstacle = level.collisionWith(newPos, this.size, "obstacle")
if (!obstacle) {
this.newPos = newPos;
} else {
this.newPos = this.pos;
}
}
RiverPlayer.prototype.act = function(step, level, keys) {
if (!Game.inInteraction) {
this.move(step, level, keys);
}
// Check for collisions with dynamic layer.
var collidedObject = level.actorAt(this);
if (collidedObject) {
if (collidedObject.speed) {
this.speed.x = collidedObject.speed.x;
this.speed.y = collidedObject.speed.y;
}
this.pos = this.newPos;
this.inCollision = true;
if (collidedObject.type == "RiverGem") {
level.playerTouched("RiverGem", collidedObject);
}
} else {
this.inCollision = false;
level.playerTouched("fierce river", null);
}
if (level.status == "lost") { // Losing animation.
this.pos.y += step;
this.size.y -= step;
}
if (level.player.playerHitTimer > 0) {
level.player.playerHitTimer--;
}
if (level.player.skipDialogTimer > 0) {
level.player.skipDialogTimer--;
}
};
function RiverGem(pos) {
Gem.call(this, pos, "RiverGem", "#7777FE", "#2222FE", "river gem.");
}
RiverGem.prototype = Object.create(Gem.prototype);
function RiverObject(pos, type, color, size) {
this.pos = pos;
this.type = type;
this.size = size;
this.color = color;
}
RiverObject.prototype.draw = function(cx, x, y) {
cx.save();
cx.fillStyle = this.color;
cx.fillRect(x, y, this.size.x * Game.scale, this.size.y * Game.scale);
cx.restore();
}
RiverObject.prototype.act = function(step, level) {}
function MovingLog(pos) {
var xSize = ~~(2 + Math.random() * 5); // Size between 2 to 7.
var ySize = 1;
var size = new Vector(xSize, ySize);
var speedX = ~~~(7 + Math.random() * 4); // Speed bw 2 to 7.
this.speed = new Vector(speedX, 0);
RiverObject.call(this, pos, "MovingLog", "brown", size);
}
MovingLog.prototype = Object.create(RiverObject.prototype);
// Called at every step of the animate.
MovingLog.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.
}
};
function StandingPlatform(pos) {
var xSize = ~~(3 + Math.random() * 7); // Size between 3 to 10.
var ySize = 1;
var size = new Vector(xSize, ySize);
RiverObject.call(this, pos, "StandingPlatform", "grey", size);
this.speed = new Vector(0, 0);
}
StandingPlatform.prototype = Object.create(RiverObject.prototype);
var riverLevelBackgroundChars = {
"x": "wall"
};
var riverLevelActorChars = {
"@": RiverPlayer,
"n": StandingPlatform,
"|": MovingLog,
"g": RiverGem
};
var riverLevel = new LevelInfo(LEVEL_TYPE.PLATFORMER, riverLevelMap, riverLevelBackgroundChars, riverLevelActorChars);
riverLevel.platformerType = "vertical";
riverLevel.generateLevel = function() {
this.level = generateRiverLevelWithObstacles(generateRiverBasicLevel(40, 90, xMargin));
}
riverLevel.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);
}
};
riverLevel.playerTouched = function(type, actor, level) {
if (type == "fierce river" && level.status == null) {
Game.hud.setGameMessage("Drowned in the fierce river.");
return "lost";
} else if (type == "RiverGem" && level.status == null) {
level.actors = level.actors.filter(function(inDivActor) {
return inDivActor != actor;
});
Game.level = waterLevel;
Game.gemsCollected["water"] = true;
Game.numberOfGemsCollected++;
Game.hud.setGameMessage(actor.winMessage);
return "won";
}
}