-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerUpBox.java
More file actions
128 lines (116 loc) · 2.31 KB
/
PowerUpBox.java
File metadata and controls
128 lines (116 loc) · 2.31 KB
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
/**
* The visual representation of the power up
* @author elan
*
*/
public class PowerUpBox extends Entity
{
/**
* Constructor for PowerUpBox
* @param enemy creates the power up box at the same place as this enemy
*/
public PowerUpBox(Enemy enemy)
{
super(enemy.getX(), enemy.getY(), 0, Controller.powerUpBoxes.size());
}
/**
* updates the powerUpBox
*/
public void update()
{
updatePos();
checkCollision();
if(dead)
{
try
{
Controller.powerUpBoxes.remove(index);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
/**
* Updates the powerUpBox's position
*/
private void updatePos()
{
moveDown(States.enemySpeed * 2);
}
/**
* draws the popwerUpBox on the screen
*/
public void render()
{
super.render("powerUpBox.png");
}
/**
* checks the things the powerUpBox could come in contact with
*/
private void checkCollision()
{
for(int j = 0; j < Controller.bullets.size(); j++)
{
if(inRadius(Controller.bullets.get(j), Controller.powerUpBoxes.get(index)))
{
collideBulletAndPowerUpBox(j, index);
break;
}
}
if(inRadius(Controller.player, Controller.powerUpBoxes.get(index)))
{
collidePlayerAndPowerUpBox(index);
}
}
/**
* Collides the PowerUpBox with a Bullet,
* destroying both and activating a power Up
* @param bulletIndex
* @param powerUpBoxIndex
*/
private void collideBulletAndPowerUpBox(int bulletIndex, int powerUpBoxIndex)
{
if(Controller.powerUp == null)
Controller.powerUp = new PowerUp();
else
Controller.powerUp.activate();
dead = true;
try
{
Controller.bullets.remove(bulletIndex);
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* Collides the PowerUpBox with a shooter,
* destroying the powerUpBox and activating a power Up
* @param powerUpBoxIndex
*/
private void collidePlayerAndPowerUpBox(int powerUpBoxIndex)
{
if(Controller.powerUp == null)
Controller.powerUp = new PowerUp();
else
Controller.powerUp.activate();
dead = true;
}
/**
* @return true if the PowerUpBox can move down,
* if it cant it moves it back onto the screen
*/
protected boolean canMoveDown()
{
if(yPos <= States.SHOOTER_SIZE/(double)2)
{
if(yPos < States.SHOOTER_SIZE/(double)2)
yPos = States.SHOOTER_SIZE/(double)2;
return false;
}
return true;
}
}