diff options
author | Lizzy Hunt <lizzy.hunt@usu.edu> | 2024-01-12 19:13:13 -0700 |
---|---|---|
committer | Lizzy Hunt <lizzy.hunt@usu.edu> | 2024-01-12 19:13:13 -0700 |
commit | 07670ef8afb5a273267ea7149d5f7eef02fdf66b (patch) | |
tree | 2d0c8e64936c7fa2588786f4af199abf1bb48a60 /centipede/js/game/objects/spider.js | |
parent | 3ac982dfa653f0eb7fbceeb1678a3cae93b512f4 (diff) | |
download | simponic.xyz-07670ef8afb5a273267ea7149d5f7eef02fdf66b.tar.gz simponic.xyz-07670ef8afb5a273267ea7149d5f7eef02fdf66b.zip |
add subprojects
Diffstat (limited to 'centipede/js/game/objects/spider.js')
-rw-r--r-- | centipede/js/game/objects/spider.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/centipede/js/game/objects/spider.js b/centipede/js/game/objects/spider.js new file mode 100644 index 0000000..732c0a3 --- /dev/null +++ b/centipede/js/game/objects/spider.js @@ -0,0 +1,45 @@ +game.Spider = (spec) => { + const object = game.Object(spec); + + const parentUpdate = object.update; + + object.randomizeVel = () => { + object.dx = Math.min(Math.random(), 0.25 + 0.05*game.level) * (Math.random() > 0.5 ? 1 : -1); + object.dy = Math.min(Math.random(), 0.25 + 0.05*game.level) * (Math.random() > 0.5 ? 1 : -1); + } + + object.update = (elapsedTime) => { + if (Math.random() < 0.01*game.level) { + object.randomizeVel(); + } + if (object.x < 0 || object.x > game.width - object.width) { + object.dx = -object.dx; + } + if (object.y < 0 || object.y > game.height - object.height) { + object.dy = -object.dy; + } + object.x = Math.max(0, Math.min(game.width - object.width, object.x)); + object.y = Math.max(0, Math.min(game.height - object.height, object.y)); + parentUpdate(elapsedTime); + }; + + object.explode = () => { + game.explosions.push(game.Explosion({x: object.x, y: object.y, width: object.width, height: object.height, sprite: game.sprites.explosionBig})); + game.sounds.enemy_hit.load(); + game.sounds.enemy_hit.play(); + } + + object.onBulletCollision = (bullet) => { + game.score += 150; + object.alive = false; + object.explode(); + } + + object.onPlayerCollision = (player) => { + object.alive = false; + player.kill(); + object.explode(); + } + + return object; +}
\ No newline at end of file |