2025-10-26 10:19:48 +08:00
|
|
|
|
using System;
|
2025-10-21 15:32:59 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using Script.Gameplay.Connect;
|
|
|
|
|
|
using Script.Gameplay.Interface;
|
|
|
|
|
|
|
2025-10-22 17:34:58 +08:00
|
|
|
|
namespace Script.Gameplay.Facility
|
2025-10-21 15:32:59 +08:00
|
|
|
|
{
|
2025-10-22 17:34:58 +08:00
|
|
|
|
public class EmitterController : BaseFacilityController
|
2025-10-21 15:32:59 +08:00
|
|
|
|
{
|
|
|
|
|
|
[Header("发射器设置")]
|
2025-10-26 10:19:48 +08:00
|
|
|
|
[SerializeField] private bool isEmittingOnStart = false;
|
2025-10-21 15:32:59 +08:00
|
|
|
|
[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; // 生成对象多少秒后销毁
|
|
|
|
|
|
|
2025-10-27 22:31:48 +08:00
|
|
|
|
// 新增:发射物缩放设置
|
|
|
|
|
|
[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; // 随机最大值(每轴)
|
|
|
|
|
|
|
2025-10-21 15:32:59 +08:00
|
|
|
|
private Coroutine emitCoroutine;
|
2025-10-26 10:19:48 +08:00
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isEmittingOnStart)
|
|
|
|
|
|
{
|
|
|
|
|
|
emitCoroutine = StartCoroutine(EmitRoutine());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-21 15:32:59 +08:00
|
|
|
|
// 接收信号
|
2025-10-22 17:34:58 +08:00
|
|
|
|
public override void OnSignalReceived(bool active, GameObject sender)
|
2025-10-21 15:32:59 +08:00
|
|
|
|
{
|
2025-10-22 17:34:58 +08:00
|
|
|
|
base.OnSignalReceived(active, sender);
|
|
|
|
|
|
if(!isOpenInEditor) return;
|
2025-10-21 15:32:59 +08:00
|
|
|
|
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);
|
2025-10-27 22:31:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 应用缩放设置
|
|
|
|
|
|
if (overridePrefabScale)
|
|
|
|
|
|
{
|
|
|
|
|
|
obj.transform.localScale = ComputeEmitScale(obj.transform.localScale);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-21 15:32:59 +08:00
|
|
|
|
var rb = obj.GetComponent<Rigidbody>();
|
|
|
|
|
|
if (rb != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
rb.AddForce(emitPoint.TransformDirection(emitDirection.normalized) * emitForce, ForceMode.Impulse);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 添加销毁逻辑
|
|
|
|
|
|
Destroy(obj, destroyDelay);
|
|
|
|
|
|
}
|
2025-10-27 22:31:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 计算要应用的缩放(考虑固定/随机,并保证 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)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-10-21 15:32:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|