summaryrefslogtreecommitdiff
path: root/centipede/js/game/objects/mushroom.js
blob: b1f4787b6de05c52635d9eff47bf85c7da07f821 (plain)
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
game.Mushroom = (spec) => {
  spec.state = spec.state ?? 4;
  const {mushX, mushY} = spec;
  const objectSpec = {...spec};
  objectSpec.x = mushX * objectSpec.width;
  objectSpec.y = mushY * objectSpec.height;
  const object = {...spec, ...game.Object(objectSpec)};
  object.onBulletCollision = (bullet) => {
    if (bullet.alive) {
      object.state--;
      game.score += 5;
      game.sounds.mushroom_hit.load();
      game.sounds.mushroom_hit.play();
    }
    bullet.alive = false;
  }; 
  object.draw = (elapsedTime) => {
    if (object.state) {
      object.sprite = object.poisoned ? game.sprites.poisonMushrooms[object.state-1] : game.sprites.regularMushrooms[object.state-1];
      object.sprite.draw(elapsedTime, object);
    }
  }
  return object;
};

game.Mushroom.toMushCoords = (coords) => {
  return {mushX: Math.ceil(coords.x / game.mushroomDims.width), mushY: Math.ceil(coords.y / game.mushroomDims.height)};
}

game.Mushroom.toGameCoords = (mushCoords) => {
  return {x: mushCoords.mushX * game.mushroomDims.width, y: mushCoords.mushY * game.mushroomDims.height};
}

game.Mushroom.generateMushrooms = (mushroomSpec) => {
  const mushPositions = new Set();
  for (let i = 0; i < Math.max(Math.random(), 0.05) * game.height / mushroomSpec.height * game.width / mushroomSpec.width * game.level * 0.5; i++) {
    mushPositions.add(JSON.stringify([Math.floor(Math.random() * game.width / mushroomSpec.width), Math.max(1, Math.floor(Math.random() * (game.height / mushroomSpec.height - 3)))]));
  }
  return Array.from(mushPositions).map((pos) => {
    const [mushX, mushY] = JSON.parse(pos);
    return game.Mushroom({...mushroomSpec, mushX, mushY});
  });
}