-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotor.java
More file actions
75 lines (68 loc) · 2.97 KB
/
Motor.java
File metadata and controls
75 lines (68 loc) · 2.97 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
64
65
66
67
68
69
70
71
72
73
74
75
public class Motor extends Neuron{
public MotorMethod motorMethod;
private static int numberOfMotorMethods = 6; // Update this when creating new Motor methods
public Motor(int methodID){
super("Motor");
switch(methodID%(numberOfMotorMethods)){
case 0: this.motorMethod = Motor::MoveUp; break;
case 1: this.motorMethod = Motor::MoveDown; break;
case 2: this.motorMethod = Motor::MoveRight; break;
case 3: this.motorMethod = Motor::MoveLeft; break;
case 4: this.motorMethod = Motor::Eat; break;
case 5: this.motorMethod = Motor::DoNothing; break;
}
}
public Motor(){
super("Motor");
this.motorMethod = Motor::DoNothing;
}
public double getMaxValue(){
double maxValue = -1000000000;
for(double value : super.getValues()){
if(maxValue<value){
maxValue = value;
}
}
return maxValue;
}
public interface MotorMethod{
void invoke(Creature subject);
}
////////////////////////////////////////////////////////
// MOTOR METHODS // MOTOR METHODS // MOTOR METHODS //
////////////////////////////////////////////////////////
private static void MoveUp(Creature creature){
Main.loaded.creatureLocations[creature.getPosX()][creature.getPosY()]-=1;
creature.setPosY((creature.getPosY() + 1) % Main.loaded.worldSize);
Main.loaded.creatureLocations[creature.getPosX()][creature.getPosY()]+=1;
creature.moved();
}
private static void MoveDown(Creature creature){
Main.loaded.creatureLocations[creature.getPosX()][creature.getPosY()]-=1;
creature.setPosY((creature.getPosY() - 1 + Main.loaded.worldSize) % Main.loaded.worldSize);
Main.loaded.creatureLocations[creature.getPosX()][creature.getPosY()]+=1;
creature.moved();
}
private static void MoveLeft(Creature creature){
Main.loaded.creatureLocations[creature.getPosX()][creature.getPosY()]-=1;
creature.setPosX((creature.getPosX() - 1 + Main.loaded.worldSize) % Main.loaded.worldSize);
Main.loaded.creatureLocations[creature.getPosX()][creature.getPosY()]+=1;
creature.moved();
}
private static void MoveRight(Creature creature){
Main.loaded.creatureLocations[creature.getPosX()][creature.getPosY()]-=1;
creature.setPosX((creature.getPosX() + 1) % Main.loaded.worldSize);
Main.loaded.creatureLocations[creature.getPosX()][creature.getPosY()]+=1;
creature.moved();
}
private static void DoNothing(Creature creature) {
// Only meant as a default method as it serves no value
}
private static void Eat(Creature creature) {
if (Main.loaded.foodLocations[creature.getPosX()][creature.getPosY()] >= 1) {
creature.ateFood();
Main.loaded.foodLocations[creature.getPosX()][creature.getPosY()]--;
Main.loaded.currentFoodCount--;
}
}
}