-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPed2.java
108 lines (101 loc) · 2.95 KB
/
Ped2.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
/**
* Pedestrian 2 approaches the nearest Pedestrian 1 and stop them
* before they explodes
*
* @author Adrian Lee
* @version 20240317
*/
public class Ped2 extends Pedestrian
{
VehicleWorld vw;
private double speed;
private double maxSpeed;
private int direction;
private int target;
private boolean awake, entering;
public Ped2(int direction){
super(direction);
maxSpeed = Math.random() * 2 + 1;
speed = maxSpeed*0.8;
enableStaticRotation();
}
public void addedToWorld(World w){
vw = (VehicleWorld) w;
}
/**
* Act - do whatever the Ped2 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Awake is false if the Pedestrian is "knocked down"
if (super.isAwake()){
followNearestP();
arrestPed1(detectCollidingPed1());
}
}
/**
* Get distance to a given Pedestrian 1
* Inspired by Gevater_Tod4177 on greenfoot.org
* https://www.greenfoot.org/topics/4911
*
* @param p Pedestrian 1
*/
public double getDistance(Ped1 p) {
return Math.hypot(p.getX() - getX(), p.getY() - getY());
}
/**
* Get the nearest Pedestrian 1 by comparing distances between
* all the Pedestrian 1s in range
* Inspired by Gevater_Tod4177 on greenfoot.org
* https://www.greenfoot.org/topics/4911
*/
public Ped1 getNearestPed() {
ArrayList<Ped1> pNear = (ArrayList<Ped1>) getObjectsInRange(1280,Ped1.class);
Ped1 nearestP = null;
double nearestDistance = 999;
double distance;
for(Ped1 p: pNear){
distance = getDistance(p);
if(distance < nearestDistance){
nearestP = p;
nearestDistance = distance;
}
}
return nearestP;
}
/**
* Move towards the nearest Pedestrian 1 using getNearestPed()
*/
public void followNearestP() {
Ped1 nearestPed = getNearestPed();
if(nearestPed != null){
turnTowards(nearestPed);
move(speed);
}
}
/**
* Returns intersecting Pedestrian 1
*/
public Ped1 detectCollidingPed1() {
Ped1 p = (Ped1) getOneIntersectingObject(Ped1.class);
return p;
}
/**
* Arrests Pedestrian 1 if intersecting
* Currently simply removing them from world
* Random triggers Pedestrian 1 explode
*/
public void arrestPed1(Ped1 collidingPed1) {
if (collidingPed1 != null){
if(Greenfoot.getRandomNumber(2) == 1){
collidingPed1.explode();
}
vw.removeObject(collidingPed1);
vw.removeObject(this);
vw.reducePed2Count();
}
}
}