summaryrefslogtreecommitdiff
path: root/src/creature.cpp
blob: 1564fc250d52b853c282b0827e8fd3ca7cb13e2c (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "creature.hpp"
#include <cstdio>

Creature::Creature(Rectangle r, DNA d)
{
        rect    = r;
        myDNA   = d;

        if(rect.x == 0 && rect.y == 0){
                rect.x = getRandom(30);
                rect.y = getRandom(30);
        }

        type            = CREATURE_TYPE;
        health          = myDNA.maxHealth/2;
        gender          = rand() % 2;
        age             = 0;
        pregnancyTime   = 0;
        pregnancyReady  = false;
        pregnate        = false;
        hasTarget       = false;

        if(gender)
                gfxData = GraphicsData(rect.x, rect.y, 1, 0, 0, CREATURE_SIDES);
        else
                gfxData = GraphicsData(rect.x, rect.y, 0, 0, 1, CREATURE_SIDES);
}

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

	this->Priority();

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

	this->Action();

        if(pregnate){
                pregnancyTime++;
                if(pregnancyTime > myDNA.expectedPregnancyTime)
                        pregnancyReady = true;
        }

        age++;
        if(age > myDNA.expectedAge)
                health = 0;
}

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

void Creature::setTarget()
{
        std::random_shuffle(nearMe.begin(),nearMe.end());

        for(std::vector <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++){
                if( (*it)->getType() == RESOURCE_TYPE && hungry){ 
                        target = *it;
                        hasTarget = true;
                        wander = false;
                        break;
                } 
                if( (*it)->getType() == CREATURE_TYPE && able && (*it)->getGender() != gender ){
                        target = *it;
                        hasTarget = true;
                        wander = false;
                        break;
                }       
        }

        if(!hasTarget&&!wander){
                wander = true;
                Rectangle tmp;
                tmp.x = getRandom(30);
                tmp.y = getRandom(30);
                wTarget = tmp;
        }
}

void Creature::checkTarget()
{
        for(std::vector <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++)
                if( target == *it )
                        return;

        hasTarget = false;
}


void Creature::Action()
{	
        if(hasTarget){ 
                if(Distance(rect,target->getRectangle()) < myDNA.reach){
                        if(target->getType() == RESOURCE_TYPE){
                                target->eat(myDNA.bite);
                                health+=myDNA.bite;
                                amountAte++;
                                if(target->getAmount()<=0)
                                        hasTarget = false;
                        }
                        else if (target->getType() == CREATURE_TYPE){
                                if(target->getGender() != gender){
                                        target->impregnate(myDNA);
                                }
                                hasTarget = false;
                        }

                }
                else
                        moveTowards(target->getRectangle());
        }
        else if(wander){
                if(Distance(rect,wTarget) < myDNA.reach)
                        wander = false;
                else
                        moveTowards(wTarget);
        }
}

void Creature::moveTowards(Rectangle t)
{
        if( rect.x == t.x ){
                if( rect.y < t.y )
                        rect.y+=myDNA.speed;
                else
                        rect.y-=myDNA.speed;
        }
        else if( rect.y == t.y ){
                if( rect.x < t.x )
                        rect.x+=myDNA.speed;
                else
                        rect.x-=myDNA.speed;
        }
        else if( rect.x < t.x ){
                if( rect.y < t.y ){
                        rect.x+=myDNA.speed;
                        rect.y+=myDNA.speed;
                }
                else{
                        rect.x+=myDNA.speed;
                        rect.y-=myDNA.speed;
                }
        }
        else if ( rect.x > t.x ){
                if( rect.y < t.y ){
                        rect.x-=myDNA.speed;
                        rect.y+=myDNA.speed;
                }
                else{
                        rect.x-=myDNA.speed;
                        rect.y-=myDNA.speed;
                }
        }
}

void Creature::impregnate(DNA D)
{
        if(!pregnate){
                pregnate = true;
                pregnancyTime = 0;
                childsDNA = myDNA.combine(D);
        }
}