From cff6ba0a316f6f23852f7451adaade8454b6592f Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 3 Sep 2019 20:03:41 -0400 Subject: lua handling movement events? idea --- src/components/EventListener.hpp | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/components/EventListener.hpp (limited to 'src/components') diff --git a/src/components/EventListener.hpp b/src/components/EventListener.hpp new file mode 100644 index 0000000..77a004e --- /dev/null +++ b/src/components/EventListener.hpp @@ -0,0 +1,60 @@ +/** + * @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 . + */ + +#ifndef COMPONENT_EVENTLISTENER_HPP_ +#define COMPONENT_EVENTLISTENER_HPP_ + +#include "Component.hpp" + +struct EventListener : Component +{ +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_ + -- cgit v1.2.3