-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGun.java
More file actions
162 lines (155 loc) · 5.46 KB
/
Copy pathGun.java
File metadata and controls
162 lines (155 loc) · 5.46 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
155
156
157
158
159
160
161
162
import greenfoot.*;
import java.awt.Color;
/**
* Write a description of class Gun here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Gun extends Weapon
{
/**
* Act - do whatever the Gun wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
/**
* Class: Gun (subclass of Actor -- inner class of Chased): the gun for this actor
*/
private GunDec decGun1;
private GunDec decGun2;
/**
* creates image for actor
*/
public Gun()
{
// the image for this actor
decGun1 = new ConcreteDecoratorA(Gun.this);
decGun2 = new ConcreteDecoratorB(decGun1);
GreenfootImage image = new GreenfootImage(50, 10);
image.setColor(Color.black);
for (int i=0; i<4; i++) image.fillRect(20, i, 9+i*4, 10-i*2);
setImage(image);
}
public void addedToWorld(World world) {
world.addObject(decGun1, getX(), getY());
world.addObject(decGun2, getX(), getY());
}
/**
* responds to mouse movement and mouse button clicks
*/
public void act()
{
// turn towards mouse when mouse moves
if (Greenfoot.mouseMoved(null) || Greenfoot.mouseDragged(null))
{
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null) turnTowards(mouse.getX(), mouse.getY());
}
decGun1.setLocation(getX(), getY());
decGun2.setLocation(getX(), getY());
// detect mouse clicks to fire shot
if (Greenfoot.mouseClicked(null)) {
int curScore = ((Welt)getWorld()).score;
if (curScore < 10) {
updateGun();
} else if (curScore < 50) {
decGun1.updateGun();
} else {
decGun2.updateGun();
}
}
}
/**
* overwrite updateGun() method
*/
public void updateGun(){
getWorld().addObject(new Shot(0, Gun.this), getX(), getY());
}
public void morePower() {
getWorld().addObject(new Shot(0, Gun.this), getX(), getY());
getWorld().addObject(new Shot(22, Gun.this), getX(), getY());
getWorld().addObject(new Shot(-22, Gun.this), getX(), getY());
}
public void mostPower() {
getWorld().addObject(new Shot(0, Gun.this), getX(), getY());
getWorld().addObject(new Shot(22, Gun.this), getX(), getY());
getWorld().addObject(new Shot(-22, Gun.this), getX(), getY());
getWorld().addObject(new Shot(45, Gun.this), getX(), getY());
getWorld().addObject(new Shot(-45, Gun.this), getX(), getY());
}
/**
* Class Shot (subclass of QActor -- inner class of Chased.Gun): the shots from this gun
*/
// private class Shot extends QActor
// {
// /**
// * sets bounds fields and creates the image for this actor
// */
// private int RotationDegree;
// public Shot(int degree)
// {
// this.RotationDegree = degree;
// setBoundedAction(QActor.REMOVE, 5); // set bounds fields
// // create image for this actor
// GreenfootImage image = new GreenfootImage(5, 2);
// image.fill();
// setImage(image);
// }
//
// /**
// * initializes rotation and position of actor
// *
// * @param world the world this actor was added into
// */
// public void addedToWorld(World world)
// {
// setRotation(Gun.this.getRotation() + RotationDegree); // set rotation (to that of gun)
// move(Gun.this.getImage().getWidth()/2); // set position (at end of gun)
// }
//
// /**
// * moves actor and checks for removal of actor
// */
// public void act()
// {
// setVX(0);
// setVY(0);
// addForce(500, getRotation()*QVAL);
// move(); // moving (equivalent to 'move(5)' for a non-QActor)
// if (hits(Chaser.class) || atWorldEdge()) getWorld().removeObject(this); // removing
// }
//
// /**
// * internal method to detect object collision; returns true if collision detected, else false
// *
// * @param cls the class of object to check for collision with
// * @return a flag indicating whether an object of given class was detected or not
// */
// private boolean hits(Class cls)
// {
// // get intersecting object and return result
// Actor clsActor = getOneIntersectingObject(cls);
// if (clsActor != null)
// {
// // remove intersector and bump score
// getWorld().removeObject(clsActor);
// ((Welt)getWorld()).adjustScore(1);
// return true;
// }
// return false;
// }
//
// /**
// * internal method that returns a flag indicating world edge encroachment
// *
// * @return a flag indicating whether the actor has encroached on a world edge or not
// */
// private boolean atWorldEdge()
// {
// // return state of encroachment on world edge
// return getX() <= 0 || getY() <= 0 ||
// getX() >= getWorld().getWidth()-1 ||
// getY() >= getWorld().getHeight()-1;
// }
// }
}