-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bullet.java
73 lines (60 loc) · 2.02 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class Bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bullet extends Mover
{
Point m_speed;
Point init_pos;
int act_count = 1;
/**
* Act - do whatever the Bullet wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Bullet(Point init_pos, Point init_speed, double direction){
super(6);
setLocation(init_pos);
setSpeed(init_speed, direction);
this.init_pos = init_pos;
act_count = 1;
}
public void act()
{
move();
act_count++;
// Add your action code here.
}
public void move(){
Point displacement = new Point(m_speed);
displacement.scale(act_count);
displacement.y *= -1;//reverse y axis to match the stupid coordinate
setLocation(MyMath.add(init_pos, displacement));
}
public void setSpeed(Point init_speed, double direction){
setRotation((int)MyMath.angleInRange(-direction));
//direction = Math.toRadians(direction);
direction *= 0.0175;
m_speed = new Point(Math.cos(direction), Math.sin(direction));
m_speed.setLength(getMaxSpeed());
m_speed = MyMath.add(init_speed, m_speed);
//setRotation((int)MyMath.angleInRange(-m_speed.getTheta()));
}
public boolean hitMover(){
return isTouching(Mover.class);
}
public boolean hitBarrier(){
return isTouching(Barrier.class);
}
public void removeTouching(){
List<TankBody> allTouched = getIntersectingObjects(TankBody.class);
for(TankBody t : allTouched){
((TankWorld)getWorld()).removeObject(t.m_turret);
((TankWorld)getWorld()).removeObject(t);
}
removeTouching(Mover.class);
}
}