- Home /
How to Instantiate a Combat Manager to handle attack order?
So In this game, when 2 enemy units collide, they enter melee combat. The game is RTSish, so any number of melee combats could be occurring simultaneously. When in combat, I want to ensure that units do not just hack away at each other, but rather that when one is attacking, the other "receives" the attack (in order to run a blocking animation etc.)
My idea here, is to have a "combat manager" that essentially contains a list of attacks. The manager would build this list based on unit attack speed, so that when a unit is ready to attack, it adds an action to the combat manager. Then the combat manager, pops the top action off the list, listens for it to resolve, then pops the next, and so on.
My question is, when two units run into each other, how do I handle instantiating an instance of this combat manager for that specific melee? I had though to call it on the units "EnterMelee" method, but how do I ensure that both units don't call a combat manager, and that they instead share one?
I'm writing this in c#, but i'm looking for more conceptual advice than straight code.
Answer by AlwaysSunny · Nov 08, 2014 at 03:53 PM
I like high-level managerial classes, so I'd make mine a singleton, and let it resolve Conflicts. Each agent entering conflict would signal the manager. The method arguments would include a reference to the agent and its combatant. If there's no conflict in the list between those two agents, create a new Conflict object.
A calls with (A,B);
Manager creates Conflict;
B calls with (B,A);
Conflict already exists so do nothing;
A loop on the manager runs to resolve active conflicts. The order-of-attack and other mechanics are thus separated from the lets-do-combat logic. The Conflict object could examine whatever logic necessary, have its own timers, whatever's needed.
Doesn't that create a race condition? If a and b enter at the same time it would always spawn 2 instances right? so the situation where a spawns instance and then b joins would never work?
Nothing ever happens "at the same time" in computer science! ;)
A triggers the manager to create a Conflict object representing (A,B) which is entered into the list. When B triggers the manager to create Conflict (B,A) immediately thereafter, all you must do is exa$$anonymous$$e existing conflicts prior to creating one to find that a conflict between these two already exists. B doesn't need to "join" - it's involved because A's call included it as an argument.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Melee Combat 1 Answer
Procedual Wall Generation 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer