entity spawning

This commit is contained in:
Ben
2020-09-06 13:29:30 +01:00
parent 27d52c05ad
commit 5f1c63cd59
6 changed files with 36 additions and 12 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -472,6 +472,13 @@ void Dungeon::Input(olc::PixelGameEngine* engine, float fTime)
lastTile = currentTile; lastTile = currentTile;
} }
template<typename T>
float vecDistance(T v1, T v2)
{
return sqrt(pow(v2.x - v1.x, 2.0f) + pow(v2.y - v1.y, 2.0f) * 1.0f);
}
void Dungeon::Update(olc::PixelGameEngine* engine, float fTime) void Dungeon::Update(olc::PixelGameEngine* engine, float fTime)
{ {
ActiveCamera->Update(fTime); ActiveCamera->Update(fTime);
@@ -479,25 +486,36 @@ void Dungeon::Update(olc::PixelGameEngine* engine, float fTime)
if (!HasBegun) return; if (!HasBegun) return;
// spawn enemies // spawn enemies
if (Enemies.size() == 0)// rand() % 1000 < 1) if (rand() % 100 < 1)
{ {
Enemy* enemy = new Enemy(); Enemy* enemy = new Enemy();
enemy->Type = EEntity::Type::Enemy; enemy->Type = EEntity::Type::Enemy;
enemy->Renderable = EnemyRenderable; enemy->Renderable = EnemyRenderable;
enemy->Animator = EnemyAnimator; enemy->Animator = EnemyAnimator;
enemy->Coords = { Player->Coords.x + static_cast<float>((rand() % 100) - 50 ), Player->Coords.y + static_cast<float>((rand() % 100) - 50) }; enemy->Coords = { static_cast<float>(((float)rand() / (float)RAND_MAX) * 1280.0f), static_cast<float>(((float)rand() / (float)RAND_MAX) * 720.0f) };
enemy->dxdy = { static_cast<float>(rand() % 10 - 5), static_cast<float>(rand() % 10 - 5) };
float distanceFromPlayer = vecDistance(Player->Coords, enemy->Coords);
_Logger.Debug(enemy->Coords.x, " ", enemy->Coords.y);
if (distanceFromPlayer > 100)
{
enemy->HitBox = new Collider{ 0, 0, 28, 36 };
Enemies.push_back(enemy);
}
else
{
delete enemy;
}
enemy->HitBox = new Collider{ 0, 0, 28, 36 };
Enemies.push_back(enemy);
} }
olc::vf2d desiredLocation = Player->Coords; olc::vf2d desiredLocation = Player->Coords;
for (auto enemy : Enemies) for (auto enemy : Enemies)
{ {
enemy->Velocity = static_cast<float>(TileSize) * (fTime * (Player->Speed / 1.5f) * olc::vf2d(desiredLocation - enemy->Coords).norm()); enemy->dxdy = static_cast<float>(TileSize) * (fTime * (Player->Speed / 1.5f) * olc::vf2d(desiredLocation - enemy->Coords).norm());
enemy->Coords += enemy->Velocity; enemy->Coords += enemy->dxdy;
} }
} }
@@ -611,6 +629,12 @@ void Dungeon::Draw(olc::PixelGameEngine* engine, float fTime)
// left // left
engine->FillRectDecal({0.0f, 0.0f}, {lightLeft, static_cast<float>(engine->ScreenHeight())}, olc::BLACK); engine->FillRectDecal({0.0f, 0.0f}, {lightLeft, static_cast<float>(engine->ScreenHeight())}, olc::BLACK);
// UI Layer
engine->SetDrawTarget(1);
engine->Clear(olc::BLANK);
engine->DrawString({15, 15}, std::to_string(Enemies.size()), olc::WHITE, 5);
} }
Dungeon::~Dungeon() Dungeon::~Dungeon()

View File

@@ -132,7 +132,7 @@ class Playable : public Entity
class Enemy : public Entity class Enemy : public Entity
{ {
public: public:
olc::vf2d Velocity = { 0.0f, 0.0f }; olc::vf2d dxdy = { 0.0f, 0.0f };
}; };

View File

@@ -92,24 +92,24 @@ class Game : public olc::PixelGameEngine
// TODO: Center these omd // TODO: Center these omd
SetPixelMode(olc::Pixel::ALPHA); SetPixelMode(olc::Pixel::ALPHA);
if (_TimeAccumilator > 16.0f) if (_TimeAccumilator > 16.0f)
DrawString((ScreenWidth() / 2) - (7 * 4) * (std::string("the machine has done nothing for me").length() / 2), 40, "the machine has done nothing for me", olc::Pixel(255, 255, 255, static_cast<int>(_DeltaFade1)), 3); DrawString(15, 40, "the machine has done nothing for me", olc::Pixel(255, 255, 255, static_cast<int>(_DeltaFade1)), 3);
if (_TimeAccumilator > 20.0f) if (_TimeAccumilator > 20.0f)
DrawString((ScreenWidth() / 2) - (7 * 4) * (std::string("the machine has left me with nothing").length() / 2), 120, "the machine has left me with nothing", olc::Pixel(255, 255, 255, static_cast<int>(_DeltaFade1)), 3); DrawString(15, 120, "the machine has left me with nothing", olc::Pixel(255, 255, 255, static_cast<int>(_DeltaFade1)), 3);
if (_TimeAccumilator > 23.0f) if (_TimeAccumilator > 23.0f)
DrawString((ScreenWidth() / 2) - (7 * 7) * (std::string("i am nothing").length() / 2), 300, "i am nothing", olc::Pixel(255, 255, 255, static_cast<int>(_DeltaFade1)), 6); DrawString((ScreenWidth() / 2) - (7 * 7) * (std::string("i am nothing").length() / 2), 300, "i am nothing", olc::Pixel(255, 255, 255, static_cast<int>(_DeltaFade1)), 6);
if (_TimeAccumilator > 25.0f) if (_TimeAccumilator > 24.0f)
DrawString((ScreenWidth() / 2) - (7 * 3) * (std::string("your demons can't survive the dark").length() / 2), 650, "your demons can't survive the dark", olc::Pixel(255, 255, 255, static_cast<int>(_DeltaFade1)), 2); DrawString(15, 650, "your demons can't survive the dark", olc::Pixel(255, 255, 255, static_cast<int>(_DeltaFade1)), 2);
} }
} }
if (_TimeAccumilator > 26.0f) if (_TimeAccumilator > 26.0f)
{ {
bruh:
_Dungeon->IsFireLit = true; _Dungeon->IsFireLit = true;
} }
bruh:
_Dungeon->Input(this, fTime); _Dungeon->Input(this, fTime);

BIN
openal32.dll Normal file

Binary file not shown.