First commit.

This commit is contained in:
Arron David Nelson 2024-02-22 00:32:57 -08:00
commit a7774ed41e
21 changed files with 960 additions and 0 deletions

13
.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
*.obj
*.cpp.obj
*.lib
*.exe
*.a
*.ninja_deps
*.ninja_log
*.ninja
*.cmake
*.log
/.idea/
/cmake-build-release/
/cmake-build-debug/

26
CMakeLists.txt Normal file
View File

@ -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")

23
include/Com.h Normal file
View File

@ -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();
};

46
include/Ent.h Normal file
View File

@ -0,0 +1,46 @@
#pragma once
#include "Obj.h"
#include "Vec2.h"
#include <vector>
class Com;
class Ent : public Obj
{
private:
std::vector<Com*> 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();
};

44
include/GameLoop.h Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#include <vector>
#include <string>
class Level;
class GameLoop
{
private:
std::vector<Level*> 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();
};

16
include/Hash.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <string>
#include <cstdint>
/// 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);

39
include/Level.h Normal file
View File

@ -0,0 +1,39 @@
#pragma once
#include "Obj.h"
#include "Ent.h"
#include <vector>
class Level : public Obj
{
private:
std::vector<Ent> 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();
};

31
include/Obj.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include "Hash.h"
#include <string>
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;
};

24
include/Vec2.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include <cstddef>
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;
};

54
include/coms/Stats.h Normal file
View File

@ -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;
};

7
include/levels/Plains.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include "Level.h"
class Plains : public Level
{
};

49
src/Com.cpp Normal file
View File

@ -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()
{
}

108
src/Ent.cpp Normal file
View File

@ -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();
}

113
src/GameLoop.cpp Normal file
View File

@ -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;
}

35
src/Hash.cpp Normal file
View File

@ -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);
}

94
src/Level.cpp Normal file
View File

@ -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();
}

62
src/Obj.cpp Normal file
View File

@ -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;
}

16
src/Vec2.cpp Normal file
View File

@ -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)
{
}

150
src/coms/Stats.cpp Normal file
View File

@ -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;
}

3
src/levels/Plains.cpp Normal file
View File

@ -0,0 +1,3 @@
//
// Created by karutoh on 2/22/24.
//

7
src/main.cpp Normal file
View File

@ -0,0 +1,7 @@
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}