b = 1.0,
strength = 0.5
},
+ EventListeners = {
+ MousePressed = function(self, x, y, button)
+ self.Velocity.y = 3.0;
+ end
+ },
Idle = function(self)
if (self.visibleTick == 0) then
self.visibleTick = math.random(40, 60)
return *this;
}
- void tryListener(const std::string& name, sol::table& self)
+ template<typename... Args>
+ void tryListener(const std::string& name, sol::table& self, Args... args)
{
if (listeners[name] == sol::type::function)
- listeners[name](self);
+ listeners[name](self, args...);
}
void serialize([[maybe_unused]] cereal::JSONOutputArchive& ar) final {}
--- /dev/null
+/**
+ * @file input.cpp
+ * Handles user input received from SDL.
+ *
+ * Copyright (C) 2020 Clyne Sullivan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "input.hpp"
+
+#include "components/EventListener.hpp"
+#include "components/Script.hpp"
+
+InputSystem::InputSystem() :
+ isMouseDown(false) {}
+
+/**
+ * Prepares the system for running.
+ */
+void InputSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
+ [[maybe_unused]] entityx::EventManager& events) {}
+
+/**
+ * Updates the system by checking for SDL events.
+ */
+void InputSystem::update(entityx::EntityManager& entities,
+ entityx::EventManager& events,
+ [[maybe_unused]] entityx::TimeDelta dt)
+{
+ for (SDL_Event event; SDL_PollEvent(&event);) {
+ switch (event.type) {
+ case SDL_KEYUP:
+ if (auto key = event.key; key.repeat == 0)
+ events.emit<KeyUpEvent>(key.keysym);
+ break;
+ case SDL_KEYDOWN:
+ if (auto key = event.key; key.repeat == 0)
+ events.emit<KeyDownEvent>(key.keysym);
+ break;
+ case SDL_MOUSEBUTTONDOWN:
+ if (!isMouseDown) {
+ isMouseDown = true;
+ entities.each<EventListener>(
+ [&event](entityx::Entity e, EventListener& el) {
+ el.tryListener("MousePressed",
+ e.component<Scripted>()->caller,
+ event.button.x,
+ event.button.y,
+ event.button.button);
+ });
+ }
+ break;
+ case SDL_MOUSEBUTTONUP:
+ if (isMouseDown) {
+ isMouseDown = false;
+ entities.each<EventListener>(
+ [&event](entityx::Entity e, EventListener& el) {
+ el.tryListener("MouseReleased",
+ e.component<Scripted>()->caller,
+ event.button.x,
+ event.button.y,
+ event.button.button);
+ });
+ }
+ break;
+ default:
+ break;
+ }
+ }
+}
+
/**
- * @file window.hpp
+ * @file input.hpp
* Handles user input received from SDL.
*
* Copyright (C) 2019 Clyne Sullivan
class InputSystem : public entityx::System<InputSystem>
{
public:
+ InputSystem();
+
/**
* Prepares the system for running.
*/
- void configure([[maybe_unused]] entityx::EntityManager& entities,
- [[maybe_unused]] entityx::EventManager& events) final {}
+ void configure(entityx::EntityManager& entities,
+ entityx::EventManager& events) final;
/**
* Updates the system by checking for SDL events.
*/
- void update([[maybe_unused]] entityx::EntityManager& entities,
- [[maybe_unused]] entityx::EventManager& events,
- [[maybe_unused]] entityx::TimeDelta dt) final
- {
- for (SDL_Event event; SDL_PollEvent(&event);) {
- switch (event.type) {
- case SDL_KEYUP:
- if (auto key = event.key; key.repeat == 0)
- events.emit<KeyUpEvent>(key.keysym);
- break;
- case SDL_KEYDOWN:
- if (auto key = event.key; key.repeat == 0)
- events.emit<KeyDownEvent>(key.keysym);
- break;
- default:
- break;
- }
- }
- }
+ void update(entityx::EntityManager& entities,
+ entityx::EventManager& events,
+ entityx::TimeDelta dt) final;
+
+private:
+ bool isMouseDown;
};
#endif // SYSTEM_INPUT_HPP_
{
if (player.valid()) {
if (kue.sym == SDLK_a) {
- entities.each<EventListener>([&]([[maybe_unused]] entityx::Entity e,
- EventListener& el)
- {
+ entities.each<EventListener>(
+ [](entityx::Entity e, EventListener& el) {
el.tryListener("MoveLeftPressed",
e.component<Scripted>()->caller);
- });
+ });
} else if (kue.sym == SDLK_d) {
- entities.each<EventListener>([&]([[maybe_unused]] entityx::Entity e,
- EventListener& el)
- {
+ entities.each<EventListener>(
+ [](entityx::Entity e, EventListener& el) {
el.tryListener("MoveRightPressed",
e.component<Scripted>()->caller);
- });
+ });
} else if (kue.sym == SDLK_SPACE) {
- entities.each<EventListener>([&]([[maybe_unused]] entityx::Entity e,
- EventListener& el)
- {
+ entities.each<EventListener>(
+ [](entityx::Entity e, EventListener& el) {
el.tryListener("JumpKeyPressed",
e.component<Scripted>()->caller);
- });
+ });
}
}
}
{
if (player.valid()) {
if (kue.sym == SDLK_a) {
- entities.each<EventListener>([&]([[maybe_unused]] entityx::Entity e,
- EventListener& el)
- {
+ entities.each<EventListener>(
+ [](entityx::Entity e, EventListener& el) {
el.tryListener("MoveLeftReleased",
e.component<Scripted>()->caller);
- });
+ });
} else if (kue.sym == SDLK_d) {
- entities.each<EventListener>([&]([[maybe_unused]] entityx::Entity e,
- EventListener& el)
- {
+ entities.each<EventListener>(
+ [](entityx::Entity e, EventListener& el) {
el.tryListener("MoveRightReleased",
e.component<Scripted>()->caller);
- });
+ });
} else if (kue.sym == SDLK_SPACE) {
- entities.each<EventListener>([&]([[maybe_unused]] entityx::Entity e,
- EventListener& el)
- {
+ entities.each<EventListener>(
+ [](entityx::Entity e, EventListener& el) {
el.tryListener("JumpKeyReleased",
e.component<Scripted>()->caller);
- });
+ });
}
}
}