dodatna logika
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <game/agame/enemy.hpp>
|
||||
#include <game/agame/background.hpp>
|
||||
#include <object/components/boxcollider.hpp>
|
||||
#include <game/agame/player.hpp>
|
||||
#include <state/gamestate.hpp>
|
||||
@@ -31,8 +32,17 @@ namespace Game::AGame {
|
||||
return;
|
||||
}
|
||||
|
||||
// Enemies are always visible
|
||||
mIsVisible = true;
|
||||
// Enemies are visible only within a reveal radius around the player
|
||||
const float revealRadius = GameManager::getSharedData<float>("enemyRevealRadius");
|
||||
const float px = player->getTransform()->x + (player->getTexture() ? player->getTexture()->getWidth() * player->getTransform()->adjustedScaleX() / 2.f : 0.f);
|
||||
const float py = player->getTransform()->y + (player->getTexture() ? player->getTexture()->getHeight() * player->getTransform()->adjustedScaleY() / 2.f : 0.f);
|
||||
const float ew = getTexture() ? getTexture()->getWidth() * mTransform.adjustedScaleX() : 0.f;
|
||||
const float eh = getTexture() ? getTexture()->getHeight() * mTransform.adjustedScaleY() : 0.f;
|
||||
const float ex = mTransform.x + ew / 2.f;
|
||||
const float ey = mTransform.y + eh / 2.f;
|
||||
const float dxv = px - ex;
|
||||
const float dyv = py - ey;
|
||||
mIsVisible = (dxv * dxv + dyv * dyv) <= (revealRadius * revealRadius);
|
||||
|
||||
// Semi-random movement with periodic direction changes
|
||||
mDirectionChangeTimer += deltaTime;
|
||||
@@ -47,6 +57,9 @@ namespace Game::AGame {
|
||||
// Move enemy
|
||||
mTransform.x += mMoveSpeedX * deltaTime;
|
||||
mTransform.y += mMoveSpeedY * deltaTime;
|
||||
|
||||
// Decrease shoreline-spawn cooldown
|
||||
if (mShoreSpawnCooldown > 0.f) mShoreSpawnCooldown = std::max(0.f, mShoreSpawnCooldown - deltaTime);
|
||||
|
||||
// Clamp to land section
|
||||
const float landBoundaryX = GameManager::getSharedData<float>("terrainLandBoundaryX");
|
||||
@@ -67,6 +80,22 @@ namespace Game::AGame {
|
||||
if (mTransform.x + halfWidth > landBoundaryX - 25.f) {
|
||||
mTransform.x = landBoundaryX - 25.f - halfWidth;
|
||||
mMoveSpeedX = -std::abs(mMoveSpeedX);
|
||||
|
||||
// Enemy hit the shoreline on the right side; spawn a trash on the sea side
|
||||
if (mShoreSpawnCooldown <= 0.f) {
|
||||
auto* bg = GameManager::getEntityByName<AGame::Background>("BG");
|
||||
if (bg) {
|
||||
Object::Transform t;
|
||||
t.rotation = 0.f;
|
||||
t.scaleX = 5.5f;
|
||||
t.scaleY = 5.5f;
|
||||
// Place trash on sea side at same Y
|
||||
t.y = mTransform.y;
|
||||
t.x = landBoundaryX + 10.f; // will be adjusted inside spawnTrashAt
|
||||
bg->spawnTrashAt(t, true);
|
||||
}
|
||||
mShoreSpawnCooldown = 3.0f; // 3 second cooldown per enemy
|
||||
}
|
||||
}
|
||||
if (mTransform.y - halfHeight < -h / 2.f + 25.f) {
|
||||
mTransform.y = -h / 2.f + 25.f + halfHeight;
|
||||
|
||||
Reference in New Issue
Block a user