-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.3. A monster.html
63 lines (55 loc) · 1.84 KB
/
16.3. A monster.html
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
<link rel="stylesheet" href="css/game.css">
<style>.monster { background: purple }</style>
<body>
<script>
// Complete the constructor, update, and collide methods
class Monster {
constructor(pos, speed) {
this.pos = pos;
this.speed = speed;
}
get type() { return "monster"; }
static create(pos) {
return new Monster(pos.plus(new Vec(0, -1)), new Vec(3, 0));
}
update(time, state) {
let newPos = this.pos.plus(this.speed.times(time));
if (!state.level.touches(newPos, this.size, "wall")) {
return new Monster(newPos, this.speed);
} else {
return new Monster(this.pos, this.speed.times(-1));
}
}
collide(state) {
let player = state.player;
function jumpedOn(player, monster) {
// a tolerance of +/- 0.05 would be too small => false
return player.pos.y + player.size.y > monster.pos.y - 0.1 &&
player.pos.y + player.size.y < monster.pos.y + 0.1
}
if (jumpedOn(player, this)) {
let newActors = state.actors.filter(actor => actor != this);
return new State(state.level, newActors, state.status);
} else {
return new State(state.level, state.actors, "lost");
}
}
}
Monster.prototype.size = new Vec(1.2, 2);
levelChars["M"] = Monster;
runLevel(new Level(`
..................................
.################################.
.#..............................#.
.#..............................#.
.#..............................#.
.#...........................o..#.
.#..@...........................#.
.##########..............########.
..........#..o..o..o..o..#........
..........#...........M..#........
..........################........
..................................
`), DOMDisplay);
</script>
</body>