Recommended main loop style
- by Frootmig-H
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.