-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBus.java
More file actions
134 lines (120 loc) · 4.16 KB
/
Bus.java
File metadata and controls
134 lines (120 loc) · 4.16 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
/**
* Bus can pick maximum 4 pedestrians, it would stop for one second to connect
* with pedestrian, is not allowed to change lane.
*
* @author Yixin Cai
* @version 2022-10-24
*/
public class Bus extends Vehicle
{
// use static to avoid pre-loading lots of gif images during Bus object initialization
// otherwise, there would be noticeable delay while spawning a new bus
private static GifImage[][] BUS_GIF_IMAGES = {
new GifImage[] {
new GifImage("bus00Left.gif"),
new GifImage("bus00Right.gif")
},
new GifImage[] {
new GifImage("bus01Left.gif"),
new GifImage("bus01Right.gif")
},
new GifImage[] {
new GifImage("bus02Left.gif"),
new GifImage("bus02Right.gif")
},
new GifImage[] {
new GifImage("bus03Left.gif"),
new GifImage("bus03Right.gif")
},
new GifImage[] {
new GifImage("bus04Left.gif"),
new GifImage("bus04Right.gif")
}
};
public static int BUS_DELAY = 60;
public static int MAX_PEDS = 4;
// ArrayList to store picked pedestrians
private ArrayList<Pedestrian> pedList;
/**
* The constructor of Bus
* @param origin A VehicleSpawner
*/
public Bus(VehicleSpawner origin){
super (origin); // call the superclass' constructor first
//Set up values for Bus
maxSpeed = 1.5 + ((Math.random() * 10)/5);
speed = maxSpeed;
// because the Bus graphic is tall, offset it a up (this may result in some collision check issues)
yOffset = 8;
delay = 0;
pedList = new ArrayList<Pedestrian>();
updateGifImage();
}
/**
* Act - do whatever the Bus wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
super.act();
}
/**
* Method to check if it hits pedestrian. Picks up the pedestrian and stop for 1 second,
* if the pedestrian is at any point on the front and right side of the bus
* @return boolean True if it hits pedestrian, False otherwise
*/
public boolean checkHitPedestrian() {
boolean picked = false;
int height = getImage().getHeight();
int width = getImage().getWidth();
int dy = (height/2 + 1) * direction;
int dx = (width/2 + 1) * direction;
// check right side
for (int x = -(width / 2); x < width / 2; x+=5) {
picked = checkHitPedestrianAtOffset(x, dy) || picked;
}
// check front
for (int y = -(height / 2); y < height / 2; y+=5) {
picked = checkHitPedestrianAtOffset(dx, y) || picked;
}
if (picked) {
delay = BUS_DELAY;
}
return false;
}
/**
* Method to check if it hits pedestrian at off set, pick the pedestrian if the bus is not full
* @return True if it hits pedestrian at off set, False otherwise
*/
private boolean checkHitPedestrianAtOffset(int x, int y) {
boolean picked = false;
ArrayList<Pedestrian> peds = (ArrayList<Pedestrian>)getObjectsAtOffset(x, y, Pedestrian.class);
for (Pedestrian ped : peds) {
// skip pedestrians that are kncked down or skeletons
if (!ped.isAwake() || (ped instanceof Skeleton)) {
continue;
}
// pick if bus is not full
if (pedList.size() < MAX_PEDS) {
pedList.add(ped);
updateGifImage();
getWorld().removeObject(ped);
picked = true;
}
else {
return picked;
}
}
return picked;
}
/**
* Update the gif image based on number of picked pedestrians
*/
private void updateGifImage() {
int size = pedList.size();
gifImages = BUS_GIF_IMAGES[size];
gifImage = gifImages[direction == 1 ? 1 : 0];
}
}