-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerticalObjectSpawn.java
More file actions
172 lines (157 loc) · 4.58 KB
/
Copy pathVerticalObjectSpawn.java
File metadata and controls
172 lines (157 loc) · 4.58 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
163
164
165
166
167
168
169
170
171
172
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* This is the VerticalObjectSpawn class.
* The mission of this class is to spawn VerticalObjects and move horizontally.
* This class also has hp to determine whether they should exist or not.
* When they are hit by the FiredBullet class, their hp will be decrease by 1.
*
* @author George Lu
* @version final 2023/4/1
*/
public class VerticalObjectSpawn extends Actor
{
public static final Color BLACK = new Color (0, 0, 0);
private GreenfootImage image, nothing;
private int x, y, count, hp, speed, random, respawnTime, count1;
private World w;
private int topOrNot;
private ArrayList<FiredBullet> fb;
/**
* This is the constructor of the VerticalObjectsSpawn class
*
* @param x This is the x cordinate it should spawn
* @param y This is the y cordinate it should spawn
* @param topOrNot This is the integer that determines whether this object is at top or bottom
*/
public VerticalObjectSpawn(int x, int y, int topOrNot)
{
image = new GreenfootImage(35, 20);
image.setColor(BLACK);
image.fill();
setImage(image);
this.x = x;
this.y = y;
this.topOrNot = topOrNot;
count = 180;
hp = 10;
nothing = new GreenfootImage("nothing.png");
random = Greenfoot.getRandomNumber(4);
if(random == 0)
{
setRotation(0);
}
else
{
setRotation(180);
}
respawnTime = 420;
count1 = 0;
count = 0;
speed = 2;
}
//This is the act method of VerticalObjectsSpawn class
public void act()
{
moveAround();
respawn();
addvo();
count--;
checkDamage();
dead();
}
//This is the method that respawn this object after the dead time is passed.
public void respawn()
{
if(respawnTime == 0)
{
setImage(image);
speed = 2;
hp = 20;
respawnTime = 420;
}
}
//This is the method that determine whether a VerticalObjects should be add or not
public void addvo()
{
if(count%180 == 0 && hp > 0)
{
addSpawner(topOrNot);
}
}
//This is the method that checks if it is hit by a FiredBullet. It will decrase the hp by one and remove the FiredBullet object if it is true;
public void checkDamage()
{
fb = (ArrayList<FiredBullet>)(getIntersectingObjects(FiredBullet.class));
for(FiredBullet fb1: fb)
{
getWorld().removeObject(fb1);
hp--;
}
}
//This is the method that will set this object into dead mode, such as change picture and count respawn time.
public void dead()
{
if(hp <= 0)
{
speed = 0;
respawnTime--;
setImage(nothing);
}
}
//This method will return the hp of the current VerticalObjectsSpawner object
public int getHP()
{
return hp;
}
//This is the method that determines which VerticalObject should be add.
public void addSpawner(int direction)
{
int verticalObjects = Greenfoot.getRandomNumber(4);
if(verticalObjects == 0)
{
w.addObject (new Supply (direction), getX(), getY());
}
else if(verticalObjects == 1)
{
w.addObject (new SupplyBoat (direction), getX(), getY());
}
else if(verticalObjects == 2)
{
w.addObject(new Bullet(direction), getX(), getY());
}else if(verticalObjects == 3)
{
w.addObject(new Missile(direction), x, y);
}
}
//This is the method that ensure this object will move around and will be in the world.
public void moveAround()
{
if(Greenfoot.getRandomNumber(240) == 0)
{
random = Greenfoot.getRandomNumber(4);
if(random == 0)
{
setRotation(0);
}
else
{
setRotation(180);
}
}
if(getX() == 1000)
{
setRotation(180);
}
if(getX() == 100)
{
setRotation(0);
}
move(speed);
}
//This method is called when the object is added and will turn the world this object currently live in for it to use.
public void addedToWorld(World w)
{
this.w = w;
}
}