summaryrefslogtreecommitdiff
path: root/src/creature.cpp
blob: 7c4c1080fc20f4ec2e09da6e47f67fb556fb1f20 (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
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
#include "creature.hpp"

Creature::Creature(Window m, SDL_Rect R)
{
    renderer = m.getRenderer();
    rect = R; 
    
    if(rect.x == 0 && rect.y == 0){
        rect.x = rand()%WINDOW_X;
        rect.y = rand()%WINDOW_Y;
    }

    type        = CREATURE_TYPE;
    health      = CREATURE_START_HEALTH;
    maxHealth   = CREATURE_MAX_HEALTH;
    reach       = CREATURE_REACH;
    speed       = CREATURE_SPEED;
    bestSense   = CREATURE_BEST_SENSE;
    bite        = CREATURE_BITE;
    amountToGrow= CREATURE_AMOUNT_TO_GROW;

    gender      = rand() % 2;
    hungry      = false;
    hasTarget   = false;
	wander      = false;
    able        = true;
}

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

	this->Priority();

    if(!hasTarget)
        this->setTarget();

	this->Action();
}

void Creature::Priority()
{	 
    if(health < maxHealth/2){
        hungry = true;
        able = false;
    }
    else{
        hungry = false;
        able = true;
    }
}

void Creature::setTarget()
{
    for(list <Entity*>::iterator it = N.begin(); it!=N.end(); it++){
        if( (*it)->getType() == RESOURCE_TYPE && hungry){ 
            target = *it;
            hasTarget = true;
            wander = false;
            break;
        } 
    }

    if(!hasTarget&&!wander){
        wander = true;
        SDL_Rect r = {rand()%WINDOW_X, rand()%WINDOW_Y, 0, 0};
        wTarget = r; 
    }
}


void Creature::Action()
{	
    if(hasTarget){ 
        if( Distance(rect,target->getRect() ) < reach ){
            target->eat(bite);
            health+=bite;
            amountAte++;
            if(rect.w <= CREATURE_SIZE_MAX && amountToGrow <= amountAte){
                amountAte = 0;
                rect.w = rect.h = rect.w + 1;
            }
        }
        else
            Move(target->getRect());

        if(target->getAmount()<=0){
            hasTarget = false; 
        }
    }
    else{
        if(Distance(rect,wTarget) < reach)
            wander = false;
        else
            Move(wTarget);
    }
}

void Creature::Move(SDL_Rect R)
{
    if( rect.x == R.x ){
        if( rect.y < R.y )
		    rect.y+=speed;
		else
			rect.y-=speed;
	}
	else if( rect.y == R.y ){
		if( rect.x < R.x )
			rect.x+=speed;
		else
			rect.x-=speed;
	}
	else if( rect.x < R.x ){
		if( rect.y < R.y ){
			rect.x+=speed;
			rect.y+=speed;
		}
		else{
			rect.x+=speed;
			rect.y-=speed;
		}
	}
	else if ( rect.x > R.x ){
		if( rect.y < R.y ){
			rect.x-=speed;
			rect.y+=speed;
		}
		else{
			rect.x-=speed;
			rect.y-=speed;
		}
	}
}