From 3adca6fad7e32e05edfc07ba4d445442c950eec8 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 24 Feb 2019 11:56:41 -0500 Subject: Refactored how movement is started The systems other than movement add directions to a list, and then movement pops elements off until it can start moving in that direction. This adds some more abstraction, because now only the movement system has to call moveEntityOnGrid. --- src/simulation.cpp | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) (limited to 'src/simulation.cpp') diff --git a/src/simulation.cpp b/src/simulation.cpp index 326526f..a43a4e8 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -128,37 +128,27 @@ void Simulation::tick( views::canBePushedBy(entity.colliderType) | views::isOnLayer(entity.layer)) { - moveEntityOnGrid(block, lookDir); + block.shouldMoveDir.push_back(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)) + if (keystate[SDL_SCANCODE_LEFT]) { - entity.shouldMoveTo = Direction::right; + entity.shouldMoveDir.push_back(Direction::left); } - else if (keystate[SDL_SCANCODE_DOWN] && - moveEntityOnGrid(entity, Direction::down, true)) + + if (keystate[SDL_SCANCODE_UP]) { - entity.shouldMoveTo = Direction::down; + entity.shouldMoveDir.push_back(Direction::up); } - else + + if (keystate[SDL_SCANCODE_RIGHT]) { - entity.shouldMoveTo = Direction::none; + entity.shouldMoveDir.push_back(Direction::right); } - if (entity.shouldMoveTo != Direction::none) + if (keystate[SDL_SCANCODE_DOWN]) { - moveEntityOnGrid(entity, entity.shouldMoveTo); + entity.shouldMoveDir.push_back(Direction::down); } } } @@ -181,19 +171,15 @@ void Simulation::tick( if (!ranges::empty(tracks)) { Entity& track = ranges::front(tracks); - - Direction dir = Direction::none; Direction from = oppositeDir(entity.moveDir); if (from == track.trackDir1) { - dir = track.trackDir2; + entity.shouldMoveDir.push_back(track.trackDir2); } else if (from == track.trackDir2) { - dir = track.trackDir1; + entity.shouldMoveDir.push_back(track.trackDir1); } - - moveEntityOnGrid(entity, dir); } } } @@ -212,6 +198,15 @@ void Simulation::tick( // Movement for (Entity& entity : entityRange()) { + while (!entity.moving && !entity.shouldMoveDir.empty()) + { + moveEntityOnGrid(entity, entity.shouldMoveDir.front()); + + entity.shouldMoveDir.pop_front(); + } + + entity.shouldMoveDir.clear(); + if (entity.moving) { entity.movementTween += entity.speed * dt; -- cgit 1.4.1