Advertisement
ensiferum888

FamilyManager

Jul 14th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.94 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class FamillyManager : MonoBehaviour {
  6.     private int nextFamillyIndex = 0;
  7.    
  8.     public static FamillyManager Instance {get; private set;}
  9.     public Dictionary<int, List<GameObject>> famillyDictionnary;
  10.     public Dictionary<int, string> famillySurNameDictionary;
  11.     public Dictionary<int, List<FamilyNode>> familyNodeDictionary;
  12.     private List<string> surNameList;
  13.    
  14.     void Awake(){
  15.         Instance = this;   
  16.         famillyDictionnary = new Dictionary<int, List<GameObject>>(15);
  17.         famillySurNameDictionary = new Dictionary<int, string>(15);
  18.         familyNodeDictionary = new Dictionary<int, List<FamilyNode>>(15);
  19.         surNameList = new List<string>(NPCNames.surName);
  20.     }
  21.    
  22.     public int RequestNextFamilly(){
  23.         nextFamillyIndex++;
  24.         return nextFamillyIndex;
  25.     }
  26.    
  27.     public void RegisterMember(FamilyNode citizen){
  28.         int famillyIndex = citizen.FamilyIndex;
  29.         if(famillyIndex < 0){
  30.             famillyIndex = RequestNextFamilly();
  31.         }
  32.         if(familyNodeDictionary.ContainsKey(famillyIndex)){
  33.             familyNodeDictionary[famillyIndex].Add(citizen);   
  34.         }
  35.         else{
  36.             familyNodeDictionary.Add (famillyIndex, new List<FamilyNode>(5));
  37.             familyNodeDictionary[famillyIndex].Add(citizen);
  38.             if(surNameList.Count > 0){
  39.                 int nameIndex = Random.Range(0, surNameList.Count);
  40.                 famillySurNameDictionary.Add(famillyIndex, surNameList[nameIndex]);
  41.                 surNameList.RemoveAt(nameIndex);
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. [System.Serializable]
  48. public class FamilyNode {
  49.     public int FamilyIndex;
  50.     public int Generation;
  51.    
  52.     public GameObject Citizen; 
  53.    
  54.     public FamilyNode father;
  55.     public FamilyNode spouse;
  56.  
  57.    
  58.     public FamilyNode(FamilyNode father, GameObject citizen){
  59.         FamilyIndex = father.FamilyIndex;
  60.         this.Generation = father.Generation + 1;
  61.         this.father = father;
  62.         this.Citizen = citizen;
  63.     }
  64.    
  65.     public FamilyNode(GameObject citizen){
  66.         FamilyIndex = FamillyManager.Instance.RequestNextFamilly();
  67.         this.Generation = 0;
  68.         father = null;
  69.         this.Citizen = citizen;
  70.     }
  71.    
  72.     public FamilyNode(GameObject citizen, FamilyNode spouse){
  73.         FamilyIndex = spouse.FamilyIndex;
  74.         this.spouse = spouse;
  75.         spouse.spouse = this;
  76.         Generation = 0;
  77.         this.Citizen = citizen;
  78.     }
  79.    
  80.     public bool isImmediateFamily(FamilyNode ToSearch){
  81.         bool isTrue = false;
  82.         if(ToSearch.FamilyIndex != this.FamilyIndex){
  83.             return false;  
  84.         }
  85.         else{
  86.             if(ToSearch.father != null){
  87.                 if(ToSearch.father.Equals(this.father) || ToSearch.father.Equals(this)){
  88.                     isTrue = true; 
  89.                 }
  90.             }
  91.             if(ToSearch.Equals(this.father)){
  92.                 isTrue = true; 
  93.             }
  94.             if(ToSearch.spouse != null){
  95.                 if(ToSearch.spouse.Equals(this) || this.spouse.Equals(ToSearch)){
  96.                     isTrue = true;
  97.                 }
  98.             }
  99.             if(this.spouse != null && (this.spouse.Equals(ToSearch) || this.spouse.Equals(ToSearch.father))){
  100.                 isTrue = true;
  101.             }
  102.         }
  103.         return isTrue;
  104.     }
  105.    
  106.     public FamilyNode[] FindImmediateFamily(){
  107.         List<FamilyNode> newList = new List<FamilyNode>(5);
  108.         if(!FamillyManager.Instance.familyNodeDictionary.ContainsKey(FamilyIndex)){
  109.             return null;   
  110.         }
  111.         else{
  112.             foreach(FamilyNode node in FamillyManager.Instance.familyNodeDictionary[FamilyIndex]){
  113.                 //Debug.Log(node.Citizen.name);
  114.                 if(!node.Equals(this)){
  115.                     if(isImmediateFamily(node)){
  116.                         newList.Add(node); 
  117.                     }
  118.                 }
  119.             }
  120.             if(newList.Count > 0){
  121.                 return newList.ToArray();  
  122.             }
  123.             else{
  124.                 return null;   
  125.             }
  126.         }
  127.     }
  128.    
  129.     public void AssignHouseToImmediateFamily(HouseScript hs){
  130.         FamilyNode[] closeFamily = FindImmediateFamily();
  131.         for(int i = 0 ; i < closeFamily.Length ; i++){
  132.             if(closeFamily[i].Citizen != null){
  133.                 CitizenAI cs = closeFamily[i].Citizen.GetComponent<CitizenAI>();
  134.                 ChildAI cai = closeFamily[i].Citizen.GetComponent<ChildAI>();
  135.                 if(AreMarried(closeFamily[i])){
  136.                     cs.assignHouseScript(hs);  
  137.                     if(cs.state == CitizenAI.State.LookingForHome){
  138.                         cs.resetState();   
  139.                     }
  140.                 }
  141.                 else{
  142.                     if(cs != null){
  143.                         hs.addAdultToResidents(closeFamily[i].Citizen);
  144.                         if(cs.state == CitizenAI.State.LookingForHome){
  145.                             cs.resetState();   
  146.                         }
  147.                     }
  148.                     else{
  149.                         cai.assignHouse(hs);
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.         LogFamilyNodesOnHouseDetection(this);
  155.     }
  156.    
  157.     /// <summary>
  158.     /// Checks if nodeToCheck node is child of caller
  159.     /// </summary>
  160.     /// <returns>
  161.     /// True if the node to check is a child of this node
  162.     /// </returns>
  163.     /// <param name='nodeToCheck'>
  164.     /// <c>true</c> if nodeToCheck is a child of caller
  165.     /// </param>
  166.     public bool isParentOf(FamilyNode nodeToCheck){
  167.         if(nodeToCheck.father == null){
  168.             return false;  
  169.         }
  170.         else{
  171.             if(nodeToCheck.father.Equals (this)){
  172.                 return true;   
  173.             }
  174.             else if(nodeToCheck.father.spouse != null && nodeToCheck.father.spouse.Equals(this)){
  175.                 return true;   
  176.             }
  177.             else{
  178.                 return false;  
  179.             }
  180.         }
  181.     }
  182.    
  183.     public bool AreMarried(FamilyNode nodeToCheck){
  184.         if(nodeToCheck.spouse != null && nodeToCheck.spouse.Equals(this)){
  185.             return true;   
  186.         }
  187.         else if(this.spouse != null && this.spouse.Equals(nodeToCheck)){
  188.             return true;   
  189.         }
  190.         else{
  191.             return false;  
  192.         }
  193.     }
  194.    
  195.     private void LogFamilyNodesOnHouseDetection(FamilyNode root){
  196.         FamilyNode[] nodes = FindImmediateFamily();
  197.         FileIO.WriteInfo("House Request from Family: " + root.FamilyIndex.ToString() + "\" " + FamillyManager.Instance.famillySurNameDictionary[root.FamilyIndex] + "\" {");
  198.         FileIO.WriteInfo("\tFounder of House(" + root.Citizen.GetComponent<CitizenAI>().isMale + "): " + root.Citizen.GetComponent<CitizenAI>().citizenName);
  199.         foreach(FamilyNode node in nodes) {
  200.             CitizenAI cs = node.Citizen.GetComponent<CitizenAI>();
  201.             ChildAI cai = node.Citizen.GetComponent<ChildAI>();
  202.             if(cs != null){
  203.                 FileIO.WriteInfo("\tSpouse: " + cs.citizenName +", should not look for a house. Current state: " + cs.state.ToString() + " HomeName= " + cs.homeLocation.position);
  204.             }
  205.             else{
  206.                 FileIO.WriteInfo("\tChild: " + cai.citizenName);
  207.             }
  208.         }
  209.         FileIO.WriteInfo("}\n");   
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement