2023-01-07 23:24:18 +08:00
|
|
|
|
using Axibug;
|
|
|
|
|
using Axibug.Event;
|
|
|
|
|
using Game;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
public class CameraShake : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public float _magnitude = 0.02f;
|
2023-01-12 23:49:16 +08:00
|
|
|
|
public float Multiple = 1;
|
2023-01-07 23:24:18 +08:00
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
AppEntry.Event.Subscribe(CameraShakeEventArgs.EventId, OnCameraShakeEventArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
AppEntry.Event.Unsubscribe(CameraShakeEventArgs.EventId, OnCameraShakeEventArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnCameraShakeEventArgs(object sender, LogicEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
CameraShakeEventArgs msg = (CameraShakeEventArgs)e;
|
|
|
|
|
if (msg == null) throw new GameException("OnCameraShakeEventArgs is null");
|
2023-01-12 23:49:16 +08:00
|
|
|
|
Multiple = msg.Multiple;
|
2023-01-07 23:24:18 +08:00
|
|
|
|
StartShake();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void StartShake()
|
|
|
|
|
{
|
|
|
|
|
if (IEnumerator_Shake != null)
|
|
|
|
|
StopCoroutine(IEnumerator_Shake);
|
|
|
|
|
|
2023-01-12 23:49:16 +08:00
|
|
|
|
IEnumerator_Shake = StartCoroutine(PlayCameraShakeAnimation(0.1f * Multiple, _magnitude));
|
2023-01-07 23:24:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Coroutine IEnumerator_Shake;
|
|
|
|
|
|
|
|
|
|
public IEnumerator PlayCameraShakeAnimation(float duration, float magnitude)
|
|
|
|
|
{
|
|
|
|
|
Vector3 originalPosition = Vector3.zero;
|
|
|
|
|
float elapsedTime = 0f;
|
|
|
|
|
|
|
|
|
|
while (elapsedTime < duration)
|
|
|
|
|
{
|
|
|
|
|
float x = Random.Range(-1f, 1f) * magnitude;
|
|
|
|
|
float y = Random.Range(-1f, 1f) * magnitude;
|
|
|
|
|
|
|
|
|
|
transform.localPosition = new Vector3(x, y, originalPosition.z);
|
|
|
|
|
elapsedTime += Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transform.localPosition = originalPosition;
|
|
|
|
|
|
|
|
|
|
IEnumerator_Shake = null;
|
|
|
|
|
}
|
|
|
|
|
}
|