-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathACharacter.cpp
61 lines (53 loc) · 1.33 KB
/
ACharacter.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
61
#include <stdexcept>
#include "ACharacter.hh"
ACharacter::ACharacter(Direction dir)
: direction(dir)
{
}
ACharacter::~ACharacter()
{
}
void ACharacter::changeDirection(ACharacter::Direction dir)
{
static std::string const dirName[4] = {"up", "right", "down", "left"};
std::cerr << this->getName() << ": Changing direction to " << dirName[dir] << std::endl;
this->direction = dir;
}
void ACharacter::updateNewPosition(ACharacter::Direction dir) {
switch (dir) {
case ACharacter::UP:
this->posY--;
break;
case ACharacter::RIGHT:
this->posX++;
break;
case ACharacter::DOWN:
this->posY++;
break;
case ACharacter::LEFT:
this->posX--;
break;
default:
break;
}
}
void ACharacter::move()
{
int previousPosX = this->posX;
int previousPosY = this->posY;
this->updateNewPosition(this->direction);
try {
// Notify ChangeManager
std::cerr << this->getName() << ": Current position is (" << previousPosX << ";" << previousPosY << "), moving to (" << this->posX << ";" << this->posY << ")" << std::endl;
this->notify();
} catch (std::exception e) {
// Can't move to new position
std::cerr << this->getName() << ": Can't move, stay in the same position" << std::endl;
this->posX = previousPosX;
this->posY = previousPosY;
}
}
ACharacter::Direction ACharacter::getDirection() const
{
return (this->direction);
}