This commit is contained in:
Ben
2020-09-04 00:05:49 +01:00
parent a35d6d01a6
commit de75915f8f
15 changed files with 274 additions and 173 deletions

5
TODO
View File

@@ -1,5 +1,6 @@
[ ] Dungeon generation
[ ] Tile movement / playable character
[x] Dungeon generation
[x] Tile movement / playable character
[-] Tile collisions
[ ] Lighting
[ ] Gameplay
[ ] Actually figure out Gameplay

Binary file not shown.

View File

@@ -7,23 +7,23 @@ class Entity;
class Camera
{
public:
public:
olc::vf2d Coords;
olc::vi2d ViewPort;
void Update(float fTime);
void Input(olc::PixelGameEngine* engine);
void TrackEntity(Entity* e);
private:
private:
Entity* _Track = nullptr;
olc::vi2d _DesiredCoords;
float _SmoothSpeed = 0.0125f;
float _SmoothSpeed = 0.01f;
};
#endif

View File

@@ -1,6 +1,25 @@
#ifndef GREATMACHINE_COLLISIONS_H_
#define GREATMACHINE_COLLISIONS_H_
class Entity;
// class
class Collider
{
public:
int x, y, w, h;
};
class CollisionInfo
{
public:
};
// bool EntityCollide
#endif

View File

