Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.02 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Collections.ObjectModel;
  5.  
  6. public class MapCreator : MonoBehaviour
  7. {
  8.     //List of existing MapParts
  9.     [SerializeField] List<GameObject> mapPrefabs = new List<GameObject>();
  10.     //List of existing placement objects
  11.     [SerializeField] List<GameObject> interiorObjecs = new List<GameObject>();
  12.  
  13.  
  14.      public List<GameObject> activeMapParts = new List<GameObject>();
  15.      public List<GameObject> inactiveMapParts = new List<GameObject>();
  16.  
  17.     //placment objects that are currenly active inb scene
  18.     public List<GameObject> activeInteriorObjects = new List<GameObject>();
  19.     //placement objects that are moved to the object collector
  20.     public List<GameObject> inactiveInteriorObjects = new List<GameObject>();
  21.  
  22.  
  23.      [SerializeField] GameObject objectCollector;
  24.      [SerializeField] GameObject startMapHere;
  25.  
  26.     Vector3 lastPlacePos;
  27.  
  28.     int humanIdCount = 0;
  29.     int mapPartIdCount = 1;
  30.  
  31.     //
  32.     void Start()
  33.     {
  34.         lastPlacePos = startMapHere.transform.position;
  35.  
  36.         PopulateMapPartsPool();
  37.         PopulateInteriorObjPool();
  38.  
  39.         PlaceStartMapParts();
  40.     }//end start
  41.  
  42.     //Create starting MapObjects inside the ppol
  43.     void PopulateMapPartsPool()
  44.     {
  45.         for (int i = 0; i < 20; i++)
  46.         {
  47.             GameObject mapPart;
  48.             //Get MapPart
  49.             int chooseMapP= ChooseMapPart();
  50.             GameObject chosenMapPart = mapPrefabs[chooseMapP].gameObject;
  51.  
  52.             //Create new instance of MapPart
  53.             mapPart = Instantiate(chosenMapPart, objectCollector.transform);
  54.  
  55.             mapPart.name = "MapPart " + mapPartIdCount;
  56.             mapPart.SetActive(false);
  57.            
  58.             //Add Composition to list
  59.             inactiveMapParts.Add(mapPart);
  60.  
  61.             mapPartIdCount ++;
  62.         }
  63.     }//PopulateMapPartsPool end
  64.  
  65.     int ChooseMapPart()
  66.     {
  67.         int number = Random.Range(0, mapPrefabs.Count);
  68.         return number;
  69.     }
  70.  
  71.     void PlaceStartMapParts()
  72.     {
  73.         for (int i = 1; i < 10; i++)
  74.         {
  75.             //place mapPart at last empty position
  76.             PlaceMapPart();
  77.         }
  78.     }
  79.  
  80.     //Place a MapPart
  81.     public void PlaceMapPart()
  82.     {
  83.         //Select instance of MapPart
  84.         GameObject mapPart = inactiveMapParts[0];
  85.  
  86.         mapPart.transform.position = lastPlacePos;
  87.         mapPart.SetActive(true);
  88.        
  89.         //Add MPart to active list
  90.         activeMapParts.Add(mapPart);
  91.  
  92.         //Rem MPart from inactive list
  93.         inactiveMapParts.RemoveAt(0);
  94.  
  95.         //Add Placable Objects to MapPart
  96.         PlaceInteriorObj(activeMapParts[0]);
  97.  
  98.         lastPlacePos = mapPart.transform.GetChild(9).transform.position;
  99.  
  100.     }//end PlaceMapPart
  101.  
  102.     public void RemoveMapPart()
  103.     {
  104.         if(inactiveMapParts.Count < 5)
  105.         {   //move to ObjCollector
  106.             activeMapParts[0].transform.position = objectCollector.transform.position;
  107.            
  108.             //Disable GameObject
  109.             activeMapParts[0].gameObject.SetActive(false);
  110.  
  111.             //add MapP to inactive list
  112.             inactiveMapParts.Add(activeMapParts[0]);
  113.  
  114.             //print(activeMapParts[0] + " Removed2");
  115.             activeMapParts.RemoveAt(0);
  116.         }
  117.     }
  118.  
  119.  
  120.     //----------Placeable Objects---------------------------------
  121.  
  122.    
  123.     int ChooseInteriorObj()
  124.     {
  125.         int number = Random.Range(0, interiorObjecs.Count);
  126.         return number;
  127.     }
  128.  
  129.     void PopulateInteriorObjPool()
  130.     {
  131.         GameObject theObj = interiorObjecs[ChooseInteriorObj()];
  132.        
  133.         for (int i = 0; i < 9; i++)
  134.         {
  135.             Instantiate(theObj, GameManager.GMI.objectCollectorObj.transform);
  136.             humanIdCount ++;
  137.             theObj.GetComponent<IntriorObject>().idCount = humanIdCount;
  138.             //theObj.GetComponent<IntriorObject>().placed = true;
  139.             theObj.GetComponent<IntriorObject>().SetText();
  140.  
  141.             inactiveInteriorObjects.Add(theObj);
  142.         }
  143.     }//end PopulateInteriorObjPool
  144.  
  145.     void PlaceInteriorObj(GameObject mapPart)
  146.     {
  147.         for (int i = 0; i < 9; i++)
  148.         {
  149.             //dont spwawn everywhere
  150.             if(RollIfPlaced())
  151.             {
  152.                 Transform theTransform = mapPart.transform.GetChild(i).transform;
  153.                 //print(mapPart.transform.GetChild(i).transform.position);
  154.  
  155.                 if(inactiveInteriorObjects.Count > 0 )
  156.                 {
  157.                     mapPart.transform.position = theTransform.transform.position;
  158.                     mapPart.SetActive(true);
  159.  
  160.                     print("PO: " +  inactiveInteriorObjects[0].GetComponentInChildren<TextMesh>().text + " placed on: " + mapPart.name + "child: " + mapPart.transform.GetChild(i));
  161.  
  162.                     activeInteriorObjects.Add(inactiveInteriorObjects[0]);
  163.                     inactiveInteriorObjects.RemoveAt(0);
  164.                 }
  165.                 else
  166.                 {
  167.                     print("no POs in pool");
  168.                 }
  169.  
  170.             }//end if
  171.         }//end for
  172.     }//end PPO
  173.  
  174.     public void RemoveInteriorObj(GameObject item)
  175.     {
  176.         print("PO removed");
  177.         item.transform.position = GameManager.GMI.objectCollectorPosition;
  178.         item.transform.eulerAngles = new Vector3(-90,0,0);
  179.         item.GetComponent<Rigidbody>().velocity = new Vector3(0,0,0);
  180.         item.GetComponent<Rigidbody>().useGravity = false;
  181.         item.GetComponent<Rigidbody>().isKinematic = true;
  182.         //item.GetComponentInChildren<TextMesh>().text = "removed";
  183.  
  184.         item.SetActive(false);
  185.         item.GetComponent<IntriorObject>().checkLeaving = false;
  186.  
  187.         inactiveInteriorObjects.Add(item);
  188.         activeInteriorObjects.Remove(item);
  189.     }//end PPO
  190.  
  191.    
  192.     //Randomizes if object are placed or not
  193.     bool RollIfPlaced()
  194.     {
  195.         int number = Random.Range(0, 50);
  196.         if(number == 0) {return true;}
  197.         else {return false;}  
  198.     }
  199.  
  200.  
  201. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement