Browse Source

Lesson 24.8

Godzil 3 years ago
parent
commit
4788abc76b

+ 8 - 8
assets/scripts/Level1.lua

@@ -5,19 +5,19 @@ Level = {
     ----------------------------------------------------
     assets = {
         [0] =
-        { type = "texture", id = "tilemap-texture", file = "./assets/tilemaps/jungle.png" },
-        { type = "texture", id = "chopper-texture", file = "./assets/images/chopper-green-spritesheet.png" },
-        { type = "texture", id = "tank-texture",    file = "./assets/images/tank-tiger-up.png" },
-        { type = "texture", id = "bullet-texture",  file = "./assets/images/bullet.png" },
-        { type = "font"   , id = "pico8-font-5",    file = "./assets/fonts/pico8.ttf", font_size = 5 },
-        { type = "font"   , id = "pico8-font-10",   file = "./assets/fonts/pico8.ttf", font_size = 10 }
+        { type = "texture", id = "tilemap-texture", file = "assets/tilemaps/jungle.png" },
+        { type = "texture", id = "chopper-texture", file = "assets/images/chopper-green-spritesheet.png" },
+        { type = "texture", id = "tank-texture",    file = "assets/images/tank-tiger-up.png" },
+        { type = "texture", id = "bullet-texture",  file = "assets/images/bullet.png" },
+        { type = "font"   , id = "pico8-font-5",    file = "assets/fonts/pico8.ttf", font_size = 5 },
+        { type = "font"   , id = "pico8-font-10",   file = "assets/fonts/pico8.ttf", font_size = 10 }
     },
 
     ----------------------------------------------------
     -- table to define the map config variables
     ----------------------------------------------------
     tilemap = {
-        map_file = "./assets/tilemaps/jungle.map",
+        map_file = "assets/tilemaps/jungle.map",
         texture_asset_id = "tilemap-texture",
         num_rows = 20,
         num_cols = 25,
@@ -114,4 +114,4 @@ Level = {
             }
         }
     }
-}
+}

+ 22 - 0
assets/scripts/myscript.lua

@@ -0,0 +1,22 @@
+-- Hello world!
+values = 7^(3*2)
+
+config =
+{
+    title = "My Game Engine 2.0",
+    fullscreen = false,
+    resolution =
+    {
+        width = 800,
+        height = 600
+    },
+}
+
+function factorial(n)
+    if n == 1 then
+        return 1
+    end
+    return factorial(n-1) * n
+end
+
+print("Cule of 3 is "..cube(3))

+ 7 - 1
source/AssetStore.cpp

@@ -49,7 +49,7 @@ SDL_Texture *AssetStore::getTexture(const std::string &assetId)
 {
     if (!this->textures[assetId])
     {
-        Logger::Warning("Assert '%s' requested but can't be found.", assetId.c_str());
+        Logger::Warning("Asset texture '%s' requested but can't be found.", assetId.c_str());
     }
     return this->textures[assetId];
 }
@@ -63,10 +63,16 @@ void AssetStore::addFont(const std::string &assetId, const std::string &filePath
         return;
     }
 
+    Logger::Info("New font added to the Asset Store id=%s (size: %d)", assetId.c_str(), fontSize);
+
     this->fonts.emplace(assetId, font);
 }
 
 TTF_Font *AssetStore::getFont(const std::string &assetId)
 {
+    if (!this->fonts[assetId])
+    {
+        Logger::Warning("Asset font '%s' requested but can't be found.", assetId.c_str());
+    }
     return this->fonts[assetId];
 }

+ 3 - 1
source/Game.cpp

@@ -162,7 +162,9 @@ void Game::Setup()
     this->registry->addSystem<RenderHealthSystem>();
     this->registry->addSystem<RenderGUISystem>();
 
-    loader.LoadLevel(this->registry, this->assetStore, this->renderer, 1);
+    this->lua.open_libraries(sol::lib::base, sol::lib::math);
+
+    loader.LoadLevel(this->lua, this->registry, this->assetStore, this->renderer, 1);
 }
 
 void Game::Update()

+ 59 - 2
source/LevelLoader.cpp

@@ -10,6 +10,8 @@
 
 #include <fstream>
 