@@ -1,23 +1,26 @@
#include "Dungeon.hpp"
#include <algorithm>
#include <sstream>
#include "Collisions.hpp"
#include "Things.hpp"
#include "Camera.hpp"
#include "Logger.hpp"
Dungeon::Dungeon()
: _Logger(Logger::getInstance())
: _Logger(Logger::getInstance())
{
ActiveCamera = new Camera();
ActiveCamera->Coords = { 0, 0 };
ActiveCamera->ViewPort = { 1280, 720 };
Player = new Playable();
Player->Coords = { 0, 0 };
Player->Type = EEntity::Type::Player;
Player->HitBox = new Collider{ 0, 0, static_cast<int>((static_cast<float>(TileSize) / 3.0f) * 2.0f), static_cast<int>((static_cast<float>(TileSize) / 3.0f) * 2.0f) } ;
ActiveCamera->TrackEntity(Player);
ActiveCamera->Update(0.0f);
}
@@ -25,17 +28,17 @@ Dungeon::Dungeon()
void Dungeon::Generate()
{
srand(time(NULL));
TileSetDictionary = new TileDictionary();
TileSetDictionary->Register();
TileSet = new olc::Renderable();
TileSet->Load("res/dungeon_tileset.png");
_Logger.Debug("Texture Loaded: ", TileSet, " ", TileSet->Sprite()->width, " ", TileSet->Sprite()->height);
DungeonWidth = 0;
DungeonHeight = 0;
// Generate a dungeon
// Loosely follows the algorithm in section 3.3 "Agent Based Growing"
// http://pcgbook.com/wp-content/uploads/chapter03.pdf
@@ -44,39 +47,39 @@ void Dungeon::Generate()
{
int x, y, w, h;
};
auto randomRoom = [](int x, int y)
{ return new Room{ x, y, (rand() % 7) + 3, (rand() % 7) + 3 }; };
{ return new Room{ x, y, (rand() % 7) + 3, (rand() % 7) + 3 }; };
std::vector<olc::vi2d> tiles;
// Starting at 0,0
int directionChance = 5;
int roomChance = 5;
int dungeonMinSize = 5000;
struct Agent
{
// 0 up 1 right 2 down 3 left
int x, y, direction;
};
Agent* agent = new Agent();
agent->x = 0; agent->y = 0;
agent->direction = rand() % 4;
_Logger.Debug("Agent ", agent->x, " ", agent->y, " ", agent->direction);
auto addTile = [&](int x, int y) {
for (auto i : tiles)
if (i.x == x && i.y == y)
return;
return;
tiles.push_back({ x, y });
};
addTile(0, 0);
while (tiles.size() < dungeonMinSize)
{
switch (agent->direction)
@@ -97,12 +100,12 @@ void Dungeon::Generate()
{
directionChance += 5;
}
if (rand() % 300 < roomChance)
{
Room* room = randomRoom(agent->x, agent->y);
for (int x = room->x; x < room->w + room->x; x++)
for (int y = room->y; y < room->h + room->y; y++)
for (int y = room->y; y < room->h + room->y; y++)
addTile(x, y);
delete room;
roomChance = 0;
@@ -112,24 +115,26 @@ void Dungeon::Generate()
roomChance += 5;
}
}
for (auto& tile : tiles)
{
if (DungeonWidth <= tile.x) DungeonWidth = tile.x;
if (DungeonHeight <= tile.y) DungeonHeight = tile.y;
ETile::Type tileType = rand() % 100 > 1 ? ETile::Type::Floor : ETile::Type::FloorV1;
auto DoesTileExist = [&](olc::vi2d tile)
{
return std::find(tiles.begin(), tiles.end(), tile) != tiles.end();
};
bool tileBelow = DoesTileExist({ tile.x, tile.y + 1 });
bool tileAbove = DoesTileExist({ tile.x, tile.y - 1 });
bool tileLeft = DoesTileExist({ tile.x - 1, tile.y });
bool tileRight = DoesTileExist({ tile.x + 1, tile.y });
// Order here is very important! stop fucking it up
if (!tileAbove)
tileType = ETile::Type::WallU;
if (!tileRight)
@@ -138,12 +143,12 @@ void Dungeon::Generate()
tileType = ETile::Type::WallD;
if (!tileLeft)
tileType = ETile::Type::WallL;
if (!tileAbove && !tileBelow)
tileType = ETile::Type::PathAcross;
if (!tileRight && !tileLeft)
tileType = ETile::Type::PathUp;
if (!tileAbove && !tileLeft)
tileType = ETile::Type::WallTLCorner;
if (!tileAbove && !tileRight)
@@ -152,7 +157,7 @@ void Dungeon::Generate()
tileType = ETile::Type::WallBLCorner;
if (!tileBelow && !tileRight)
tileType = ETile::Type::WallBRCorner;
if (!tileAbove && !tileBelow && !tileRight)
tileType = ETile::Type::PathRight;
if (!tileAbove && !tileBelow && !tileLeft)
@@ -161,68 +166,119 @@ void Dungeon::Generate()
tileType = ETile::Type::PathTop;
if (!tileRight && !tileLeft && !tileBelow)
tileType = ETile::Type::PathBottom;
// Is the tile below free? if so, make it 3D
if (!tileBelow)
{
Tile* psuedo3DTile = new Tile({ tile.x, tile.y + 1 },
ETile::Type::ThreeDStandard, ETile::State::Default);
ETile::Type::ThreeDStandard, ETile::State::Default);
DungeonTiles[psuedo3DTile->Coords] = psuedo3DTile;
}
Tile* t = new Tile(tile, tileType, ETile::State::Default);
DungeonTiles[t->Coords] = t;
}
//DungeonRenderTarget = new olc::Renderable();
//DungeonRenderTarget->Create(DungeonWidth * TileSize, DungeonHeight * TileSize);
//DungeonRenderTarget->Create(200, 200);
}
void Dungeon::SpawnEntity(Entity* entity)
{
Entities[entity->Coords] = entity;
Entities[entity->Coords] = entity;
}
void Dungeon::Input(olc::PixelGameEngine* engine, float fTime)
{
if (engine->GetKey(olc::W).bHeld)
Player->Coords.y -= static_cast<float>(TileSize) * (fTime * 10.0f);
Player->Coords.y -= static_cast<float>(TileSize) * (fTime * Player->Speed);
if (engine->GetKey(olc::A).bHeld)
Player->Coords.x -= static_cast<float>(TileSize) * (fTime * 10.0f);
Player->Coords.x -= static_cast<float>(TileSize) * (fTime * Player->Speed);
if (engine->GetKey(olc::S).bHeld)
Player->Coords.y += static_cast<float>(TileSize) * (fTime * 10.0f);
Player->Coords.y += static_cast<float>(TileSize) * (fTime * Player->Speed);
if (engine->GetKey(olc::D).bHeld)
Player->Coords.x += static_cast<float>(TileSize) * (fTime * 10.0f);
Player->Coords.x += static_cast<float>(TileSize) * (fTime * Player->Speed);
if (engine->GetKey(olc::W).bHeld && engine->GetKey(olc::A).bHeld)
{
Player->Coords.y += static_cast<float>(TileSize) * (fTime * (Player->Speed / 3.0f));
Player->Coords.x += static_cast<float>(TileSize) * (fTime * (Player->Speed / 3.0f));
}
if (engine->GetKey(olc::W).bHeld && engine->GetKey(olc::D).bHeld)
{
Player->Coords.y += static_cast<float>(TileSize) * (fTime * (Player->Speed / 3.0f));
Player->Coords.x -= static_cast<float>(TileSize) * (fTime * (Player->Speed / 3.0f));
}
if (engine->GetKey(olc::S).bHeld && engine->GetKey(olc::D).bHeld)
{
Player->Coords.y -= static_cast<float>(TileSize) * (fTime * (Player->Speed / 3.0f));
Player->Coords.x -= static_cast<float>(TileSize) * (fTime * (Player->Speed / 3.0f));
}
if (engine->GetKey(olc::S).bHeld && engine->GetKey(olc::A).bHeld)
{
Player->Coords.y -= static_cast<float>(TileSize) * (fTime * (Player->Speed / 3.0f));
Player->Coords.x += static_cast<float>(TileSize) * (fTime * (Player->Speed / 3.0f));
}
}
void Dungeon::Update(float fTime)
{
// Map collisions
// generate a 3x3 bool map around where the player is
std::vector<bool> colliderMap;
colliderMap.reserve(9);
olc::vi2d currentTile = { static_cast<int>(Player->Coords.x / TileSize), static_cast<int>(Player->Coords.y / TileSize) };
static olc::vi2d lastTile;
auto IsVoid = [&] (int x, int y) {
std::unordered_map<olc::vi2d, Tile*>::const_iterator found = DungeonTiles.find({x, y});
if (found == DungeonTiles.end()) return true;
if (DungeonTiles[{x, y}]->Type == ETile::Type::ThreeDStandard) return true;
if (DungeonTiles[{x, y}]->Type == ETile::Type::Void) return true;
return false;
};
if (lastTile != currentTile)
_Logger.Debug("Player Tile: ", currentTile.x, " ", currentTile.y, " ISVOID: ", IsVoid(currentTile.x, currentTile.y));
lastTile = currentTile;
ActiveCamera->Update(fTime);
}
void Dungeon::Draw(olc::PixelGameEngine* engine)
{
//int
//engine->SetDrawTarget(DungeonRenderTarget->Sprite());
// Maps not gonna be big enough for me to care about optimistaion
// maybe i should care? i don't :^)
//engine->SetDrawTarget(4);
//engine->Clear({38, 36, 40});
engine->SetDrawTarget(1);
engine->Clear({38, 36, 40});
engine->SetPixelMode(olc::Pixel::ALPHA);
for (std::pair<olc::vi2d, Tile*> tile : DungeonTiles)
{
// TODO: Perform culling
// if (tile.second->Type == ETile::Type::Void) continue;
engine->DrawPartialDecal({ static_cast<float>((tile.first.x * TileSize) - ActiveCamera->Coords.x), static_cast<float>((tile.first.y * TileSize) - ActiveCamera->Coords.y) },
{ static_cast<float>(TileSize), static_cast<float>(TileSize) }, TileSet->Decal(), TileSetDictionary->Dictionary[tile.second->Type], { 16, 16 });
{ static_cast<float>(TileSize), static_cast<float>(TileSize) }, TileSet->Decal(), TileSetDictionary->Dictionary[tile.second->Type], { 16, 16 });
}
//engine->SetDrawTarget(1);
//engine->DrawSprite({ 0, 0 }, DungeonRenderTarget->Sprite());
// engine->SetPixelMode(olc::Pixel::NORMAL);
engine->SetDrawTarget(static_cast<uint8_t>(0));
engine->Clear(olc::BLANK);
engine->FillRect({0, 0}, {TileSize, TileSize}, olc::RED);
// Draw character
engine->DrawPartialDecal({ static_cast<float>(Player->Coords.x - ActiveCamera->Coords.x), static_cast<float>(Player->Coords.y - ActiveCamera->Coords.y) },
{ (static_cast<float>(TileSize) / 3.0f) * 2.0f, (static_cast<float>(TileSize) / 3.0f) * 2.0f }, TileSet->Decal(), { 143, 130 }, { 16, 16 });
{ (static_cast<float>(TileSize) / 3.0f) * 2.0f, (static_cast<float>(TileSize) / 3.0f) * 2.0f }, TileSet->Decal(), { 143, 130 }, { 16, 16 });
}
Dungeon::~Dungeon()

