-
Notifications
You must be signed in to change notification settings - Fork 1
/
C_Direction.cpp
73 lines (63 loc) · 1.22 KB
/
C_Direction.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
#include "C_Direction.hpp"
#include "Object.hpp"
C_Direction::C_Direction(Object* owner) :
Component(owner), currentDir(FacingDirection::Down) { }
void C_Direction::Awake()
{
velocity = owner->GetComponent<C_Velocity>();
}
FacingDirection C_Direction::Get()
{
const sf::Vector2f& currentVel = velocity->Get();
if (currentVel.x != 0.f || currentVel.y != 0.f)
{
float velXAbs = fabs(currentVel.x);
float velYAbs = fabs(currentVel.y);
if (velXAbs > velYAbs)
{
if (currentVel.x < 0)
{
currentDir = FacingDirection::Left;
}
else
{
currentDir = FacingDirection::Right;
}
}
else
{
if (currentVel.y < 0)
{
currentDir = FacingDirection::Up;
}
else
{
currentDir = FacingDirection::Down;
}
}
}
return currentDir;
}
//TODO: Create a static map with direction as key and heading as value
sf::Vector2i C_Direction::GetHeading()
{
FacingDirection direction = Get();
sf::Vector2i heading;
if (direction == FacingDirection::Right)
{
heading.x = 1;
}
else if (direction == FacingDirection::Left)
{
heading.x = -1;
}
else if (direction == FacingDirection::Up)
{
heading.y = -1;
}
else if (direction == FacingDirection::Down)
{
heading.y = 1;
}
return heading;
}