118 lines
3.9 KiB
C#
118 lines
3.9 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
// 一次只有一隻機器人,倒下後在隨機位置重生下一隻
|
|
// 掛在場景裡的空物件上,物件的位置就是生成範圍的中心
|
|
public class BotSpawner : MonoBehaviour
|
|
{
|
|
[Header("參照")]
|
|
[Tooltip("機器人 prefab (要有 BotBrain、BotHealth)")]
|
|
public BotBrain botPrefab;
|
|
|
|
[Tooltip("玩家 (機器人會面向它)")]
|
|
public Transform player;
|
|
|
|
[Header("生成範圍")]
|
|
[Tooltip("以這個物件為中心的半徑 (m),要比場地小,避免生成在牆裡")]
|
|
public float spawnRadius = 15f;
|
|
|
|
[Tooltip("離玩家的最小距離 (m)")]
|
|
public float minDistanceFromPlayer = 8f;
|
|
|
|
[Header("重生")]
|
|
[Tooltip("倒下後多久生成下一隻 (秒)")]
|
|
public float respawnDelay = 1.5f;
|
|
|
|
// 目前活著的機器人,倒下到重生之間是 null (給方位指示等 UI 用)
|
|
public BotHealth CurrentBot { get; private set; }
|
|
|
|
// 新的一隻生成時通知 (記錄器用來開始一回合)
|
|
public event System.Action<BotHealth> BotSpawned;
|
|
|
|
void Start()
|
|
{
|
|
// 拖進來的是 prefab 檔案本身 (不在場景裡) 或沒拖 : 改用場景裡的玩家 (只有玩家身上有 PlayerLook)
|
|
if (player == null || !player.gameObject.scene.IsValid())
|
|
{
|
|
PlayerLook look = FindAnyObjectByType<PlayerLook>();
|
|
player = look != null ? look.transform.root : null;
|
|
}
|
|
|
|
Spawn();
|
|
}
|
|
|
|
// 給其他腳本找場景裡的 BotSpawner : 沒拖、或拖進來的是 prefab 檔案本身 (不在場景裡) 都改用場景裡的那一個
|
|
// prefab 存不住場景物件的參照,而且 Inspector 很容易不小心拖到 Project 視窗裡的 prefab
|
|
public static BotSpawner FindInScene(BotSpawner assigned)
|
|
{
|
|
if (assigned != null && assigned.gameObject.scene.IsValid()) return assigned;
|
|
return FindAnyObjectByType<BotSpawner>();
|
|
}
|
|
|
|
void Spawn()
|
|
{
|
|
if (botPrefab == null) return;
|
|
|
|
BotBrain bot = Instantiate(botPrefab, PickSpawnPoint(), Quaternion.identity);
|
|
bot.target = player;
|
|
bot.areaCenter = transform.position;
|
|
bot.areaRadius = spawnRadius;
|
|
|
|
if (bot.TryGetComponent(out BotHealth health))
|
|
{
|
|
health.Died += OnBotDied;
|
|
CurrentBot = health;
|
|
BotSpawned?.Invoke(health);
|
|
}
|
|
}
|
|
|
|
void OnBotDied(BotHealth bot)
|
|
{
|
|
bot.Died -= OnBotDied;
|
|
CurrentBot = null;
|
|
StartCoroutine(RespawnAfterDelay());
|
|
}
|
|
|
|
IEnumerator RespawnAfterDelay()
|
|
{
|
|
yield return new WaitForSeconds(respawnDelay);
|
|
Spawn();
|
|
}
|
|
|
|
// 在範圍內隨機選點 : 要離玩家夠遠,而且不能卡在障礙物或牆裡 ; 試很多次都不行就用最後一次的結果
|
|
Vector3 PickSpawnPoint()
|
|
{
|
|
Vector3 point = transform.position;
|
|
|
|
for (int i = 0; i < 50; i++)
|
|
{
|
|
Vector2 offset = Random.insideUnitCircle * spawnRadius;
|
|
point = transform.position + new Vector3(offset.x, 0f, offset.y);
|
|
|
|
bool farEnough = player == null || Vector3.Distance(point, player.position) >= minDistanceFromPlayer;
|
|
if (farEnough && IsSpaceFree(point))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
return point;
|
|
}
|
|
|
|
// 用一個跟機器人差不多大的膠囊檢查這個位置有沒有東西 ; 底部抬高一點,不去碰到地板
|
|
bool IsSpaceFree(Vector3 feet)
|
|
{
|
|
const float radius = 0.4f;
|
|
Vector3 bottom = feet + Vector3.up * (radius + 0.1f);
|
|
Vector3 top = feet + Vector3.up * (1.8f - radius);
|
|
return !Physics.CheckCapsule(bottom, top, radius, ~0, QueryTriggerInteraction.Ignore);
|
|
}
|
|
|
|
// 在 Scene 視窗畫出生成範圍,方便調整
|
|
void OnDrawGizmosSelected()
|
|
{
|
|
Gizmos.color = Color.yellow;
|
|
Gizmos.DrawWireSphere(transform.position, spawnRadius);
|
|
}
|
|
}
|