bird = {
Player = 0,
+ EventListeners = {
+ MoveLeftPressed = function(self)
+ self.Velocity.x = self.Velocity.x - 100
+ end,
+ MoveLeftReleased = function(self)
+ self.Velocity.x = self.Velocity.x + 100
+ end,
+ MoveRightPressed = function(self)
+ self.Velocity.x = self.Velocity.x + 100
+ end,
+ MoveRightReleased = function(self)
+ self.Velocity.x = self.Velocity.x - 100
+ end
+ },
Position = {
x = 150,
y = 75
--- /dev/null
+/**
+ * @file EventListener.hpp
+ * Allows entities to receive events and handle them in Lua.
+ *
+ * Copyright (C) 2019 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/>.
+ */
+
+#ifndef COMPONENT_EVENTLISTENER_HPP_
+#define COMPONENT_EVENTLISTENER_HPP_
+
+#include "Component.hpp"
+
+struct EventListener : Component<EventListener>
+{
+public:
+ sol::table listeners;
+
+ EventListener(sol::table _listeners) :
+ listeners(_listeners) {}
+
+ ~EventListener(void) {}
+
+ void cleanup(void) {
+ listeners = sol::nil;
+ }
+
+ EventListener FromLua([[maybe_unused]] sol::object ref)
+ {
+ return *this;
+ }
+
+ void tryListener(const std::string& name, sol::table& self)
+ {
+ if (listeners[name] == sol::type::function)
+ listeners[name](self);
+ }
+
+ void serialize([[maybe_unused]] cereal::JSONOutputArchive& ar) final {}
+ void serialize([[maybe_unused]] cereal::JSONInputArchive& ar) final {}
+
+ std::string serializeName(void) const final {
+ return "EventListener";
+ }
+};
+
+#endif // COMPONENT_EVENTLISTENER_HPP_
+
#include "script.hpp"
#include "render.hpp"
+#include "components/EventListener.hpp"
#include "components/Script.hpp"
#include "components/Position.hpp"
#include "components/Velocity.hpp"
{
systems.add<GameRunSystem>();
systems.add<InputSystem>();
- systems.add<PlayerSystem>();
+ systems.add<PlayerSystem>(entities);
systems.add<RenderSystem>();
systems.add<ScriptSystem>();
systems.configure();
// Remove all Lua references from entities
entities.each<Scripted>([](entityx::Entity, Scripted &f){ f.cleanup(); });
+ entities.each<EventListener>([](entityx::Entity, EventListener &f){
+ f.cleanup(); });
}
void Engine::renderLoop(void)
#include "player.hpp"
+#include "components/EventListener.hpp"
+#include "components/Script.hpp"
#include "components/Velocity.hpp"
void PlayerSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
{
if (player.valid()) {
if (kue.sym == SDLK_a) {
- if (auto vel = player.component<Velocity>(); vel)
- vel->x -= GROUND_VELOCITY;
+ entities.each<EventListener>([&]([[maybe_unused]] entityx::Entity e,
+ EventListener& el)
+ {
+ el.tryListener("MoveLeftPressed",
+ e.component<Scripted>()->caller);
+ });
} else if (kue.sym == SDLK_d) {
- if (auto vel = player.component<Velocity>(); vel)
- vel->x += GROUND_VELOCITY;
+ entities.each<EventListener>([&]([[maybe_unused]] entityx::Entity e,
+ EventListener& el)
+ {
+ el.tryListener("MoveRightPressed",
+ e.component<Scripted>()->caller);
+ });
}
}
}
{
if (player.valid()) {
if (kue.sym == SDLK_a) {
- if (auto vel = player.component<Velocity>(); vel)
- vel->x += GROUND_VELOCITY;
+ entities.each<EventListener>([&]([[maybe_unused]] entityx::Entity e,
+ EventListener& el)
+ {
+ el.tryListener("MoveLeftReleased",
+ e.component<Scripted>()->caller);
+ });
} else if (kue.sym == SDLK_d) {
- if (auto vel = player.component<Velocity>(); vel)
- vel->x -= GROUND_VELOCITY;
+ entities.each<EventListener>([&]([[maybe_unused]] entityx::Entity e,
+ EventListener& el)
+ {
+ el.tryListener("MoveRightReleased",
+ e.component<Scripted>()->caller);
+ });
}
}
}
*/
constexpr static double GROUND_VELOCITY = 100;
+ entityx::EntityManager& entities;
entityx::Entity player;
public:
+ PlayerSystem(entityx::EntityManager& _entities) :
+ entities(_entities) {}
+
/**
* Prepares the system for running.
*/
/********************
* SCRIPT PARSING *
********************/
+#include <components/EventListener.hpp>
#include <components/Position.hpp>
#include <components/Player.hpp>
#include <components/Name.hpp>
e.assign<Light>(Light().FromLua(tab["Light"])).get();
}
+ // Probably should be last
+ if (tab["EventListeners"] != nullptr) {
+ sol::table listeners = tab["EventListeners"];
+ e.assign<EventListener>(listeners);
+ }
+
} else {
// TODO better logging
std::cerr << "Parameter to spawn() must be a table!" << std::endl;