implement ship behaviors
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "BuildingSystem.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <limits>
|
||||
#include <random>
|
||||
#include <set>
|
||||
|
||||
@@ -659,3 +660,61 @@ bool BuildingSystem::isTileOccupied(QPoint tile) const
|
||||
{
|
||||
return m_tileOccupancy.count({tile.x(), tile.y()}) > 0;
|
||||
}
|
||||
|
||||
const Building* BuildingSystem::findNearestBuilding(QVector2D worldPos,
|
||||
BuildingType type) const
|
||||
{
|
||||
const Building* best = nullptr;
|
||||
float bestDist = std::numeric_limits<float>::max();
|
||||
for (const Building& b : m_buildings)
|
||||
{
|
||||
if (b.type != type)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
QVector2D center(b.anchor.x() + b.footprint.width() / 2.0f,
|
||||
b.anchor.y() + b.footprint.height() / 2.0f);
|
||||
float dist = (center - worldPos).length();
|
||||
if (dist < bestDist)
|
||||
{
|
||||
bestDist = dist;
|
||||
best = &b;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
bool BuildingSystem::deliverScrapToSalvageBay(EntityId bayId)
|
||||
{
|
||||
Building* bay = nullptr;
|
||||
for (Building& b : m_buildings)
|
||||
{
|
||||
if (b.id == bayId)
|
||||
{
|
||||
bay = &b;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!bay || bay->type != BuildingType::SalvageBay)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (static_cast<int>(bay->outputBuffer.items.size()) >= bay->outputBuffer.capacity)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bay->outputBuffer.items.push_back(Item{ItemType{"scrap"}});
|
||||
return true;
|
||||
}
|
||||
|
||||
void BuildingSystem::healBuilding(EntityId id, float amount)
|
||||
{
|
||||
for (Building& b : m_buildings)
|
||||
{
|
||||
if (b.id == id)
|
||||
{
|
||||
b.hp = std::min(b.hp + amount, b.maxHp);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user