-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChild.java
More file actions
63 lines (59 loc) · 1.8 KB
/
Copy pathChild.java
File metadata and controls
63 lines (59 loc) · 1.8 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* A child can turn the move direction into different angles(20 - 60)
* while crossing lanes, can be healed by ambulance.
*
* @author Yixin Cai
* @version 2022-10-24
*/
public class Child extends Pedestrian
{
// set instance and constant variables
private static int CHANGE_COUNT_MAX = 30;
private int changeCount;
/**
* Constructor of Child, default boolean isPenguin is False
* @param direction The int that represents direction
*/
public Child(int direction) {
this(direction, false);
}
/**
* Constructor of Child with two params
* @param direction The int that represents direction
* @param isPenguin Ture if can turn to penguin, False otherwise
*/
public Child(int direction, boolean isPenguin) {
super(isPenguin);
// choose a random speed
maxSpeed = Math.random() * 2 + 1.25;
speed = maxSpeed;
// start as awake
awake = true;
this.direction = direction;
// count down for move angle change
changeCount = CHANGE_COUNT_MAX;
}
/**
* Act - do whatever the Child wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
super.act();
if (changeCount > 0) {
changeCount--;
}
else {
// move in a random angle (20 - 60), either left or right
int random = Greenfoot.getRandomNumber(41) + 20;
if (Greenfoot.getRandomNumber(2) == 1) {
angle = random;
}
else {
angle = -random;
}
changeCount = CHANGE_COUNT_MAX;
}
}
}