Files
2025TapTapGameJam/Assets/Script/Gameplay/Facility/EmitterController.cs

75 lines
2.3 KiB
C#
Raw Normal View History

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;
namespace Script.Gameplay.Facility
2025-10-21 15:32:59 +08:00
{
public class EmitterController : BaseFacilityController
2025-10-21 15:32:59 +08:00
{
[Header("发射器设置")]
[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; // 生成对象多少秒后销毁
private Coroutine emitCoroutine;
private void Start()
{
if (isEmittingOnStart)
{
emitCoroutine = StartCoroutine(EmitRoutine());
}
}
2025-10-21 15:32:59 +08:00
// 接收信号
public override void OnSignalReceived(bool active, GameObject sender)
2025-10-21 15:32:59 +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);
var rb = obj.GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddForce(emitPoint.TransformDirection(emitDirection.normalized) * emitForce, ForceMode.Impulse);
}
// 添加销毁逻辑
Destroy(obj, destroyDelay);
}
}
}