]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Merge audio; handle mouse events
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 9 May 2020 13:42:18 +0000 (09:42 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 9 May 2020 13:42:18 +0000 (09:42 -0400)
Scripts/init.lua
src/components/EventListener.hpp
src/input.cpp [new file with mode: 0644]
src/input.hpp
src/player.cpp

index 572f7224a9a3eef102cb9b25be77f9e34c8b0cba..66fbcb11fe0a4472316acdee10906c771cde454d 100644 (file)
@@ -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)
index 77a004e33ee271114dd61eeaeb78595356e28260..c39b6adb795725c96d109f2265f0d3773ee6b9a6 100644 (file)
@@ -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 (file)
index 0000000..21959c1
--- /dev/null
@@ -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;
+        }
+    }
+}
+
index fa92c393291c717aa9032eb654008951cd300dc9..6180388ed08008381ed02ca97958cfa572f784c4 100644 (file)
@@ -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_
index b9146722e42e841b5daeb22cba9ef309950cf204..f40a1d16f3466899644902b453f4825f8f65e0cc 100644 (file)
@@ -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);
-            });
+                });
         }
     }
 }