View File

@@ -18,35 +18,35 @@ class TileDictionary;
// Single level dungeon, no need for fancy levels
class Dungeon
{
public:
public:
Dungeon();
void Generate();
void SpawnEntity(Entity* entity);
void Input(olc::PixelGameEngine* engine, float fTime);
void Update(float fTime);
void Draw(olc::PixelGameEngine* engine);
Playable* Player;
Camera* ActiveCamera;
int TileSize = 64;
int TileSize = 32;
int DungeonWidth;
int DungeonHeight;
std::unordered_map<olc::vi2d, Tile*> DungeonTiles;
std::unordered_map<olc::vf2d, Entity*> Entities; // key here could be room?
std::unordered_map<olc::vf2d, Entity*> Entities;
std::unordered_map<olc::vf2d, FixedItem*> FixedItems;
TileDictionary* TileSetDictionary;
olc::Renderable* TileSet;
olc::Renderable* DungeonRenderTarget;
~Dungeon();
private:
private:
Logger& _Logger;
};

View File

@@ -74,7 +74,7 @@ namespace EConsoleColour
class Colour
{
public:
public:
static inline void ResetColour()
{
#if defined(_WINDOWS)
@@ -86,9 +86,9 @@ public:
<< "\033[" << EConsoleColour::BG_DEFAULT << "m";
#endif
}
template<typename T>
static inline void ConsoleColour(T colour)
static inline void ConsoleColour(T colour)
{
#if defined(_WINDOWS)
SetConsoleTextAttribute(h, colour);
@@ -100,7 +100,7 @@ public:
class LogManager
{
};
namespace ELogType
@@ -125,21 +125,21 @@ struct LogEntity
// Config must be loaded first
class Logger
{
public:
public:
Logger();
static Logger& getInstance()
{
static Logger instance;
return instance;
}
void InitializeFileLogging(std::filesystem::path path);
void InitializeLoggingThread();
template<typename T>
void LogElement(T t)
void LogElement(T t)
{
if constexpr (std::is_same<char, T>::value)
{
@@ -148,12 +148,13 @@ public:
}
std::cout << t;
}
// Basic log doesn't use the logthread so
// can be used before the thread is setup
// unless it is already ready
// Does not support the logging of vectors
template<typename... Args>
void BasicLog(Args... args)
void BasicLog(Args... args)
{
while (_IsLogging) {}
_IsLogging = true;
@@ -161,7 +162,7 @@ public:
LogElement('\n');
_IsLogging = false;
}
void FastLog(std::string args)
{
if (!_IsRunning)
@@ -172,12 +173,12 @@ public:
_QueueLock.lock();
_LogQueue.push(new LogEntity{ args, ELogType::NONE });
_QueueLock.unlock();
_TaskEnqueued.notify_one();
}
template<typename... Args>
void Log(Args... args)
void Log(Args... args)
{
if (!_IsRunning)
{
@@ -187,16 +188,16 @@ public:
std::stringstream s;
_FillStringStream(s, args...);
LogEntity* e = new LogEntity{ s.str(), ELogType::NONE };
_QueueLock.lock();
_LogQueue.push(e);
_QueueLock.unlock();
_TaskEnqueued.notify_one();
}
template<typename... Args>
void Info(Args... args)
void Info(Args... args)
{
if (!_IsRunning)
{
@@ -205,9 +206,9 @@ public:
}
_Log(ELogType::INFO, args...);
}
template<typename... Args>
void Debug(Args... args)
void Debug(Args... args)
{
if (!_IsRunning)
{
@@ -216,9 +217,9 @@ public:
}
_Log(ELogType::DEBUG, args...);
}
template<typename... Args>
void Warn(Args... args)
void Warn(Args... args)
{
if (!_IsRunning)
{
@@ -227,9 +228,9 @@ public:
}
_Log(ELogType::WARN, args...);
}
template<typename... Args>
void Error(Args... args)
void Error(Args... args)
{
if (!_IsRunning)
{
@@ -238,9 +239,9 @@ public:
}
_Log(ELogType::ERR, args...);
}
template<typename... Args>
void Panic(Args... args)
void Panic(Args... args)
{
if (!_IsRunning)
{
@@ -250,60 +251,60 @@ public:
}
_Log(ELogType::PANIC, args...);
}
~Logger();
public:
public:
std::atomic<bool> _IsRunning = false;
std::condition_variable _TaskEnqueued;
std::queue<LogEntity*> _LogQueue;
std::mutex _QueueLock;
std::atomic<bool> _IsLogging = false;
std::atomic<bool> _HasFileHandle = false;
std::ofstream _FileOutput;
private:
private:
template <typename T, typename... A>
void _FillStringStream(std::stringstream& s, T head, A... a)
void _FillStringStream(std::stringstream& s, T head, A... a)
{
s << head;// << ' ';
s << head;
if constexpr (sizeof...(a))
{
_FillStringStream(s, a...);
}
}
template<typename... Args>
void _Log(ELogType::Type type, Args... args)
void _Log(ELogType::Type type, Args... args)
{
std::stringstream s;
_FillStringStream(s, args...);
LogEntity* e = new LogEntity{ s.str(), type };
_QueueLock.lock();
_LogQueue.push(e);
_QueueLock.unlock();
_TaskEnqueued.notify_one();
}
std::thread* _OutputWorker;
};
class ScopedLogger
{
};
// times how long the scope took etc
class ProfileLogger : public ScopedLogger
{
};
#endif

View File

@@ -147,6 +147,7 @@
<ClCompile Include="Logger.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Things.cpp" />
<ClCompile Include="UI.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Camera.hpp" />
@@ -155,6 +156,7 @@
<ClInclude Include="Logger.hpp" />
<ClInclude Include="olcPixelGameEngine.hpp" />
<ClInclude Include="Things.hpp" />
<ClInclude Include="UI.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@@ -33,6 +33,9 @@
<ClCompile Include="Collisions.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="UI.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="olcPixelGameEngine.hpp">
@@ -53,5 +56,8 @@
<ClInclude Include="Collisions.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="UI.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -4,11 +4,12 @@
void Tile::Update(float fTime)
{
}
void TileDictionary::Register()
{
Dictionary[ETile::Type::Void] = { 217, 15 };
Dictionary[ETile::Type::Floor] = { 27, 37 };
Dictionary[ETile::Type::FloorV1] = { 44, 16 };
Dictionary[ETile::Type::WallL] = { 0, 16 };

View File

@@ -22,6 +22,7 @@ namespace ETile
enum Type
{
None,
Void,
Floor,
FloorV1,
WallL,
@@ -49,13 +50,13 @@ namespace ETile
DoorOpenTop,
DoorClosedTop,
DoorPortcullisTop,
ThreeDStandard,
ThreeDSolid,
ThreeDSpike,
ThreeDSpikeBottom
};
enum State
{
Default,
@@ -73,7 +74,7 @@ namespace EEntity
Enemy,
Boss
};
namespace EItem
{
enum Item
@@ -81,7 +82,7 @@ namespace EEntity
None,
Sword
};
// Fixed items can have states #door open / closed
enum FixedItem
{
@@ -91,58 +92,67 @@ namespace EEntity
}
}
class HitBox;
class Collider;
class Entity
{
public:
public:
olc::vf2d Coords;
EEntity::Type Type;
HitBox* AABBHitBox;
Collider* HitBox;
olc::vf2d SpriteTextureMask;
olc::Renderable* SpriteMap;
};
class Item : public Entity
{
public:
public:
EEntity::EItem::Item Item;
};
class FixedItem : public Entity
{
public:
public:
EEntity::EItem::FixedItem Item;
};
class Playable : public Entity
{
public:
public:
float Speed = 10.0f;
int SelectedInventoryItem = 0;
std::array<Item*, 6> Inventory;
};
class TileDictionary
{
public:
public:
void Register();
std::map<ETile::Type, olc::vi2d> Dictionary;
};
class Tile
{
public:
public:
Tile(olc::vi2d coords, ETile::Type type, ETile::State state)
: Coords(coords)
, Type(type)
, State(state)
{ }
{
if (Type == ETile::Type::None || Type == ETile::Type::Void || Type == ETile::Type::ThreeDStandard)
IsSolid = true;
else
IsSolid = false;
}
olc::vi2d Coords;
ETile::Type Type;
ETile::State State;
HitBox* AABBHitBox;
bool IsSolid;
Collider* HitBox;
virtual void Update(float fTime);
};

0
The Great Machine/UI.cpp Normal file
View File

0
The Great Machine/UI.hpp Normal file
View File

View File

@@ -7,26 +7,29 @@
class Game : public olc::PixelGameEngine
{
private:
private:
Logger& _Logger;
Dungeon* _Dungeon;
public:
public:
Game()
: _Logger(Logger::getInstance())
{
sAppName = "The Great Machine";
}
bool OnUserCreate() override
{
_Dungeon = new Dungeon();
_Dungeon->Generate();
for (int i = 0; i < 5; i++)
CreateLayer();
return true;
}
void DisplayTitle(float fTime)
{
if (m_TimeAccumilator > 2.0f)
@@ -38,36 +41,38 @@ public:
DrawString(5, ScreenHeight() - 15, "Powered by the OLC Pixel Game Engine", olc::Pixel(255, 255, 255, static_cast<int>(m_DeltaFade)));
DrawString(ScreenWidth() - (8 * (std::string("Copyright Benjamin Kyd 2020").length())) - 5, ScreenHeight() - 15, "Copyright Benjamin Kyd 2020", olc::Pixel(255, 255, 255, static_cast<int>(m_DeltaFade)));
}
bool OnUserUpdate(float fTime) override
{
SetPixelMode(olc::Pixel::ALPHA);
m_TimeAccumilator += fTime;
Clear({ 38, 36, 40 });
// _Logger.Debug(m_TimeAccumilator);
//if (m_TimeAccumilator < 4.0f)
//{
// DisplayTitle(fTime);
// return true;
//}
_Dungeon->Input(this, fTime);
Clear({38, 36, 40});
// _Logger.Debug(m_TimeAccumilator);
//if (m_TimeAccumilator < 4.0f)
//{
//DisplayTitle(fTime);
//return true;
//}
_Dungeon->Input(this, fTime);
_Dungeon->Update(fTime);
_Dungeon->Draw(this);
for (int i = 0; i < 5; i++)
EnableLayer(i, true);
return true;
}
private:
private:
long float m_TimeAccumilator = 0;
float m_DeltaFade = 255.0f;
};
@@ -76,7 +81,7 @@ int main()
Logger& _Logger = Logger::getInstance();
_Logger.InitializeFileLogging("./logs.log");
_Logger.InitializeLoggingThread();
Game _Game;
_Game.Construct(1280, 720, 1, 1, false, false);
_Logger.Info("Game Constructed");