aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2022-11-16 12:26:55 -0500
committerClyne Sullivan <clyne@bitgloo.com>2022-11-16 12:26:55 -0500
commit0c535a1526d725fbd242c5ce348a7f2252d1fd34 (patch)
treeec32e08f0e70a1058bf99204ef28fd240b426d4a
parent034c802edd39be537a626c1961272a6137b5980e (diff)
input bindings; mouse clear dialog box
-rw-r--r--Scripts/init.lua20
-rw-r--r--src/engine.cpp5
-rw-r--r--src/events/input.hpp67
-rw-r--r--src/events/render.hpp1
-rw-r--r--src/input.cpp90
-rw-r--r--src/input.hpp60
-rw-r--r--src/player.cpp62
-rw-r--r--src/player.hpp22
-rw-r--r--src/ui.cpp22
-rw-r--r--src/ui.hpp1
10 files changed, 180 insertions, 170 deletions
diff --git a/Scripts/init.lua b/Scripts/init.lua
index 370fc0e..fde6c86 100644
--- a/Scripts/init.lua
+++ b/Scripts/init.lua
@@ -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,
diff --git a/src/engine.cpp b/src/engine.cpp
index 40f6e62..25e3cd3 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -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
index 0000000..3aafd1a
--- /dev/null
+++ b/src/events/input.hpp
@@ -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_
+
diff --git a/src/events/render.hpp b/src/events/render.hpp
index 07f5d64..ebc7588 100644
--- a/src/events/render.hpp
+++ b/src/events/render.hpp
@@ -20,6 +20,7 @@
#include <GL/glew.h>
+// TODO this is only for UI renders...
struct NewRenderEvent
{
GLuint vbo;
diff --git a/src/input.cpp b/src/input.cpp
index 21959c1..ed8b75f 100644
--- a/src/input.cpp
+++ b/src/input.cpp
@@ -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();
}
}
}
diff --git a/src/input.hpp b/src/input.hpp
index 8f7aa8f..3e666b2 100644
--- a/src/input.hpp
+++ b/src/input.hpp
@@ -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
@@ -21,51 +21,11 @@
#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;
};
diff --git a/src/player.cpp b/src/player.cpp
index f40a1d1..91e505c 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -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
@@ -24,18 +24,16 @@
#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);
- });
- }
- }
-}
-
diff --git a/src/player.hpp b/src/player.hpp
index d9a9237..1fda62b 100644
--- a/src/player.hpp
+++ b/src/player.hpp
@@ -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
@@ -30,23 +30,15 @@
/**
* @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_
diff --git a/src/ui.cpp b/src/ui.cpp
index 009d6ff..19b939d 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -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&,
diff --git a/src/ui.hpp b/src/ui.hpp
index e6e1c09..6dadef0 100644
--- a/src/ui.hpp
+++ b/src/ui.hpp
@@ -1,7 +1,6 @@
/**
* @file ui.hpp
*
- *
* Copyright (C) 2022 Clyne Sullivan
*
* This program is free software: you can redistribute it and/or modify