const Entity &reference = world.Player();
for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) {
Entity &e = (*iter)->Controlled();
+ if (e.Dead()) {
+ delete *iter;
+ iter = controllers.erase(iter);
+ continue;
+ }
glm::vec3 diff(reference.AbsoluteDifference(e));
if (dot(diff, diff) > despawn_range) {
- e.Remove();
+ e.Kill();
delete *iter;
iter = controllers.erase(iter);
} else {
, flee_speed(-0.005f)
, stop_dist(10)
, flee_dist(5) {
-
+ tgt.Ref();
}
Chaser::~Chaser() {
-
+ tgt.UnRef();
}
void Chaser::Update(int dt) {
Controller::Controller(Entity &e) noexcept
: entity(e) {
-
+ entity.Ref();
}
Controller::~Controller() {
-
+ entity.UnRef();
}
: entity(entity)
, pitch(0)
, yaw(0) {
+ entity.Ref();
+}
+FPSController::~FPSController() {
+ entity.UnRef();
}
public:
explicit FPSController(Entity &) noexcept;
+ ~FPSController();
Entity &Controlled() noexcept { return entity; }
const Entity &Controlled() const noexcept { return entity; }
, velocity(0, 0, 0)
, chunk(0, 0, 0)
, angular_velocity(0.0f)
+, ref_count(0)
, world_collision(false)
-, remove(false) {
+, dead(false) {
}
glm::mat4 Transform(const Chunk::Pos &chunk_offset) const noexcept;
Ray Aim(const Chunk::Pos &chunk_offset) const noexcept;
- void Remove() noexcept { remove = true; }
- bool CanRemove() const noexcept { return remove; }
+ void Ref() noexcept { ++ref_count; }
+ void UnRef() noexcept { --ref_count; }
+ void Kill() noexcept { dead = true; }
+ bool Referenced() const noexcept { return ref_count > 0; }
+ bool Dead() const noexcept { return dead; }
+ bool CanRemove() const noexcept { return dead && ref_count <= 0; }
void Update(int dt) noexcept;
glm::vec3 angular_velocity;
+ int ref_count;
+
bool world_collision;
- bool remove;
+ bool dead;
};