-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBullet.java
106 lines (97 loc) · 2.21 KB
/
Bullet.java
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
// The "Bullet" class.
/*
Programmed by: Tony Ng
Last Modified: 02/06/2014
Purpose: Defines Bullet and their behaviours, inherits Blob
*/
import java.awt.*;
import hsa.Console;
import java.io.*;
import javax.imageio.*;
public class Bullet extends Blob
{
private int bounce = 0;
public int bulletCd = Consts.BULLETCOOLDOWN; //When the bullet can start killing tanks (avoids bullet killing firing tank when just being fired
public Bullet (int x, int y, int angle)
{
tag = Consts.BULLETNOCOLLIDE;
//Find the velocity
yVel = (Math.sin (Math.toRadians(angle))) * Consts.BULLETVELOCITY;
xVel = (Math.cos (Math.toRadians(angle))) * Consts.BULLETVELOCITY;
xLoc = x;
yLoc = y;
trueXBound = Consts.XBULLETSIZE;
trueYBound = Consts.YBULLETSIZE;
xBound1 = xLoc;
xBound2 = trueXBound + xLoc;
yBound1 = yLoc;
yBound2 = trueYBound + yLoc;
try
{
image = ImageIO.read( new File( Consts.IMG_BULLET ) );
}
catch(Exception e){};
}
public void calculateTurn()
{
//Frictions?
//Limitors (may be included in frictions
if(bulletCd > 0)
{
bulletCd --;
}
else
{
tag = Consts.BULLET;
}
xLoc += xVel;
yLoc += yVel;
xDrawLoc = xLoc;
yDrawLoc = yLoc;
xBound1 = xLoc;
xBound2 = trueXBound + xLoc;
yBound1 = yLoc;
yBound2 = trueYBound + yLoc;
}
public boolean onCollision (int id, int dir)
{
//System.out.println("Bullet Collide");
if(id == Consts.WALL)
{
//System.out.println(bounce);
//Maximum number of bounces
if (bounce < Consts.MAX_BOUNCE)
{
if(dir <= Consts.RIGHTCOLLIDE)
{
xVel = xVel * -1; //Reflect X
}
else
{
yVel = yVel * -1; //Reflect Y
}
bounce++; //Bounce count increase
}
else
return true; //Bounced 5 times -- Delete itself
return false;
}
else if(id == Consts.TANK)
{
if(bulletCd == 0) //If bullet is not freshly shot from tank
{
//Draw Explosion
return true;
}
else
{
return false;
}
}
else if(id == Consts.TARGET)
{
return true; //Destroy itself if hits target
}
return false; //Defaults to do nothing
}
} // Bullet class