naive (not working)collisions

This commit is contained in:
Ben Kyd
2020-09-04 17:10:24 +00:00
parent de75915f8f
commit e4b0b8b141
9 changed files with 121 additions and 27 deletions

View File

@@ -10,22 +10,23 @@ void Camera::Update(float fTime)
if (_Track == nullptr) return; if (_Track == nullptr) return;
_DesiredCoords.x = _Track->Coords.x - (ViewPort.x / 2); _DesiredCoords.x = _Track->Coords.x - (ViewPort.x / 2);
_DesiredCoords.y = _Track->Coords.y - (ViewPort.y / 2); _DesiredCoords.y = _Track->Coords.y - (ViewPort.y / 2);
float lerpX = std::lerp(Coords.x, _DesiredCoords.x, _SmoothSpeed); float lerpX = std::lerp(Coords.x, _DesiredCoords.x, _SmoothSpeed);
float lerpY = std::lerp(Coords.y, _DesiredCoords.y, _SmoothSpeed); float lerpY = std::lerp(Coords.y, _DesiredCoords.y, _SmoothSpeed);
Coords.x = lerpX; Coords.x = lerpX;
Coords.y = lerpY; Coords.y = lerpY;
} }
void Camera::Input(olc::PixelGameEngine* engine) void Camera::Input(olc::PixelGameEngine* engine)
{ {
} }
void Camera::TrackEntity(Entity* entity) void Camera::TrackEntity(Entity* entity)
{ {
_Track = 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); Coords.y = _Track->Coords.y - (ViewPort.y / 2);
} }

View File

@@ -22,7 +22,7 @@ class Camera
Entity* _Track = nullptr; Entity* _Track = nullptr;
olc::vi2d _DesiredCoords; olc::vi2d _DesiredCoords;
float _SmoothSpeed = 0.01f; float _SmoothSpeed = 0.025f;
}; };

View File

@@ -1,3 +1,18 @@
#include "Collisions.hpp" #include "Collisions.hpp"
#include "Camera.hpp"
#include "Things.hpp"
#include "Logger.hpp"
bool EntityCollide(Entity* entity, std::vector<Tile*>& 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<int>((tile->Coords.x + tileSize) - entity->TrackingCamera->Coords.x), static_cast<int>((tile->Coords.y + tileSize) - entity->TrackingCamera->Coords.y) }, {tileSize, tileSize}, olc::RED);
}
}

View File

@@ -1,8 +1,13 @@
#ifndef GREATMACHINE_COLLISIONS_H_ #ifndef GREATMACHINE_COLLISIONS_H_
#define GREATMACHINE_COLLISIONS_H_ #define GREATMACHINE_COLLISIONS_H_
#include <unordered_map>
#include <vector>
#include "olcPixelGameEngine.hpp"
class Entity; class Entity;
// class class Tile;
class Collider class Collider
{ {
@@ -12,14 +17,11 @@ class Collider
class CollisionInfo class CollisionInfo
{ {
public: public:
}; };
bool EntityCollide(Entity* entity, std::vector<Tile*>& nearby, int tileSize, CollisionInfo* info, olc::PixelGameEngine* engine);
// bool EntityCollide
#endif #endif

View File

@@ -19,6 +19,7 @@ Dungeon::Dungeon()
Player->Coords = { 0, 0 }; Player->Coords = { 0, 0 };
Player->Type = EEntity::Type::Player; Player->Type = EEntity::Type::Player;
// Relative to player TL corner
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) } ; 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->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 // 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) }; olc::vi2d currentTile = { static_cast<int>(Player->Coords.x / TileSize), static_cast<int>(Player->Coords.y / TileSize) };
static olc::vi2d lastTile; static olc::vi2d lastTile;
auto IsVoid = [&] (int x, int y) { auto IsMapMember = [&] (int x, int y) {
std::unordered_map<olc::vi2d, Tile*>::const_iterator found = DungeonTiles.find({x, y}); std::unordered_map<olc::vi2d, Tile*>::const_iterator found = DungeonTiles.find({x, y});
if (found == DungeonTiles.end()) return true; if (found == DungeonTiles.end()) return false;
if (DungeonTiles[{x, y}]->Type == ETile::Type::ThreeDStandard) return true;
if (DungeonTiles[{x, y}]->Type == ETile::Type::Void) return true;
return false; return false;
}; };
if (lastTile != currentTile) 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<Tile*> 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; lastTile = currentTile;
ActiveCamera->Update(fTime); ActiveCamera->Update(fTime);
@@ -273,12 +282,11 @@ void Dungeon::Draw(olc::PixelGameEngine* engine)
engine->SetDrawTarget(static_cast<uint8_t>(0)); engine->SetDrawTarget(static_cast<uint8_t>(0));
engine->Clear(olc::BLANK); engine->Clear(olc::BLANK);
engine->FillRect({0, 0}, {TileSize, TileSize}, olc::RED);
// Draw character // Draw character
engine->DrawPartialDecal({ static_cast<float>(Player->Coords.x - ActiveCamera->Coords.x), static_cast<float>(Player->Coords.y - ActiveCamera->Coords.y) }, 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 });
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() Dungeon::~Dungeon()

View File

@@ -25,7 +25,7 @@ class Dungeon
void SpawnEntity(Entity* entity); void SpawnEntity(Entity* entity);
void Input(olc::PixelGameEngine* engine, float fTime); void Input(olc::PixelGameEngine* engine, float fTime);
void Update(float fTime); void Update(olc::PixelGameEngine* engine, float fTime);
void Draw(olc::PixelGameEngine* engine); void Draw(olc::PixelGameEngine* engine);
Playable* Player; Playable* Player;

View File

@@ -93,6 +93,7 @@ namespace EEntity
} }
class Collider; class Collider;
class Camera;
class Entity class Entity
{ {
@@ -100,6 +101,7 @@ class Entity
olc::vf2d Coords; olc::vf2d Coords;
EEntity::Type Type; EEntity::Type Type;
Collider* HitBox; Collider* HitBox;
Camera* TrackingCamera;
olc::vf2d SpriteTextureMask; olc::vf2d SpriteTextureMask;
olc::Renderable* SpriteMap; olc::Renderable* SpriteMap;
}; };
@@ -152,7 +154,8 @@ class Tile
ETile::State State; ETile::State State;
bool IsSolid; bool IsSolid;
Collider* HitBox; // CBA, just gonna use coords and size
// Collider* HitBox;
virtual void Update(float fTime); virtual void Update(float fTime);
}; };

View File

@@ -58,7 +58,7 @@ class Game : public olc::PixelGameEngine
_Dungeon->Input(this, fTime); _Dungeon->Input(this, fTime);
_Dungeon->Update(fTime); _Dungeon->Update(this, fTime);
_Dungeon->Draw(this); _Dungeon->Draw(this);

65
project.4coder Normal file
View File

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