Geek Culture
Published in

Geek Culture

Turn-Based Tactical Command System, Part 2

Leveraging Linked Lists for Lightweight Loops

Poor Yellow.

Welcome to part 2. The objective before us: take all the information we collected in the previous four articles (the initiative list, the turn list, the action lists) and use that data to move the proper pieces on the map in the proper sequence.

The bulk of the process happens here, in the ProcessRoundRoutine coroutine:

This can easily be chopped up into three bites:

Finding the Front of Each List

This is pretty self explanatory. At the start of the round we need to be indexed to the front of each list.

Then we start a while loop, which will continue until the Round list is empty. Inside it, we do two things:

Executing the Current Action

Here we’re checking to see what sort of information we’ve been passed, and sending the appropriate overload to the ExecuteAction() method:

If we get a feat and a target, then that’s one of three targeted feats: Move a movable object, Break a breakable object, Save a save-able object. For now, I just have Move defined.

If we get a feat with no target, then that’s one of two untargeted feats: Panic in place, or take a moment to try and Loot an object in spite of present danger. For now we express those behaviors in the debug log.

Finally, if we are passed only a target, that must be the player requesting the move of an actor. All we need to do is signal the selected actor to complete the move.

Traversing the Lists

At last, we get to the lists. Here’s the pseudocode I worked this up from:

set current action index to the next action node
if the action node IS NOT null, we have another action
…remove the previous action node from the action list
if the action node IS null, we’re at the end of the action list
…clear the action list
…set the current turn index to the next turn node
…if the current turn node IS NOT null, we have another turn
……remove the previous turn node from the turn list
……set the current action index to the first node of the new turn’s action list
…if the current turn node IS null, we are all out of turns
……clear the turn list

If you are at the end of a LinkedList when you call for the next node, you get a null value. So, we just nudge the list index along until it comes up null, and when it does we clear the list. When both lists are clear the round is over.

Putting It All Together

To get it all running, we wrap this whole process up in a coroutine.

Let’s see it all in action:

You can see some of the debug information on the bottom. I haven’t done much to display what’s going on in the interface yet, but that’ll change. What’s important is that we can queue multiple actions from multiple actors and have them play out in sequence. Exactly what we wanted.

That’s all for now. The next article will focus on cleaning up this implementation into something a bit more elegant and streamlined.

Thanks for reading!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Micha Davis

Micha Davis

Unity Developer / Game Developer / Artist / Problem Solver