From a7774ed41eb09de315fcc0e50bad5d60988dd67a Mon Sep 17 00:00:00 2001 From: karutoh Date: Thu, 22 Feb 2024 00:32:57 -0800 Subject: [PATCH] First commit. --- .gitignore | 13 ++++ CMakeLists.txt | 26 +++++++ include/Com.h | 23 ++++++ include/Ent.h | 46 ++++++++++++ include/GameLoop.h | 44 ++++++++++++ include/Hash.h | 16 +++++ include/Level.h | 39 +++++++++++ include/Obj.h | 31 +++++++++ include/Vec2.h | 24 +++++++ include/coms/Stats.h | 54 +++++++++++++++ include/levels/Plains.h | 7 ++ src/Com.cpp | 49 +++++++++++++ src/Ent.cpp | 108 +++++++++++++++++++++++++++++ src/GameLoop.cpp | 113 ++++++++++++++++++++++++++++++ src/Hash.cpp | 35 ++++++++++ src/Level.cpp | 94 +++++++++++++++++++++++++ src/Obj.cpp | 62 +++++++++++++++++ src/Vec2.cpp | 16 +++++ src/coms/Stats.cpp | 150 ++++++++++++++++++++++++++++++++++++++++ src/levels/Plains.cpp | 3 + src/main.cpp | 7 ++ 21 files changed, 960 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 include/Com.h create mode 100644 include/Ent.h create mode 100644 include/GameLoop.h create mode 100644 include/Hash.h create mode 100644 include/Level.h create mode 100644 include/Obj.h create mode 100644 include/Vec2.h create mode 100644 include/coms/Stats.h create mode 100644 include/levels/Plains.h create mode 100644 src/Com.cpp create mode 100644 src/Ent.cpp create mode 100644 src/GameLoop.cpp create mode 100644 src/Hash.cpp create mode 100644 src/Level.cpp create mode 100644 src/Obj.cpp create mode 100644 src/Vec2.cpp create mode 100644 src/coms/Stats.cpp create mode 100644 src/levels/Plains.cpp create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ebe792b --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +*.obj +*.cpp.obj +*.lib +*.exe +*.a +*.ninja_deps +*.ninja_log +*.ninja +*.cmake +*.log +/.idea/ +/cmake-build-release/ +/cmake-build-debug/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1c369d9 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.27) +project(RPG) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(RPG src/main.cpp + include/Obj.h + src/Obj.cpp + include/Hash.h + src/Hash.cpp + include/coms/Stats.h + src/coms/Stats.cpp + include/Com.h + src/Com.cpp + include/Ent.h + src/Ent.cpp + include/Vec2.h + src/Vec2.cpp + include/Level.h + src/Level.cpp + include/GameLoop.h + src/GameLoop.cpp + include/levels/Plains.h + src/levels/Plains.cpp) + +target_include_directories(RPG PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") diff --git a/include/Com.h b/include/Com.h new file mode 100644 index 0000000..0ff50ad --- /dev/null +++ b/include/Com.h @@ -0,0 +1,23 @@ +#pragma once + +#include "Obj.h" + +class Com : public Obj +{ +public: + Com(); + + Com(std::string id); + + Com(Com&& com) noexcept; + + Com(const Com& com); + + Com& operator=(Com&& com) noexcept; + + Com& operator=(const Com& com); + + virtual void Update(); + + virtual void Render(); +}; \ No newline at end of file diff --git a/include/Ent.h b/include/Ent.h new file mode 100644 index 0000000..91407fd --- /dev/null +++ b/include/Ent.h @@ -0,0 +1,46 @@ +#pragma once + +#include "Obj.h" +#include "Vec2.h" + +#include + +class Com; + +class Ent : public Obj +{ +private: + std::vector coms; + Vec2 pos; + +public: + Ent(); + + Ent(std::string id); + + Ent(Ent&& ent) noexcept; + + Ent(const Ent& ent); + + Ent& operator=(Ent&& ent) noexcept; + + Ent& operator=(const Ent& ent); + + bool HasCom(size_t hashId); + + bool HasCom(const std::string& id); + + bool AddCom(Com* newCom); + + Com* GetCom(size_t hashId) const; + + Com* GetCom(const std::string& id) const; + + void SetPos(const Vec2& newPos); + + Vec2 GetPos() const; + + void Update(); + + void Render(); +}; \ No newline at end of file diff --git a/include/GameLoop.h b/include/GameLoop.h new file mode 100644 index 0000000..9bb95c5 --- /dev/null +++ b/include/GameLoop.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include + +class Level; + +class GameLoop +{ +private: + std::vector lvls; + bool running; + +public: + GameLoop(); + + GameLoop(GameLoop&& gl) noexcept; + + GameLoop(const GameLoop& gl); + + GameLoop& operator=(GameLoop&& gl) noexcept; + + GameLoop& operator=(const GameLoop& gl); + + bool HasLevel(size_t hashId) const; + + bool HasLevel(const std::string& id) const; + + bool AddLevel(Level* newLvl); + + Level* GetLevel(size_t hashId) const; + + Level* GetLevel(const std::string& id) const; + + void Update(); + + void Render(); + + void Tick(); + + void Start(); + + void Stop(); +}; \ No newline at end of file diff --git a/include/Hash.h b/include/Hash.h new file mode 100644 index 0000000..341208d --- /dev/null +++ b/include/Hash.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +/// A 32-bit FNV-1a hash algorithm. +/// @param [in] str The string to hash. +/// @returns The resulting hash. Zero if string does not contain any characters. +uint32_t Hash_32(const std::string& str); + +/// A 64-bit FNV-1a hash algorithm. +/// @param [in] str The string to hash. +/// @returns The resulting hash. Zero if string does not contain any characters. +uint64_t Hash_64(const std::string& str); + +size_t Hash(const std::string& str); \ No newline at end of file diff --git a/include/Level.h b/include/Level.h new file mode 100644 index 0000000..d86d142 --- /dev/null +++ b/include/Level.h @@ -0,0 +1,39 @@ +#pragma once + +#include "Obj.h" +#include "Ent.h" + +#include + +class Level : public Obj +{ +private: + std::vector ents; + +public: + Level(); + + Level(std::string id); + + Level(Level&& lvl) noexcept; + + Level(const Level& lvl); + + Level& operator=(Level&& lvl) noexcept; + + Level& operator=(const Level& lvl); + + bool HasEnt(size_t hashId) const; + + bool HasEnt(const std::string& id) const; + + bool AddEnt(Ent newEnt); + + Ent* GetEnt(size_t hashId); + + Ent* GetEnt(const std::string& id); + + virtual void Update(); + + virtual void Render(); +}; \ No newline at end of file diff --git a/include/Obj.h b/include/Obj.h new file mode 100644 index 0000000..a37e8e8 --- /dev/null +++ b/include/Obj.h @@ -0,0 +1,31 @@ +#pragma once + +#include "Hash.h" + +#include + +class Obj +{ +private: + size_t hashId; + std::string id; + +public: + Obj(); + + Obj(std::string id); + + Obj(Obj&& obj) noexcept; + + Obj(const Obj& obj); + + Obj& operator=(Obj&& obj) noexcept; + + Obj& operator=(const Obj& obj); + + size_t GetHashId() const; + + void SetId(std::string newId); + + std::string GetId() const; +}; \ No newline at end of file diff --git a/include/Vec2.h b/include/Vec2.h new file mode 100644 index 0000000..b619a11 --- /dev/null +++ b/include/Vec2.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +class Vec2 +{ +public: + size_t x; + size_t y; + + Vec2(); + + Vec2(size_t scalar); + + Vec2(size_t x, size_t y); + + Vec2(Vec2&& vec) noexcept = default; + + Vec2(const Vec2& vec) = default; + + Vec2& operator=(Vec2&& vec) noexcept = default; + + Vec2& operator=(const Vec2& vec) = default; +}; \ No newline at end of file diff --git a/include/coms/Stats.h b/include/coms/Stats.h new file mode 100644 index 0000000..ba692e9 --- /dev/null +++ b/include/coms/Stats.h @@ -0,0 +1,54 @@ +#pragma once + +#include "Com.h" + +class Stats : public Com +{ +private: + uint8_t level; + uint8_t strength; + uint8_t dexterity; + uint8_t constitution; + uint8_t intelligence; + uint8_t wisdom; + uint8_t charisma; + uint8_t maxHealth; + uint8_t health; + +public: + Stats(); + + Stats(uint8_t strength, uint8_t dexterity, uint8_t constitution, uint8_t intelligence, uint8_t wisdom, uint8_t charisma); + + Stats(Stats&& stats) noexcept; + + Stats(const Stats& stats); + + Stats& operator=(Stats&& stats) noexcept; + + Stats& operator=(const Stats& stats); + + uint8_t GetLevel() const; + + uint8_t GetStrength() const; + + uint8_t GetDexterity() const; + + uint8_t GetConstitution() const; + + uint8_t GetIntelligence() const; + + uint8_t GetWisdom() const; + + uint8_t GetCharisma() const; + + uint8_t GetMaxHealth() const; + + void SetHealth(uint8_t newHealth); + + void TakeDmg(uint8_t dmg); + + void Heal(uint8_t heal); + + uint8_t GetHealth() const; +}; \ No newline at end of file diff --git a/include/levels/Plains.h b/include/levels/Plains.h new file mode 100644 index 0000000..616f4d3 --- /dev/null +++ b/include/levels/Plains.h @@ -0,0 +1,7 @@ +#pragma once + +#include "Level.h" + +class Plains : public Level +{ +}; \ No newline at end of file diff --git a/src/Com.cpp b/src/Com.cpp new file mode 100644 index 0000000..8ca7b6d --- /dev/null +++ b/src/Com.cpp @@ -0,0 +1,49 @@ +#include "Com.h" + +Com::Com() +{ +} + +Com::Com(std::string id) + : Obj(std::move(id)) +{ +} + +Com::Com(Com&& com) noexcept + : Obj(std::move(com)) +{ +} + +Com::Com(const Com& com) + : Obj(com) +{ +} + +Com& Com::operator=(Com&& com) noexcept +{ + if (this == &com) + return *this; + + Obj::operator=(std::move(com)); + + return *this; +} + +Com& Com::operator=(const Com& com) +{ + if (this == &com) + return *this; + + Obj::operator=(com); + + return *this; +} + +void Com::Update() +{ +} + +void Com::Render() +{ +} + diff --git a/src/Ent.cpp b/src/Ent.cpp new file mode 100644 index 0000000..a9fdd18 --- /dev/null +++ b/src/Ent.cpp @@ -0,0 +1,108 @@ +#include "Ent.h" +#include "Com.h" + +Ent::Ent() +{ +} + +Ent::Ent(std::string id) + : Obj(std::move(id)) +{ +} + +Ent::Ent(Ent&& ent) noexcept + : Obj(std::move(ent)), coms(std::move(ent.coms)), pos(ent.pos) +{ + ent.pos = {}; +} + +Ent::Ent(const Ent& ent) + : Obj(ent), coms(ent.coms), pos(ent.pos) +{ +} + +Ent& Ent::operator=(Ent&& ent) noexcept +{ + if (this == &ent) + return *this; + + Obj::operator=(std::move(ent)); + + coms = std::move(ent.coms); + pos = ent.pos; + + ent.pos = {}; + + return *this; +} + +Ent& Ent::operator=(const Ent& ent) +{ + if (this == &ent) + return *this; + + coms = ent.coms; + pos = ent.pos; + + return *this; +} + +bool Ent::HasCom(size_t hashId) +{ + for (size_t i = 0; i < coms.size(); i++) + if (coms[i]->GetHashId() == hashId) + return true; + + return false; +} + +bool Ent::HasCom(const std::string& id) +{ + return HasCom(Hash(id)); +} + +bool Ent::AddCom(Com* newCom) +{ + if (HasCom(newCom->GetHashId())) + return false; + + coms.push_back(newCom); + + return true; +} + +Com* Ent::GetCom(size_t hashId) const +{ + for (size_t i = 0; i < coms.size(); i++) + if (coms[i]->GetHashId() == hashId) + return coms[i]; + + return nullptr; +} + +Com* Ent::GetCom(const std::string& id) const +{ + return GetCom(Hash(id)); +} + +void Ent::SetPos(const Vec2& newPos) +{ + pos = newPos; +} + +Vec2 Ent::GetPos() const +{ + return pos; +} + +void Ent::Update() +{ + for (size_t i = 0; i < coms.size(); i++) + coms[i]->Update(); +} + +void Ent::Render() +{ + for (size_t i = 0; i < coms.size(); i++) + coms[i]->Render(); +} diff --git a/src/GameLoop.cpp b/src/GameLoop.cpp new file mode 100644 index 0000000..18f1347 --- /dev/null +++ b/src/GameLoop.cpp @@ -0,0 +1,113 @@ +#include "GameLoop.h" +#include "Level.h" + +GameLoop::GameLoop() + : running(false) +{ +} + +GameLoop::GameLoop(GameLoop&& gl) noexcept + : lvls(std::move(gl.lvls)), running(gl.running) +{ + gl.running = false; +} + +GameLoop::GameLoop(const GameLoop& gl) + : lvls(gl.lvls), running(gl.running) +{ +} + +GameLoop& GameLoop::operator=(GameLoop&& gl) noexcept +{ + if (this == &gl) + return *this; + + lvls = std::move(gl.lvls); + running = gl.running; + + gl.running = false; + + return *this; +} + +GameLoop& GameLoop::operator=(const GameLoop& gl) +{ + if (this == &gl) + return *this; + + lvls = gl.lvls; + running = gl.running; + + return *this; +} + +bool GameLoop::HasLevel(size_t hashId) const +{ + for (size_t i = 0; i < lvls.size(); i++) + if (lvls[i]->GetHashId() == hashId) + return true; + + return false; +} + +bool GameLoop::HasLevel(const std::string& id) const +{ + return HasLevel(Hash(id)); +} + +bool GameLoop::AddLevel(Level* newLvl) +{ + if (HasLevel(newLvl->GetHashId())) + return false; + + lvls.push_back(newLvl); + + return true; +} + +Level* GameLoop::GetLevel(size_t hashId) const +{ + for (size_t i = 0; i < lvls.size(); i++) + if (lvls[i]->GetHashId() == hashId) + return lvls[i]; + + return nullptr; +} + +Level* GameLoop::GetLevel(const std::string& id) const +{ + return GetLevel(Hash(id)); +} + +void GameLoop::Update() +{ + for (size_t i = 0; i < lvls.size(); i++) + lvls[i]->Update(); +} + +void GameLoop::Render() +{ + for (size_t i = 0; i < lvls.size(); i++) + lvls[i]->Render(); +} + +void GameLoop::Tick() +{ + Update(); + Render(); +} + +void GameLoop::Start() +{ + if (running) + return; + + running = true; + while (running) + Tick(); +} + +void GameLoop::Stop() +{ + running = false; +} diff --git a/src/Hash.cpp b/src/Hash.cpp new file mode 100644 index 0000000..0a7a575 --- /dev/null +++ b/src/Hash.cpp @@ -0,0 +1,35 @@ +#include "Hash.h" + +uint32_t Hash_32(const std::string& str) +{ + if (str.empty()) + return 0; + + uint32_t hash = 2166136261ul; + + for (char c : str) + hash = (hash ^ c) * 16777619; + + return hash; +} + +uint64_t Hash_64(const std::string& str) +{ + if (str.empty()) + return 0; + + uint64_t hash = 14695981039346656037ull; + + for (char c : str) + hash = (hash ^ c) * 1099511628211; + + return hash; +} + +size_t Hash(const std::string& str) +{ + if (sizeof(void*) == 8) + return Hash_64(str); + + return Hash_32(str); +} \ No newline at end of file diff --git a/src/Level.cpp b/src/Level.cpp new file mode 100644 index 0000000..21bdbdc --- /dev/null +++ b/src/Level.cpp @@ -0,0 +1,94 @@ +#include "Level.h" + +Level::Level() +{ +} + +Level::Level(std::string id) + : Obj(std::move(id)) +{ +} + +Level::Level(Level&& lvl) noexcept + : Obj(std::move(lvl)), ents(std::move(lvl.ents)) +{ +} + +Level::Level(const Level& lvl) + : Obj(lvl), ents(lvl.ents) +{ +} + +Level& Level::operator=(Level&& lvl) noexcept +{ + if (this == &lvl) + return *this; + + Obj::operator=(std::move(lvl)); + + ents = std::move(lvl.ents); + + return *this; +} + +Level& Level::operator=(const Level& lvl) +{ + if (this == &lvl) + return *this; + + Obj::operator=(lvl); + + ents = lvl.ents; + + return *this; +} + +bool Level::HasEnt(size_t hashId) const +{ + for (size_t i = 0; i < ents.size(); i++) + if (ents[i].GetHashId() == hashId) + return true; + + return false; +} + +bool Level::HasEnt(const std::string& id) const +{ + return HasEnt(Hash(id)); +} + +bool Level::AddEnt(Ent newEnt) +{ + if (HasEnt(newEnt.GetHashId())) + return false; + + ents.push_back(std::move(newEnt)); + + return true; +} + +Ent* Level::GetEnt(size_t hashId) +{ + for (size_t i = 0; i < ents.size(); i++) + if (ents[i].GetHashId() == hashId) + return &ents[i]; + + return nullptr; +} + +Ent* Level::GetEnt(const std::string& id) +{ + return GetEnt(Hash(id)); +} + +void Level::Update() +{ + for (size_t i = 0; i < ents.size(); i++) + ents[i].Update(); +} + +void Level::Render() +{ + for (size_t i = 0; i < ents.size(); i++) + ents[i].Render(); +} diff --git a/src/Obj.cpp b/src/Obj.cpp new file mode 100644 index 0000000..1b14ae5 --- /dev/null +++ b/src/Obj.cpp @@ -0,0 +1,62 @@ +#include "../include/Obj.h" + +Obj::Obj() + : hashId(0) +{ +} + +Obj::Obj(std::string id) + : hashId(Hash(id)), id(std::move(id)) +{ +} + +Obj::Obj(Obj&& obj) noexcept + : hashId(obj.hashId), id(std::move(obj.id)) +{ + obj.hashId = 0; +} + +Obj::Obj(const Obj& obj) + : hashId(obj.hashId), id(obj.id) +{ +} + +Obj& Obj::operator=(Obj&& obj) noexcept +{ + if (this == &obj) + return *this; + + hashId = obj.hashId; + id = std::move(obj.id); + + obj.hashId = 0; + + return *this; +} + +Obj& Obj::operator=(const Obj& obj) +{ + if (this == &obj) + return *this; + + hashId = obj.hashId; + id = obj.id; + + return *this; +} + +size_t Obj::GetHashId() const +{ + return hashId; +} + +void Obj::SetId(std::string newId) +{ + hashId = Hash(newId); + id = std::move(newId); +} + +std::string Obj::GetId() const +{ + return id; +} \ No newline at end of file diff --git a/src/Vec2.cpp b/src/Vec2.cpp new file mode 100644 index 0000000..220c926 --- /dev/null +++ b/src/Vec2.cpp @@ -0,0 +1,16 @@ +#include "Vec2.h" + +Vec2::Vec2() + : x(0), y(0) +{ +} + +Vec2::Vec2(size_t scalar) + : x(scalar), y(scalar) +{ +} + +Vec2::Vec2(size_t x, size_t y) + : x(x), y(y) +{ +} \ No newline at end of file diff --git a/src/coms/Stats.cpp b/src/coms/Stats.cpp new file mode 100644 index 0000000..2ac2bca --- /dev/null +++ b/src/coms/Stats.cpp @@ -0,0 +1,150 @@ +#include "coms/Stats.h" + +Stats::Stats() + : Com("Stats"), level(0), strength(0), dexterity(0), constitution(0), intelligence(0), wisdom(0), charisma(0), + maxHealth(0), health(0) +{ +} + +Stats::Stats(uint8_t strength, uint8_t dexterity, uint8_t constitution, uint8_t intelligence, uint8_t wisdom, uint8_t charisma) + : Com("Stats"), level(1), strength(strength), dexterity(dexterity), constitution(constitution), intelligence(intelligence), + wisdom(wisdom), charisma(charisma) +{ +} + +Stats::Stats(Stats&& stats) noexcept + : Com(std::move(stats)), level(stats.level), strength(stats.strength), dexterity(stats.dexterity), + constitution(stats.constitution), intelligence(stats.intelligence), wisdom(stats.wisdom), charisma(stats.charisma), + maxHealth(stats.maxHealth), health(stats.health) +{ + stats.level = 0; + stats.strength = 0; + stats.dexterity = 0; + stats.constitution = 0; + stats.intelligence = 0; + stats.wisdom = 0; + stats.wisdom = 0; + stats.charisma = 0; + stats.maxHealth = 0; + stats.health = 0; +} + +Stats::Stats(const Stats& stats) + : Com(stats), level(stats.level), strength(stats.strength), dexterity(stats.dexterity), + constitution(stats.constitution), intelligence(stats.intelligence), wisdom(stats.wisdom), charisma(stats.charisma), + maxHealth(stats.maxHealth), health(stats.health) +{ + +} + +Stats& Stats::operator=(Stats&& stats) noexcept +{ + if (this == &stats) + return *this; + + Com::operator=(std::move(stats)); + + level = stats.level; + strength = stats.strength; + dexterity = stats.dexterity; + constitution = stats.constitution; + intelligence = stats.intelligence; + wisdom = stats.wisdom; + charisma = stats.charisma; + maxHealth = stats.maxHealth; + health = stats.health; + + stats.level = 0; + stats.strength = 0; + stats.dexterity = 0; + stats.constitution = 0; + stats.intelligence = 0; + stats.wisdom = 0; + stats.wisdom = 0; + stats.charisma = 0; + stats.maxHealth = 0; + stats.health = 0; + + return *this; +} + +Stats& Stats::operator=(const Stats& stats) +{ + if (this == &stats) + return *this; + + Com::operator=(stats); + + level = stats.level; + strength = stats.strength; + dexterity = stats.dexterity; + constitution = stats.constitution; + intelligence = stats.intelligence; + wisdom = stats.wisdom; + charisma = stats.charisma; + maxHealth = stats.maxHealth; + health = stats.health; + + return *this; +} + +uint8_t Stats::GetLevel() const +{ + return level; +} + +uint8_t Stats::GetStrength() const +{ + return strength; +} + +uint8_t Stats::GetDexterity() const +{ + return dexterity; +} + +uint8_t Stats::GetConstitution() const +{ + return constitution; +} + +uint8_t Stats::GetIntelligence() const +{ + return intelligence; +} + +uint8_t Stats::GetWisdom() const +{ + return wisdom; +} + +uint8_t Stats::GetCharisma() const +{ + return charisma; +} + +uint8_t Stats::GetMaxHealth() const +{ + return maxHealth; +} + +void Stats::SetHealth(uint8_t newHealth) +{ + health = newHealth; +} + +void Stats::TakeDmg(uint8_t dmg) +{ + health -= dmg; +} + +void Stats::Heal(uint8_t heal) +{ + health += heal; +} + +uint8_t Stats::GetHealth() const +{ + return health; +} + diff --git a/src/levels/Plains.cpp b/src/levels/Plains.cpp new file mode 100644 index 0000000..e86fcc3 --- /dev/null +++ b/src/levels/Plains.cpp @@ -0,0 +1,3 @@ +// +// Created by karutoh on 2/22/24. +// diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..11d982c --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,7 @@ +#include + +int main() +{ + std::cout << "Hello, World!" << std::endl; + return 0; +}