-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboardInputComponent.h
53 lines (44 loc) · 1.73 KB
/
KeyboardInputComponent.h
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
#pragma once
#include "BaseComponent.h"
#include "Entity.h"
class KeyboardInputComponent : public BaseComponent
{
private:
void ApplyFriction()
{
owner->setAccel(sf::Vector2f(owner->getAccel().x * owner->getRateDecel().x, owner->getAccel().y * owner->getRateDecel().y));
owner->setVel(sf::Vector2f(owner->getVel().x * owner->getRateDecel().x, owner->getVel().y * owner->getRateDecel().y));
}
void ApplyKeypress()
{
for (auto& i : *owner->getKeybinds())
{
if (sf::Keyboard::isKeyPressed(i.second))
{
if (i.first == "UP") owner->setAccel(owner->getAccel() + sf::Vector2f(0.0f, -owner->getRateAccel().y));
else if (i.first == "DOWN") owner->setAccel(owner->getAccel() + sf::Vector2f(0.0f, owner->getRateAccel().y));
else if (i.first == "LEFT") owner->setAccel(owner->getAccel() + sf::Vector2f(-owner->getRateAccel().x, 0.0f));
else if (i.first == "RIGHT") owner->setAccel(owner->getAccel() + sf::Vector2f(owner->getRateAccel().x, 0.0f));
}
}
}
void CapSpeed()
{
if (owner->getVel().x > owner->getMaxVel().x) owner->setVel(sf::Vector2f(owner->getMaxVel().x, owner->getVel().y));
else if (owner->getVel().x < -owner->getMaxVel().x) owner->setVel(sf::Vector2f(-owner->getMaxVel().x, owner->getVel().y));
if (owner->getVel().y > owner->getMaxVel().y) owner->setVel(sf::Vector2f(owner->getVel().x, owner->getMaxVel().y));
else if (owner->getVel().y < -owner->getMaxVel().y) owner->setVel(sf::Vector2f(owner->getVel().x, -owner->getMaxVel().y));
}
public:
KeyboardInputComponent(Entity* o) : BaseComponent(o) { }
virtual ~KeyboardInputComponent() {}
virtual void Update()
{
ApplyFriction();
ApplyKeypress();
CapSpeed();
}
virtual void Render()
{
}
};