-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovementComponent.cpp
More file actions
144 lines (131 loc) · 2.92 KB
/
Copy pathMovementComponent.cpp
File metadata and controls
144 lines (131 loc) · 2.92 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
137
138
139
140
141
142
143
144
#include "MovementComponent.h"
MovementComponent::MovementComponent(sf::Sprite& sprite,sf::RectangleShape *hitbox,
float maxVelocity, float acceleration, float deceleration)
: sprite(sprite), hitbox(hitbox),
maxVelocity(maxVelocity),acceleration(acceleration), deceleration(deceleration)
{
this->onfloor = false;
this->jump = false;
this->crouch = false;
this->jumpHeight = 0;
}
MovementComponent::~MovementComponent()
{
}
//Accessor
const sf::Vector2f& MovementComponent::getVeclocity() const
{
return this->velocity;
}
const float& MovementComponent::getMaxVeclocity() const
{
return this->maxVelocity;
}
void MovementComponent::setveclocity(sf::Vector2f veclocity)
{
this->velocity = veclocity;
}
const bool MovementComponent::getState(const short unsigned state) const
{
switch (state)
{
case IDLE:
if (this->velocity.x == 0.f && this->velocity.y == 0.f) {
return true;
}
break;
case MOVING_LEFT:
if (this->velocity.x < 0.f) {
return true;
}
break;
case MOVING_RIGHT:
if (this->velocity.x > 0.f) {
return true;
}
break;
case JUMP:
if (this->velocity.y < 0.f) {
return true;
}
break;
case DROP:
if (this->velocity.y > 0.f) {
return true;
}
break;
case CROUCH:
if (this->crouch) {
return true;
}
break;
case MID_AIR:
if (!this->onfloor) {
return true;
}
break;
}
return false;
}
void MovementComponent::push(const float x, const float y)
{
this->velocity.x += this->acceleration * x;
this->velocity.y = y * 3;
}
void MovementComponent::move(const float x, const float y,const float deltaTime)
{
//Acceleration
this->velocity.x += this->acceleration*x;
if (y < 0) this->jump = true;
else if (y > 0) {
this->crouch = true;
}
this->jumpHeight = y;
}
//function
void MovementComponent::Update(const float& deltaTime)
{
//gravity (acceleration * scale)
this->velocity.y += 9.80665f*3;
//floor
if (this->hitbox->getPosition().y + this->hitbox->getGlobalBounds().height >= 705.f) {
this->velocity.y = 0;
}
//check if on floor
if (this->velocity.y != 0) this->onfloor = false;
else this->onfloor = true;
//crouch
if (!this->onfloor || velocity.x) this->crouch = false;
//slide
if (this->crouch) {
}
//jump
if (!this->onfloor) this->jump = false;
if (this->jump) {
this->jump = false;
this->crouch = false;
this->velocity.y = jumpHeight* 3;
}
//X Positive
if (this->velocity.x > 0.f) {
//Max Velocity Check
if (this->velocity.x > this->maxVelocity)
this->velocity.x = maxVelocity;
//Deceleration
if(this->onfloor)
this->velocity.x -= deceleration;
if (this->velocity.x < 0.f) this->velocity.x = 0;
}
//X Negative
else if (this->velocity.x < 0.f) {
//Max Velocity Check
if (this->velocity.x < -this->maxVelocity)
this->velocity.x = -maxVelocity;
//Deceleration
if (this->onfloor)
this->velocity.x += deceleration;
if (this->velocity.x > 0.f) this->velocity.x = 0;
}
//Final move
this->sprite.move(this->velocity * deltaTime);
}