Using a dictionary of two enums
Well, I'm making a space shooter. Sometimes my ships create a formation. They do this by targeting a gameobject MainFormationObject that stores an array of empty gameobject WayPoints. The ships target the MainFormationObject and this object tells the ship which WayPoint will it target. For the sake of keeping it short, I'll call the ship that is targetting the leader waypoint the Leader and the ships that are targeting the escort waypoints the Escorts. Now I'm trying to break the formation when certain conditions are met (by breaking the formation I mean: tell the ships to not target these WayPoints anymore / change their target).
Basically, in the inspector I want a way of having the drop down menus for "What triggers the break of the formation" and then selecting by another drop down menu "How does the formation breaks" so its kind of user friendly.
I thought of using two enums for that. The enums would look something like this:
1) What triggers the break of the formation {
leader gets fired at,
escort gets fired at,
leader dies,
escort dies,
formation changes target,
formation's target gets destroyed }
and
2) How does the formation breaks {
escorts will target the ship that is firing at leader (only used if the leader is getting fired at),
make all the ships get a different target,
leader gets a different target,
escorts get a different target,
do nothing }
I didnt know how to link one enum (lets call it the battleEvent) to the other enum (lets call it the reaction), but I asked on a different topic and I found out about dictionaries and using this code structure I was able to throw in a key to the dictionary (the battleEvent) and make it spit a value (the reaction) but then I don't know what to do with it.
public enum BattleEvent
{
LeaderFiredAt,
EscortFiredAt,
LeaderDied,
EscortDied,
FormationTargetChanged,
FormationTargetDestroyed
}
public enum Reaction
{
DefendLeader,
FireAtWill,
LeaderRetarget,
EscortRetarget,
None
}
public Reaction LeaderFiredAtReaction;
public Reaction EscortFiredAtReaction;
public Reaction LeaderDiedReaction;
public Reaction EscortDiedReaction;
public Reaction FormationTargetChangedReaction;
public Reaction FormationTargetDestroyedReaction;
public Dictionary<BattleEvent, Reaction> ReactionMap =
new Dictionary<BattleEvent, Reaction>();
void Start () {
ReactionMap.Add(BattleEvent.LeaderFiredAt, LeaderFiredAtReaction);
ReactionMap.Add(BattleEvent.EscortFiredAt, EscortFiredAtReaction);
ReactionMap.Add(BattleEvent.LeaderDied, LeaderDiedReaction);
ReactionMap.Add(BattleEvent.EscortDied, EscortDiedReaction);
ReactionMap.Add(BattleEvent.FormationTargetChanged, FormationTargetChangedReaction);
ReactionMap.Add(BattleEvent.FormationTargetDestroyed, FormationTargetDestroyedReaction);
}
public Reaction GetReactionTo(BattleEvent bEvent)
{
var react = Reaction.None;
ReactionMap.TryGetValue(bEvent, out react);
return react;
}
I have code for the reactions (as an instance: making the Leader target something else, and all the others) but I dont know where to put it. Can you give me a hand with some pseudocode?
Thank you in advance for your time :)