-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPredator.pde
133 lines (106 loc) · 2.87 KB
/
Predator.pde
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
public class Predator extends Animal
{
private PredatorState m_State = PredatorState.WANDER;
private float m_HuntMovementSpeed = 20f;
private Prey m_TargetedPrey;
private float m_HuntRadius = 150;
private float m_NutritionBoostPrPrey = 40; // Nutrition gain per eaten prey
private boolean m_ShowHuntRadius = false;
private boolean m_ShowTargetedPrey = false;
public PredatorState getState() { return m_State; }
public Predator(String name)
{
m_Name = name;
}
@Override
public void setup()
{
super.setup();
m_NutritionMax = 100f;
modifyNutrition(m_NutritionMax / 2);
m_NutritionLossPrSecond = 1f;
m_SplitNutritionPercent = 0.6f;
m_SplitCooldown = 10f;
m_Sprite = loadShape("predator.svg");
}
@Override
public void update()
{
super.update();
checkForHunt();
switch (m_State)
{
case WANDER:
m_CurrentMovementSpeed = m_WanderMovementSpeed;
wander();
break;
case HUNT:
m_CurrentMovementSpeed = m_HuntMovementSpeed;
seek(m_TargetedPrey.GetPosition());
tryEatPrey(m_TargetedPrey);
break;
}
}
@Override
public void enableDebug()
{
super.enableDebug();
m_ShowHuntRadius = true;
m_ShowTargetedPrey = true;
}
@Override
public void disableDebug()
{
super.disableDebug();
m_ShowHuntRadius = false;
m_ShowTargetedPrey = false;
}
@Override
public ZVector getCenter() { return m_Position; }
@Override
public ZVector getHalfExtents() { return new ZVector(50, 20); }
private void checkForHunt()
{
if (m_ShowHuntRadius)
{
fill(0, 0, 0, 100);
circle(m_Position.x, m_Position.y, m_HuntRadius*2);
}
// Check if a prey is nearby and if so, switch state to HUNT
Prey closest = m_GameScene.getClosestAnimalOfType(Prey.class, m_Position);
if (closest == null)
{
m_State = PredatorState.WANDER;
return;
}
// Validate that closest prey is within hunt radius
if (ZVector.sub(closest.GetPosition(), m_Position).mag() > m_HuntRadius)
{
m_State = PredatorState.WANDER;
return;
}
m_State = PredatorState.HUNT;
m_TargetedPrey = closest;
if (closest != null && m_ShowTargetedPrey)
{
fill(255, 0, 0);
stroke(2);
line(m_Position.x, m_Position.y, m_TargetedPrey.GetPosition().x, m_TargetedPrey.GetPosition().y);
}
}
private void tryEatPrey(Prey prey)
{
// If the predator and the prey are colliding then eat the prey
if (!m_GameScene.AABBvsAABB(this, prey))
return;
m_GameScene.DestroyAnimal(prey);
modifyNutrition(m_NutritionBoostPrPrey);
}
@Override
protected void handleSplitting()
{
m_TimeSinceSplit = 0f;
m_GameScene.addAnimal(new Predator("child of " + getName()), m_Position);
}
private float m_NutritionForSplit = 0.8; // Percentage nutrition required for split
}