27 lines
606 B
C++
27 lines
606 B
C++
#pragma once
|
|
|
|
#include <optional>
|
|
|
|
#include <QVector2D>
|
|
|
|
// Accumulates positions to produce their centroid (the center between them).
|
|
// Shared by the behavior executors that steer toward the middle of a group of
|
|
// entities (AdvanceExecutor: defence stations; StandbyExecutor: friendly ships).
|
|
struct Centroid
|
|
{
|
|
QVector2D sum;
|
|
int count = 0;
|
|
|
|
void add(const QVector2D& point)
|
|
{
|
|
sum += point;
|
|
count += 1;
|
|
}
|
|
|
|
std::optional<QVector2D> value() const
|
|
{
|
|
if (count == 0) { return std::nullopt; }
|
|
return sum / static_cast<float>(count);
|
|
}
|
|
};
|