summaryrefslogtreecommitdiff
path: root/src/creature.cpp
blob: 24508bf970820f9d54c2c2c876e67b48865759de (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
101
102
103
#include "creature.hpp"

Creature::Creature(Window m, std::string s)
{
	texture = loadTexture(s, m);
	renderer = m.getRenderer();
	health = 500;
	maxHealth = 1000;
	hunger = 0;

	L.y=rand()%800;
	L.x=rand()%1200;
    type = 1;

    hasTarget = false;
	wandering = false;
	able = true;
	n=0;
}

void Creature::Behavior()
{
	//health-=1; 

	this->Priority();

	this->Action();
}

void Creature::Priority()
{	
    for(vector <Entity*>::iterator it = N.begin(); it!=N.end(); it++){
       if((*it)->getType() == 2){
           if(!hasTarget){
                target = *it;
                wandering = false;
                hasTarget = true; 
           }
           else
               break;
       }
    }
}

void Creature::Action()
{	

	if(hasTarget){ 
        if(Distance(L,target->getLocation())<5){
            target->eat();
            health+=1000; 
        }
        if(target->getAmount()==0)
            hasTarget = false;
    } 

	//Makes moves towards target coordinates
	if(L.x==target->getLocation().x)
	{
		if(L.y<target->getLocation().y)
			L.y+=speed;
		else
			L.y-=speed;
	}

	else if(L.y==target->getLocation().y)
	{
		if(L.x<target->getLocation().x)
			L.x+=speed;
		else
			L.x-=speed;
	}

	else if(L.x<target->getLocation().x)
	{
		if(L.y<target->getLocation().y)
		{
			L.x+=speed;
			L.y+=speed;
		}

		else
		{
			L.x+=speed;
			L.y-=speed;
		}
	}

	else if (L.x>target->getLocation().x)
	{
		if(L.y<target->getLocation().y)
		{
			L.x-=speed;
			L.y+=speed;
		}

		else
		{
			L.x-=speed;
			L.y-=speed;
		}
	}	
}