-
Notifications
You must be signed in to change notification settings - Fork 3
/
usersystem.cpp
158 lines (120 loc) · 3.81 KB
/
usersystem.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "engine.hpp"
#include "usersystem.hpp"
#include "components\portal.hpp"
#include "components\body.hpp"
#include "components\colorable.hpp"
#include "components\renderable.hpp"
#include "components\lerpable.hpp"
#include "components\holdable.hpp"
#include "components\teleportable.hpp"
#include "utils.hpp"
using namespace GameComponent;
UserSystem::UserSystem()
{
}
void UserSystem::onInit()
{
eventDispatcher->sink<GameEvent::SpawnBall>().connect(this);
eventDispatcher->sink<GameEvent::SpawnPortal>().connect(this);
eventDispatcher->sink<GameEvent::FreeArea>().connect(this);
eventDispatcher->sink<GameEvent::EscapeFromArea>().connect(this);
}
sf::Vector2f UserSystem::generateRandomDirection()
{
sf::Vector2f dir((std::rand() % 200) - 100, (std::rand() % 200) - 100);
return utils::normalize(dir);
}
sf::FloatRect UserSystem::generateRect(const sf::Vector2f& position)
{
const float size = 200;
sf::FloatRect rect(position, sf::Vector2f(size, size));
rect.top -= size * 0.5f;
rect.left -= size * 0.5f;
return rect;
}
void UserSystem::receive(const GameEvent::SpawnBall &event)
{
auto wsize = engine->getWindow().getSize();
auto position = event.position;
auto direction = event.direction;
if (event.position == sf::Vector2f())
{
position = sf::Vector2f(std::rand() % wsize.x, std::rand() % wsize.y);
direction = generateRandomDirection();
}
float size = Body::MIN_START_SIZE + static_cast<float>(std::rand() % 2);
auto entity = registry->create();
registry->assign<Body>(entity, position, direction * static_cast<float>(Body::SPEED), size);
registry->assign<Colorable>(entity);
registry->assign<Teleportable>(entity);
registry->assign<Renderable>(entity, position);
}
void UserSystem::receive(const GameEvent::SpawnPortal &event)
{
static int e_link = -1; // todo remove this dirty flag
auto entity = registry->create();
registry->assign<Body>(entity, event.position, sf::Vector2f(), (float)Body::PORTAL_SIZE);
registry->assign<Portal>(entity);
registry->assign<Colorable>(entity);
registry->assign<Renderable>(entity, event.position);
auto& c = registry->get<Colorable>(entity);
c.color = sf::Color(20, 20, 40);
c.decay = sf::seconds(65536);
if (e_link != -1)
{
registry->get<Portal>(entity).link = e_link;
registry->get<Portal>(e_link).link = entity;
e_link = -1;
}
else
e_link = entity;
}
void UserSystem::receive(const GameEvent::FreeArea &event)
{
auto border = generateRect(event.position);
registry->view<Body, Colorable>().each([&](auto entity, Body &body, Colorable& colorable)
{
if (!registry->has<Portal>(entity) && border.contains(body.position))
if (event.collapse)
{
colorable.color = sf::Color(123, 212, 80);
colorable.decay = sf::seconds(3);
colorable.timer = sf::Time::Zero;
registry->assign<Lerpable>(entity, event.position);
}
else
registry->destroy(entity);
});
}
void UserSystem::receive(const GameEvent::EscapeFromArea &event)
{
static bool ready = true; // todo again remove this dirty flag
if (!event.hold)
{
auto view = registry->view<Body, Colorable, Holdable>();
for (auto entity : view)
{
auto &body = view.get<Body>(entity);
auto &colorable = view.get<Colorable>(entity);
body.direction = utils::normalize(body.position - event.position) * static_cast<float>(Body::SPEED) * 2.f;
colorable.decay = sf::seconds(3);
colorable.timer = sf::Time::Zero;
}
registry->reset<Holdable>();
ready = true;
}
else if (ready == true)
{
auto border = generateRect(event.position);
registry->view<Body, Colorable>().each([&](auto entity, Body &body, Colorable& colorable)
{
if (!registry->has<Portal>(entity) && border.contains(body.position))
{
colorable.color = sf::Color(194, 177, 128);
colorable.decay = sf::seconds(2048);
registry->assign<Holdable>(entity);
}
});
ready = false;
}
}