forked from ivogeorg/ucd-csci2312-pa4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Agent.cpp
60 lines (55 loc) · 1.51 KB
/
Agent.cpp
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
//
// Created by Mario on 4/11/2016.
//
#include "Agent.h"
#include "Resource.h"
namespace Gaming {
const double Agent::AGENT_FATIGUE_RATE = 0.3;
Agent::Agent(const Game &g, const Position &p, double energy) : Piece(g, p) {
__energy = energy;
}
Agent::~Agent() {
}
void Agent::age() {
__energy -= AGENT_FATIGUE_RATE;
}
Piece& Agent::interact(Agent *other) {
if(other->getEnergy() == this->getEnergy()) {
other->finish();
this->finish();
}
else if(other->getEnergy() > this->getEnergy()) {
other->__energy -= this->getEnergy();
this->finish();
}
else if(other->getEnergy() < this->getEnergy()) {
__energy -= other->getEnergy();
other->finish();
}
return *this;
}
Piece& Agent::interact(Resource *other) {
__energy += other->consume();
return *this;
}
Piece& Agent::operator*(Piece &other) {
Piece *piece = &other;
Resource *resource = dynamic_cast<Resource *>(piece);
if(resource) {
interact(resource);
}
Agent *agent = dynamic_cast<Agent *>(piece);
if(agent) {
interact(agent);
}
if(!isFinished()) {
Position newPos;
newPos = other.getPosition();
Position oldPos;
oldPos = getPosition();
setPosition(newPos);
other.setPosition(oldPos);
}
return *this;
}
}