Browse Source

Lesson 24.13

Godzil 3 years ago
parent
commit
5c22997046

+ 8 - 6
assets/scripts/Level1.lua

@@ -51,7 +51,7 @@ Level = {
         { type = "texture", id = "army-gun-right-texture",      file = "./assets/images/army-gun-right.png" },
         { type = "texture", id = "army-gun-down-texture",       file = "./assets/images/army-gun-down.png" },
         { type = "texture", id = "army-gun-left-texture",       file = "./assets/images/army-gun-left.png" },
-        { type = "texture", id = "army-gun-killed-texture",     file = "./assets/images/army-gun-killed.png" },
+        { type = "texture", id = "army-gun-killed-texture",     file = "./assets/images/army-walk-killed.png" },
         { type = "texture", id = "sam-truck-right-texture",     file = "./assets/images/sam-truck-right.png" },
         { type = "texture", id = "sam-tank-left-texture",       file = "./assets/images/sam-tank-left-spritesheet.png" },
         { type = "texture", id = "sam-tank-right-texture",      file = "./assets/images/sam-tank-right-spritesheet.png" },
@@ -2755,7 +2755,6 @@ Level = {
                 }
             }
         },
-        --[[
         {
             -- SU-27 fighter jet
             group = "enemies",
@@ -2795,9 +2794,10 @@ Level = {
                 on_update_script = {
                     [0] =
                     function(entity, delta_time, ellapsed_time)
-                        -- print("Executing the SU-27 fighter jet Lua script!")
+                        Logger.Debug("Executing the SU-27 fighter jet Lua script!")
 
                         -- this function makes the fighter jet move up and down the map shooting projectiles
+                        --[[
                         local current_position_x, current_position_y = get_position(entity)
                         local current_velocity_x, current_velocity_y = get_velocity(entity)
 
@@ -2816,6 +2816,7 @@ Level = {
                             set_rotation(entity, 180) -- point down
                             set_projectile_velocity(entity, 0, 200) -- shoot projectiles down
                         end
+                        --]]
                     end
                 }
             }
@@ -2859,20 +2860,21 @@ Level = {
                 on_update_script = {
                     [0] =
                     function(entity, delta_time, ellapsed_time)
-                        -- print("Executing BF-109 Lua script!")
+                        Logger.Debug("Executing F-22 Lua script!")
 
+                    --[[
                         -- change the position of the the airplane to follow a sine wave movement
                         local new_x = ellapsed_time * 0.09
                         local new_y = 200 + (math.sin(ellapsed_time * 0.001) * 50)
                         set_position(entity, new_x, new_y) -- set the new position
+                    --]]
                     end
                 }
             }
         }
-        --]]
     }
 }
 
 -- Define some useful global variables
 map_width = Level.tilemap.num_cols * Level.tilemap.tile_size * Level.tilemap.scale
-map_height = Level.tilemap.num_rows * Level.tilemap.tile_size * Level.tilemap.scale
+map_height = Level.tilemap.num_rows * Level.tilemap.tile_size * Level.tilemap.scale

+ 10 - 0
source/Game.cpp

@@ -36,6 +36,7 @@
 #include <Systems/RenderText.h>
 #include <Systems/RenderHealth.h>
 #include <Systems/RenderGUI.h>
+#include <Systems/ScriptExecutor.h>
 
 uint32_t Game::windowsWidth = 0;
 uint32_t Game::windowsHeight = 0;
@@ -161,9 +162,17 @@ void Game::Setup()
     this->registry->addSystem<RenderTextSystem>();
     this->registry->addSystem<RenderHealthSystem>();
     this->registry->addSystem<RenderGUISystem>();
+    this->registry->addSystem<ScriptExecutorSystem>();
 
     this->lua.open_libraries(sol::lib::base, sol::lib::math, sol::lib::os);
 
+    this->lua.create_named_table("Logger");
+    sol::table Log = this->lua["Logger"];
+    Log["Info"] = Logger::Info;
+    Log["Error"] = Logger::Error;
+    Log["Warning"] = Logger::Warning;
+    Log["Debug"] = Logger::Debug;
+
     loader.LoadLevel(this->lua, this->registry, this->assetStore, this->renderer, 1);
 }
 
@@ -193,6 +202,7 @@ void Game::Update()
     registry->getSystem<ProjectileEmitSystem>().update(this->registry);
     registry->getSystem<ProjectileLifeCycleSystem>().update();
     registry->getSystem<CameraMovementSystem>().update(this->camera);
+    registry->getSystem<ScriptExecutorSystem>().update();
 }
 
 void Game::ProcessInput()

+ 6 - 103
source/LevelLoader.cpp

@@ -25,6 +25,7 @@
 #include <Components/Heakth.h>
 #include <Components/ProjectileEmitter.h>
 #include <Components/TextLabel.h>
+#include <Components/Script.h>
 
 LevelLoader::LevelLoader()
 {
@@ -191,6 +192,11 @@ void LevelLoader::loadEntities(sol::table &entitiesTable,
             {
                 newEntity.addComponent<CameraFollowComponent>();
             }
+            else if (componentName == "on_update_script")
+            {
+                sol::function func = component[0];
+                newEntity.addComponent<ScriptComponent>(func);
+            }
             else
             {
                 Logger::Error("Unknown component: %s for entity id#%d", componentName.c_str(), newEntity.getId());
@@ -232,107 +238,4 @@ void LevelLoader::LoadLevel(sol::state &lua,
     this->loadAssetTable(assetsTable, assetStore, renderer);
     this->loadTileMap(tileMap, registry);
     this->loadEntities(entitiesTable, registry);
-
-#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");
-    assetStore->addTexture(renderer, "chopper-image", "assets/images/chopper-spritesheet.png");
-    assetStore->addTexture(renderer, "main-tileset", "assets/tilemaps/jungle.png");
-    assetStore->addTexture(renderer, "radar-image", "assets/images/radar.png");
-    assetStore->addTexture(renderer, "bullet-image", "assets/images/bullet.png");
-
-    assetStore->addFont("charriot-font", "assets/fonts/charriot.ttf", 20);
-    assetStore->addFont("arial-font", "assets/fonts/charriot.ttf", 20);
-    assetStore->addFont("pico8-font", "assets/fonts/charriot.ttf", 10);
-
-
-    // jungle.map
-    int tileSize = 32;
-    double tileScale = 2.0;
-    int mapNumCols = 25;
-    int mapNumRows = 20;
-
-    std::fstream mapFile;
-    mapFile.open("assets/tilemaps/jungle.map");
-
-    assert(mapFile.is_open());
-
-    for (int y = 0; y < mapNumRows; y++)
-    {
-        for (int x = 0; x < mapNumCols; x++)
-        {
-            char ch[2] = {0, 0};
-            mapFile.get(ch[0]);
-            int srcRectY = atoi(ch) * tileSize;
-            mapFile.get(ch[0]);
-            int srcRectX = atoi(ch) * tileSize;
-            mapFile.ignore();
-
-            Entity tile = registry->createEntity();
-            tile.addComponent<TransformComponent>(glm::vec2(x * (tileScale * tileSize), y * (tileScale * tileSize)), glm::vec2(tileScale, tileScale), 0.0);
-            tile.addComponent<SpriteComponent>("main-tileset", 0, tileSize, tileSize, false, srcRectX, srcRectY);
-            tile.group("tiles");
-        }
-    }
-    mapFile.close();
-
-    Game::mapWidth = mapNumCols * tileSize * tileScale;
-    Game::mapHeight = mapNumRows * tileSize *  tileScale;
-
-    Entity chopper = registry->createEntity();
-    chopper.addComponent<TransformComponent>(glm::vec2(240, 110), glm::vec2(1, 1), 0);
-    chopper.addComponent<SpriteComponent>("chopper-image", 1, 32, 32);
-    chopper.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
-    chopper.addComponent<BoxColliderComponent>(32, 25, glm::vec2(0, 5));
-    chopper.addComponent<AnimationComponent>(2, 15, true);
-    chopper.addComponent<KeyboardControlComponent>(glm::vec2(0, -80), glm::vec2(80, 0),
-                                                   glm::vec2(0, 80), glm::vec2(-80, 0));
-    chopper.addComponent<ProjectileEmitterComponent>(glm::vec2(180, 180), 0, 10000, 10, true);
-    chopper.addComponent<HealthComponent>(100);
-    chopper.addComponent<CameraFollowComponent>();
-    chopper.tag("player");
-
-
-    Entity radar = registry->createEntity();
-    radar.addComponent<TransformComponent>(glm::vec2(Game::windowsWidth - 75, 10), glm::vec2(1, 1), 0);
-    radar.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
-    radar.addComponent<SpriteComponent>("radar-image", 3, 64, 64, true);
-    radar.addComponent<AnimationComponent>(8, 5, true);
-
-    Entity tank = registry->createEntity();
-    tank.addComponent<TransformComponent>(glm::vec2(500, 500), glm::vec2(1, 1), 0);
-    tank.addComponent<RigidBodyComponent>(glm::vec2(20, 0));
-    tank.addComponent<SpriteComponent>("tank-image", 1, 32, 32);
-    tank.addComponent<BoxColliderComponent>(25, 18, glm::vec2(5, 7));
-    tank.addComponent<HealthComponent>(100);
-    tank.group("enemies");
-
-    Entity truck = registry->createEntity();
-    truck.addComponent<TransformComponent>(glm::vec2(120, 500), glm::vec2(1, 1), 0);
-    truck.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
-    truck.addComponent<SpriteComponent>("truck-image", 2, 32, 32);
-    truck.addComponent<BoxColliderComponent>(25, 20, glm::vec2(5, 5));
-    truck.addComponent<ProjectileEmitterComponent>(glm::vec2(0, -100), 2000, 5000, 10, false);
-    truck.addComponent<HealthComponent>(100);
-    truck.group("enemies");
-
-
-    Entity treeA = registry->createEntity();
-    treeA.addComponent<TransformComponent>(glm::vec2(600, 495), glm::vec2(1, 1), 0);
-    treeA.addComponent<SpriteComponent>("tree-image", 2, 16, 32);
-    treeA.addComponent<BoxColliderComponent>(16, 32);
-    treeA.group("obstacles");
-
-    Entity treeB = registry->createEntity();
-    treeB.addComponent<TransformComponent>(glm::vec2(400, 495), glm::vec2(1, 1), 0);
-    treeB.addComponent<SpriteComponent>("tree-image", 2, 16, 32);
-    treeB.addComponent<BoxColliderComponent>(16, 32);
-    treeB.group("obstacles");
-
-    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
-
 }

+ 22 - 0
source/include/Components/Script.h

@@ -0,0 +1,22 @@
+/*
+ * 2D Game Engine 
+ * Script.h: 
+ * Based on pikuma.com 2D game engine in C++ and Lua course
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 01/03/2021.
+ */
+
+#ifndef IMGUI_SOURCE_INCLUDE_COMPONENTS_SCRIPT_H
+#define IMGUI_SOURCE_INCLUDE_COMPONENTS_SCRIPT_H
+
+#include <sol/sol.hpp>
+
+struct ScriptComponent
+{
+    sol::function func;
+
+    explicit ScriptComponent(sol::function func = sol::lua_nil) : func(func) {};
+};
+
+#endif /* IMGUI_SOURCE_INCLUDE_COMPONENTS_SCRIPT_H */

+ 2 - 2
source/include/ECS.h

@@ -187,7 +187,7 @@ template<typename T, typename ...TArgs> void Registry::addComponent(Entity entit
     componentPool->set(entityId, newComponent);
     this->entityComponentSignatures[entityId].set(componentId);
 
-    Logger::Debug("Component id#%d was added to entity id#%d", componentId, entityId);
+    //Logger::Debug("Component id#%d was added to entity id#%d", componentId, entityId);
 }
 
 template <typename T> void Registry::removeComponent(Entity entity)
@@ -201,7 +201,7 @@ template <typename T> void Registry::removeComponent(Entity entity)
     componentPool = std::static_pointer_cast<PoolToUse<T>>(this->componentPools[componentId]);
     componentPool.remove(entityId);
 
-    Logger::Debug("Component id#%d was removed to entity id#%d", componentId, entityId);
+    //Logger::Debug("Component id#%d was removed to entity id#%d", componentId, entityId);
 }
 
 template <typename T> bool Registry::hasComponent(Entity entity)

+ 1 - 1
source/include/Systems/Collision.h

@@ -67,7 +67,7 @@ public:
 
                 if (boxA.collideWith(boxB))
                 {
-                    Logger::Debug("Entity #%d collided with #%d!", i->getId(), j->getId());
+                    //Logger::Debug("Entity #%d collided with #%d!", i->getId(), j->getId());
 
                     collideB.isColliding = true;
                     collideA.isColliding = true;

+ 38 - 0
source/include/Systems/ScriptExecutor.h

@@ -0,0 +1,38 @@
+/*
+ * 2D Game Engine 
+ * ScriptExecutor.h: 
+ * Based on pikuma.com 2D game engine in C++ and Lua course
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 01/03/2021.
+ */
+
+#ifndef IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H
+#define IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H
+
+#include <sol/sol.hpp>
+
+#include <ECS.h>
+
+#include <Components/Script.h>
+
+class ScriptExecutorSystem : public System
+{
+public:
+    ScriptExecutorSystem()
+    {
+        this->requireComponent<ScriptComponent>();
+    }
+
+    void update()
+    {
+        for(auto entity : this->getSystemEntities())
+        {
+            const auto script = entity.getComponent<ScriptComponent>();
+
+            script.func();
+        }
+    }
+};
+
+#endif /* IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H */