Skip to content

Commit

Permalink
minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
feresr committed Aug 22, 2020
1 parent 56f5c81 commit 065810b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
7 changes: 6 additions & 1 deletion include/ecs/ecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,14 @@ class World {
for (auto entity : entities) if (entity->has<Components...>()) callback(entity);
}

template<typename... Components>
void findAny(const std::function<void(Entity*)>& callback) {
for (auto entity : entities) if (entity->hasAny<Components...>()) callback(entity);
}

template<typename... Components>
std::vector<Entity*> findAny() {
// todo: Optimize, avoid creating a new vector an returning it by copy
//todo: optimize, this should return an iterator and not a new vector
std::vector<Entity*> result;
std::copy_if(
entities.begin(),
Expand Down
16 changes: 8 additions & 8 deletions src/systems/CallbackSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ void CallbackSystem::onAddedToWorld(World* world) {
}

void CallbackSystem::tick(World* world) {
for (auto entities : world->find<CallbackComponent>()) {
auto callback = entities->get<CallbackComponent>();
if (callback->time <= 0) {
entities->remove<CallbackComponent>();
continue;
world->find<CallbackComponent>([](Entity* entity){
auto callback = entity->get<CallbackComponent>();
if (callback->time > 0) {
callback->time--;
if (callback->time == 0) callback->callback();
} else {
entity->remove<CallbackComponent>();
}
callback->time--;
if (callback->time <= 0) callback->callback();
}
});
}

void CallbackSystem::handleEvent(SDL_Event& event) {
Expand Down
17 changes: 9 additions & 8 deletions src/systems/PlayerSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ void PlayerSystem::onGameOver(World* world, Entity* player) {
kinetic->accY = 0.0f;
kinetic->accX = 0.0f;

for (auto walkable : world->findAny<WalkComponent, KineticComponent>()) {
if (walkable == player) continue;
walkable->remove<WalkComponent>();
walkable->remove<KineticComponent>();
walkable->remove<AnimationComponent>();
walkable->remove<CallbackComponent>();
}
world->findAny<WalkComponent, KineticComponent>([=](Entity* walker) {
if (walker != player) {
walker->remove<WalkComponent>();
walker->remove<KineticComponent>();
walker->remove<AnimationComponent>();
walker->remove<CallbackComponent>();
}
});

player->assign<CallbackComponent>([=]() {
player->remove<SolidComponent>();
Expand All @@ -110,7 +111,7 @@ void PlayerSystem::onGameOver(World* world, Entity* player) {
kinetic->accY = 0.0f;
kinetic->accX = 0.0f;

player->assign<CallbackComponent>([=]() { gameOverCallback(); }, 200);
player->assign<CallbackComponent>([&]() { gameOverCallback(); }, 200);
}, 50);
}

Expand Down

0 comments on commit 065810b

Please sign in to comment.