-
Notifications
You must be signed in to change notification settings - Fork 2
/
Food.java
75 lines (66 loc) · 2.44 KB
/
Food.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
public class Food extends Actor{
private Timer timer = new Timer();
public World world;
public int energyValue;
public int respawnDelay;
private boolean isWorm = false;
public GreenfootSound playerSound = new GreenfootSound("slurp.wav");
public GreenfootSound predatorSound = new GreenfootSound("eat.wav");
public Food(){
}
public Food(World world, int energyValue, int respawnDelay){
this.world = world;
this.energyValue = energyValue;
if(energyValue == 0) isWorm = true;
this.respawnDelay = respawnDelay;
timer.markTimer();
}
public void behavior(){
if(timer.getTimer() > respawnDelay){
randomLocation();
timer.markTimer();
}
checkCollision();
}
private void randomLocation(){
while(true){
int xPos = Greenfoot.getRandomNumber(world.getWidth());
int yPos = Greenfoot.getRandomNumber(world.getHeight());
if(isValidLocation(xPos, yPos)){
setLocation(xPos, yPos);
return;
}
}
}
private void checkCollision(){
if(isTouching(Bird.class)){
randomLocation();
timer.markTimer();
playerSound.play();
}
if(isTouching(Snake.class)){
randomLocation();
timer.markTimer();
predatorSound.play();
}
}
public boolean isWorm(){
return isWorm;
}
private boolean isValidLocation(int x, int y){
int spawnRange = 15; //Jarak toleransi antar object
//Pengecekan object apa saja yang berada pada posisi x dan y
List<Actor> actors = world.getObjectsAt(x, y, null);
actors.addAll(world.getObjectsAt(x+spawnRange, y+spawnRange, null));
actors.addAll(world.getObjectsAt(x-spawnRange, y-spawnRange, null));
actors.addAll(world.getObjectsAt(x+spawnRange, y-spawnRange, null));
actors.addAll(world.getObjectsAt(x-spawnRange, y+spawnRange, null));
actors.addAll(world.getObjectsAt(x+spawnRange, y, null));
actors.addAll(world.getObjectsAt(x-spawnRange, y, null));
actors.addAll(world.getObjectsAt(x, y-spawnRange, null));
actors.addAll(world.getObjectsAt(x, y+spawnRange, null));
return actors.isEmpty();
}
}