-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerup.js
155 lines (135 loc) · 5.34 KB
/
powerup.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class Powerup
{
constructor(powerupNumber, paramPosition)
{
this.id = objectIdCounter++;
this.name = "Powerup " + powerupNumber;
this.position = paramPosition;
this.rotation = 0;
this.mass = 100;
this.rotationSpeed = 0;
this.powerupNumber = powerupNumber;
this.isCollected = false;
this.isAlive = true;
this.initPowerup();
MovablesEngine.addObject(this);
}
initPowerup()
{
switch(this.powerupNumber)
{
case(1):
this.img = GraphicsRooster.getImgByName("powerup_1");
break;
case(2):
this.img = GraphicsRooster.getImgByName("powerup_2");
break;
case(3):
this.img = GraphicsRooster.getImgByName("powerup_3");
break;
}
}
destroy ()
{
//MovablesEngine.removeObject(this);
this.isAlive = false;
}
PowerupEffect()
{
switch(this.powerupNumber)
{
case(1):
{
Protagonist.spaceship.addToShield(100);
break;
}
case(2):
Protagonist.amountOfBullets++;
break;
case(3):
{
Protagonist.spaceship.curHealth += 25;
if(Protagonist.spaceship.curHealth > Protagonist.spaceship.maxHealth)
{
Protagonist.spaceship.curHealth = Protagonist.spaceship.maxHealth;
}
break;
}
}
}
update (timeSinceLastFrame)
{ /* same as for spaceship */
this.updateHitbox();
}
updateHitbox ()
{
this.hitbox = [this.position.x - this.img.width * 1000 / 2 / Viewport.pixelsPerThousand,
this.position.y - this.img.height* 1000 / 2 / Viewport.pixelsPerThousand,
this.img.width * 1000 / Viewport.pixelsPerThousand,
this.img.height * 1000/ Viewport.pixelsPerThousand];
}
paint (ctx, viewportOffset, viewportSize, timeSinceLastFrame)
{
this.update(timeSinceLastFrame);
var tileSource = [0,0,this.img.width,this.img.height];
var tileDest = [Viewport.paintOffset[0] + (this.position.x - viewportOffset.x) * Viewport.pixelsPerThousand / 1000, Viewport.paintOffset[1] + (Viewport.viewportSize.y - this.position.y + viewportOffset.y) * Viewport.pixelsPerThousand / 1000, tileSource[2], tileSource[3]];
var origPoints = [tileDest[0], tileDest[1]];
if(this.rotation != 0) {
tileDest[0] = Math.round(0 - this.img.width / 2);
tileDest[1] = Math.round(0 - this.img.height/ 2);
ctx.save();
ctx.translate(origPoints[0], origPoints[1]);
ctx.rotate(this.rotation);
ctx.drawImage(this.img.src, tileSource[0], tileSource[1], tileSource[2], tileSource[3], tileDest[0], tileDest[1], tileDest[2], tileDest[3]);
ctx.restore();
} else
{
tileDest[0] = Math.round(tileDest[0] - this.img.width / 2);
tileDest[1] = Math.round(tileDest[1] - this.img.height/ 2);
ctx.drawImage(this.img.src, tileSource[0], tileSource[1], tileSource[2], tileSource[3], tileDest[0], tileDest[1], tileDest[2], tileDest[3]);
}
}
hitcheck (otherSpaceship)
{
if (otherSpaceship && Protagonist.spaceship.id == otherSpaceship.id && !this.isCollected && this.hitbox)
{
if (this.hitbox[0] <= (otherSpaceship.hitbox[0] + otherSpaceship.hitbox[2]) &&
(this.hitbox[0] + this.hitbox[2]) >= otherSpaceship.hitbox[0] &&
this.hitbox[1] <= (otherSpaceship.hitbox[1] + otherSpaceship.hitbox[3]) &&
(this.hitbox[1] + this.hitbox[3] >= otherSpaceship.hitbox[1]))
{
/*NEW code */
var hitRect = [0, 0, 0, 0];
hitRect[0] = this.hitbox[0];
if (this.hitbox[0] < otherSpaceship.hitbox[0])
{
hitRect[0] = otherSpaceship.hitbox[0];
}
hitRect[1] = this.hitbox[1];
if (this.hitbox[1] < otherSpaceship.hitbox[1])
{
hitRect[1] = otherSpaceship.hitbox[1];
}
hitRect[2] = this.hitbox[0] + this.hitbox[2];
if (hitRect[2] > (otherSpaceship.hitbox[0] + otherSpaceship.hitbox[2]))
{
hitRect[2] = otherSpaceship.hitbox[0] + otherSpaceship.hitbox[2];
}
hitRect[3] = this.hitbox[1] + this.hitbox[3];
if (hitRect[3] > (otherSpaceship.hitbox[1] + otherSpaceship.hitbox[3]))
{
hitRect[3] = otherSpaceship.hitbox[1] + otherSpaceship.hitbox[3];
}
var markerPosition = [ hitRect[2], hitRect[3] ];
hitRect[2] = hitRect[2] - hitRect[0];
hitRect[3] = hitRect[3] - hitRect[1];
if (hitRect[2] > 0 && hitRect[3] > 0)
{
this.isCollected = true;
this.PowerupEffect();
this.destroy();
}
}
}
}
}