refactor(Facility): 使用继承让所有机关都拥有了独自的 可交互 可编辑 可连接设定。大面积重构
This commit is contained in:
64
Assets/Script/Gameplay/Facility/EmitterController.cs
Normal file
64
Assets/Script/Gameplay/Facility/EmitterController.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
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 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; // 生成对象多少秒后销毁
|
||||
|
||||
private Coroutine emitCoroutine;
|
||||
|
||||
// 接收信号
|
||||
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);
|
||||
var rb = obj.GetComponent<Rigidbody>();
|
||||
if (rb != null)
|
||||
{
|
||||
rb.AddForce(emitPoint.TransformDirection(emitDirection.normalized) * emitForce, ForceMode.Impulse);
|
||||
}
|
||||
// 添加销毁逻辑
|
||||
Destroy(obj, destroyDelay);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user