Browse Source

getEntsInArea

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
656f558744
3 changed files with 31 additions and 2 deletions
  1. 5
    0
      libswan/include/swan/Vector2.h
  2. 3
    0
      libswan/include/swan/WorldPlane.h
  3. 23
    2
      libswan/src/WorldPlane.cc

+ 5
- 0
libswan/include/swan/Vector2.h View File

@@ -2,6 +2,7 @@

#include <utility>
#include <ostream>
#include <cmath>

namespace Swan {

@@ -18,6 +19,10 @@ struct Vector2 {
return *this;
}

constexpr T length() {
return (T)std::sqrt((double)(this->x * this->x + this->y * this->y));
}

constexpr operator std::pair<T, T>() const {
return std::pair<T, T>(x, y);
}

+ 3
- 0
libswan/include/swan/WorldPlane.h View File

@@ -8,6 +8,7 @@

#include "common.h"
#include "traits/BodyTrait.h"
#include "util.h"
#include "Chunk.h"
#include "Tile.h"
#include "WorldGen.h"
@@ -37,6 +38,8 @@ public:
Tile::ID getTileID(TilePos pos);
Tile &getTile(TilePos pos);

Iter<Entity *> getEntsInArea(Vec2 center, float radius);

BodyTrait::HasBody *spawnPlayer();
void breakBlock(TilePos pos);


+ 23
- 2
libswan/src/WorldPlane.cc View File

@@ -97,13 +97,34 @@ Tile::ID WorldPlane::getTileID(TilePos pos) {
active_chunks_.insert(&chunk);

return chunk.getTileID(relPos(pos));

}

Tile &WorldPlane::getTile(TilePos pos) {
return world_->getTileByID(getTileID(pos));
}

Iter<Entity *> WorldPlane::getEntsInArea(Vec2 center, float radius) {
// TODO: Optimize this using fancy data structures.
return mapFilter(entities_.begin(), entities_.end(), [=](std::unique_ptr<Entity> &ent)
-> std::optional<Entity *> {

// Filter out things which don't have bodies
auto *has_body = dynamic_cast<BodyTrait::HasBody *>(ent.get());
if (has_body == nullptr)
return std::nullopt;

// Filter out things which are too far away from 'center'
auto &body = has_body->getBody();
auto bounds = body.getBounds();
Vec2 entcenter = bounds.pos + (bounds.size / 2);
auto dist = (entcenter - center).length();
if (dist > radius)
return std::nullopt;

return ent.get();
});
}

BodyTrait::HasBody *WorldPlane::spawnPlayer() {
return gen_->spawnPlayer(*this);
}
@@ -164,7 +185,7 @@ void WorldPlane::update(float dt) {
spawn_list_.clear();

for (auto entptr: despawn_list_) {
for (auto it = std::begin(entities_); it != std::end(entities_); ++it) {
for (auto it = entities_.begin(); it != entities_.end(); ++it) {
if (it->get() == entptr) {
entities_.erase(it);
break;

Loading…
Cancel
Save