90 lines
2.2 KiB
Java
90 lines
2.2 KiB
Java
package backend;
|
|
import java.awt.Color;
|
|
import java.util.ArrayList;
|
|
import java.util.Random;
|
|
|
|
public class Wolf extends Agent {
|
|
|
|
int hunger;
|
|
Random rand;
|
|
|
|
Wolf(int x,int y){
|
|
//first we call the constructor of the superClass(Animal)
|
|
super(x,y,Color.red, 50, 5);
|
|
hunger = 0;
|
|
rand = new Random();
|
|
}
|
|
|
|
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
|
|
|
// color darken the more it is starved
|
|
if(hunger<=100 && hunger>0) {
|
|
Color color = new Color((int)(255-2.54*hunger), 0, 0);
|
|
this.changeColor(color);
|
|
}
|
|
|
|
if(this.searchPrey(triggerRadius, world).isEmpty()) { // no prey nearby ?
|
|
this.lurk(world);
|
|
hunger++;
|
|
}
|
|
else { // hunt prey !
|
|
this.hunt(this.searchPrey(triggerRadius, world).get(0), this.searchPrey(triggerRadius, world).get(1), world);
|
|
if(world.getCell(x, y)==1) {
|
|
world.setCell(x, y, 0);
|
|
if(hunger > 30) {hunger = hunger - 30;}
|
|
}
|
|
}
|
|
return hunger>100;
|
|
}
|
|
|
|
private ArrayList<Integer> searchPrey(int radius, Simulator world) { // targets the nearest prey (= sheep)
|
|
ArrayList<Integer> coordinates = new ArrayList<Integer>();
|
|
ArrayList<Agent> preys = new ArrayList<Agent>();
|
|
preys = world.getNeighboringAnimals(this.x, this.y, radius);
|
|
coordinates.add(preys.get(0).getX()); // start tracking down the first sheep in sight
|
|
coordinates.add(preys.get(0).getY());
|
|
return coordinates;
|
|
}
|
|
|
|
private void lurk(Simulator world) {
|
|
int direction = rand.nextInt(4);
|
|
if(direction == 0 && x < world.getWidth()-1) {
|
|
x+=1;
|
|
}
|
|
if(direction == 1 && y < world.getHeight()-1) {
|
|
y+=1;
|
|
}
|
|
if(direction == 2 && x > 0) {
|
|
x-=1;
|
|
}
|
|
if(direction == 3 && y > 0) {
|
|
y-=1;
|
|
}
|
|
}
|
|
|
|
private void hunt(int x, int y, Simulator world) {
|
|
int clever = rand.nextInt(6 - this.intelligence);
|
|
System.out.println(this.intelligence);
|
|
if(this.intelligence == 0) {
|
|
this.lurk(world);
|
|
}
|
|
|
|
if(clever == 0) {
|
|
if(this.x < x && x < world.getWidth()-1) {
|
|
this.x+=1;
|
|
}
|
|
if(this.y < y && y < world.getHeight()-1) {
|
|
this.y+=1;
|
|
}
|
|
if(this.x > x && x > 0) {
|
|
this.x-=1;
|
|
}
|
|
if(this.y > y && y > 0) {
|
|
this.y-=1;
|
|
}
|
|
}
|
|
else {this.lurk(world);}
|
|
}
|
|
|
|
}
|