]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Refactored script system code and added license headers
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Fri, 30 Aug 2019 00:55:19 +0000 (20:55 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Fri, 30 Aug 2019 00:55:19 +0000 (20:55 -0400)
src/components/Component.hpp
src/components/Position.hpp
src/engine.cpp
src/script.cpp [new file with mode: 0644]
src/script.hpp

index 9c6b79df876e16d58a49e1a440603991fe273f6d..3c0a6d5cdc37946dcd13c2a0bc6f6aa8018a993a 100644 (file)
@@ -1,6 +1,5 @@
-/*
+/**
  * Copyright (C) 2019  Belle-Isle, Andrew <drumsetmonkey@gmail.com>
- * Author: Belle-Isle, Andrew <drumsetmonkey@gmail.com>
  *
  * 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
index 856b3d7786b746d8a00b767c67a836386aeb4d89..8684a7f15edce4058674d9455474100c7f6ddbe5 100644 (file)
@@ -1,6 +1,5 @@
-/*
+/**
  * Copyright (C) 2019  Belle-Isle, Andrew <drumsetmonkey@gmail.com>
- * Author: Belle-Isle, Andrew <drumsetmonkey@gmail.com>
  *
  * 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
index 0a3b81082adbe81fd9da479ad5e1488296488205..4803c64b6102f630952c68d82dd892232d388489 100644 (file)
@@ -3,6 +3,7 @@
  * Manages all game systems.
  *
  * Copyright (C) 2019 Clyne Sullivan
+ * Copyright (C) 2019 Belle-Isle, Andrew <drumsetmonkey@gmail.com>
  *
  * 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
diff --git a/src/script.cpp b/src/script.cpp
new file mode 100644 (file)
index 0000000..4630440
--- /dev/null
@@ -0,0 +1,119 @@
+/**
+ * @file script.cpp
+ * Manages all Lua scripts.
+ *
+ * Copyright (C) 2019  Belle-Isle, Andrew <drumsetmonkey@gmail.com>
+ *
+ * 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 <script.hpp>
+
+/*********************
+*  SYSTEM SPECIFIC  *
+*********************/
+
+void ScriptSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
+                             [[maybe_unused]]entityx::EventManager& events)
+{
+    this->manager = &entities;
+    this->events = &events;
+
+    events.subscribe<EntitySpawnEvent>(*this);
+
+    init();
+}
+
+void ScriptSystem::update([[maybe_unused]] entityx::EntityManager& entites,
+                          [[maybe_unused]] entityx::EventManager& events,
+                          [[maybe_unused]] entityx::TimeDelta dt)
+{
+
+}
+
+
+void ScriptSystem::receive(const EntitySpawnEvent &toSpawn)
+{
+    (void)toSpawn;
+}
+
+/*********************
+*  CLASS FUNCTIONS  *
+*********************/
+
+int ScriptSystem::init(void)
+{
+    lua.open_libraries(sol::lib::base);
+
+    scriptExport();
+    doFile();
+
+    return 0;
+}
+
+void ScriptSystem::doFile(void)
+{
+    auto result = lua.script_file("Scripts/init.lua");
+    if (!result.valid()) {
+        std::cout << "Lua error: " << std::endl;
+    }
+
+    manager->each<Position>(
+            [&](entityx::Entity, Position& p)
+            {std::cout << p.x << "," << p.y << std::endl;});
+}
+
+/********************
+*  SCRIPT PARSING  *
+********************/
+
+void ScriptSystem::scriptExport()
+{
+
+    std::function<sol::table(sol::table)> func = 
+        [this](sol::table t){ return spawn(t);};
+
+    lua.new_usertype<Position>("Position",
+            sol::constructors<Position(float x, float y), Position()>(),
+            "x", &Position::x,
+            "y", &Position::y);
+
+    auto gamespace = lua["game"].get_or_create<sol::table>();
+    gamespace.set_function("spawn", func);
+}
+
+sol::table ScriptSystem::spawn(sol::object param)
+{
+    sol::table toRet = lua.create_table_with();
+    
+    if (param.get_type() == sol::type::table) {
+        sol::table tab = param;
+
+        entityx::Entity e = manager->create();
+
+        if (tab["Position"] != nullptr) {
+            toRet["Position"] = 
+                e.assign<Position>(Position().FromLua(tab["Position"])).get();
+        }
+
+        if (tab["init"] != nullptr) {
+            toRet["init"] = tab["init"];
+        }
+
+    } else {
+        std::cerr << "Parameter to spawn() must be a table!" << std::endl;
+    }
+
+    return toRet;
+}
index f6111d0d6118e6877dfdec502b198d7d359000b2..baad0b183ea792d1719ece71d10124d134aa06cf 100644 (file)
@@ -1,6 +1,8 @@
-/*
+/**
+ * @file script.hpp
+ * Manages all Lua scripts.
+ *
  * Copyright (C) 2019  Belle-Isle, Andrew <drumsetmonkey@gmail.com>
- * Author: Belle-Isle, Andrew <drumsetmonkey@gmail.com>
  *
  * 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
@@ -20,7 +22,6 @@
 #define SCRIPT_HPP_
 
 #include <entityx/entityx.h>
-#include <lua.hpp>
 #include <sol/sol.hpp>
 
 /****************
@@ -47,109 +48,57 @@ class ScriptSystem : public entityx::System<ScriptSystem>
          * The script systems internal lua state that handles all
          * interactions between C and Lua
          */
