aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2020-05-09 09:42:18 -0400
committerClyne Sullivan <clyne@bitgloo.com>2020-05-09 09:42:18 -0400
commitda0913771538fd9b1ca538615fd9aa0388608466 (patch)
tree18b23720b0a9c24e05a8f3eceb6b03b0b858fcb2
parentf461087223a80cd06619517e355690654f406d63 (diff)
Merge audio; handle mouse events
-rw-r--r--Scripts/init.lua5
-rw-r--r--src/components/EventListener.hpp5
-rw-r--r--src/input.cpp83
-rw-r--r--src/input.hpp33
-rw-r--r--src/player.cpp42
5 files changed, 120 insertions, 48 deletions
diff --git a/Scripts/init.lua b/Scripts/init.lua
index 572f722..66fbcb1 100644
--- a/Scripts/init.lua
+++ b/Scripts/init.lua
@@ -111,6 +111,11 @@ npc = {
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)
diff --git a/src/components/EventListener.hpp b/src/components/EventListener.hpp
index 77a004e..c39b6ad 100644
--- a/src/components/EventListener.hpp
+++ b/src/components/EventListener.hpp
@@ -42,10 +42,11 @@ public:
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 {}
diff --git a/src/input.cpp b/src/input.cpp
new file mode 100644
index 0000000..21959c1
--- /dev/null
+++ b/src/input.cpp
@@ -0,0 +1,83 @@
+/**
+ * @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;
+ }
+ }
+}
+
diff --git a/src/input.hpp b/src/input.hpp
index fa92c39..6180388 100644
--- a/src/input.hpp
+++ b/src/input.hpp
@@ -1,5 +1,5 @@
/**
- * @file window.hpp
+ * @file input.hpp
* Handles user input received from SDL.
*
* Copyright (C) 2019 Clyne Sullivan
@@ -56,34 +56,23 @@ struct KeyDownEvent {
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_
diff --git a/src/player.cpp b/src/player.cpp
index b914672..f40a1d1 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -54,26 +54,23 @@ void PlayerSystem::receive(const KeyDownEvent& kue)
{
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);
- });
+ });
}
}
}
@@ -82,26 +79,23 @@ void PlayerSystem::receive(const KeyUpEvent& kue)
{
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);
- });
+ });
}
}
}