From 15b511e694f976686fdec1fb9f959f8a92f3b594 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 23 Feb 2019 12:02:00 -0500 Subject: More ranges stuff! Now with custom views --- src/entity.h | 11 +++ src/renderer.cpp | 4 +- src/simulation.cpp | 231 +++++++++++++++++++++++++---------------------------- src/simulation.h | 19 ++++- src/views.h | 62 ++++++++++++++ 5 files changed, 200 insertions(+), 127 deletions(-) create mode 100644 src/views.h diff --git a/src/entity.h b/src/entity.h index c47f1e7..3387219 100644 --- a/src/entity.h +++ b/src/entity.h @@ -2,13 +2,24 @@ #define ENTITY_H_0D6CB29A #include +#include #include "vector.h" #include "enums.h" #include "direction.h" +class Entity; + +using id_type = std::vector::size_type; + class Entity { public: + Entity(id_type id) : id(id) + { + } + + id_type id; + // Transform vec2s pos; vec2s size; diff --git a/src/renderer.cpp b/src/renderer.cpp index 64b3a8e..0feee50 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -76,10 +76,8 @@ void Renderer::render(const Simulation& sim) } } - for (Simulation::id_type id : sim.getActive()) + for (const Entity& entity : sim.getActive() | sim.entityView()) { - const Entity& entity = sim.getEntity(id); - SDL_SetRenderDrawColor(ren_.get(), entity.colorVal, entity.colorVal, 65, 255); SDL_Rect rect { diff --git a/src/simulation.cpp b/src/simulation.cpp index 77a9a3e..f75bd3d 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -1,8 +1,8 @@ #include "simulation.h" -#include #include "consts.h" #include "level.h" +#include "views.h" Simulation::Simulation( const Level& level) : @@ -61,14 +61,14 @@ Simulation::Simulation( train.gridPos = vec2s { 6, 1 }; train.moveDir = Direction::left; - id_type player = emplaceEntity(); - Entity& entity = getEntity(player); - entity.size = TILE_SIZE; - entity.speed = 3.0; - entity.controllable = true; - entity.colliderType = ColliderType::player; - entity.colorVal = 180; - entity.gridPos = vec2s { 1, 5 }; + id_type playerId = emplaceEntity(); + Entity& player = getEntity(playerId); + player.size = TILE_SIZE; + player.speed = 3.0; + player.controllable = true; + player.colliderType = ColliderType::player; + player.colorVal = 180; + player.gridPos = vec2s { 1, 5 }; id_type crateId = emplaceEntity(); Entity& crate = getEntity(crateId); @@ -91,11 +91,9 @@ Simulation::Simulation( crate2.gridPos = vec2s { 6, 7 }; - for (id_type id : active_) + for (Entity& entity : active_ | entityView()) { - Entity& entity = entities_.at(id); - - posCache_.set(id, entity.gridPos); + posCache_.set(entity.id, entity.gridPos); } } @@ -104,75 +102,73 @@ void Simulation::tick( const Uint8* keystate) { // Control - for (id_type id : active_) + for (Entity& entity : + active_ | + entityView() | + views::isControllable() | + views::isNotMoving()) { - Entity& entity = entities_.at(id); - - if (entity.controllable && - !entity.moving) + if (keystate[SDL_SCANCODE_LSHIFT] || keystate[SDL_SCANCODE_RSHIFT]) { - if (keystate[SDL_SCANCODE_LSHIFT] || keystate[SDL_SCANCODE_RSHIFT]) - { - Direction lookDir = Direction::none; + Direction lookDir = Direction::none; - if (keystate[SDL_SCANCODE_LEFT]) - { - lookDir = Direction::left; - } - else if (keystate[SDL_SCANCODE_UP]) - { - lookDir = Direction::up; - } - else if (keystate[SDL_SCANCODE_RIGHT]) - { - lookDir = Direction::right; - } - else if (keystate[SDL_SCANCODE_DOWN]) - { - lookDir = Direction::down; - } - - vec2s lookPos = posInDir(entity.gridPos, lookDir); + if (keystate[SDL_SCANCODE_LEFT]) + { + lookDir = Direction::left; + } + else if (keystate[SDL_SCANCODE_UP]) + { + lookDir = Direction::up; + } + else if (keystate[SDL_SCANCODE_RIGHT]) + { + lookDir = Direction::right; + } + else if (keystate[SDL_SCANCODE_DOWN]) + { + lookDir = Direction::down; + } - for (id_type blockId : posCache_.at(lookPos)) - { - Entity& block = entities_.at(blockId); + vec2s lookPos = posInDir(entity.gridPos, lookDir); - if (!block.moving && block.canBePushedBy.count(ColliderType::player)) - { - moveEntityOnGrid(blockId, lookDir); - } - } - } else { - if (keystate[SDL_SCANCODE_LEFT] && - moveEntityOnGrid(id, Direction::left, true)) - { - entity.shouldMoveTo = Direction::left; - } - else if (keystate[SDL_SCANCODE_UP] && - moveEntityOnGrid(id, Direction::up, true)) - { - entity.shouldMoveTo = Direction::up; - } - else if (keystate[SDL_SCANCODE_RIGHT] && - moveEntityOnGrid(id, Direction::right, true)) - { - entity.shouldMoveTo = Direction::right; - } - else if (keystate[SDL_SCANCODE_DOWN] && - moveEntityOnGrid(id, Direction::down, true)) - { - entity.shouldMoveTo = Direction::down; - } - else - { - entity.shouldMoveTo = Direction::none; - } + for (Entity& block : + posCache_.at(lookPos) | + entityView() | + views::isNotMoving() | + views::canBePushedBy(entity.colliderType) | + views::isOnLayer(entity.layer)) + { + moveEntityOnGrid(block, lookDir); + } + } else { + if (keystate[SDL_SCANCODE_LEFT] && + moveEntityOnGrid(entity, Direction::left, true)) + { + entity.shouldMoveTo = Direction::left; + } + else if (keystate[SDL_SCANCODE_UP] && + moveEntityOnGrid(entity, Direction::up, true)) + { + entity.shouldMoveTo = Direction::up; + } + else if (keystate[SDL_SCANCODE_RIGHT] && + moveEntityOnGrid(entity, Direction::right, true)) + { + entity.shouldMoveTo = Direction::right; + } + else if (keystate[SDL_SCANCODE_DOWN] && + moveEntityOnGrid(entity, Direction::down, true)) + { + entity.shouldMoveTo = Direction::down; + } + else + { + entity.shouldMoveTo = Direction::none; + } - if (entity.shouldMoveTo != Direction::none) - { - moveEntityOnGrid(id, entity.shouldMoveTo); - } + if (entity.shouldMoveTo != Direction::none) + { + moveEntityOnGrid(entity, entity.shouldMoveTo); } } } @@ -182,34 +178,33 @@ void Simulation::tick( if (schedule_.step()) { - for (id_type id : active_) + for (Entity& entity : + active_ | + entityView() | + views::isScheduled() | + views::isNotMoving()) { - Entity& entity = entities_.at(id); + auto tracks = + posCache_.at(entity.gridPos) | + entityView() | + views::isTrack(); - if (entity.scheduled && !entity.moving) + if (!ranges::empty(tracks)) { - auto tracks = - posCache_.at(entity.gridPos) | - ranges::view::transform([&] (id_type id) -> Entity& { return entities_.at(id); }) | - ranges::view::filter([&] (Entity& other) { return other.isTrack; }); - - if (!ranges::empty(tracks)) - { - Entity& track = ranges::front(tracks); - - Direction dir = Direction::none; - Direction from = oppositeDir(entity.moveDir); + Entity& track = ranges::front(tracks); - if (from == track.trackDir1) - { - dir = track.trackDir2; - } else if (from == track.trackDir2) - { - dir = track.trackDir1; - } + Direction dir = Direction::none; + Direction from = oppositeDir(entity.moveDir); - moveEntityOnGrid(id, dir); + if (from == track.trackDir1) + { + dir = track.trackDir2; + } else if (from == track.trackDir2) + { + dir = track.trackDir1; } + + moveEntityOnGrid(entity, dir); } } } @@ -226,10 +221,10 @@ void Simulation::tick( // Movement - for (id_type id : active_) + for (Entity& entity : + active_ | + entityView()) { - Entity& entity = entities_.at(id); - if (entity.moving) { entity.movementTween += entity.speed * dt; @@ -238,8 +233,8 @@ void Simulation::tick( { entity.moving = false; entity.gridPos = entity.destPos; - posCache_.set(id, entity.gridPos); - moveToCache_.remove(id); + posCache_.set(entity.id, entity.gridPos); + moveToCache_.remove(entity.id); } } @@ -258,7 +253,7 @@ void Simulation::tick( } } -Simulation::id_type Simulation::emplaceEntity() +id_type Simulation::emplaceEntity() { id_type nextId; @@ -267,10 +262,10 @@ Simulation::id_type Simulation::emplaceEntity() nextId = available_.front(); available_.pop_front(); - entities_.at(nextId) = Entity(); + entities_.at(nextId) = Entity(nextId); } else { nextId = entities_.size(); - entities_.emplace_back(); + entities_.emplace_back(nextId); } active_.insert(nextId); @@ -285,14 +280,12 @@ void Simulation::deleteEntity(id_type id) } bool Simulation::moveEntityOnGrid( - id_type id, + Entity& entity, Direction moveDir, bool validate) { bool actuallyMove = true; - Entity& entity = entities_.at(id); - vec2s shouldMoveTo = posInDir(entity.gridPos, moveDir); switch (moveDir) @@ -348,21 +341,17 @@ bool Simulation::moveEntityOnGrid( // Can't move into a space that something else is already moving into. if (!ranges::empty( moveToCache_.at(shouldMoveTo) | - ranges::view::transform([&] (id_type id) -> Entity& { return entities_.at(id); }) | - ranges::view::filter([&] (Entity& other) { return other.layer == entity.layer; }))) + entityView() | + views::isOnLayer(entity.layer))) { return false; } - for (id_type blockId : posCache_.at(shouldMoveTo)) + for (Entity& block : + posCache_.at(shouldMoveTo) | + entityView() | + views::isOnLayer(entity.layer)) { - Entity& block = entities_.at(blockId); - - if (block.layer != entity.layer) - { - continue; - } - if (!block.moving) { if (!block.canBePushedBy.count(entity.colliderType)) @@ -370,7 +359,7 @@ bool Simulation::moveEntityOnGrid( return false; } - if (!moveEntityOnGrid(blockId, moveDir, validate)) + if (!moveEntityOnGrid(block, moveDir, validate)) { return false; } @@ -401,7 +390,7 @@ bool Simulation::moveEntityOnGrid( entity.movementTween = 0.0; entity.moveDir = moveDir; - moveToCache_.set(id, entity.destPos); + moveToCache_.set(entity.id, entity.destPos); } return true; diff --git a/src/simulation.h b/src/simulation.h index fbe0a43..a205796 100644 --- a/src/simulation.h +++ b/src/simulation.h @@ -5,6 +5,7 @@ #include "renderer.h" #include "schedule.h" #include "grid_cache.h" +#include #include #include #include @@ -14,8 +15,6 @@ class Level; class Simulation { public: - using id_type = std::vector::size_type; - // Constructor explicit Simulation(const Level& level); @@ -47,13 +46,27 @@ public: return level_; } + auto entityView() + { + return ranges::view::transform([&] (id_type id) -> Entity& { + return entities_.at(id); + }); + } + + auto entityView() const + { + return ranges::view::transform([&] (id_type id) -> const Entity& { + return entities_.at(id); + }); + } + private: bool moveEntityOnGrid( - id_type id, + Entity& entity, Direction moveDir, bool validate = false); diff --git a/src/views.h b/src/views.h new file mode 100644 index 0000000..69bd2b5 --- /dev/null +++ b/src/views.h @@ -0,0 +1,62 @@ +#ifndef VIEWS_H_98651096 +#define VIEWS_H_98651096 + +#include "entity.h" +#include + +namespace views { + + inline auto isMoving() + { + return ranges::view::filter([] (const Entity& entity) { + return entity.moving; + }); + } + + inline auto isNotMoving() + { + return ranges::view::filter([] (const Entity& entity) { + return !entity.moving; + }); + } + + inline auto isControllable() + { + return ranges::view::filter([] (const Entity& entity) { + return entity.controllable; + }); + } + + inline auto isTrack() + { + return ranges::view::filter([] (const Entity& entity) { + return entity.isTrack; + }); + } + + inline auto isScheduled() + { + return ranges::view::filter([] (const Entity& entity) { + return entity.scheduled; + }); + } + + inline auto isOnLayer(Layer layer) + { + return ranges::view::filter([layer] (const Entity& entity) { + return entity.layer == layer; + }); + } + + inline auto canBePushedBy(ColliderType ctype) + { + return ranges::view::filter([ctype] (const Entity& entity) { + return entity.canBePushedBy.count(ctype); + }); + } + + + +} + +#endif /* end of include guard: VIEWS_H_98651096 */ -- cgit 1.4.1