From e4b0b8b141a265e98b6cd1c9c1975cd01b3d6000 Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Fri, 4 Sep 2020 17:10:24 +0000 Subject: [PATCH] naive (not working)collisions --- The Great Machine/Camera.cpp | 9 +++-- The Great Machine/Camera.hpp | 2 +- The Great Machine/Collisions.cpp | 15 ++++++++ The Great Machine/Collisions.hpp | 16 ++++---- The Great Machine/Dungeon.cpp | 32 ++++++++++------ The Great Machine/Dungeon.hpp | 2 +- The Great Machine/Things.hpp | 5 ++- The Great Machine/main.cpp | 2 +- project.4coder | 65 ++++++++++++++++++++++++++++++++ 9 files changed, 121 insertions(+), 27 deletions(-) create mode 100644 project.4coder diff --git a/The Great Machine/Camera.cpp b/The Great Machine/Camera.cpp index 19e7d8f..34fd206 100644 --- a/The Great Machine/Camera.cpp +++ b/The Great Machine/Camera.cpp @@ -10,22 +10,23 @@ void Camera::Update(float fTime) if (_Track == nullptr) return; _DesiredCoords.x = _Track->Coords.x - (ViewPort.x / 2); _DesiredCoords.y = _Track->Coords.y - (ViewPort.y / 2); - + float lerpX = std::lerp(Coords.x, _DesiredCoords.x, _SmoothSpeed); float lerpY = std::lerp(Coords.y, _DesiredCoords.y, _SmoothSpeed); - + Coords.x = lerpX; Coords.y = lerpY; } void Camera::Input(olc::PixelGameEngine* engine) { - + } void Camera::TrackEntity(Entity* entity) { _Track = entity; - Coords.x = _Track->Coords.x - (ViewPort.x / 2); + entity->TrackingCamera = this; + Coords.x = _Track->Coords.x - (ViewPort.x / 2); Coords.y = _Track->Coords.y - (ViewPort.y / 2); } diff --git a/The Great Machine/Camera.hpp b/The Great Machine/Camera.hpp index 5c53f35..baa3d16 100644 --- a/The Great Machine/Camera.hpp +++ b/The Great Machine/Camera.hpp @@ -22,7 +22,7 @@ class Camera Entity* _Track = nullptr; olc::vi2d _DesiredCoords; - float _SmoothSpeed = 0.01f; + float _SmoothSpeed = 0.025f; }; diff --git a/The Great Machine/Collisions.cpp b/The Great Machine/Collisions.cpp index cd0145f..d5eaf7c 100644 --- a/The Great Machine/Collisions.cpp +++ b/The Great Machine/Collisions.cpp @@ -1,3 +1,18 @@ #include "Collisions.hpp" +#include "Camera.hpp" +#include "Things.hpp" +#include "Logger.hpp" + +bool EntityCollide(Entity* entity, std::vector& nearby, int tileSize, CollisionInfo* info, olc::PixelGameEngine* engine) +{ + if (!entity->HitBox) return false; + + for (auto tile : nearby) + { + Logger::getInstance().Debug(tile->Coords.x, " ", tile->Coords.y); + engine->DrawRect({ static_cast((tile->Coords.x + tileSize) - entity->TrackingCamera->Coords.x), static_cast((tile->Coords.y + tileSize) - entity->TrackingCamera->Coords.y) }, {tileSize, tileSize}, olc::RED); + } + +} diff --git a/The Great Machine/Collisions.hpp b/The Great Machine/Collisions.hpp index 86f5697..afdcc22 100644 --- a/The Great Machine/Collisions.hpp +++ b/The Great Machine/Collisions.hpp @@ -1,8 +1,13 @@ #ifndef GREATMACHINE_COLLISIONS_H_ #define GREATMACHINE_COLLISIONS_H_ +#include +#include + +#include "olcPixelGameEngine.hpp" + class Entity; -// class +class Tile; class Collider { @@ -12,14 +17,11 @@ class Collider class CollisionInfo { -public: - + public: + }; - - -// bool EntityCollide - +bool EntityCollide(Entity* entity, std::vector& nearby, int tileSize, CollisionInfo* info, olc::PixelGameEngine* engine); #endif diff --git a/The Great Machine/Dungeon.cpp b/The Great Machine/Dungeon.cpp index 14f83c2..08be288 100644 --- a/The Great Machine/Dungeon.cpp +++ b/The Great Machine/Dungeon.cpp @@ -19,6 +19,7 @@ Dungeon::Dungeon() Player->Coords = { 0, 0 }; Player->Type = EEntity::Type::Player; + // Relative to player TL corner Player->HitBox = new Collider{ 0, 0, static_cast((static_cast(TileSize) / 3.0f) * 2.0f), static_cast((static_cast(TileSize) / 3.0f) * 2.0f) } ; ActiveCamera->TrackEntity(Player); @@ -218,31 +219,39 @@ void Dungeon::Input(olc::PixelGameEngine* engine, float fTime) } } -void Dungeon::Update(float fTime) +void Dungeon::Update(olc::PixelGameEngine* engine, float fTime) { // Map collisions - // generate a 3x3 bool map around where the player is - std::vector colliderMap; - colliderMap.reserve(9); - olc::vi2d currentTile = { static_cast(Player->Coords.x / TileSize), static_cast(Player->Coords.y / TileSize) }; static olc::vi2d lastTile; - auto IsVoid = [&] (int x, int y) { + auto IsMapMember = [&] (int x, int y) { std::unordered_map::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; + if (found == DungeonTiles.end()) return false; return false; }; if (lastTile != currentTile) - _Logger.Debug("Player Tile: ", currentTile.x, " ", currentTile.y, " ISVOID: ", IsVoid(currentTile.x, currentTile.y)); + _Logger.Debug("Player Tile: ", currentTile.x, " ", currentTile.y); + // get nearby collidables + std::vector nearbyCollidables; + for (int x = currentTile.x - 2; x <= currentTile.x + 2; x++) + for (int y = currentTile.y - 2; y <= currentTile.y + 2; y++) + { + if (IsMapMember(x,y)) + nearbyCollidables.push_back(DungeonTiles[{x, y}]); + } + + // Passes ownership back + CollisionInfo* collisionInfo = new CollisionInfo(); + bool colliding = EntityCollide(Player, nearbyCollidables, TileSize, collisionInfo, engine); + + delete collisionInfo; lastTile = currentTile; ActiveCamera->Update(fTime); @@ -273,12 +282,11 @@ void Dungeon::Draw(olc::PixelGameEngine* engine) engine->SetDrawTarget(static_cast(0)); engine->Clear(olc::BLANK); - engine->FillRect({0, 0}, {TileSize, TileSize}, olc::RED); - // Draw character engine->DrawPartialDecal({ static_cast(Player->Coords.x - ActiveCamera->Coords.x), static_cast(Player->Coords.y - ActiveCamera->Coords.y) }, { (static_cast(TileSize) / 3.0f) * 2.0f, (static_cast(TileSize) / 3.0f) * 2.0f }, TileSet->Decal(), { 143, 130 }, { 16, 16 }); + engine->DrawRect({(Player->HitBox->x + (int)Player->Coords.x) - (int)ActiveCamera->Coords.x, (Player->HitBox->y + (int)Player->Coords.y) - (int)ActiveCamera->Coords.y}, {Player->HitBox->w, Player->HitBox->h}, olc::RED); } Dungeon::~Dungeon() diff --git a/The Great Machine/Dungeon.hpp b/The Great Machine/Dungeon.hpp index 164f2a9..410ce67 100644 --- a/The Great Machine/Dungeon.hpp +++ b/The Great Machine/Dungeon.hpp @@ -25,7 +25,7 @@ class Dungeon void SpawnEntity(Entity* entity); void Input(olc::PixelGameEngine* engine, float fTime); - void Update(float fTime); + void Update(olc::PixelGameEngine* engine, float fTime); void Draw(olc::PixelGameEngine* engine); Playable* Player; diff --git a/The Great Machine/Things.hpp b/The Great Machine/Things.hpp index b27e7e9..dab04ef 100644 --- a/The Great Machine/Things.hpp +++ b/The Great Machine/Things.hpp @@ -93,6 +93,7 @@ namespace EEntity } class Collider; +class Camera; class Entity { @@ -100,6 +101,7 @@ class Entity olc::vf2d Coords; EEntity::Type Type; Collider* HitBox; + Camera* TrackingCamera; olc::vf2d SpriteTextureMask; olc::Renderable* SpriteMap; }; @@ -152,7 +154,8 @@ class Tile ETile::State State; bool IsSolid; - Collider* HitBox; + // CBA, just gonna use coords and size + // Collider* HitBox; virtual void Update(float fTime); }; diff --git a/The Great Machine/main.cpp b/The Great Machine/main.cpp index 898723b..dd9a4fd 100644 --- a/The Great Machine/main.cpp +++ b/The Great Machine/main.cpp @@ -58,7 +58,7 @@ class Game : public olc::PixelGameEngine _Dungeon->Input(this, fTime); - _Dungeon->Update(fTime); + _Dungeon->Update(this, fTime); _Dungeon->Draw(this); diff --git a/project.4coder b/project.4coder new file mode 100644 index 0000000..fc2bd71 --- /dev/null +++ b/project.4coder @@ -0,0 +1,65 @@ +version(1); +project_name = "4coder custom"; + +patterns = { +"*.c", +"*.cpp", +"*.h", +"*.m", +"*.mm", +"*.bat", +"*.sh", +"*.4coder", +"*.txt", +}; +blacklist_patterns = { +".*", +}; +load_paths_custom = { + {"."}, +}; +load_paths = { + { load_paths_custom, .os = "win" }, + { load_paths_custom, .os = "linux"}, + { load_paths_custom, .os = "mac" }, +}; + +build_super_x64_win32 = "custom\\bin\\buildsuper_x64-win.bat"; +build_super_x86_win32 = "custom\\bin\\buildsuper_x86-win.bat"; +build_super_x64_linux = "custom/bin/buildsuper_x64-linux.sh"; +build_super_x86_linux = "custom/bin/buildsuper_x86-linux.sh"; +build_super_x64_mac = "custom/bin/buildsuper_x64-mac.sh"; + +command_list = { + { .name = "build super x64", + .out = "*compilation*", .footer_panel = true, .save_dirty_files = true, + .cmd = { {build_super_x64_win32, .os ="win" }, + {build_super_x64_linux , .os ="linux"}, + {build_super_x64_mac , .os ="mac" }, }, }, + + { .name = "build super x86", + .out = "*compilation*", .footer_panel = true, .save_dirty_files = true, + .cmd = { {build_super_x86_win32, .os ="win" }, + {build_super_x86_linux, .os ="linux" }, }, }, + + { .name = "build C++ lexer generator", + .out = "*compilation*", .footer_panel = true, .save_dirty_files = true, + .cmd = { {"custom\\bin\\build_one_time custom\\languages\\4coder_cpp_lexer_gen.cpp ..\\build", .os ="win" }, + }, }, + + { .name = "build token tester", + .out = "*compilation*", .footer_panel = true, .save_dirty_files = true, + .cmd = { {"custom\\bin\\build_one_time custom\\languages\\4coder_cpp_lexer_test.cpp ..\\build", .os = "win" }, + }, }, + + { .name = "run one time", + .out = "*run*", .footer_panel = false, .save_dirty_files = false, + .cmd = { {"pushd ..\\build & one_time", .os = "win" }, + }, }, +}; + +fkey_command[1] = "build super x64"; +fkey_command[2] = "build C++ lexer generator"; +fkey_command[3] = "build token tester"; +fkey_command[4] = "run one time"; +fkey_command[5] = "build super x86";