Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyuu17 committed Jun 30, 2024
1 parent d2dbdec commit 879d440
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 42 deletions.
27 changes: 19 additions & 8 deletions src/SWBF2/Core.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@

#include <godot_cpp/variant/utility_functions.hpp>

#include "Native/Chunks/ChunkProcessor.hpp"
#include "Native/SWBF2.hpp"

#include "Core.hpp"
#include "Level.hpp"
#include "Version.h"

#include "Native/Chunks/ChunkProcessor.hpp"

#include <chrono>
#include <iostream>
#include <thread>

namespace SWBF2
{
Core::Core()
Expand All @@ -22,16 +19,30 @@ namespace SWBF2
set_name("Core");

godot::UtilityFunctions::print("hello world!");
}

void Core::LoadLevel(const godot::String &mapName)
{
m_curMapName = mapName;

if (mapName.is_empty())
{
remove_child(find_child("Level", false));

// SWBF2::Native::UcfbChunk::ReadUcfbFile("data/_lvl_pc/common.lvl");
// SWBF2::Native::UcfbChunk::ReadUcfbFile("data/_lvl_pc/core.lvl");
SWBF2::Native::SWBF2::Reset();
return;
}

Level *lvl = memnew(Level);
add_child(lvl);
lvl->set_owner(this);
lvl->LoadLevel(mapName);
}

void Core::_bind_methods()
{
godot::ClassDB::bind_method(godot::D_METHOD("get_mapname"), &Core::GetMapName);
godot::ClassDB::bind_method(godot::D_METHOD("load_level", "mapname"), &Core::LoadLevel);
godot::ClassDB::add_property("Core", godot::PropertyInfo(godot::Variant::STRING, "mapname", godot::PROPERTY_HINT_ENUM, "cor1"), "load_level", "get_mapname");
}
}
8 changes: 8 additions & 0 deletions src/SWBF2/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ namespace SWBF2
class Core : public godot::Node {
GDCLASS(Core, godot::Node)

private:
godot::String m_curMapName;

public:
Core();
~Core() = default;

public:
const godot::String &GetMapName() { return m_curMapName; };
void LoadLevel(const godot::String &mapName);

public:
void _ready() override;

private:
Expand Down
24 changes: 17 additions & 7 deletions src/SWBF2/Level.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include <godot_cpp/classes/class_db_singleton.hpp>
#include <godot_cpp/classes/array_mesh.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/classes/image_texture.hpp>
Expand All @@ -22,13 +23,6 @@ namespace SWBF2
void Level::_ready()
{
set_name("Level");

if (!Native::SWBF2::LoadLevelWithGamemode("cor/cor1", "ctf"))
throw std::runtime_error{ "failed to load the game level" };

LoadLevelInstances();
LoadWorldEnvironment();
LoadLights();
}

godot::MeshInstance3D *Level::LoadModel(const std::string &id)
Expand Down Expand Up @@ -175,6 +169,22 @@ namespace SWBF2
}
}

void Level::LoadLevel(const godot::String &mapName)
{
m_curMapName = mapName;

if (!Native::SWBF2::LoadLevelFile(mapName.ascii().get_data()))
throw std::runtime_error{ "failed to load the game level" };

LoadLevelInstances();
LoadWorldEnvironment();
LoadLights();
}

void Level::LoadGamemode(const godot::String &gamemode)
{
}

void Level::_process(double delta_time)
{
}
Expand Down
17 changes: 15 additions & 2 deletions src/SWBF2/Level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,32 @@ namespace SWBF2
class Level : public godot::Node3D {
GDCLASS(Level, godot::Node3D)

private:
godot::String m_curMapName;
godot::String m_curGamemode;

public:
Level() {}
~Level() = default;

MaterialPools m_materialPool;

virtual void _ready() override;

godot::MeshInstance3D *LoadModel(const std::string &id);

private:
void LoadLevelInstances();
void LoadWorldEnvironment();
void LoadLights();

public:
const godot::String &GetMapName() { return m_curMapName; };
void LoadLevel(const godot::String &mapName);

const godot::String &GetGamemode() { return m_curGamemode; };
void LoadGamemode(const godot::String &gamemode);
public:
virtual void _ready() override;

void _process(double delta_time) override;

void activate(bool active = true);
Expand Down
2 changes: 0 additions & 2 deletions src/SWBF2/MaterialPools.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#pragma once

#include "Native/SWBF2.hpp"

#include "MaterialPools.hpp"
Expand Down
26 changes: 18 additions & 8 deletions src/SWBF2/Native/Chunks/LevelChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ namespace SWBF2::Native
{
Level lvl{};

uint32_t hash;
FNVHash hash;
streamReader >> hash;

// std::string lvlName{ GameHashes.at(hash) };

streamReader.SkipBytes(sizeof(uint32_t)); // lvl_ size left

std::optional<StreamReader> readerChild;
Expand All @@ -41,10 +39,22 @@ namespace SWBF2::Native
}
}

/*const auto gamemodes = {"ctf", "conquest", "centerflag", "campaign", "tdm"};
std::string type = lvlName.substr(lvlName.find_first_of('_') + 1);
if (std::find(gamemodes.begin(), gamemodes.end(), type) != gamemodes.end())
SWBF2::m_levels.insert_or_assign(type, lvl);*/
const static std::unordered_map<LevelGamemode, std::string_view> LevelGamemodesStr
{
{ LevelGamemode::CTF, "ctf" },
{ LevelGamemode::CONQUEST, "conquest" },
{ LevelGamemode::CENTERFLAG, "centerflag" },
{ LevelGamemode::CAMPAIGN, "campaign" },
{ LevelGamemode::TDM, "tdm" }
};

for (const auto &[id, str] : LevelGamemodesStr)
{
if (FNVGenerateHash(std::format("{}_{}", SWBF2::m_curMapName, str)) == hash)
{
SWBF2::m_levels.insert_or_assign(id, lvl);
break;
}
}
}
}
2 changes: 0 additions & 2 deletions src/SWBF2/Native/Hashes.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#pragma once

#include "Hashes.hpp"

namespace SWBF2::Native
Expand Down
2 changes: 0 additions & 2 deletions src/SWBF2/Native/Models/Model.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#pragma once

#include "Model.hpp"

namespace SWBF2::Native
Expand Down
39 changes: 33 additions & 6 deletions src/SWBF2/Native/SWBF2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,49 @@ namespace SWBF2::Native
{
SkyDome SWBF2::m_skyDome;

std::string SWBF2::m_curLevel;
std::string SWBF2::m_curMapName;
LevelGamemode SWBF2::m_curLevel;

std::unordered_map<std::string, Level> SWBF2::m_levels;
std::unordered_map<LevelGamemode, Level> SWBF2::m_levels;
std::unordered_map<std::string, World> SWBF2::m_worlds;
std::unordered_map<std::string, Light> SWBF2::m_lights;
std::unordered_map<std::string, Model> SWBF2::m_models;
std::unordered_map<std::string, Texture> SWBF2::m_tex;
std::unordered_map<std::string, SWBF2::LoclEntriesMap> SWBF2::m_locl;

bool SWBF2::LoadLevelWithGamemode(const std::string &lvlfile, const std::string &lvl)
void SWBF2::Init()
{
bool ret = UcfbChunk::ReadUcfbFile(std::format("data/_lvl_pc/{}.lvl", lvlfile));
if (!UcfbChunk::ReadUcfbFile("data/_lvl_pc/core.lvl"))
throw std::runtime_error{ "failed to load core.lvl from game directory" };

m_curLevel = lvl;
if (!UcfbChunk::ReadUcfbFile("data/_lvl_pc/common.lvl"))
throw std::runtime_error{ "failed to load common.lvl from game directory" };
}

return ret;
void SWBF2::Reset()
{
m_skyDome = {};
m_curMapName = {};
m_curLevel = {};

m_levels.clear();
m_worlds.clear();
m_lights.clear();
m_models.clear();
m_tex.clear();
m_locl.clear();
}

bool SWBF2::LoadLevelFile(const std::string &levelname)
{
m_curMapName = levelname;

return UcfbChunk::ReadUcfbFile(std::format("data/_lvl_pc/{}.lvl", GameMaps.at(levelname)));
}

void SWBF2::LoadGamemode(LevelGamemode gamemode)
{
m_curLevel = gamemode;
}

const Level &SWBF2::GetLevel()
Expand Down
26 changes: 23 additions & 3 deletions src/SWBF2/Native/SWBF2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,30 @@

namespace SWBF2::Native
{
const static inline std::unordered_map<std::string_view, std::string_view> GameMaps
{
// name, path
{ "cor1", "cor/cor1" }
};

enum class LevelGamemode
{
NONE = 0,
CTF,
CONQUEST,
CENTERFLAG,
CAMPAIGN,
TDM
};

class SWBF2 {
public:
static SkyDome m_skyDome;

static std::string m_curLevel;
static std::string m_curMapName;
static LevelGamemode m_curLevel;

static std::unordered_map<std::string, Level> m_levels;
static std::unordered_map<LevelGamemode, Level> m_levels;
static std::unordered_map<std::string, World> m_worlds;
static std::unordered_map<std::string, Light> m_lights;
static std::unordered_map<std::string, Model> m_models;
Expand All @@ -24,7 +41,10 @@ namespace SWBF2::Native
using LoclEntriesMap = std::unordered_map<FNVHash, std::u16string>;
static std::unordered_map<std::string, LoclEntriesMap> m_locl;

static bool LoadLevelWithGamemode(const std::string &lvlfile, const std::string &gamemode);
static void Init();
static void Reset();
static bool LoadLevelFile(const std::string &levelname);
static void LoadGamemode(LevelGamemode gamemode);
static const Level &GetLevel();
};
}
2 changes: 0 additions & 2 deletions src/SWBF2/Native/Texture/Texture.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#pragma once

#include "Texture.hpp"

namespace SWBF2::Native
Expand Down

0 comments on commit 879d440

Please sign in to comment.