-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMyWorld.java
64 lines (56 loc) · 1.8 KB
/
MyWorld.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
private static final Random _rand = new Random(37L);
private static final AtomicInteger _babyCounter = new AtomicInteger(0);
Fish fish;
List<Baby> babies;
final int numBabies = 20;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
fish = new Fish();
addObject(fish, getWidth() / 2, getHeight() / 2);
babies = new ArrayList();
for (int i = 0; i < numBabies; i++) {
Baby baby = nextBaby();
babies.add(baby);
addObject(baby, baby.getX(), baby.getY());
}
}
private Baby nextBaby() {
int x = _rand.nextInt(getWidth());
int y = _rand.nextInt(getHeight());
Baby baby = new Baby(_babyCounter.getAndIncrement(), x, y);
System.out.println(baby.getId());
return baby;
}
public void remove(Actor actor) {
babies.remove(actor);
removeObject(actor);
int threshold = _rand.nextInt(100) + 1;
if (babies.size() <= threshold) {
int numToAdd = _rand.nextInt(5) + 1;
for (int i = 0; i < numToAdd; i++) {
Baby baby = nextBaby();
babies.add(baby);
addObject(baby, baby.getX(), baby.getY());
}
}
}
}