-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit.cpp
84 lines (73 loc) · 1.47 KB
/
Unit.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
//
// Created by kevin on 22/12/17.
//
#include "Unit.h"
//This is bad. But I can't seem to include from Unit.h file.
#include "Tile.h"
Unit::Unit() : SDLGameObject()
{
m_type = "Unit";
m_moveRange = 0;
m_movePathSet = false;
}
void Unit::load(const LoaderParams* pParams)
{
SDLGameObject::load(pParams);
}
void Unit::draw()
{
SDLGameObject::draw();
}
void Unit::update()
{
if(m_movePathSet)
{
moveToPathElement();
}
SDLGameObject::update();
}
void Unit::clean()
{
SDLGameObject::clean();
}
std::string Unit::getType() {return m_type;}
int Unit::getMoveRange() {return m_moveRange;}
void Unit::setMovePath(std::stack<Tile *> movePath)
{
m_movePath = movePath;
m_movePathSet = true;
}
void Unit::moveToPathElement()
{
int nX = (int)m_movePath.top()->getPosition().getX();
int nY = (int)m_movePath.top()->getPosition().getY();
int cX = (int)m_position.getX();
int cY = (int)m_position.getY();
if(nX > cX) //next is right
{
m_velocity.setX(2);
}
else if (nX < cX) // next is left
{
m_velocity.setX(-2);
}
else if( nY > cY) //next is down
{
m_velocity.setY(2);
}
else if (nY < cY) //next is up
{
m_velocity.setY(-2);
}
if(nX == cX && nY == cY)
{
m_velocity = Vector2D(0,0);
m_movePath.pop();
}
if(m_movePath.empty())
{
m_velocity.setX(0);
m_velocity.setY(0);
m_movePathSet = false;
}
}