Advertisement
Cookie042

Spawner

Dec 4th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PrefabScatterSpawner : MonoBehaviour
  6. {
  7.     [System.Serializable]
  8.     public struct SpawnInfo
  9.     {
  10.         public float frequency;
  11.         public GameObject prefab;
  12.     }
  13.  
  14.     public List<GameObject> spawnList = new List<GameObject>();
  15.  
  16.     public int minSpawnCount, maxSpawnCount;
  17.  
  18.     public Vector2 spawnArea = Vector2.one;
  19.  
  20.     [Range(0,1)]
  21.     public float minSafeSpawnRadius= .5f;
  22.  
  23.     public float castStartHeight = 5;
  24.     [Range(0, 5)]
  25.     public float castOvershoot = 2;
  26.  
  27.     public int gizmoRaySamples = 4;
  28.     public float sampleRate = 2;
  29.  
  30.     public LayerMask RaycastHitMask;
  31.  
  32.     [System.Serializable]
  33.     public class GizmoColors
  34.     {
  35.         public Color areaColor = new Color(1f, 0.92f, 0.02f, 0.25f);
  36.         public Color areaOvershootColor = new Color(1f, 0.56f, 0.02f, 0.25f);
  37.         public Color raycastOriginColor = new Color(1f, 0f, 0f, 0.25f);
  38.         public Color raycastHitColor = new Color(1f, 0f, 0f, 0.52f);
  39.         public Color rayColor = new Color(0f, 1f, 0f, 0.25f);
  40.     }
  41.  
  42.     [SerializeField] GizmoColors gizmoColors = new GizmoColors();
  43.  
  44.     // Start is called before the first frame update
  45.     void Start()
  46.     {
  47.  
  48.     }
  49.  
  50.     // Update is called once per frame
  51.     void Update()
  52.     {
  53.  
  54.     }
  55.  
  56.     private void OnValidate()
  57.     {
  58.         t = sampleRate;
  59.     }
  60.  
  61.     public Vector3 GetRandomOffsetInSpawnArea()
  62.     {
  63.         float halfAreaY = spawnArea.y / 2;
  64.         float halfAreaX = spawnArea.x / 2;
  65.  
  66.         return new Vector3(Random.Range(-halfAreaX, halfAreaX), 0, Random.Range(-halfAreaY, halfAreaY));
  67.     }
  68.  
  69.     private float t = 0;
  70.     private List<Vector3> samplePoints = new List<Vector3>();
  71.     private void OnDrawGizmos()
  72.     {
  73.         Gizmos.color = gizmoColors.areaOvershootColor;
  74.         Gizmos.DrawWireCube(transform.position + Vector3.up * castStartHeight / 2, new Vector3(spawnArea.x, castStartHeight, spawnArea.y));
  75.  
  76.         Gizmos.color = gizmoColors.areaColor;
  77.         Gizmos.DrawWireCube(transform.position + Vector3.up * (castStartHeight - castOvershoot) / 2, new Vector3(spawnArea.x, castStartHeight + castOvershoot, spawnArea.y));
  78.  
  79.         Gizmos.color = gizmoColors.raycastOriginColor;
  80.         Gizmos.DrawWireSphere(transform.position + Vector3.up * castStartHeight, .1f);
  81.  
  82.  
  83.         foreach (var samplePoint in samplePoints)
  84.         {
  85.             Gizmos.color = gizmoColors.rayColor;
  86.             Gizmos.DrawRay(samplePoint, Vector3.down * (castStartHeight + castOvershoot));
  87.             if (Physics.Raycast(samplePoint, Vector3.down, out RaycastHit hit, castOvershoot + castStartHeight, RaycastHitMask))
  88.             {
  89.                 Gizmos.color = gizmoColors.raycastHitColor;
  90.                 //Gizmos.DrawWireSphere(hit.point, .1f);
  91.                 CookieGizmo.DrawWireCircle(hit.point + Vector3.up * .01f, minSafeSpawnRadius, Vector3.up, Vector3.forward );
  92.             }
  93.         }
  94.        
  95.         t += Time.deltaTime;
  96.         if (t < sampleRate)
  97.             return;
  98.  
  99.         t -= sampleRate;
  100.  
  101.         samplePoints.Clear();
  102.  
  103.         for (int i = 0; i < gizmoRaySamples; i++)
  104.         {
  105.             for (int attempt = 0; attempt < 10; attempt++)
  106.             {
  107.                 var randOffset = GetRandomOffsetInSpawnArea();
  108.                 var randomPosition = transform.position + Vector3.up * castStartHeight + randOffset;
  109.  
  110.                 bool tooClose = false;
  111.                 foreach (var validPoints in samplePoints)
  112.                 {
  113.                     if (Vector3.Distance(randomPosition, validPoints) < minSafeSpawnRadius*2)
  114.                     {
  115.                         tooClose = true;
  116.                         break;
  117.                     }
  118.                 }
  119.  
  120.                 if (!tooClose)
  121.                 {
  122.                     samplePoints.Add(randomPosition);
  123.                 }
  124.             }
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement