]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
input bindings; mouse clear dialog box
authorClyne Sullivan <clyne@bitgloo.com>
Wed, 16 Nov 2022 17:26:55 +0000 (12:26 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Wed, 16 Nov 2022 17:26:55 +0000 (12:26 -0500)
Scripts/init.lua
src/engine.cpp
src/events/input.hpp [new file with mode: 0644]
src/events/render.hpp
src/input.cpp
src/input.hpp
src/player.cpp
src/player.hpp
src/ui.cpp
src/ui.hpp

index 370fc0e9b6fa8cc3062a8894c2c80a3a68d59841..fde6c8642a64c9ff32c6490e931183d93ba7cd85 100644 (file)
@@ -6,36 +6,36 @@ player = {
     EventListeners = {
         MoveLeftPressed = function(self)
             self.Velocity.x = self.Velocity.x - 3.0
-            --self.Velocity.y = self.Velocity.y - 1.0
-            self.Render.flipx = true;
-            game.dialogClear()
+            self.Render.flipx = true
         end,
         MoveLeftReleased = function(self)
             self.Velocity.x = self.Velocity.x + 3.0
-            --self.Velocity.y = self.Velocity.y + 1.0
         end,
         MoveRightPressed = function(self)
             self.Velocity.x = self.Velocity.x + 3.0
-            --self.Velocity.y = self.Velocity.y + 1.0
-            self.Render.flipx = false;
+            self.Render.flipx = false
         end,
         MoveRightReleased = function(self)
             self.Velocity.x = self.Velocity.x - 3.0
-            --self.Velocity.y = self.Velocity.y - 1.0
         end,
         JumpKeyPressed = function(self)
             if self.Physics.standing == true then
                 game.play(self.Position, self.Audio)
-                --self.Velocity.y = self.Velocity.y + 9
             end
         end,
         JumpKeyReleased = function(self)
-            game.dialog(30, 150, 400, 100)
+            game.dialog(30, 50, 400, 100)
             game.puts("dialog", 36, 52, "Hey. Hag?")
+        end,
+        MousePressed = function(self, mx, my)
+            if mx > 30 and mx < 430 and my > 50 and my < 150 then
+                game.dialogClear()
+            end
         end
     },
     Position = {
-        15.0, 20.0
+        x = 15.0,
+        y = 20.0
     },
     Velocity = {
         x = 0.0,
index 40f6e62e7f328df18266467696e38ab58ea1e6f7..25e3cd30d560b709dd9b845add1d5f71d3b5bf4c 100644 (file)
@@ -50,7 +50,7 @@ int Engine::init(void)
 
     systems.add<GameRunSystem>();
     systems.add<InputSystem>();
-    systems.add<PlayerSystem>(entities);
+    systems.add<PlayerSystem>();
     systems.add<WorldSystem>();
     systems.add<RenderSystem>();
     systems.add<ScriptSystem>(entities, *(systems.system<WorldSystem>().get()));
@@ -180,8 +180,7 @@ void Engine::run(void)
                 put("default", 0, 0, "fps: "s + std::to_string(fps));
 
             entities.each<Player, Position>(
-                [this](entityx::Entity, Player &p, Position &pos){
-                (void)p;
+                [this](entityx::Entity, Player&, Position &pos){
                 std::string pr = "pos: " + std::to_string(pos.x) 
                                + "," + std::to_string(pos.y);
                 systems.system<TextSystem>()->put("default", 0, 24, pr);
diff --git a/src/events/input.hpp b/src/events/input.hpp
new file mode 100644 (file)
index 0000000..3aafd1a
--- /dev/null
@@ -0,0 +1,67 @@
+/**
+ * Copyright (C) 2022 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/>.
+ */
+
+#ifndef EVENTS_INPUT_HPP_
+#define EVENTS_INPUT_HPP_
+
+#include <SDL2/SDL.h>
+
+/**
+ * @class KeyUpEvent
+ * Stores info regarding key releases.
+ */
+struct KeyUpEvent
+{
+    SDL_Keycode sym;
+    Uint16 mod;
+
+    explicit KeyUpEvent(const SDL_Keysym& keysym) :
+        sym(keysym.sym), mod(keysym.mod) {}
+};
+
+/**
+ * @class KeyDownEvent
+ * Stores info regarding key presses.
+ */
+struct KeyDownEvent {
+    SDL_Keycode sym;
+    Uint16 mod;
+
+    explicit KeyDownEvent(const SDL_Keysym& keysym) :
+        sym(keysym.sym), mod(keysym.mod) {}
+};
+
+struct MouseUpEvent {
+    Uint8 button;
+    Sint32 x;
+    Sint32 y;
+
+    explicit MouseUpEvent(const SDL_MouseButtonEvent& mbe) :
+        button(mbe.button), x(mbe.x), y(mbe.y) {}
+};
+
+struct MouseDownEvent {
+    Uint8 button;
+    Sint32 x;
+    Sint32 y;
+
+    explicit MouseDownEvent(const SDL_MouseButtonEvent& mbe) :
+        button(mbe.button), x(mbe.x), y(mbe.y) {}
+};
+
+#endif // EVENTS_INPUT_HPP_
+
index 07f5d64e36cb63a4126e9ab53f774e595c5d1721..ebc7588071fad26b93930d0629b1c9028df0c2a5 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <GL/glew.h>
 
+// TODO this is only for UI renders...
 struct NewRenderEvent
 {
     GLuint vbo;
index 21959c19df9695aa4d1bded738d337787d5f1f0b..ed8b75fc66c57c92a26335462d69ed7a1d524a50 100644 (file)
@@ -2,7 +2,7 @@
  * @file input.cpp
  * Handles user input received from SDL.
  *
- * Copyright (C) 2020 Clyne Sullivan
+ * Copyright (C) 2022 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
@@ -30,53 +30,75 @@ InputSystem::InputSystem() :
  * Prepares the system for running.
  */
 void InputSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
-                            [[maybe_unused]] entityx::EventManager& events) {}
+                            [[maybe_unused]] entityx::EventManager& events)
+{
+    bindings.emplace(Binding {SDLK_a, 0}, "MoveLeft");
+    bindings.emplace(Binding {SDLK_d, 0}, "MoveRight");
+    bindings.emplace(Binding {SDLK_SPACE, 0}, "JumpKey");
+}
 
 /**
  * Updates the system by checking for SDL events.
  */
 void InputSystem::update(entityx::EntityManager& entities,
             entityx::EventManager& events,
-            [[maybe_unused]] entityx::TimeDelta dt)
+            entityx::TimeDelta)
 {
+    std::string listener;
+
     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:
+        std::function tryListener =
+            [&listener](entityx::Entity, EventListener& el, Scripted& s) {
+                el.tryListener(listener, s.caller);
+            };
+
+        if (event.type == SDL_KEYUP) {
+            if (event.key.repeat == 0) {
+                auto b = bindings.find({event.key.keysym.sym, event.key.keysym.mod});
+
+                if (b != bindings.end())
+                    listener = b->second + "Released";
+                else
+                    events.emit<KeyUpEvent>(event.key.keysym);
+            }
+        } else if (event.type == SDL_KEYDOWN) {
+            if (event.key.repeat == 0) {
+                auto b = bindings.find({event.key.keysym.sym, event.key.keysym.mod});
+
+                if (b != bindings.end())
+                    listener = b->second + "Pressed";
+                else
+                    events.emit<KeyDownEvent>(event.key.keysym);
+            }
+        } else if (event.type == 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);
-                    });
+                listener = "MousePressed";
+
+                tryListener =
+                    [&listener, m = event.button]
+                    (entityx::Entity, EventListener& el, Scripted& s) {
+                        el.tryListener(listener, s.caller,
+                                       m.x, m.y, m.button);
+                    };
             }
-            break;
-        case SDL_MOUSEBUTTONUP:
+        } else if (event.type == 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);
-                    });
+                listener = "MouseReleased";
+
+                tryListener =
+                    [&listener, m = event.button]
+                    (entityx::Entity, EventListener& el, Scripted& s) {
+                        el.tryListener(listener, s.caller,
+                                       m.x, m.y, m.button);
+                    };
             }
-            break;
-        default:
-            break;
+        }
+
+        if (!listener.empty()) {
+            entities.each<EventListener, Scripted>(tryListener);
+            listener.clear();
         }
     }
 }
index 8f7aa8f518270f31a3088fc6dc6e51b8f8760ad1..3e666b284798c68bb876c0946d59130b6d2009e6 100644 (file)
@@ -2,7 +2,7 @@
  * @file input.hpp
  * Handles user input received from SDL.
  *
- * Copyright (C) 2019 Clyne Sullivan
+ * Copyright (C) 2022 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
 #ifndef SYSTEM_INPUT_HPP_
 #define SYSTEM_INPUT_HPP_
 
-#include <entityx/entityx.h>
-#include <SDL2/SDL.h>
-
-/**
- * @class KeyUpEvent
- * Stores info regarding key releases.
- */
-struct KeyUpEvent
-{
-    SDL_Keycode sym;
-    Uint16 mod;
+#include "events/input.hpp"
 
-    explicit KeyUpEvent(const SDL_Keysym& keysym) :
-        sym(keysym.sym), mod(keysym.mod) {}
-};
-
-/**
- * @class KeyDownEvent
- * Stores info regarding key presses.
- */
-struct KeyDownEvent {
-    SDL_Keycode sym;
-    Uint16 mod;
-
-    explicit KeyDownEvent(const SDL_Keysym& keysym) :
-        sym(keysym.sym), mod(keysym.mod) {}
-};
-
-struct MouseUpEvent {
-    Uint8 button;
-    Sint32 x;
-    Sint32 y;
-
-    explicit MouseUpEvent(const SDL_MouseButtonEvent& mbe) :
-        button(mbe.button), x(mbe.x), y(mbe.y) {}
-};
-
-struct MouseDownEvent {
-    Uint8 button;
-    Sint32 x;
-    Sint32 y;
+#include <entityx/entityx.h>
 
-    explicit MouseDownEvent(const SDL_MouseButtonEvent& mbe) :
-        button(mbe.button), x(mbe.x), y(mbe.y) {}
-};
+#include <map>
 
 /**
  * @class InputSystem
@@ -90,6 +50,18 @@ public:
                 entityx::TimeDelta dt) final;
 
 private:
+    // TODO add 'mod' to comparison check
+    // TODO allow binding configuration
+    struct Binding {
+        SDL_Keycode sym = 0;
+        Uint16 mod = 0;
+
+        auto operator<=>(const Binding& other) const {
+            return sym <=> other.sym;
+        }
+    };
+
+    std::map<Binding, std::string> bindings;
     bool isMouseDown;
 };
 
index f40a1d16f3466899644902b453f4825f8f65e0cc..91e505cdb143ffadc44c42c99fe1f00a4a616a0c 100644 (file)
@@ -2,7 +2,7 @@
  * @file player.cpp
  * Manages player input.
  *
- * Copyright (C) 2019 Clyne Sullivan
+ * Copyright (C) 2022 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
 #include "components/Script.hpp"
 #include "components/Velocity.hpp"
 
-void PlayerSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
+void PlayerSystem::configure(entityx::EntityManager&,
                              entityx::EventManager& events)
 {
     events.subscribe<entityx::ComponentAddedEvent<Player>>(*this);
     events.subscribe<entityx::ComponentRemovedEvent<Player>>(*this);
-    events.subscribe<KeyUpEvent>(*this);
-    events.subscribe<KeyDownEvent>(*this);
 }
 
-void PlayerSystem::update([[maybe_unused]] entityx::EntityManager& entites,
-                          [[maybe_unused]] entityx::EventManager& events,
-                          [[maybe_unused]] entityx::TimeDelta dt)
+void PlayerSystem::update(entityx::EntityManager&,
+                          entityx::EventManager&,
+                          entityx::TimeDelta)
 {
 }
 
@@ -50,53 +48,3 @@ void PlayerSystem::receive(const entityx::ComponentRemovedEvent<Player>& cre)
         player.invalidate();
 }
 
-void PlayerSystem::receive(const KeyDownEvent& kue)
-{
-    if (player.valid()) {
-        if (kue.sym == SDLK_a) {
-            entities.each<EventListener>(
-                [](entityx::Entity e, EventListener& el) {
-                    el.tryListener("MoveLeftPressed",
-                                   e.component<Scripted>()->caller);
-                });
-        } else if (kue.sym == SDLK_d) {
-            entities.each<EventListener>(
-                [](entityx::Entity e, EventListener& el) {
-                    el.tryListener("MoveRightPressed",
-                                   e.component<Scripted>()->caller);
-                });
-        } else if (kue.sym == SDLK_SPACE) {
-            entities.each<EventListener>(
-                [](entityx::Entity e, EventListener& el) {
-                    el.tryListener("JumpKeyPressed",
-                                   e.component<Scripted>()->caller);
-                });
-        }
-    }
-}
-
-void PlayerSystem::receive(const KeyUpEvent& kue)
-{
-    if (player.valid()) {
-        if (kue.sym == SDLK_a) {
-            entities.each<EventListener>(
-                [](entityx::Entity e, EventListener& el) {
-                    el.tryListener("MoveLeftReleased",
-                                   e.component<Scripted>()->caller);
-                });
-        } else if (kue.sym == SDLK_d) {
-            entities.each<EventListener>(
-                [](entityx::Entity e, EventListener& el) {
-                    el.tryListener("MoveRightReleased",
-                                   e.component<Scripted>()->caller);
-                });
-        } else if (kue.sym == SDLK_SPACE) {
-            entities.each<EventListener>(
-                [](entityx::Entity e, EventListener& el) {
-                    el.tryListener("JumpKeyReleased",
-                                   e.component<Scripted>()->caller);
-                });
-        }
-    }
-}
-
index d9a9237011acf316d433cde1f1f1e890981f22ce..1fda62b87c47ca187a623b1e2b79bb0b95a67c50 100644 (file)
@@ -2,7 +2,7 @@
  * @file player.hpp
  * Manages player input.
  *
- * Copyright (C) 2019 Clyne Sullivan
+ * Copyright (C) 2022 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
 /**
  * @class PlayerSystem
  * Controls player entity movement.
+ * TODO is this system necessary?
  */
 class PlayerSystem : public entityx::System<PlayerSystem>,
                      public entityx::Receiver<PlayerSystem>
 {
 private:
-    /**
-     * Defines player's horizontal movement velocity.
-     */
-    constexpr static double GROUND_VELOCITY = 100;
-
-    entityx::EntityManager& entities;
     entityx::Entity player;
 
 public:
-    PlayerSystem(entityx::EntityManager& _entities) :
-        entities(_entities) {}
-
     /**
      * Prepares the system for running.
      */
@@ -69,16 +61,6 @@ public:
      * Invalidates the system's player entity (assume player is gone).
      */
     void receive(const entityx::ComponentRemovedEvent<Player>& cre);
-
-    /**
-     * Applies velocity based on key press.
-     */
-    void receive(const KeyDownEvent& kue);
-
-    /**
-     * Removes applied velocity
-     */
-    void receive(const KeyUpEvent& kue);
 };
 
 #endif // SYSTEM_PLAYER_HPP_
index 009d6ff7317ccd4a9f90c28b94e2ed6a82a3e39b..19b939d896fa127ec0cc730f584a822cedf971f4 100644 (file)
@@ -1,3 +1,22 @@
+/**
+ * @file ui.cpp
+ *
+ * Copyright (C) 2022 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 <http://www.gnu.org/licenses/>.
+ */
+
 #include "ui.hpp"
 
 #include "text.hpp"
@@ -16,7 +35,8 @@ void UISystem::configure(entityx::EntityManager&, entityx::EventManager &events)
 void UISystem::createDialogBox(float x, float y, float w, float h)
 {
     // Queue for generation in the main thread.
-    m_boxes.emplace_back(0, x, -y, w, h);
+    // TODO Should y inversion be hidden in the render system?
+    m_boxes.emplace_back(0, x, -y - h, w, h);
 }
 
 void UISystem::update(entityx::EntityManager&,
index e6e1c099b3cfd607b28cd3e03dbf0dc1eb545b91..6dadef0f6c4688bda8d57569d62483761c2ea846 100644 (file)
@@ -1,7 +1,6 @@
 /**
  * @file ui.hpp
  *
- *
  * Copyright (C) 2022 Clyne Sullivan
  *
  * This program is free software: you can redistribute it and/or modify