all repos — katbugjs @ main

side-scrolling infinite arcade game in Javascript with HTML5 Canvas

Pickup.js (raw)

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
window.stuff = [{},{},{},{},{},{},{},{},{},{}]

function Pickup()
{
  var pType = Math.floor(Math.random()*10);
  this.y = 0 + Math.floor(Math.random()*148);
  this.x = 320 + Math.floor(Math.random()*120);
  this.vX = -4 - Math.floor(Math.random()*catbug.maxHP);
  this.vY = 0;

  switch (pType)
  {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
      this.sprite = renderer.gfx.peas;
      this.points = 1;
      break;
    case 5:
    case 6:
    case 7:
      this.sprite = renderer.gfx.pbs
      this.points = 2;
      break;
    case 8:
    case 9:
      this.sprite = renderer.gfx.taco;
      this.points = 3;
      break;
  }
}

function managePickups()
{
  var genSpeed = 0;
  var i;
  if (gameState.points > 10)
  {
    genSpeed = Math.floor(Math.random()*gameState.points);
  }
  else genSpeed = Math.floor(Math.random()*10);
  for (i = 0; i < 10; i++)
  {
    if (stuff[i] != null)
    {
      movePickup(stuff[i]);
      if (stuff[i].x < -32)
      {
        stuff[i] = null;
        catbug.HP--;
      }
      else
      {
        box = {
          x: stuff[i].x,
          y: stuff[i].y,
          w: 36,
          h: 36
        }
        
        if (catbug.isInRect(box))
        {
        getPickup(stuff[i]);
        stuff[i] = null;
        }
      }
    }
    else
    {
      if (( gameState.points >= 40 && genSpeed*genSpeed >= gameState.points*4) ||
        ( gameState.points <= 40 && genSpeed*genSpeed >= gameState.threshold*2))
      {
        var now = new Date();
        if (now - gameState.ticker >= 1000)
        {
          stuff[i] = new Pickup();
          gameState.ticker = now;
        }
      }
    }  
  }
}

function movePickup(self)
{
  self.x += self.vX;
}

function getPickup(self)
{
  gameState.points += self.points;
  if (gameState.points >= gameState.threshold)
  {
    gameState.threshold *= 2;
    catbug.maxHP++;
    catbug.HP = catbug.maxHP;
  }
  
}