Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Flocking
  5. {
  6.     public class Mission
  7.     {
  8.         public string missionName;
  9.        
  10.         private List<SubMission> subMissions;   // submissions that must be completed one after the other
  11.         private List<Goal.State> status;        // status of each submission
  12.        
  13.         private int activeIndex = 0; // index of the active sub-mission
  14.        
  15.         public Mission()
  16.         {
  17.             subMissions = new List<SubMission>();
  18.             status = new List<Goal.State>();
  19.         }
  20.        
  21.         /// <summary>
  22.         /// Adds a sub mission to this mission.
  23.         /// </summary>
  24.         public void AddSubMission(SubMission s)
  25.         {
  26.             subMissions.Add(s);
  27.             status.Add(Goal.State.InProgress);
  28.         }
  29.        
  30.         public void Update(float dt)
  31.         {
  32.             if( GetState() != Goal.State.Accomplished )
  33.             {
  34.                 // update the active sub-mission
  35.                 var activeSubMission = subMissions[activeIndex];
  36.                 activeSubMission.Update(dt);
  37.                
  38.                 // if the active sub-mission was completed, move on to the next
  39.                 if( activeSubMission.GetState() == Goal.State.Accomplished )
  40.                 {
  41.                     status[activeIndex] = Goal.State.Accomplished;
  42.                     ++activeIndex;
  43.                 }
  44.                 else if( activeSubMission.GetState() == Goal.State.Failed )
  45.                 {
  46.                     status[activeIndex] = Goal.State.Failed;
  47.                     ++activeIndex;
  48.                 }
  49.                    
  50.             }
  51.         }
  52.        
  53.         public Goal.State GetState()
  54.         {
  55.             // first, check the list of accomplished/failed missions. If one of those has Failed, return Failed
  56.             for(int i = 0; i < activeIndex; ++i)
  57.                 if( status[i] == Goal.State.Failed )
  58.                     return Goal.State.Failed;
  59.            
  60.             // to determine if the mission is still in progress, check the last's sub-missions status
  61.             // if it's In-Progress, the whole mission is in progress
  62.             if( status[status.Count - 1] == Goal.State.InProgress )
  63.                 return Goal.State.InProgress;
  64.            
  65.             // if we reached this point, the mission was successful
  66.             return Goal.State.Accomplished;
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement