summary refs log tree commit diff stats
path: root/src/simulation.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2019-02-24 11:56:41 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2019-02-24 11:56:41 -0500
commit3adca6fad7e32e05edfc07ba4d445442c950eec8 (patch)
tree0a2ee1209857a797ae4652355518ce4e1154d538 /src/simulation.cpp
parentbd603e58152d0b84f72c5461012fab9046411fc8 (diff)
downloaddispatcher-3adca6fad7e32e05edfc07ba4d445442c950eec8.tar.gz
dispatcher-3adca6fad7e32e05edfc07ba4d445442c950eec8.tar.bz2
dispatcher-3adca6fad7e32e05edfc07ba4d445442c950eec8.zip
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.
Diffstat (limited to 'src/simulation.cpp')
-rw-r--r--src/simulation.cpp49
1 files changed, 22 insertions, 27 deletions
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(
128 views::canBePushedBy(entity.colliderType) | 128 views::canBePushedBy(entity.colliderType) |
129 views::isOnLayer(entity.layer)) 129 views::isOnLayer(entity.layer))
130 { 130 {
131 moveEntityOnGrid(block, lookDir); 131 block.shouldMoveDir.push_back(lookDir);
132 } 132 }
133 } else { 133 } else {
134 if (keystate[SDL_SCANCODE_LEFT] && 134 if (keystate[SDL_SCANCODE_LEFT])
135 moveEntityOnGrid(entity, Direction::left, true))
136 {
137 entity.shouldMoveTo = Direction::left;
138 }
139 else if (keystate[SDL_SCANCODE_UP] &&
140 moveEntityOnGrid(entity, Direction::up, true))
141 {
142 entity.shouldMoveTo = Direction::up;
143 }
144 else if (keystate[SDL_SCANCODE_RIGHT] &&
145 moveEntityOnGrid(entity, Direction::right, true))
146 { 135 {
147 entity.shouldMoveTo = Direction::right; 136 entity.shouldMoveDir.push_back(Direction::left);
148 } 137 }
149 else if (keystate[SDL_SCANCODE_DOWN] && 138
150 moveEntityOnGrid(entity, Direction::down, true)) 139 if (keystate[SDL_SCANCODE_UP])
151 { 140 {
152 entity.shouldMoveTo = Direction::down; 141 entity.shouldMoveDir.push_back(Direction::up);
153 } 142 }
154 else 143
144 if (keystate[SDL_SCANCODE_RIGHT])
155 { 145 {
156 entity.shouldMoveTo = Direction::none; 146 entity.shouldMoveDir.push_back(Direction::right);
157 } 147 }
158 148
159 if (entity.shouldMoveTo != Direction::none) 149 if (keystate[SDL_SCANCODE_DOWN])
160 { 150 {
161 moveEntityOnGrid(entity, entity.shouldMoveTo); 151 entity.shouldMoveDir.push_back(Direction::down);
162 } 152 }
163 } 153 }
164 } 154 }
@@ -181,19 +171,15 @@ void Simulation::tick(
181 if (!ranges::empty(tracks)) 171 if (!ranges::empty(tracks))
182 { 172 {
183 Entity& track = ranges::front(tracks); 173 Entity& track = ranges::front(tracks);
184
185 Direction dir = Direction::none;
186 Direction from = oppositeDir(entity.moveDir); 174 Direction from = oppositeDir(entity.moveDir);
187 175
188 if (from == track.trackDir1) 176 if (from == track.trackDir1)
189 { 177 {
190 dir = track.trackDir2; 178 entity.shouldMoveDir.push_back(track.trackDir2);
191 } else if (from == track.trackDir2) 179 } else if (from == track.trackDir2)
192 { 180 {
193 dir = track.trackDir1; 181 entity.shouldMoveDir.push_back(track.trackDir1);
194 } 182 }
195
196 moveEntityOnGrid(entity, dir);
197 } 183 }
198 } 184 }
199 } 185 }
@@ -212,6 +198,15 @@ void Simulation::tick(
212 // Movement 198 // Movement
213 for (Entity& entity : entityRange()) 199 for (Entity& entity : entityRange())
214 { 200 {
201 while (!entity.moving && !entity.shouldMoveDir.empty())
202 {
203 moveEntityOnGrid(entity, entity.shouldMoveDir.front());
204
205 entity.shouldMoveDir.pop_front();
206 }
207
208 entity.shouldMoveDir.clear();
209
215 if (entity.moving) 210 if (entity.moving)
216 { 211 {
217 entity.movementTween += entity.speed * dt; 212 entity.movementTween += entity.speed * dt;