118 lines
3.9 KiB
C#
118 lines
3.9 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using Script.Gameplay.Connect;
|
||
using Script.Gameplay.Interface;
|
||
|
||
namespace Script.Gameplay.Facility
|
||
{
|
||
public class EmitterController : BaseFacilityController
|
||
{
|
||
[Header("发射器设置")]
|
||
[SerializeField] private bool isEmittingOnStart = false;
|
||
[SerializeField] private GameObject prefabToEmit;
|
||
[SerializeField] private Transform emitPoint;
|
||
[SerializeField] private Vector3 emitDirection = Vector3.forward;
|
||
[SerializeField] private float emitForce = 10f;
|
||
[SerializeField] private float emitInterval = 1f;
|
||
[Header("生成对象销毁时间")]
|
||
[SerializeField] private float destroyDelay = 5f; // 生成对象多少秒后销毁
|
||
|
||
// 新增:发射物缩放设置
|
||
[Header("发射物缩放")]
|
||
[SerializeField] private bool overridePrefabScale = false; // 是否覆盖预制体原始缩放
|
||
[SerializeField] private Vector3 fixedScale = Vector3.one; // 固定缩放
|
||
[SerializeField] private bool randomizeScale = false; // 是否随机缩放
|
||
[SerializeField] private Vector3 minScale = Vector3.one; // 随机最小值(每轴)
|
||
[SerializeField] private Vector3 maxScale = Vector3.one; // 随机最大值(每轴)
|
||
|
||
private Coroutine emitCoroutine;
|
||
|
||
private void Start()
|
||
{
|
||
if (isEmittingOnStart)
|
||
{
|
||
emitCoroutine = StartCoroutine(EmitRoutine());
|
||
}
|
||
}
|
||
|
||
// 接收信号
|
||
public override void OnSignalReceived(bool active, GameObject sender)
|
||
{
|
||
base.OnSignalReceived(active, sender);
|
||
if(!isOpenInEditor) return;
|
||
if (active)
|
||
{
|
||
if (emitCoroutine == null)
|
||
emitCoroutine = StartCoroutine(EmitRoutine());
|
||
}
|
||
else
|
||
{
|
||
if (emitCoroutine != null)
|
||
{
|
||
StopCoroutine(emitCoroutine);
|
||
emitCoroutine = null;
|
||
}
|
||
}
|
||
}
|
||
|
||
private IEnumerator EmitRoutine()
|
||
{
|
||
while (true)
|
||
{
|
||
Emit();
|
||
yield return new WaitForSeconds(emitInterval);
|
||
}
|
||
}
|
||
|
||
private void Emit()
|
||
{
|
||
if (prefabToEmit == null || emitPoint == null) return;
|
||
var obj = Instantiate(prefabToEmit, emitPoint.position, emitPoint.rotation);
|
||
|
||
// 应用缩放设置
|
||
if (overridePrefabScale)
|
||
{
|
||
obj.transform.localScale = ComputeEmitScale(obj.transform.localScale);
|
||
}
|
||
|
||
var rb = obj.GetComponent<Rigidbody>();
|
||
if (rb != null)
|
||
{
|
||
rb.AddForce(emitPoint.TransformDirection(emitDirection.normalized) * emitForce, ForceMode.Impulse);
|
||
}
|
||
// 添加销毁逻辑
|
||
Destroy(obj, destroyDelay);
|
||
}
|
||
|
||
// 计算要应用的缩放(考虑固定/随机,并保证 min<=max)
|
||
private Vector3 ComputeEmitScale(Vector3 prefabScale)
|
||
{
|
||
if (!overridePrefabScale)
|
||
return prefabScale;
|
||
|
||
if (!randomizeScale)
|
||
return fixedScale;
|
||
|
||
// 确保 min <= max 各轴
|
||
Vector3 minV = new Vector3(
|
||
Mathf.Min(minScale.x, maxScale.x),
|
||
Mathf.Min(minScale.y, maxScale.y),
|
||
Mathf.Min(minScale.z, maxScale.z)
|
||
);
|
||
Vector3 maxV = new Vector3(
|
||
Mathf.Max(minScale.x, maxScale.x),
|
||
Mathf.Max(minScale.y, maxScale.y),
|
||
Mathf.Max(minScale.z, maxScale.z)
|
||
);
|
||
|
||
return new Vector3(
|
||
UnityEngine.Random.Range(minV.x, maxV.x),
|
||
UnityEngine.Random.Range(minV.y, maxV.y),
|
||
UnityEngine.Random.Range(minV.z, maxV.z)
|
||
);
|
||
}
|
||
}
|
||
}
|