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/simulation.cpp | 231 +++++++++++++++++++++++++---------------------------- 1 file changed, 110 insertions(+), 121 deletions(-) (limited to 'src/simulation.cpp') 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; -- cgit 1.4.1