-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullet.java
More file actions
69 lines (64 loc) · 1.89 KB
/
Bullet.java
File metadata and controls
69 lines (64 loc) · 1.89 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
/**
* @author Bryan Xu, Brian Wu
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
public class Bullet extends JComponent
{
public Rectangle body;
public int angle;
public int velocity;
public ArrayList<Integer> xVelocity;
public ArrayList<Integer> yVelocity;
public int constant = 1000;
public Bullet(int rX, int rY, int rWidth, int rHeight, int ang, int vel){
body = new Rectangle(rX, rY, rWidth, rHeight);
angle = ang;
velocity = vel;
xVelocity = new ArrayList<Integer>();
yVelocity = new ArrayList<Integer>();
}
public void start(){
int totalX = (int)(constant*velocity*Math.cos(Math.toRadians(angle)));
int aX = (int)(totalX/constant);
int bX = aX+1;
int numOfaX;
int numOfbX;
numOfbX = (totalX-aX*constant);
numOfaX = constant-numOfbX;
for(int i = 0; i<constant; i++){
if(i<numOfaX){
xVelocity.add(aX);
}else{
xVelocity.add(bX);
}
}
int totalY = (int)(constant*velocity*Math.sin(Math.toRadians(angle)));
int aY = (int)(totalY/constant);
int bY = aY+1;
int numOfaY;
int numOfbY;
numOfbY = (totalY-aY*constant);
numOfaY = constant-numOfbY;
for(int i = 0; i<constant; i++){
if(i<numOfaY){
yVelocity.add(aY);
}else{
yVelocity.add(bY);
}
}
}
public void move(){
int ranIndex = (int)(Math.random()*xVelocity.size());
if(xVelocity.size()>0){
int xMove = xVelocity.get(ranIndex);
xVelocity.remove(ranIndex);
int yMove = yVelocity.get(ranIndex);
yVelocity.remove(ranIndex);
body.translate(xMove, yMove);
}
}
}