+#include <sol/sol.hpp>
+
 #include <LevelLoader.h>
 
 #include <Game.h>
@@ -35,11 +37,64 @@ LevelLoader::~LevelLoader()
 
 }
 
-void LevelLoader::LoadLevel(const std::unique_ptr<Registry> &registry,
+void LevelLoader::loadAssetTable(sol::state &lua,
+                                 sol::table &assetsTable,
+                                 const std::unique_ptr<AssetStore> &assetStore,
+                                 SDL_Renderer *renderer)
+{
+    int i = 0;
+    while(true)
+    {
+        sol::optional<sol::table> hasAsset = assetsTable[i];
+
+        if (hasAsset == sol::nullopt)
+        {
+            break;
+        }
+
+        sol::table asset = assetsTable[i];
+        std::string assetType = asset["type"];
+
+        if (assetType == "texture")
+        {
+            assetStore->addTexture(renderer, asset["id"], asset["file"]);
+        }
+        else if (assetType == "font")
+        {
+            assetStore->addFont(asset["id"], asset["file"], asset["font_size"]);
+        }
+
+        i++;
+    }
+}
+
+void LevelLoader::LoadLevel(sol::state &lua,
+                            const std::unique_ptr<Registry> &registry,
                             const std::unique_ptr<AssetStore> &assetStore,
                             SDL_Renderer *renderer,
-                            int level)
+                            int levelNumber)
 {
+    char luaFileName[50];
+    sprintf(luaFileName, "assets/scripts/Level%d.lua", levelNumber);
+
+    sol::load_result script = lua.load_file(luaFileName);
+    if (!script.valid())
+    {
+        Logger::Error("Error in the Lua script for level #%d:", levelNumber);
+        sol::error err = script;
+        std::string errorMessage = err.what();
+        Logger::Error(errorMessage.c_str());
+        return;
+    }
+
+    lua.script_file(luaFileName);
+    Logger::Info("Opened level #%d", levelNumber);
+
+    sol::table level = lua["Level"];
+    sol::table assetsTable = level["assets"];
+    this->loadAssetTable(lua, assetsTable, assetStore, renderer);
+
+#if 0
     assetStore->addTexture(renderer, "tank-image", "assets/images/tank-panther-right.png");
     assetStore->addTexture(renderer, "tree-image", "assets/images/tree.png");
     assetStore->addTexture(renderer, "truck-image", "assets/images/truck-ford-right.png");
@@ -139,4 +194,6 @@ void LevelLoader::LoadLevel(const std::unique_ptr<Registry> &registry,
     SDL_Color green = { 64, 255, 127, 255 };
     Entity label = registry->createEntity();
     label.addComponent<TextLabelComponent>(glm::vec2(Game::windowsWidth/2 - 60, 10), "SUPER CHOPPER BROS 1.0", "charriot-font", green);
+#endif
+
 }

+ 3 - 0
source/include/Game.h

@@ -12,6 +12,7 @@
 #include <stdint.h>
 #include <memory>
 #include <SDL.h>
+#include <sol/sol.hpp>
 
 #include <ECS.h>
 #include <AssetStore.h>
@@ -34,6 +35,8 @@ private:
     std::unique_ptr<AssetStore> assetStore;
     std::unique_ptr<EventBus> eventBus;
 
+    sol::state lua;
+
 public:
     Game();
     ~Game();

+ 8 - 2
source/include/LevelLoader.h

@@ -18,14 +18,20 @@
 
 struct LevelLoader
 {
+private:
+    void loadAssetTable(sol::state &lua,
+                        sol::table &assetsTable,
+                        const std::unique_ptr<AssetStore> &assetStore,
+                        SDL_Renderer *renderer);
 public:
     LevelLoader();
     ~LevelLoader();
 
-    void LoadLevel(const std::unique_ptr<Registry> &registry,
+    void LoadLevel(sol::state &lua,
+                   const std::unique_ptr<Registry> &registry,
                    const std::unique_ptr<AssetStore> &assetStore,
                    SDL_Renderer *renderer,
-                   int level);
+                   int levelNumber);
 };
 
 #endif /* IMGUI_SOURCE_INCLUDE_LEVELLOADER_H */