-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.java
More file actions
78 lines (71 loc) · 2.5 KB
/
Car.java
File metadata and controls
78 lines (71 loc) · 2.5 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Car inherited by RaceCar, can knock down the pedestrian it touches,
* allowed to change lane within a cool down
*
* @author Yixin Cai
* @version 2022-10-24
*/
public class Car extends Vehicle
{
private boolean isPoliceCared;
private boolean shouldSpwanPoliceCar;
/**
* The constructor of Car
* @param origin The VehicleSpawner
*/
public Car(VehicleSpawner origin) {
super(origin); // call the superclass' constructor
// set instance variables
isPoliceCared = false;
shouldSpwanPoliceCar = false;
maxSpeed = 1.5 + ((Math.random() * 30)/5);
speed = maxSpeed;
yOffset = 0;
gifImages = new GifImage[]{
new GifImage("carLeft.gif"),
new GifImage("carRight.gif")
};
gifImage = gifImages[direction == 1 ? 1 : 0];
// pre-load sound file
sound = new GreenfootSound("car.wav");
}
/**
* Act - do whatever the Car wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act() {
super.act();
VehicleWorld vw = (VehicleWorld)getWorld();
// spwan a police car if should spwan a police car when the VehicleSpwaner is not spwaning car
if (shouldSpwanPoliceCar && !origin.isTouchingVehicle()) {
vw.spawnPoliceCar(origin);
// turn the shouldSpwanPoliceCar to False, since one car can only activate one police car
shouldSpwanPoliceCar = false;
isPoliceCared = true;
}
}
/**
* When a Car hit's a Pedestrian, it should knock it over
* @return boolean True if it hits the pedestrian, Flase otherwise
*/
public boolean checkHitPedestrian() {
int dx = (int)speed + getImage().getWidth()/2;
Pedestrian p = (Pedestrian)getOneObjectAtOffset(dx * direction, 0, Pedestrian.class);
if (p != null && p.isAwake()){
p.knockDown();
// make sure one car can only activate one police car, and skeleton is not included
if (!(p instanceof Skeleton) && !isPoliceCared ) {
shouldSpwanPoliceCar = true;
}
return true;
}
return false;
}
/**
* To allow spawning a PoliceCar next time hitting a pedestrian
*/
public void resetIsPoliceCared() {
isPoliceCared = false;
}
}