Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class FamillyManager : MonoBehaviour {
- private int nextFamillyIndex = 0;
- public static FamillyManager Instance {get; private set;}
- public Dictionary<int, List<GameObject>> famillyDictionnary;
- public Dictionary<int, string> famillySurNameDictionary;
- public Dictionary<int, List<FamilyNode>> familyNodeDictionary;
- private List<string> surNameList;
- void Awake(){
- Instance = this;
- famillyDictionnary = new Dictionary<int, List<GameObject>>(15);
- famillySurNameDictionary = new Dictionary<int, string>(15);
- familyNodeDictionary = new Dictionary<int, List<FamilyNode>>(15);
- surNameList = new List<string>(NPCNames.surName);
- }
- public int RequestNextFamilly(){
- nextFamillyIndex++;
- return nextFamillyIndex;
- }
- public void RegisterMember(FamilyNode citizen){
- int famillyIndex = citizen.FamilyIndex;
- if(famillyIndex < 0){
- famillyIndex = RequestNextFamilly();
- }
- if(familyNodeDictionary.ContainsKey(famillyIndex)){
- familyNodeDictionary[famillyIndex].Add(citizen);
- }
- else{
- familyNodeDictionary.Add (famillyIndex, new List<FamilyNode>(5));
- familyNodeDictionary[famillyIndex].Add(citizen);
- if(surNameList.Count > 0){
- int nameIndex = Random.Range(0, surNameList.Count);
- famillySurNameDictionary.Add(famillyIndex, surNameList[nameIndex]);
- surNameList.RemoveAt(nameIndex);
- }
- }
- }
- }
- [System.Serializable]
- public class FamilyNode {
- public int FamilyIndex;
- public int Generation;
- public GameObject Citizen;
- public FamilyNode father;
- public FamilyNode spouse;
- public FamilyNode(FamilyNode father, GameObject citizen){
- FamilyIndex = father.FamilyIndex;
- this.Generation = father.Generation + 1;
- this.father = father;
- this.Citizen = citizen;
- }
- public FamilyNode(GameObject citizen){
- FamilyIndex = FamillyManager.Instance.RequestNextFamilly();
- this.Generation = 0;
- father = null;
- this.Citizen = citizen;
- }
- public FamilyNode(GameObject citizen, FamilyNode spouse){
- FamilyIndex = spouse.FamilyIndex;
- this.spouse = spouse;
- spouse.spouse = this;
- Generation = 0;
- this.Citizen = citizen;
- }
- public bool isImmediateFamily(FamilyNode ToSearch){
- bool isTrue = false;
- if(ToSearch.FamilyIndex != this.FamilyIndex){
- return false;
- }
- else{
- if(ToSearch.father != null){
- if(ToSearch.father.Equals(this.father) || ToSearch.father.Equals(this)){
- isTrue = true;
- }
- }
- if(ToSearch.Equals(this.father)){
- isTrue = true;
- }
- if(ToSearch.spouse != null){
- if(ToSearch.spouse.Equals(this) || this.spouse.Equals(ToSearch)){
- isTrue = true;
- }
- }
- if(this.spouse != null && (this.spouse.Equals(ToSearch) || this.spouse.Equals(ToSearch.father))){
- isTrue = true;
- }
- }
- return isTrue;
- }
- public FamilyNode[] FindImmediateFamily(){
- List<FamilyNode> newList = new List<FamilyNode>(5);
- if(!FamillyManager.Instance.familyNodeDictionary.ContainsKey(FamilyIndex)){
- return null;
- }
- else{
- foreach(FamilyNode node in FamillyManager.Instance.familyNodeDictionary[FamilyIndex]){
- //Debug.Log(node.Citizen.name);
- if(!node.Equals(this)){
- if(isImmediateFamily(node)){
- newList.Add(node);
- }
- }
- }
- if(newList.Count > 0){
- return newList.ToArray();
- }
- else{
- return null;
- }
- }
- }
- public void AssignHouseToImmediateFamily(HouseScript hs){
- FamilyNode[] closeFamily = FindImmediateFamily();
- for(int i = 0 ; i < closeFamily.Length ; i++){
- if(closeFamily[i].Citizen != null){
- CitizenAI cs = closeFamily[i].Citizen.GetComponent<CitizenAI>();
- ChildAI cai = closeFamily[i].Citizen.GetComponent<ChildAI>();
- if(AreMarried(closeFamily[i])){
- cs.assignHouseScript(hs);
- if(cs.state == CitizenAI.State.LookingForHome){
- cs.resetState();
- }
- }
- else{
- if(cs != null){
- hs.addAdultToResidents(closeFamily[i].Citizen);
- if(cs.state == CitizenAI.State.LookingForHome){
- cs.resetState();
- }
- }
- else{
- cai.assignHouse(hs);
- }
- }
- }
- }
- LogFamilyNodesOnHouseDetection(this);
- }
- /// <summary>
- /// Checks if nodeToCheck node is child of caller
- /// </summary>
- /// <returns>
- /// True if the node to check is a child of this node
- /// </returns>
- /// <param name='nodeToCheck'>
- /// <c>true</c> if nodeToCheck is a child of caller
- /// </param>
- public bool isParentOf(FamilyNode nodeToCheck){
- if(nodeToCheck.father == null){
- return false;
- }
- else{
- if(nodeToCheck.father.Equals (this)){
- return true;
- }
- else if(nodeToCheck.father.spouse != null && nodeToCheck.father.spouse.Equals(this)){
- return true;
- }
- else{
- return false;
- }
- }
- }
- public bool AreMarried(FamilyNode nodeToCheck){
- if(nodeToCheck.spouse != null && nodeToCheck.spouse.Equals(this)){
- return true;
- }
- else if(this.spouse != null && this.spouse.Equals(nodeToCheck)){
- return true;
- }
- else{
- return false;
- }
- }
- private void LogFamilyNodesOnHouseDetection(FamilyNode root){
- FamilyNode[] nodes = FindImmediateFamily();
- FileIO.WriteInfo("House Request from Family: " + root.FamilyIndex.ToString() + "\" " + FamillyManager.Instance.famillySurNameDictionary[root.FamilyIndex] + "\" {");
- FileIO.WriteInfo("\tFounder of House(" + root.Citizen.GetComponent<CitizenAI>().isMale + "): " + root.Citizen.GetComponent<CitizenAI>().citizenName);
- foreach(FamilyNode node in nodes) {
- CitizenAI cs = node.Citizen.GetComponent<CitizenAI>();
- ChildAI cai = node.Citizen.GetComponent<ChildAI>();
- if(cs != null){
- FileIO.WriteInfo("\tSpouse: " + cs.citizenName +", should not look for a house. Current state: " + cs.state.ToString() + " HomeName= " + cs.homeLocation.position);
- }
- else{
- FileIO.WriteInfo("\tChild: " + cai.citizenName);
- }
- }
- FileIO.WriteInfo("}\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement