-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShip.java
192 lines (171 loc) · 4.91 KB
/
Ship.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.awt.Font;
/**
* A space ship. It comes from space, lands, and releases some Greeps into the world.
*
* @author Michael Kolling
* @author Davin McCall
* @author Poul Henriksen
* @version 2.0
*/
public class Ship extends Actor
{
/**
* Method that creates the Greeps.
* You can change the class that objects are created from here.
*/
private Greep createGreep()
{
if(teamNumber == 1) {
return new MyGreep(this);
}
else {
return new SimpleGreep(this);
}
}
private int totalPassengers = 20; // Total number of passengers in this ship.
private int passengersReleased = 0; // Number of passengers that left so far.
private Counter foodCounter; // Tomato counter
private int targetPosition; // The vertical position for landing
private int stepCount = 0;
private boolean hatchOpen = false; // Whether the greeps can deploy yet
private int[] dataBank = new int[1000]; // Ship's databank. Holds a large amount of information.
private int teamNumber; // Team number. Should be 1 or 2.
private int direction = 1; // 1 is positive y-direction, -1 is negative.
private String greepName; // Name of the Greeps produced by this ship.
/**
* Create a space ship. The parameter specifies at what height to land.
*/
public Ship(String imageName, int position, int teamNumber)
{
targetPosition = position;
this.teamNumber = teamNumber;
GreenfootImage im = new GreenfootImage(imageName);
greepName = createGreep().getName();
setImage(im);
}
/**
* Find out which direction we are moving in.
*/
public void addedToWorld(World w) {
if(getY() > targetPosition) {
direction = -1;
}
else {
direction = 1;
}
}
/**
* Let the ship act: move or release greeps.
*/
public void act()
{
if(inPosition() && hatchOpen) {
if(! isEmpty()) {
releasePassenger();
}
}
else {
move();
}
}
/**
* True if all passengers are out.
*/
public boolean isEmpty()
{
return passengersReleased == totalPassengers;
}
/**
* Move the ship down or up (for movement before landing).
*/
public void move()
{
int dist = (targetPosition - getY()) / 16;
if(dist == 0) {
dist = direction;
}
setLocation(getX(), getY() + dist);
if(inPosition()) {
// Make sure we are at exactly the right target position
setLocation(getX(), targetPosition);
}
}
/**
* True if we have reached the intended landing position.
*/
public boolean inPosition()
{
int diff = (getY() - targetPosition) * direction ;
return diff >= 0;
}
/**
* Open the ship's hatch. This allows the greeps to come out.
*/
public void openHatch()
{
hatchOpen = true;
}
/**
* Possibly: Let one of the passengers out. Passengers appear at intervals,
* so this may or may not release the passenger.
*/
private void releasePassenger()
{
if(passengersReleased < totalPassengers) {
stepCount++;
if(stepCount == 10) {
Greep newGreep = createGreep();
getWorld().addObject(newGreep, getX(), getY() + 30);
passengersReleased++;
stepCount = 0;
}
}
}
/**
* Record that we have collected another tomato.
*/
public void storeTomato()
{
if(foodCounter == null) {
foodCounter = new Counter("Tomatoes: ");
int x = getX();
int y = getY() + getImage().getHeight() / 2 + 10;
if(y >= getWorld().getHeight()) {
y = getWorld().getHeight();
}
getWorld().addObject(foodCounter, x, y);
}
foodCounter.increment();
}
/**
* Return the current count of tomatos collected.
*/
public int getTomatoCount()
{
if(foodCounter == null)
return 0;
else
return foodCounter.getValue();
}
/**
* Get the ship's data bank array.
*/
public int[] getData()
{
return dataBank;
}
/**
* Return the author name of this ship's Greeps.
*/
public String getGreepName()
{
return greepName;
}
public boolean isTeamTwo()
{
return teamNumber == 2;
}
}