-
Notifications
You must be signed in to change notification settings - Fork 0
/
movement_component.inl
88 lines (70 loc) · 2.38 KB
/
movement_component.inl
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
#include "stdafx.h"
//
#include "movement_component.h"
MovementComponent::MovementComponent(sf::Sprite& sprite, float velocity_max,
float acceleration, float deceleration)
: sprite_(sprite),
velocity_max_(velocity_max),
acceleration_(acceleration),
deceleration_(deceleration) {}
MovementComponent::~MovementComponent() {}
const float& MovementComponent::VelocityMax() const { return velocity_max_; }
const sf::Vector2f& MovementComponent::Velocity() const { return velocity_; }
const bool MovementComponent::GetState(const short unsigned state) const {
switch (state) {
case kIdle:
if (velocity_.x == 0.f && velocity_.y == 0.f) return true;
break;
case kMoving:
if (velocity_.x != 0.f || velocity_.y != 0.f) return true;
break;
case kMovingLeft:
if (velocity_.x < 0.f) return true;
break;
case kMovingRight:
if (velocity_.x > 0.f) return true;
break;
case kMovingUp:
if (velocity_.y < 0.f) return true;
break;
case kMovingDown:
if (velocity_.y > 0.f) return true;
break;
}
return false;
}
void MovementComponent::StopVelocity() {
velocity_.x = 0.f;
velocity_.y = 0.f;
}
void MovementComponent::StopVelocityX() {
/* Resets the velocity_ x to 0.*/
velocity_.x = 0.f;
}
void MovementComponent::StopVelocityY() { velocity_.y = 0.f; }
void MovementComponent::Move(const float dir_x, const float dir_y,
const float& dt) {
velocity_.x += acceleration * dir_x * dt;
velocity_.y += acceleration * dir_y * dt;
}
void MovementComponent::Update(const float& dt) {
if (velocity_.x > 0.f) {
if (velocity_.x > velocity_max) velocity_.x = velocity_max;
velocity_.x -= deceleration * dt;
if (velocity_.x < 0.f) velocity_.x = 0.f;
} else if (velocity_.x < 0.f) {
if (velocity_.x < -velocity_max) velocity_.x = -velocity_max;
velocity_.x += deceleration * dt;
if (velocity_.x > 0.f) velocity_.x = 0.f;
}
if (velocity_.y > 0.f) {
if (velocity_.y > velocity_max) velocity_.y = velocity_max;
velocity_.y -= deceleration * dt;
if (velocity_.y < 0.f) velocity_.y = 0.f;
} else if (velocity_.y < 0.f) {
if (velocity_.y < -velocity_max) velocity_.y = -velocity_max;
velocity_.y += deceleration * dt;
if (velocity_.y > 0.f) velocity_.y = 0.f;
}
sprite.move(velocity_ * dt);
}