~leinnan/raylibGame unlisted

2b27fb84856e88f62b1331710942a57aed296320 — Leinnan 1 year, 3 months ago 732f6a0
Create spawn points
M CMakeLists.txt => CMakeLists.txt +1 -1
@@ 22,7 22,7 @@ endif()
file(COPY ${CMAKE_SOURCE_DIR}/data DESTINATION ${CMAKE_BINARY_DIR}/output)

include_directories(src ext src/controllers src/components src/systems src/utils src/presets src/factories)
add_executable(${PROJECT_NAME} src/main.cpp src/utils/Arkona.cpp src/utils/JsonParser.cpp src/systems/DamageSystem.cpp src/systems/MissileSystem.cpp src/systems/AiSystem.cpp src/systems/CollisionSystem.cpp src/systems/DestroySystem.cpp src/systems/MovementSystem.cpp src/systems/RenderSystem.cpp src/systems/PlayerInputSystem.cpp)
add_executable(${PROJECT_NAME} src/main.cpp src/utils/Arkona.cpp src/utils/JsonParser.cpp  src/systems/SpawnSystem.cpp src/systems/DamageSystem.cpp src/systems/MissileSystem.cpp src/systems/AiSystem.cpp src/systems/CollisionSystem.cpp src/systems/DestroySystem.cpp src/systems/MovementSystem.cpp src/systems/RenderSystem.cpp src/systems/PlayerInputSystem.cpp)
#set(raylib_VERBOSE 1)
target_link_libraries(${PROJECT_NAME} raylib EnTT)
if(${PLATFORM} MATCHES "Web")

M data/test.json => data/test.json +13 -0
@@ 1,6 1,19 @@
[
    [
        {
            "idToSpawn": "player",
            "type": "SpawnPoint"
        },
        {
            "angle": 0.0,
            "posX": 7.0,
            "posY": 0.0,
            "posZ": 0.0,
            "type": "Transform"
        }
    ],
    [
        {
            "diffusePath": "data/gfx/grass.png",
            "modelPath": "data/gfx/planeGround.obj",
            "scale": 4.0,

A src/components/SpawnPoint.hpp => src/components/SpawnPoint.hpp +13 -0
@@ 0,0 1,13 @@
#ifndef SPAWN_POINT_HPP
#define SPAWN_POINT_HPP
#include <string>

namespace Components {

struct SpawnPoint final {
    std::string idToSpawn;
    bool spawned = false;
};
} // namespace Components

#endif

M src/main.cpp => src/main.cpp +3 -1
@@ 8,6 8,7 @@
#include "systems/MovementSystem.hpp"
#include "systems/PlayerInputSystem.hpp"
#include "systems/RenderSystem.hpp"
#include "systems/SpawnSystem.hpp"
#include "utils/JsonParser.hpp"
#include <iostream>
#include "INIReader.hpp"


@@ 25,6 26,7 @@ const char* CONFIG_PATH = "data/mod.ini";
void UpdateDrawFrame(void);

Systems::RenderSystem renderSystem;
Systems::SpawnSystem spawnSystem;
Systems::PlayerInputSystem playerInputSystem;
Systems::CollisionSystem collisionSystem;
Systems::MovementSystem movementSystem;


@@ 62,7 64,6 @@ int main(int argc, char **argv) {
    damageSystem.Init();

    unitFactory.loadFromFile(reader.Get("game","units","units.json"));
    auto result = unitFactory.spawnUnit(registry,entt::hashed_string("player"));
    Components::readFromFile(registry, jsonFile.c_str());

    camera = {0};


@@ 91,6 92,7 @@ int main(int argc, char **argv) {

//----------------------------------------------------------------------------------
void UpdateDrawFrame(void) {
    spawnSystem.Update(registry);
    destroySystem.Update(registry);
    damageSystem.Update(registry,GetFrameTime());
    playerInputSystem.Update(registry, camera);

A src/systems/SpawnSystem.cpp => src/systems/SpawnSystem.cpp +17 -0
@@ 0,0 1,17 @@
#include "SpawnSystem.hpp"
#include "components/SpawnPoint.hpp"
#include "components/Transform.hpp"
#include "factories/UnitFactory.hpp"

namespace Systems {
void SpawnSystem::Update(entt::registry<> &registry) {
    registry.view<Components::SpawnPoint, Components::Transform>().each(
        [&](const auto, auto &spawnPoint, const auto &transform) {
            if(spawnPoint.spawned)
                return;
            uint32_t spawnedEntity = Factories::UnitFactory::getInstance().spawnUnit(registry,entt::hashed_string(spawnPoint.idToSpawn.c_str()));
            registry.assign_or_replace<Components::Transform>(spawnedEntity,transform);
            spawnPoint.spawned = true;
        });
}
} // namespace Systems

A src/systems/SpawnSystem.hpp => src/systems/SpawnSystem.hpp +13 -0
@@ 0,0 1,13 @@
#ifndef SPAWN_SYSTEM_HPP
#define SPAWN_SYSTEM_HPP
#include <entt/entt.hpp>

namespace Systems {
class SpawnSystem {
  public:
    SpawnSystem(){};
    void Update(entt::registry<> &registry);
    void Shutdown(){};
};
} // namespace Systems
#endif // MISSILESYSTEM_HPP

M src/utils/JsonParser.cpp => src/utils/JsonParser.cpp +15 -0
@@ 76,6 76,14 @@ void Components::from_json(const json &j, Components::RotatingObject &t) {
    t.rotationSpeed = j.at("rotationSpeed").get<float>();
}

void Components::to_json(json &j, const Components::SpawnPoint &t) {
    j = json{{"type", "SpawnPoint"}, {"idToSpawn", t.idToSpawn}};
}

void Components::from_json(const json &j, Components::SpawnPoint &t) {
    t.idToSpawn = j.at("idToSpawn").get<std::string>();
}

void Components::to_json(json &j, const Components::Missile &t) {
    j = json{{"type", "Missile"}, {"angle", t.angle}, {"speed", t.speed}};
}


@@ 223,6 231,11 @@ void Components::parseToFile(entt::registry<> &registry, const char *filename) {
            Components::to_json(component, registry.get<Components::Destroyable>(entity));
            entityObject.push_back(component);
        }
        if(registry.has<Components::SpawnPoint>(entity)) {
            json component;
            Components::to_json(component, registry.get<Components::SpawnPoint>(entity));
            entityObject.push_back(component);
        }
        jsonReader.push_back(entityObject);
    });



@@ 284,6 297,8 @@ void Components::readFromFile(entt::registry<> &registry, const char *filename) 
                registry.assign<Components::Fighter>(entity, component.get<Components::Fighter>());
            } else if(type.compare("Destroyable") == 0) {
                registry.assign<Components::Destroyable>(entity, component.get<Components::Destroyable>());
            } else if(type.compare("SpawnPoint") == 0) {
                registry.assign<Components::SpawnPoint>(entity, component.get<Components::SpawnPoint>());
            }
        }
        return entity;

M src/utils/JsonParser.hpp => src/utils/JsonParser.hpp +4 -0
@@ 16,6 16,7 @@
#include "components/Transform.hpp"
#include "components/Velocity.hpp"
#include "components/WorldScreenText.hpp"
#include "components/SpawnPoint.hpp"

#include <entt/entt.hpp>
#include <json.hpp>


@@ 67,6 68,9 @@ void from_json(const json &j, Components::Fighter &t);
void to_json(json &j, const Components::Destroyable &t);
void from_json(const json &j, Components::Destroyable &t);

void to_json(json &j, const Components::SpawnPoint &t);
void from_json(const json &j, Components::SpawnPoint &t);

void parseToFile(entt::registry<> &registry, const char *filename);
void readFromFile(entt::registry<> &registry, const char *filename);