diff options
-rw-r--r-- | src/character_system.cpp | 94 | ||||
-rw-r--r-- | src/character_system.h | 2 | ||||
-rw-r--r-- | src/sprite.h | 1 |
3 files changed, 61 insertions, 36 deletions
diff --git a/src/character_system.cpp b/src/character_system.cpp index 8a2034a..bc02e6c 100644 --- a/src/character_system.cpp +++ b/src/character_system.cpp | |||
@@ -11,6 +11,7 @@ void CharacterSystem::initSprite(int spriteId, int movementSpeed) { | |||
11 | Sprite& sprite = game_.getSprite(spriteId); | 11 | Sprite& sprite = game_.getSprite(spriteId); |
12 | sprite.orientable = true; | 12 | sprite.orientable = true; |
13 | sprite.movementSpeed = movementSpeed; | 13 | sprite.movementSpeed = movementSpeed; |
14 | sprite.trailsAreHalved = false; | ||
14 | } | 15 | } |
15 | 16 | ||
16 | void CharacterSystem::addSpriteToParty(int leaderId, int followerId) { | 17 | void CharacterSystem::addSpriteToParty(int leaderId, int followerId) { |
@@ -48,6 +49,7 @@ void CharacterSystem::transplantParty(int leaderId, vec2i pos, Direction dir) { | |||
48 | 49 | ||
49 | std::vector<int> followers = leader.followers; | 50 | std::vector<int> followers = leader.followers; |
50 | leader.followers.clear(); | 51 | leader.followers.clear(); |
52 | leader.trailsAreHalved = false; | ||
51 | 53 | ||
52 | game_.getSystem<TransformSystem>().moveSprite(leaderId, pos); | 54 | game_.getSystem<TransformSystem>().moveSprite(leaderId, pos); |
53 | game_.getSystem<AnimationSystem>().setSpriteDirection(leaderId, dir); | 55 | game_.getSystem<AnimationSystem>().setSpriteDirection(leaderId, dir); |
@@ -123,6 +125,7 @@ void CharacterSystem::tick(double dt) { | |||
123 | continue; | 125 | continue; |
124 | } | 126 | } |
125 | 127 | ||
128 | // Make sure this matches up with the code in adjustPartyTrails(). | ||
126 | int speed = sprite.movementSpeed; | 129 | int speed = sprite.movementSpeed; |
127 | if (sprite.characterState == CharacterState::Running && sprite.characterMedium == CharacterMedium::Normal) { | 130 | if (sprite.characterState == CharacterState::Running && sprite.characterMedium == CharacterMedium::Normal) { |
128 | speed *= 2; | 131 | speed *= 2; |
@@ -167,6 +170,7 @@ void CharacterSystem::tick(double dt) { | |||
167 | if (newMedium != sprite.characterMedium) { | 170 | if (newMedium != sprite.characterMedium) { |
168 | sprite.characterMedium = newMedium; | 171 | sprite.characterMedium = newMedium; |
169 | setAnimationFor(spriteId, sprite.characterState); | 172 | setAnimationFor(spriteId, sprite.characterState); |
173 | adjustPartyTrails(spriteId); | ||
170 | } | 174 | } |
171 | 175 | ||
172 | if (sprite.characterState == CharacterState::Running) { | 176 | if (sprite.characterState == CharacterState::Running) { |
@@ -227,49 +231,15 @@ void CharacterSystem::endCrouch(int spriteId) { | |||
227 | } | 231 | } |
228 | 232 | ||
229 | void CharacterSystem::startRunning(int spriteId) { | 233 | void CharacterSystem::startRunning(int spriteId) { |
230 | Sprite& sprite = game_.getSprite(spriteId); | ||
231 | setPartyState(spriteId, CharacterState::Running); | 234 | setPartyState(spriteId, CharacterState::Running); |
232 | 235 | adjustPartyTrails(spriteId); | |
233 | for (int followerId : sprite.followers) { | ||
234 | // Halve the movement buffer for the followers. | ||
235 | Sprite& follower = game_.getSprite(followerId); | ||
236 | std::deque<Movement> newMove; | ||
237 | |||
238 | while (!follower.trail.empty()) { | ||
239 | newMove.push_back(follower.trail.front()); | ||
240 | follower.trail.pop_front(); | ||
241 | follower.trail.pop_front(); | ||
242 | } | ||
243 | |||
244 | follower.trail = std::move(newMove); | ||
245 | } | ||
246 | } | 236 | } |
247 | 237 | ||
248 | void CharacterSystem::stopRunning(int spriteId) { | 238 | void CharacterSystem::stopRunning(int spriteId) { |
249 | Sprite& sprite = game_.getSprite(spriteId); | 239 | Sprite& sprite = game_.getSprite(spriteId); |
250 | setPartyState(spriteId, CharacterState::Still); | 240 | setPartyState(spriteId, CharacterState::Still); |
251 | stopRunningSound(sprite); | 241 | stopRunningSound(sprite); |
252 | 242 | adjustPartyTrails(spriteId); | |
253 | // Double the movement buffer for the followers. | ||
254 | for (int followerId : sprite.followers) { | ||
255 | Sprite& follower = game_.getSprite(followerId); | ||
256 | |||
257 | std::deque<Movement> newMove; | ||
258 | vec2i lastPos = follower.loc; | ||
259 | |||
260 | while (!follower.trail.empty()) { | ||
261 | Movement m1 = follower.trail.front(); | ||
262 | Movement m2 = m1; | ||
263 | m1.pos = (m1.pos + lastPos) / 2; | ||
264 | lastPos = m2.pos; | ||
265 | |||
266 | newMove.push_back(m1); | ||
267 | newMove.push_back(m2); | ||
268 | follower.trail.pop_front(); | ||
269 | } | ||
270 | |||
271 | follower.trail = std::move(newMove); | ||
272 | } | ||
273 | } | 243 | } |
274 | 244 | ||
275 | void CharacterSystem::setPartyState(int spriteId, CharacterState state) { | 245 | void CharacterSystem::setPartyState(int spriteId, CharacterState state) { |
@@ -355,3 +325,55 @@ void CharacterSystem::setAnimationFor(int spriteId, CharacterState state) { | |||
355 | } | 325 | } |
356 | } | 326 | } |
357 | } | 327 | } |
328 | |||
329 | void CharacterSystem::adjustPartyTrails(int spriteId) { | ||
330 | Sprite& sprite = game_.getSprite(spriteId); | ||
331 | |||
332 | // Trails should be halved if the effective moving speed is twice the normal | ||
333 | // speed, which right now is whenever you are running and not on a ladder. | ||
334 | bool shouldBeHalved = false; | ||
335 | if (sprite.characterState == CharacterState::Running && sprite.characterMedium == CharacterMedium::Normal) { | ||
336 | shouldBeHalved = true; | ||
337 | } | ||
338 | |||
339 | if (sprite.trailsAreHalved != shouldBeHalved) { | ||
340 | sprite.trailsAreHalved = shouldBeHalved; | ||
341 | |||
342 | if (shouldBeHalved) { | ||
343 | // Halve the movement buffer for the followers. | ||
344 | for (int followerId : sprite.followers) { | ||
345 | Sprite& follower = game_.getSprite(followerId); | ||
346 | std::deque<Movement> newMove; | ||
347 | |||
348 | while (!follower.trail.empty()) { | ||
349 | newMove.push_back(follower.trail.front()); | ||
350 | follower.trail.pop_front(); | ||
351 | follower.trail.pop_front(); | ||
352 | } | ||
353 | |||
354 | follower.trail = std::move(newMove); | ||
355 | } | ||
356 | } else { | ||
357 | // Double the movement buffer for the followers. | ||
358 | for (int followerId : sprite.followers) { | ||
359 | Sprite& follower = game_.getSprite(followerId); | ||
360 | |||
361 | std::deque<Movement> newMove; | ||
362 | vec2i lastPos = follower.loc; | ||
363 | |||
364 | while (!follower.trail.empty()) { | ||
365 | Movement m1 = follower.trail.front(); | ||
366 | Movement m2 = m1; | ||
367 | m1.pos = (m1.pos + lastPos) / 2; | ||
368 | lastPos = m2.pos; | ||
369 | |||
370 | newMove.push_back(m1); | ||
371 | newMove.push_back(m2); | ||
372 | follower.trail.pop_front(); | ||
373 | } | ||
374 | |||
375 | follower.trail = std::move(newMove); | ||
376 | } | ||
377 | } | ||
378 | } | ||
379 | } | ||
diff --git a/src/character_system.h b/src/character_system.h index ef49d0e..0962a6a 100644 --- a/src/character_system.h +++ b/src/character_system.h | |||
@@ -50,6 +50,8 @@ private: | |||
50 | // state should be the party leader's state | 50 | // state should be the party leader's state |
51 | void setAnimationFor(int spriteId, CharacterState state); | 51 | void setAnimationFor(int spriteId, CharacterState state); |
52 | 52 | ||
53 | void adjustPartyTrails(int spriteId); | ||
54 | |||
53 | Game& game_; | 55 | Game& game_; |
54 | Timer inputTimer_ {33}; | 56 | Timer inputTimer_ {33}; |
55 | }; | 57 | }; |
diff --git a/src/sprite.h b/src/sprite.h index 3c2eade..bb78442 100644 --- a/src/sprite.h +++ b/src/sprite.h | |||
@@ -83,6 +83,7 @@ public: | |||
83 | int movementSpeed = 0; // 1 is slow (good for NPCs), 2 is Lucas's default walking speed | 83 | int movementSpeed = 0; // 1 is slow (good for NPCs), 2 is Lucas's default walking speed |
84 | std::vector<int> followers; | 84 | std::vector<int> followers; |
85 | std::deque<Movement> trail; | 85 | std::deque<Movement> trail; |
86 | bool trailsAreHalved = false; | ||
86 | CharacterState characterState = CharacterState::Still; | 87 | CharacterState characterState = CharacterState::Still; |
87 | CharacterMedium characterMedium = CharacterMedium::Normal; | 88 | CharacterMedium characterMedium = CharacterMedium::Normal; |
88 | Direction movementDir = Direction::down; | 89 | Direction movementDir = Direction::down; |