91 lines
2.9 KiB
C#
91 lines
2.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class ShadowArea : MonoBehaviour
|
|
{
|
|
public Vector3 m_areaSize = new Vector3(2f, 2f, 2f);
|
|
public LayerMask raycastMask = ~0;// = LayerMask.GetMask("Default","Ground");
|
|
public int m_numShadows = 3;
|
|
public int m_regenerateShadows = 0;
|
|
[SerializeField] private WonderingShadow.ShadowData m_shadowData;
|
|
[SerializeField] private WonderingShadow m_shadowPrefab;
|
|
|
|
public WonderingShadow ShadowPrefab => m_shadowPrefab;
|
|
public WonderingShadow.ShadowData ShadowData => m_shadowData;
|
|
|
|
IEnumerator DelayStart()
|
|
{
|
|
for (int i = 0; i < m_numShadows; i++)
|
|
{
|
|
yield return new WaitForSeconds(1f);
|
|
CreateShadow();
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
StartCoroutine(DelayStart());
|
|
}
|
|
|
|
public void CreateShadow()
|
|
{
|
|
var shadow = Instantiate<WonderingShadow>(m_shadowPrefab, GetRandomPosition(), Quaternion.identity, transform);
|
|
//shadow.m_spawnedObjectRayMask = raycastMask;
|
|
//shadow.m_targetPosition = GetRandomPosition();
|
|
//shadow.m_destroyOnTarget = true;
|
|
m_shadowData.targetPosition = GetRandomPosition();
|
|
shadow.m_shadowData = m_shadowData;
|
|
shadow.m_OnStateChanged.AddListener(OnShadowStateChanged);
|
|
}
|
|
|
|
void OnShadowStateChanged(WonderingShadow.State state)
|
|
{
|
|
if (m_regenerateShadows > 0 && state == WonderingShadow.State.GONE)
|
|
{
|
|
m_regenerateShadows--;
|
|
CreateShadow();
|
|
}
|
|
}
|
|
public Vector3 GetRandomPosition()
|
|
{
|
|
const float RAY_OFFSET = 30f;
|
|
const float Y_OFFSET = 1f;
|
|
Vector3 pos;
|
|
pos.x = Random.Range(-m_areaSize.x*0.5f, m_areaSize.x*0.5f);
|
|
pos.y = Random.Range(-m_areaSize.y*0.5f, m_areaSize.y*0.5f);
|
|
pos.z = Random.Range(-m_areaSize.z*0.5f, m_areaSize.z*0.5f);
|
|
pos += transform.position;
|
|
pos.y += RAY_OFFSET;
|
|
RaycastHit hit = new RaycastHit();
|
|
if (Physics.Raycast(pos, Vector3.down, out hit, 100f, raycastMask))
|
|
{
|
|
pos.y -= RAY_OFFSET;
|
|
if (pos.y < hit.point.y + Y_OFFSET)
|
|
{
|
|
pos.y = hit.point.y + Y_OFFSET;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("WonderingShadow error: raycast failed");
|
|
}
|
|
return pos;
|
|
}
|
|
|
|
void OnDrawGizmosSelected()
|
|
{
|
|
//if (Application.isPlaying)
|
|
// return;
|
|
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawWireCube(transform.position, m_areaSize );
|
|
|
|
}
|
|
}
|
|
}
|