I'm working on a game that is controlled like an RTS, ie left-click and drag a box to select units, right click to issue a move command, or press a hotkey and left-click to issue some ability command. You can also hold shift to queue commands after the current one (move here, then attack this building).
My question is how I can detect when a command finishes so I can start executing the next one. My current implementation is as follows:
I have Command structs that just hold data (MoveCommand holds a world position, AttackCommand holds the id of the unit to attack, etc).
I have a CommandRunner that has a method for each type of command. These methods return a boolean that is true if the command finished, or false otherwise.
I have a CommandManager that has a map that maps IDs to linked lists of commands.
My algorithm is to loop through CommandManager's map. For each element, execute the first command in the list using the CommandRunner. If it returns true, pop it off the front of the list.
This works great, except that my conditions for a command being done aren't good enough. For example, my naive first attempt for MoveCommands was just if( myPosition == goal ). This works for a single unit, but obviously adding any more creates problems since most of the units can't get to their goal. They end up just sitting around the point jostling to get closer, but never actually reaching it.
Obviously the conditions are easy for some commands ( AttackCommand would be if( target.dead() ) ), but I'm not sure of the best way for a Movement Command. I'm thinking I'll have to implement some flocking behaviors, since it seems it would be more reliable to check if the flock as a whole has reached the destination.