-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.cpp
More file actions
136 lines (128 loc) · 4.32 KB
/
GameObject.cpp
File metadata and controls
136 lines (128 loc) · 4.32 KB
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
#include "GameObject.h"
GameObject::GameObject() : curPos(0,0), direction((char)Keys::STAY)//, currStep(0)
{
steps.clear();
}
//--------------------------------------------------------------------------------------//
GameObject::GameObject(int startPosX, int startPosY, char direction) : curPos(startPosX, startPosY), direction(direction)//, currStep(0)
{
steps.clear();
}
//--------------------------------------------------------------------------------------//
Coord& GameObject::getCurPos() // returns 'curPos'
{
return this->curPos;
}
//--------------------------------------------------------------------------------------//
char GameObject::getDirection() const // returns 'direction'
{
return this->direction;
}
//--------------------------------------------------------------------------------------//
string GameObject::getSteps() const // retrun the string 'Steps'
{
return this->steps;
}
//--------------------------------------------------------------------------------------//
void GameObject::setCurPos(Coord& pos) // sets a new value to 'curPos'
{
this->curPos.setX(pos.getX());
this->curPos.setY(pos.getY());
}
//--------------------------------------------------------------------------------------//
void GameObject::setCurPosY(int y) // sets a new value to 'curPos' 'y' value
{
this->curPos.setY(y);
}
//--------------------------------------------------------------------------------------//
void GameObject::setCurPosX(int x) // sets a new value to 'curPos' 'x' value
{
this->curPos.setX(x);
}
//--------------------------------------------------------------------------------------//
void GameObject::setDirection(char direction) // sets a new value to 'direction'
{
switch (direction)
{
case 'w':
direction = (char)Keys::UP;
break;
case 'd':
direction = (char)Keys::RIGHT;
break;
case 'x':
direction = (char)Keys::DOWN;
break;
case 'a':
direction = (char)Keys::LEFT;
break;
case 's':
direction = (char)Keys::STAY;
}
if (direction == (char)Keys::UP || direction == (char)Keys::RIGHT || direction == (char)Keys::DOWN || direction == (char)Keys::LEFT || direction == (char)Keys::STAY)
{
this->direction = direction;
}
}
//--------------------------------------------------------------------------------------//
void GameObject::addMove(char move) // add the move to 'steps' string
{
steps.push_back(move);
}
//--------------------------------------------------------------------------------------//
char GameObject::nextMove() // pop the next move from 'steps' string
{
char next = EMPTY_STR;
if (steps.empty())
{
return next;
}
next= steps.back();
steps.pop_back();
return next;
}
//--------------------------------------------------------------------------------------//
bool GameObject::isValidDir(char direction) // check the validation of the direction
{
return(direction == (char)Keys::UP || direction == (char)Keys::RIGHT || direction == (char)Keys::DOWN || direction == (char)Keys::LEFT || direction == (char)Keys::STAY);
}
//--------------------------------------------------------------------------------------//
int GameObject::randomBetween(int start, int end) // randomly picks a number between 'start' and 'end'
{
return rand() % (end - start) + start;
}
//--------------------------------------------------------------------------------------//
char GameObject::randomDirection() // randomly picks a direction
{
int randNum;
randNum = rand() % 4;
while (dircArray[randNum] == this->direction)
{
randNum = rand() % 4;
}
return dircArray[randNum];
}
//--------------------------------------------------------------------------------------//
void GameObject::addMoves(ifstream& file, bool& isOpened) // fill the string 'steps' with data from the file
{
char currCh;
currCh = file.get();
while (currCh != END_LINE )
{
if (!isValidDir(currCh) || file.eof())
{
isOpened = false;
file.close();
throw "input error";
}
addMove(currCh);
currCh = file.get();
}
reverse(steps.begin(), steps.end());
}
//--------------------------------------------------------------------------------------//
void GameObject::clearSteps() // clear the 'steps' string
{
this->steps.clear();
}
//--------------------------------------------------------------------------------------//