-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerson.cpp
92 lines (71 loc) · 1.78 KB
/
Person.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
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
#include "Person.h"
#include "Obstacle.h"
#include <iostream>
Person::Person() : Entity('+') {
xPos = 0;
yPos = 0;
name = "";
}
//'move' function returns 1 if player was moved involuntarily (just like 'touched')
int Person::move(Maze* maze, int aKey) {
int moved = 0; //value to store whether player was moved involuntarily
mvaddch(yPos, xPos, maze->getCurrentObstacle(this)->getSprite()); //restore the character in players previous position
//Determine the player's next position
int nextY = yPos;
int nextX = xPos;
switch (aKey) {
case KEY_UP:
nextY--;
break;
case KEY_DOWN:
nextY++;
break;
case KEY_LEFT:
nextX--;
break;
case KEY_RIGHT:
nextX++;
break;
default:
return 0;
}
Obstacle* nextOb = maze->getNextObstacle(this, aKey);
if (!(nextOb->isWall())) //if the next obstacle isn't a wall
{
yPos = nextY; //update the player's position
xPos = nextX;
moved = maze->getCurrentObstacle(this)->touched(maze, this, aKey); //touch whatever is there and test if it moved the player involuntarily
} else {
nextOb->touched(maze, this, aKey); //there is a wall in the way, call it's touched method without moving onto it.
moved = 0;
}
mvaddch(yPos,xPos, sprite);
return moved;
}
bool Person::setPos(int x, int y, Maze* maze) {
if (x>0 && x < maze->getMapWidth() && y>0 && y<maze->getMapHeight()) { //check the player isnt being moved out of bounds
xPos = x;
yPos = y;
return 1;
} else {
return 0;
}
}
int Person::getxPos() {
return xPos;
}
int Person::getyPos() {
return yPos;
}
bool Person::drawPerson() {
mvaddch(yPos,xPos, sprite);
return 1;
}
void Person::setName(std::string aName) {
name= aName;
}
std::string Person::getName() {
return name;
}
Person::~Person() {
}