summaryrefslogtreecommitdiff
path: root/src/creature.cpp
blob: 7f31003ea8f84da4b49a2212122a92853bc2576d (plain)
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
#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;
}

void Creature::Behavior()
{
	health-=1; //Decrements health each time a behavior is executed
	this->Priority(); //Checks which action has priority (doesn't really do this right now)

	if(this->Action())
	{
		health+=10;
	}
}

void Creature::Priority()
{
	//Traverses location vector, if object at [i] is resource (2), then creature's target coordinates are set
	int i;
	for(i=0;i<location.size();i++)
	{
		if(location[i].type==2)
		{
			xTarget = location[i].x;
			yTarget = location[i].y;
		}
	}
}

bool Creature::Action()
{
	//If the distance is close, will return an bool
	if(sqrt(pow(xPosition - xTarget, 2) + pow(yPosition - yTarget, 2)) < 2)
		return true;

	//Makes moves towards target coordinates
	if(xPosition==xTarget)
	{
		if(yPosition<yTarget)
			yPosition++;
		else
			yPosition--;
	}

	else if(yPosition==yTarget)
	{
		if(xPosition<xTarget)
			xPosition++;
		else
			xPosition--;
	}

	else if(xPosition<xTarget)
	{
		if(yPosition<yTarget)
		{
			xPosition++;
			yPosition++;
		}

		else
		{
			xPosition++;
			yPosition--;
		}
	}

	else if (xPosition>xTarget)
	{
		if(yPosition<yTarget)
		{
			xPosition--;
			yPosition++;
		}

		else
		{
			xPosition--;
			yPosition--;
		}
	}
	return false;
}

Location Creature::getLocation()
{
	//returns location object of the specific creature
	Location L(xPosition, yPosition, 1);
	return L;
}