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
134
135
136
137
138
|
#include "creature.hpp"
Creature::Creature(Window m, std::string s) //Constructor
{
texture = loadTexture(s, m);
renderer = m.getRenderer();
health = 100;
hunger = 0;
//initializes random start coordinates for creature, target position is equivalent to it's position
yPosition=yTarget=rand()%800;
xPosition=xTarget=rand()%1200;
hasTarget = false;
n=0;
}
int Creature::Behavior()
{
health-=1; //Decrements health each time a behavior is executed
this->Priority();
if(this->Action())
{
if(nR.size())
{
nR[n]->eat();
if(health<500)
health+=10;
}
}
return 0;
}
void Creature::Priority()
{
double d; // lol
for(int i = 0; i < nR.size(); i++)
{
if(!i)
d = Distance(this->getLocation(),nR[0]->getLocation());
if(d>Distance(this->getLocation(),nR[i]->getLocation()))
{
d=Distance(this->getLocation(),nR[i]->getLocation());
n=i;
}
}
if(nR.size())
{
xTarget = nR[n]->getLocation().x;
yTarget = nR[n]->getLocation().y;
hasTarget = true;
}
else
hasTarget = false;
}
bool Creature::Action()
{
//If the distance is close, will return an bool
//if(xPosition == xTarget && yPosition == yTarget)
// return false;
if(nR.size())
if(5 > Distance(this->getLocation(),nR[n]->getLocation()))
{
if(hasTarget)
return true;
else
return false;
}
//Makes moves towards target coordinates
if(xPosition==xTarget)
{
if(yPosition<yTarget)
yPosition+=speed;
else
yPosition-=speed;
}
else if(yPosition==yTarget)
{
if(xPosition<xTarget)
xPosition+=speed;
else
xPosition-=speed;
}
else if(xPosition<xTarget)
{
if(yPosition<yTarget)
{
xPosition+=speed;
yPosition+=speed;
}
else
{
xPosition+=speed;
yPosition-=speed;
}
}
else if (xPosition>xTarget)
{
if(yPosition<yTarget)
{
xPosition-=speed;
yPosition+=speed;
}
else
{
xPosition-=speed;
yPosition-=speed;
}
}
return false;
}
Location Creature::getLocation()
{
//returns location object of the specific creature
Location L(xPosition, yPosition, 1);
return L;
}
double Creature::Distance(Location A, Location B)
{
//computes distance between two points
return sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2));
}
|