Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Collections.ObjectModel;
- public class MapCreator : MonoBehaviour
- {
- //List of existing MapParts
- [SerializeField] List<GameObject> mapPrefabs = new List<GameObject>();
- //List of existing placement objects
- [SerializeField] List<GameObject> interiorObjecs = new List<GameObject>();
- public List<GameObject> activeMapParts = new List<GameObject>();
- public List<GameObject> inactiveMapParts = new List<GameObject>();
- //placment objects that are currenly active inb scene
- public List<GameObject> activeInteriorObjects = new List<GameObject>();
- //placement objects that are moved to the object collector
- public List<GameObject> inactiveInteriorObjects = new List<GameObject>();
- [SerializeField] GameObject objectCollector;
- [SerializeField] GameObject startMapHere;
- Vector3 lastPlacePos;
- int humanIdCount = 0;
- int mapPartIdCount = 1;
- //
- void Start()
- {
- lastPlacePos = startMapHere.transform.position;
- PopulateMapPartsPool();
- PopulateInteriorObjPool();
- PlaceStartMapParts();
- }//end start
- //Create starting MapObjects inside the ppol
- void PopulateMapPartsPool()
- {
- for (int i = 0; i < 20; i++)
- {
- GameObject mapPart;
- //Get MapPart
- int chooseMapP= ChooseMapPart();
- GameObject chosenMapPart = mapPrefabs[chooseMapP].gameObject;
- //Create new instance of MapPart
- mapPart = Instantiate(chosenMapPart, objectCollector.transform);
- mapPart.name = "MapPart " + mapPartIdCount;
- mapPart.SetActive(false);
- //Add Composition to list
- inactiveMapParts.Add(mapPart);
- mapPartIdCount ++;
- }
- }//PopulateMapPartsPool end
- int ChooseMapPart()
- {
- int number = Random.Range(0, mapPrefabs.Count);
- return number;
- }
- void PlaceStartMapParts()
- {
- for (int i = 1; i < 10; i++)
- {
- //place mapPart at last empty position
- PlaceMapPart();
- }
- }
- //Place a MapPart
- public void PlaceMapPart()
- {
- //Select instance of MapPart
- GameObject mapPart = inactiveMapParts[0];
- mapPart.transform.position = lastPlacePos;
- mapPart.SetActive(true);
- //Add MPart to active list
- activeMapParts.Add(mapPart);
- //Rem MPart from inactive list
- inactiveMapParts.RemoveAt(0);
- //Add Placable Objects to MapPart
- PlaceInteriorObj(activeMapParts[0]);
- lastPlacePos = mapPart.transform.GetChild(9).transform.position;
- }//end PlaceMapPart
- public void RemoveMapPart()
- {
- if(inactiveMapParts.Count < 5)
- { //move to ObjCollector
- activeMapParts[0].transform.position = objectCollector.transform.position;
- //Disable GameObject
- activeMapParts[0].gameObject.SetActive(false);
- //add MapP to inactive list
- inactiveMapParts.Add(activeMapParts[0]);
- //print(activeMapParts[0] + " Removed2");
- activeMapParts.RemoveAt(0);
- }
- }
- //----------Placeable Objects---------------------------------
- int ChooseInteriorObj()
- {
- int number = Random.Range(0, interiorObjecs.Count);
- return number;
- }
- void PopulateInteriorObjPool()
- {
- GameObject theObj = interiorObjecs[ChooseInteriorObj()];
- for (int i = 0; i < 9; i++)
- {
- Instantiate(theObj, GameManager.GMI.objectCollectorObj.transform);
- humanIdCount ++;
- theObj.GetComponent<IntriorObject>().idCount = humanIdCount;
- //theObj.GetComponent<IntriorObject>().placed = true;
- theObj.GetComponent<IntriorObject>().SetText();
- inactiveInteriorObjects.Add(theObj);
- }
- }//end PopulateInteriorObjPool
- void PlaceInteriorObj(GameObject mapPart)
- {
- for (int i = 0; i < 9; i++)
- {
- //dont spwawn everywhere
- if(RollIfPlaced())
- {
- Transform theTransform = mapPart.transform.GetChild(i).transform;
- //print(mapPart.transform.GetChild(i).transform.position);
- if(inactiveInteriorObjects.Count > 0 )
- {
- mapPart.transform.position = theTransform.transform.position;
- mapPart.SetActive(true);
- print("PO: " + inactiveInteriorObjects[0].GetComponentInChildren<TextMesh>().text + " placed on: " + mapPart.name + "child: " + mapPart.transform.GetChild(i));
- activeInteriorObjects.Add(inactiveInteriorObjects[0]);
- inactiveInteriorObjects.RemoveAt(0);
- }
- else
- {
- print("no POs in pool");
- }
- }//end if
- }//end for
- }//end PPO
- public void RemoveInteriorObj(GameObject item)
- {
- print("PO removed");
- item.transform.position = GameManager.GMI.objectCollectorPosition;
- item.transform.eulerAngles = new Vector3(-90,0,0);
- item.GetComponent<Rigidbody>().velocity = new Vector3(0,0,0);
- item.GetComponent<Rigidbody>().useGravity = false;
- item.GetComponent<Rigidbody>().isKinematic = true;
- //item.GetComponentInChildren<TextMesh>().text = "removed";
- item.SetActive(false);
- item.GetComponent<IntriorObject>().checkLeaving = false;
- inactiveInteriorObjects.Add(item);
- activeInteriorObjects.Remove(item);
- }//end PPO
- //Randomizes if object are placed or not
- bool RollIfPlaced()
- {
- int number = Random.Range(0, 50);
- if(number == 0) {return true;}
- else {return false;}
- }
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement