-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullet.java
More file actions
154 lines (136 loc) · 3.28 KB
/
Bullet.java
File metadata and controls
154 lines (136 loc) · 3.28 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
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
/**
* This Class can be used to create bullet objects
* @author elan
*
*/
public class Bullet extends Entity
{
/**
* phasable is final and private as it is determined when the object is created and never altered
* and should not be seen by other classes
*/
private final boolean phasable = determinePhasability();
/**
* Cunstructor for the Bullet object
* @param entity Bullet will be created with same x, y, and angle as this entity
* @param index Give the Bullet a unique index to identify it
*/
public Bullet(Entity entity, int index)
{
super(entity.getX(), entity.getY(), entity.getAngle(), index);
}
/**
* Cunstructor for the Bullet object
* @param entity Bullet will be created with same x, y, and angle as this entity
* @param index Give the Bullet a unique index to identify it
* @param angleDeviation this angle will be subtracted from the entity's angle and the bullet will be created with the resulting angle
*/
public Bullet(Entity entity, int index, double angleDeviation)
{
super(entity.getX(), entity.getY(), entity.getAngle() - angleDeviation, index);
}
/**
* Does all nessisary updates for the Bullet object
*/
public void update()
{
updatePos();
checkBounds();
}
/**
* updates the Bullets x and y co-ordinates
*/
protected void updatePos()
{
moveLeft(States.BULLET_SPEED*Math.cos(Math.toRadians(90 - angle)));
moveUp(States.BULLET_SPEED*Math.sin(Math.toRadians(90 - angle)));
}
/**
* draws the Bullet on the screen
*/
public void render()
{
String type = "";
if(States.playerDamage > 23)
type = "bullet4.png";
else if(States.playerDamage > 18)
type = "bullet3.png";
else if(States.playerDamage > 13)
type = "bullet2.png";
else
type = "bullet1.png";
StdDraw.picture(xPos, yPos, type, States.BULLET_SIZE, States.BULLET_SIZE, angle);
}
/**
* checks if the bullet is still on the screen, if it is not it is destroyed
*/
private void checkBounds()
{
if(!(canMoveLeft() && canMoveRight() && canMoveUp() && canMoveDown()))
{
try
{
Controller.bullets.remove(index);
}
catch(IndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
}
/**
* Returns true if Bullet can move left
*/
protected boolean canMoveLeft()
{
if(xPos <= 0)
return false;
return true;
}
/**
* Returns true if Bullet can move right
*/
protected boolean canMoveRight()
{
if(xPos >= States.WINDOW_RESOLUTION)
return false;
return true;
}
/**
* Returns true if Bullet can move down
*/
protected boolean canMoveDown()
{
if(yPos <= 0)
return false;
return true;
}
/**
* Returns how much damage this bullet does
* @return damage of the bullet
*/
public int getDamage()
{
return States.playerDamage;
}
/**
* Determines if this bullet is allowed to pass through the bunker without causing damage to it
* @return true if bullet may pass through the bunker
*/
private boolean determinePhasability()
{
if(!(this instanceof EnemyBullet || this instanceof HomingBullet) && (int)(Math.random()*10 + 1) > 10 - 1.5*States.bulletPhaseChance)//gives the bullet a chance to phase through the bunker
{
return true;
}
return false;
}
/**
* Returns if bullet is phasable
* @return true if bullet is phasable
*/
public boolean isPhasable()
{
return phasable;
}
}