Recommended main loop style

Posted by Frootmig-H on Game Development See other posts from Game Development or by Frootmig-H
Published on 2013-11-08T13:10:54Z Indexed on 2013/11/08 16:18 UTC
Read the original article Hit count: 392

Filed under:
|

I've just begun attempting an FPS with JMonkeyEngine, but I'm currently stuck as to the best way to implement the main loop - especially with regards to non-instantaneous user actions. By that, I mean things like reloading a weapon. The user starts the action, and it continues for a while with an animation and some sound, and when it completes, game state updates. (I should mention that it's not technically a loop, it's an update method, called as often as possible. Is that different? Me no understand terminology). So, far I've considered :

Animation driven

  • Player presses reload
  • Start reload animation
  • If user stars another action, abort animation, start new action.
  • When the animation_complete event is received (JMonkeyEngine provides this), update ammo counters.

Event driven

  • Player presses reload
  • Start reload animation
  • Queue up a out-of-thread method to be called at time t + (duration of reload animation)
  • If user starts another action, cancel both animation and queued method. When queued method executes, update ammo.

This avoids relying on the animation event (JMonkeyEngine has a particular quirk), but brings in the possibility of thread problems.

'Blocking' (not sure of the correct term)

  • Player presses reload
  • Start reloading animation
  • reloading = true
  • reloadedStartTime = now
  • while (reloading && ((now - reloadingStartTime) < reloadingDuration)) {

    If user starts another action, break and cancel reloading.

    }

  • Update ammo counters
  • reloading = false

My main concern is that actions can interrupt each other. Reloading can be interrupted by firing, or by dropping or changing weapon, crouching can be interrupted by running, etc.

What's the recommended way to handle this? What are the advantages and disadvantages of each method? I'm leaning towards event-driven, even though it requires more care; failing that, blocking.

© Game Development or respective owner

Related posts about input

Related posts about game-loop