Created
May 22, 2022 23:37
-
-
Save yitonghe00/0cf4c7e3807f493b32bc273d8d4bcc58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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(2, 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) { | |
const playerBottom = state.player.pos.y + state.player.size.y; | |
const monsterTop = this.pos.y; | |
if (playerBottom - monsterTop < 0.5) { | |
// Having overlapping with a player: check if it's the top | |
return new State(state.level, state.actors.filter(a => a != this), state.status); | |
} | |
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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment