Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.19 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class LevelLayoutGenerator : MonoBehaviour {
  6.  
  7.     public LevelModule starterRoom;
  8.     public int iterCount;
  9.     public int RNGSeed;
  10.     public bool purgeExits; // MatchExits also deactivates them
  11.  
  12.     public LevelModule[] prefabList; // list of prefabs to choose from
  13.  
  14.     private Vector3 POS_ORIGIN = new Vector3(0f,0f,0f);
  15.  
  16.     private List<LevelModule> levelGraph = new List<LevelModule>(); // Spawned modules
  17.     private List<ModuleExit> availableExits = new List<ModuleExit>(); // will hold the exits for each iteration
  18.  
  19.  
  20.     void Start()
  21.     {  
  22.         Debug.Log("Starting Level Generator");
  23.  
  24.         // SET THE RNG SEED
  25.         //SetRandomGeneratorSeed();
  26.  
  27.         // Place starter room in the graph and add its exits
  28.         LevelModule starter = (LevelModule)Instantiate(starterRoom,transform.position,transform.rotation);
  29.         LevelModule oldModule = starter;
  30.  
  31.         availableExits.AddRange(starter.GetExitList());
  32.  
  33.         // SET THE RNG SEED MANUALLY
  34.         Random.InitState(RNGSeed);
  35.  
  36.         // Start the iteration loop to fill exits
  37.         for (int iter = 0; iter <= iterCount; iter++)
  38.         {
  39.             //yield return new WaitForSeconds(1);
  40.  
  41.             List<ModuleExit> newExits = new List<ModuleExit>();
  42.  
  43.             // Add a random prefab room to each available exit in the graph
  44.             foreach (ModuleExit currExit in availableExits)
  45.             {
  46.                 // Spawn a random prefab for the selected exit, push to newExits and mark them as connected
  47.                 LevelModule newModule = ChooseRandomModule(prefabList);
  48.  
  49.                 newModule = (LevelModule)Instantiate(newModule);
  50.                 newExits.AddRange(newModule.GetExitList());
  51.  
  52.                 ModuleExit newExit = ChooseRandomExit(newModule.GetExitList());
  53.  
  54.                 MatchExits(currExit,newExit,purgeExits);
  55.  
  56.                 // Check for collision
  57.  
  58.             }
  59.  
  60.             // Replace the initial available exits with the new list
  61.             RebuildExitList(newExits);
  62.         }
  63.     }
  64.  
  65.     // METHODS
  66.  
  67.     LevelModule ChooseRandomModule(LevelModule[] prefabs)
  68.     {
  69.         int index;
  70.  
  71.         index = Random.Range(0,prefabs.Length);
  72.  
  73.         return prefabs[index];
  74.     }
  75.  
  76.     ModuleExit ChooseRandomExit(ModuleExit[] exit)
  77.     {
  78.         int index;
  79.  
  80.         index = Random.Range(0, exit.Length);
  81.  
  82.         return exit[index];
  83.     }
  84.        
  85.  
  86.  
  87.     void SetRandomGeneratorSeed() // Sets seed to a known function
  88.     {
  89.         // This chooses the seed method
  90.         Random.InitState(System.DateTime.Now.Millisecond);
  91.  
  92.     }
  93.  
  94.  
  95.     void SetRandomGeneratorSeed(int seed) // When seed is provided
  96.     {
  97.         Random.InitState(seed);
  98.     }
  99.  
  100.     private static float Azimuth(Vector3 vector)
  101.     {
  102.         return Vector3.Angle(Vector3.forward, vector) * Mathf.Sign(vector.x);
  103.     }
  104.  
  105.     private void MatchExits(ModuleExit oldExit, ModuleExit newExit, bool purge)
  106.     {
  107.         var newModule = newExit.transform.parent;
  108.         var forwardVectorToMatch = -oldExit.transform.forward;
  109.         var correctiveRotation = Azimuth(forwardVectorToMatch) - Azimuth(newExit.transform.forward);
  110.         newModule.RotateAround(newExit.transform.position, Vector3.up, correctiveRotation);
  111.         var correctiveTranslation = oldExit.transform.position - newExit.transform.position;
  112.         newModule.transform.position += correctiveTranslation;
  113.  
  114.         // Deactivate exits test functionality
  115.         if (purge)
  116.         {
  117.             newExit.enabled = false;
  118.             oldExit.enabled = false;
  119.  
  120.             Destroy(newExit);
  121.             Destroy(oldExit);
  122.         }
  123.     }
  124.  
  125.     private void RebuildExitList(List<ModuleExit> newEx)
  126.     {
  127.         availableExits.Clear();
  128.  
  129.         foreach (ModuleExit ex in newEx)
  130.         {
  131.             if (!ex.isMatched)
  132.             {
  133.                 availableExits.Add(ex);
  134.             }
  135.         }
  136.     }
  137.  
  138.     private bool CheckOverlaps(LevelModule newModule, ModuleExit oldExit)
  139.     {
  140.         // Ignores the new and old modules and checks for overlaps
  141.         // with other objects
  142.  
  143.         LevelModule oldModule = oldExit.GetComponentInParent<LevelModule>();
  144.  
  145.         if (oldModule == null)
  146.         {
  147.             Debug.Log("Can't find a LevelModule component in this exit!");
  148.         }
  149.  
  150.         // Set a temporary ignore mask for both
  151.         int ignoreMask = LayerMask.GetMask("TempIgnoreCollision");
  152.  
  153.         newModule.gameObject.layer = ignoreMask;
  154.         oldModule.gameObject.layer = ignoreMask;
  155.  
  156.         Collider[] newColliders = Physics.OverlapBox(newModule.transform.position, transform.localScale / 2, transform.rotation,ignoreMask);
  157.  
  158.         if (newColliders.Length != 0 )
  159.         {
  160.             return true;
  161.         }
  162.  
  163.         else
  164.             return false;
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement