~leinnan/raylibGame unlisted

c72996b6fb32207eb0db6279e9f8f486561be358 — Piotr Siuszko 4 months ago eb48ade
Working reloading the game
M src/components/Actor.hpp => src/components/Actor.hpp +1 -0
@@ 3,6 3,7 @@
#include <string>

namespace Components {
    struct GameRestart{};

enum class Activity { Idle, Walking, AttackAnticipation, AttackContact, AttackRecovery };


M src/factories/UnitFactory.hpp => src/factories/UnitFactory.hpp +7 -0
@@ 19,6 19,7 @@
#include <memory>
#include <sstream>
#include <string>
#include <components/Collisions.hpp>

namespace Factories {
struct UnitLoader final : entt::resource_loader<UnitLoader, Presets::Unit> {


@@ 87,6 88,12 @@ class UnitFactory final : public Controllers::Controller<UnitFactory> {
            cameraTarget.perspectiveOffset = {-7.0f, 6.0f, -7.0f};
            cameraTarget.targetYOffset = 1.4f;
            registry.assign<Components::CameraTarget>(entity, cameraTarget);

            Components::Collision collision;
            collision.size = {0.5,2.0,0.5};
            collision.offset = {0,1,0};
            Components::Collisions collisions({collision});
            registry.assign<Components::Collisions>(entity, collisions);
        }
        registry.assign<Components::Patrol>(entity);


M src/main.cpp => src/main.cpp +39 -20
@@ 1,8 1,7 @@
#include "raylib.h"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"

#include "INIReader.hpp"
#include "components/Actor.hpp"
#include "factories/UnitFactory.hpp"
#include "systems/AiSystem.hpp"
#include "systems/CollisionSystem.hpp"


@@ 38,27 37,43 @@ Systems::MissileSystem missileSystem;
Systems::DamageSystem damageSystem;
entt::registry<> registry;
Camera camera;
std::string jsonFile;

void resetCamera()
{
    camera = {0};
    camera.target = (Vector3){0.0f, 0.5f, 0.0f};
    camera.up = (Vector3){0.0f, 1.0f, 0.0f};
    camera.fovy = 20.0f;
    camera.type = CAMERA_ORTHOGRAPHIC;
    camera.position = {0.0f, 0.f, 0.0f};
}

void restartGame()
{
    registry.reset();
    resetCamera();
    Components::readFromFile(registry, jsonFile.c_str());
}

int main(int argc, char **argv) {
    INIReader reader(argc == 2 ? argv[1] : CONFIG_PATH);
    auto fileLogger = spdlog::basic_logger_mt("basic_logger", "logs/game.log");
    spdlog::set_default_logger(fileLogger);    
    spdlog::set_pattern("[%H:%M:%S] %v");
//    auto fileLogger = spdlog::basic_logger_mt("basic_logger", "logs/game.log");
//    spdlog::set_default_logger(fileLogger);
//    spdlog::set_pattern("[%H:%M:%S] %v");

    if(reader.ParseError() != 0) {
        spdlog::error("Some error message with arg: {}", argc == 2 ? argv[1] : CONFIG_PATH);
//        spdlog::error("Some error message with arg: {}", argc == 2 ? argv[1] : CONFIG_PATH);
        return 1;
    }
    const auto windowName = reader.Get("game", "name", "Raylib");
    const auto jsonFile = reader.Get("game", "firstJson", "data.json");
    jsonFile = reader.Get("game", "firstJson", "data.json");

    int flags = FLAG_WINDOW_RESIZABLE;
    if(reader.GetBoolean("gfx", "fullscreen", false))
        flags |= FLAG_FULLSCREEN_MODE;
    if(reader.GetBoolean("gfx", "showLogo", false))
        flags |= FLAG_SHOW_LOGO;
    if(reader.GetBoolean("gfx", "msaa", false))
        flags |= FLAG_MSAA_4X_HINT;
//    if(reader.GetBoolean("gfx", "msaa", false))
//        flags |= FLAG_MSAA_4X_HINT;

    SetConfigFlags(flags);
    InitWindow(reader.GetInteger("gfx", "width", SCREEN_WIDTH), reader.GetInteger("gfx", "height", SCREEN_HEIGHT),


@@ 68,17 83,12 @@ int main(int argc, char **argv) {
    aiSystem.Init(registry);
    damageSystem.Init();

    spdlog::info("Loading files");
//    spdlog::info("Loading files");
    Factories::UnitFactory::getInstance().loadFromFile(reader.Get("game", "units", "units.json"));
    Components::readFromFile(registry, jsonFile.c_str());
    spdlog::info("Loading files ended");
    restartGame();
//    spdlog::info("Loading files ended");


    camera = {0};
    camera.target = (Vector3){0.0f, 0.5f, 0.0f};
    camera.up = (Vector3){0.0f, 1.0f, 0.0f};
    camera.fovy = 20.0f;
    camera.type = CAMERA_ORTHOGRAPHIC;
    camera.position = {0.0f, 0.f, 0.0f};
#if defined(PLATFORM_WEB)
    emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else


@@ 99,6 109,7 @@ int main(int argc, char **argv) {

//----------------------------------------------------------------------------------
void UpdateDrawFrame(void) {

    spawnSystem.Update(registry);
    destroySystem.Update(registry);
    damageSystem.Update(registry, GetFrameTime());


@@ 108,4 119,12 @@ void UpdateDrawFrame(void) {
    movementSystem.Update(registry);
    collisionSystem.Update(registry);
    renderSystem.Update(registry, camera);
    bool restartRequired = false;
    registry.view<Components::GameRestart>().each([&](const auto, const auto &component){
        restartRequired = true;
    });
    if(restartRequired)
    {
        restartGame();
    }
}

M src/systems/PlayerInputSystem.cpp => src/systems/PlayerInputSystem.cpp +7 -0
@@ 6,6 6,7 @@
#include "components/PlayerInput.hpp"
#include "components/Transform.hpp"
#include "components/Velocity.hpp"
#include "components/Actor.hpp"

#include <cmath>
#include <iostream>


@@ 17,6 18,12 @@ void PlayerInputSystem::Update(entt::registry<> &registry, const Camera &camera)
    const bool isLeftMouseBtnDown = IsMouseButtonDown(MOUSE_LEFT_BUTTON);
    const float mouseWheelMovement = GetMouseWheelMove() * GetFrameTime() * MOUSE_WHEEL_MODIFIER;

    if(IsKeyReleased(KEY_R))
    {
        auto entity = registry.create();
        registry.assign<Components::GameRestart>(entity);
    }

    registry.view<Components::CameraTarget>().each([&](const auto, auto &target) {
        const float newMultiplier = target.multiplier + mouseWheelMovement;


M src/systems/RenderSystem.cpp => src/systems/RenderSystem.cpp +9 -1
@@ 22,9 22,14 @@ void RenderSystem::Init(entt::registry<> &registry) {
}

void RenderSystem::Update(entt::registry<> &registry, Camera &camera) {
    const float w = GetScreenWidth();
    const float h = GetScreenHeight();
    auto target = LoadRenderTexture(w,h);
    UpdateCamera(registry,camera);
    BeginDrawing();
    ClearBackground(SKYBLUE);
    BeginTextureMode(target);
    ClearBackground(SKYBLUE);
    BeginMode3D(camera);
    registry.view<Components::CylinderRenderer, Components::Transform>().each(
        [&](const auto, const auto &cylinder, const auto &transform) {


@@ 50,6 55,8 @@ void RenderSystem::Update(entt::registry<> &registry, Camera &camera) {
        [&](const auto, const auto &damageBox, const auto &transform) { DrawBoundingBox(damageBox.box, ORANGE); });

    EndMode3D();
    EndTextureMode();
    DrawTextureRec(target.texture, {0,0,w,-h},{0,0},WHITE);
    registry.view<Components::WorldScreenText, Components::Transform>().each(
        [&](const auto, const auto &screenText, const auto &transform) {
            Vector2 textScreenPos =


@@ 61,7 68,7 @@ void RenderSystem::Update(entt::registry<> &registry, Camera &camera) {
            DrawText(textToShow, textScreenPos.x - MeasureText(textToShow, screenText.fontSize) / 2, textScreenPos.y,
                     screenText.fontSize, GOLD);
        });
    DrawText("Debug build", 10, 10, 10, CLITERAL{230, 102, 1, 255});
    DrawText("Debug build", 10, 10, 10, {230, 102, 1, 255});
    registry.view<Components::RectangleVeil>().each(
        [&](const auto, Components::RectangleVeil &rectVeil) {
            rectVeil.elapsed += GetFrameTime();// TODO- move outside render system to UI system


@@ 78,6 85,7 @@ void RenderSystem::Update(entt::registry<> &registry, Camera &camera) {
            }
        });
    EndDrawing();
    UnloadRenderTexture(target);
}

Vector3 RenderSystem::getCameraPos(const Vector3 &pos, const float &angle, const Vector3 &cameraOffset) {