-        entityx::EventManager events;
-        entityx::EntityManager manager;
-
         sol::state lua;
 
+        entityx::EventManager* events;
+        entityx::EntityManager* manager;
+
     public:
         ScriptSystem(void)
-            : manager(events){}
+        {}
 
         ~ScriptSystem(void)
-        {
-
-        }
-
-        int init(void)
-        {
-            lua.open_libraries(sol::lib::base);
-            scriptExport();
-            doFile();
-
-            //Script::CreateNewState();
-
-            return 0;
-        }
+        {}
 
+        /**
+         * Prepares the system for running.
+         */
         void configure([[maybe_unused]]entityx::EntityManager& entities,
-                       [[maybe_unused]]entityx::EventManager& events) final
-        {
-            //manager = std::make_shared<entityx::EntityManager>(std::move(entities));
-            
-            events.subscribe<EntitySpawnEvent>(*this);
-
-            init();
-        }
+                       [[maybe_unused]]entityx::EventManager& events) final;
         
+        /**
+         * Updates the scripting system.
+         */
         void update([[maybe_unused]] entityx::EntityManager& entites,
                     [[maybe_unused]] entityx::EventManager& events,
-                    [[maybe_unused]] entityx::TimeDelta dt) final
-        {
-
-        }
-
-        void doFile(void)
-        {
-            auto result = lua.script_file("Scripts/init.lua");
-            if (!result.valid()) {
-                std::cout << "Lua error: " << std::endl;
-            }
-
-            manager.each<Position>(
-                    [&](entityx::Entity, Position& p)
-                    {std::cout << p.x << "," << p.y << std::endl;});
-        }
-
-        sol::table spawn([[maybe_unused]]sol::object param)
-        {
-            sol::table toRet = lua.create_table_with();
-
-            //lb::LuaRef entity(state.get());
-            //entity = lb::newTable(state.get());
-            
-            if (param.get_type() == sol::type::table) {
-                sol::table tab = param;
-
-                entityx::Entity e = manager.create();
-
-                if (tab["Position"] != nullptr) {
-                    toRet["Position"] = 
-                        e.assign<Position>(Position().FromLua(tab["Position"])).get();
-                }
-
-                if (tab["init"] != nullptr) {
-                    toRet["init"] = tab["init"];
-                }
-
-            } else {
-                std::cerr << "Parameter to spawn() must be a table!" << std::endl;
-            }
-
-            //return entity;
-            return toRet;
-        }
-
-        void scriptExport()
-        {
-
-            std::function<sol::table(sol::table)> func = 
-                [this](sol::table t){ return spawn(t);};
-
-            lua.new_usertype<Position>("Position",
-                    sol::constructors<Position(float x, float y), Position()>(),
-                    "x", &Position::x,
-                    "y", &Position::y);
-
-            auto gamespace = lua["game"].get_or_create<sol::table>();
-            gamespace.set_function("spawn", func);
-        }
-
-        void receive (const EntitySpawnEvent &toSpawn)
-        {
-            //toSpawn.ret = spawn(toSpawn.ref);
-            (void)toSpawn;
-        }
+                    [[maybe_unused]] entityx::TimeDelta dt) final;
+
+        /**
+         * Receives all entity spawning events and manages the
+         * script counterpart.
+         */
+        void receive (const EntitySpawnEvent &toSpawn);
+
+        /**
+         * Initializes the lua states and libraries.
+         * @return Zero on success, non-zero on error
+         */
+        int init(void);
+
+        /**
+         * Run the initialization file.
+         */
+        void doFile(void);
+
+        /**
+         * The function called by lua scripts in order to spawn an entity.
+         */
+        sol::table spawn(sol::object param);
+
+        /**
+         * Contains all calls that export components/functions to lua.
+         */
+        void scriptExport();
 };
 
 #endif//SCRIPT_HPP_