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
|
#include <player.hpp>
#include <brice.hpp>
#include <ui.hpp>
#include <gametime.hpp>
#include <world.hpp>
#include <particle.hpp>
void PlayerSystem::create(void)
{
player = game::entities.create();
player.assign<Position>(0.0f, 100.0f);
player.assign<Direction>(0.0f, 0.0f);
// The new g value is a multiplier for the gravity constant. This allows for air resistance simulation.
//player.assign<Physics>(-0.001f);
player.assign<Physics>(1);
player.assign<Visible>(-0.2f);
auto sprite = player.assign<Sprite>();
sprite->addSpriteSegment(SpriteData("assets/cat.png",
vec2(0, 0)),
vec2(0, 0));
vec2 dim = player.component<Sprite>().get()->getSpriteSize();
float cdat[2] = {dim.x, dim.y};
player.assign<Solid>(cdat[0], cdat[1]);
}
void PlayerSystem::configure(entityx::EventManager &ev)
{
ev.subscribe<KeyUpEvent>(*this);
ev.subscribe<KeyDownEvent>(*this);
}
void PlayerSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) {
(void)en;
(void)ev;
(void)dt;
auto& vel = *player.component<Direction>().get();
if (moveLeft & !moveRight)
vel.x = -PLAYER_SPEED_CONSTANT;
else if (!moveLeft & moveRight)
vel.x = PLAYER_SPEED_CONSTANT;
else
vel.x = 0;
vel.x *= speed;
if (std::stoi(game::getValue("Slow")) == 1)
vel.x /= 2.0f;
}
void PlayerSystem::receive(const KeyUpEvent &kue)
{
auto kc = kue.keycode;
if (kc == getControl(1)) {
moveLeft = false;
} else if (kc == getControl(2)) {
moveRight = false;
} else if (kc == getControl(3) || kc == getControl(4)) {
speed = 1.0f;
} else if (kc == getControl(5)) {
/*if (p->inv->invHover) {
p->inv->invHover = false;
} else {
if (!p->inv->selected)
p->inv->invOpening ^= true;
else
p->inv->selected = false;
p->inv->mouseSel = false;
}*/
}
}
void PlayerSystem::receive(const KeyDownEvent &kde)
{
static auto& worldSystem = *game::engine.getSystem<WorldSystem>();
auto kc = kde.keycode;
auto& loc = *player.component<Position>().get();
auto& vel = *player.component<Direction>().get();
if ((kc == SDLK_SPACE) && game::canJump && ((vel.y > -0.01) & (vel.y < 0.01))) {
loc.y += HLINES(2);
vel.y = .4;
vel.grounded = false;
} else if (!ui::dialogBoxExists || ui::dialogPassive) {
if (kc == getControl(0)) {
if (!ui::fadeIntensity)
worldSystem.goWorldPortal(loc);
} else if (kc == getControl(1)) {
if (!ui::fadeEnable) {
moveLeft = true;
moveRight = false;
worldSystem.goWorldLeft(loc);
}
} else if (kc == getControl(2)) {
if (!ui::fadeEnable) {
moveLeft = false;
moveRight = true;
worldSystem.goWorldRight(loc, *player.component<Solid>().get());
}
} else if (kc == getControl(3)) {
if (game::canSprint)
speed = 2.0f;
game::engine.getSystem<ParticleSystem>()->addMultiple(10, ParticleType::SmallBlast,
[&](){ return vec2(loc.x, loc.y); }, 500, 7);
} else if (kc == getControl(4)) {
speed = .5;
} else if (kc == getControl(5)) {
//static int heyOhLetsGo = 0;
//edown = true;
// start hover counter?
//if (!heyOhLetsGo) {
// heyOhLetsGo = game::time::getTickCount();
// p->inv->mouseSel = false;
//}
// run hover thing
//if (game::time::getTickCount() - heyOhLetsGo >= 2 && !(p->inv->invOpen) && !(p->inv->selected))
// p->inv->invHover = true;
}
} else if (kc == SDLK_DELETE) {
game::endGame();
} else if (kc == SDLK_t) {
game::time::tick(50);
}
}
vec2 PlayerSystem::getPosition(void) const
{
auto& loc = *game::entities.component<Position>(player.id()).get();
return vec2(loc.x, loc.y);
}
|