naive (not working)collisions
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ class Camera
|
||||
Entity* _Track = nullptr;
|
||||
|
||||
olc::vi2d _DesiredCoords;
|
||||
float _SmoothSpeed = 0.01f;
|
||||
float _SmoothSpeed = 0.025f;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
#ifndef GREATMACHINE_COLLISIONS_H_
|
||||
#define GREATMACHINE_COLLISIONS_H_
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#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<Tile*>& nearby, int tileSize, CollisionInfo* info, olc::PixelGameEngine* engine);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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<int>((static_cast<float>(TileSize) / 3.0f) * 2.0f), static_cast<int>((static_cast<float>(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<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) {
|
||||
auto IsMapMember = [&] (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;
|
||||
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<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;
|
||||
ActiveCamera->Update(fTime);
|
||||
@@ -273,12 +282,11 @@ void Dungeon::Draw(olc::PixelGameEngine* engine)
|
||||
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 });
|
||||
|
||||
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()
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -58,7 +58,7 @@ class Game : public olc::PixelGameEngine
|
||||
|
||||
_Dungeon->Input(this, fTime);
|
||||
|
||||
_Dungeon->Update(fTime);
|
||||
_Dungeon->Update(this, fTime);
|
||||
|
||||
_Dungeon->Draw(this);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user