TH1/Unity/Assets/Scripts/TH1_Renderer/GridVFXRenderer.cs
2025-08-18 23:16:13 +08:00

328 lines
8.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using UnityEngine;
using RuntimeData;
using Animancer;
using Logic;
using NUnit.Framework;
using TMPro;
using TMPro.Examples;
using Unity.IO.LowLevel.Unsafe;
using Unity.VisualScripting;
using Object = UnityEngine.Object;
using PropertyAttribute = UnityEngine.PropertyAttribute;
using Random = UnityEngine.Random;
namespace TH1Renderer
{
public enum GridVFXType { Heal,Die,DieHint,CounterDieHint,Flag,Damage,ROYALFLAMES,Fog};
public static class GridVFXRendererFactory
{
public static GridVFXRenderer Create(GridVFXParams param)
{
return param.Type switch
{
GridVFXType.Flag => new FlagGridVFXRenderer(param),
GridVFXType.Damage => new DamageGridVFXRenderer(param),
GridVFXType.ROYALFLAMES => new ROYALFLAMESGridVFXRenderer(param),
GridVFXType.Fog => new FogGridVFXRenderer(param),
_ => null
};
}
}
public class GridVFXParams
{
// 通用
public GridVFXType Type;
public GameObject VFXObject;
public AnimationClip VFXAnim;
public Sprite Sprite;
// 特殊子类用
public uint CivId;
public int Damage;
//从gridData持有的GridVFXRenderMark结构转化为GridVFXRenderer所需要的param结构
public GridVFXParams(GridVFXRenderMark gridVFXType)
{
Damage = gridVFXType.Damage;
CivId = gridVFXType.CivId;
}
public GridVFXParams(GridVFXType gridVFXType, AnimationClip vfxAnim, Sprite sprite, GameObject vfxObject)
{
Type = gridVFXType;
VFXObject = vfxObject;
Sprite = sprite;
VFXAnim = vfxAnim;
Damage = 0;
CivId = 0;
}
}
public abstract class GridVFXRenderer
{
public GridVFXType Type;
public AnimationClip VFXAnim;
public Sprite Sprite;
public GameObject VFXObject;
public bool NeedPlay;
public bool NeedStop;
public bool IsPlaying;
public GridVFXRenderer(GridVFXParams param)
{
Type = param.Type;
VFXAnim = param.VFXAnim;
Sprite = param.Sprite;
VFXObject = param.VFXObject;
NeedPlay = false;
NeedStop = false;
IsPlaying = false;
}
public virtual void SetPlay()
{
//Debug.Log(Type + " : SetPlay!!!");
if (IsPlaying) return;
NeedPlay = true;
}
public virtual void Play()
{
VFXObject.SetActive(true);
var animancer = VFXObject.GetComponent<AnimancerComponent>();
if (VFXAnim != null)
{
IsPlaying = true;
animancer.Play(VFXAnim);
}
}
public virtual void ClearRenderMark()
{
NeedStop = false;
NeedPlay = false;
IsPlaying = false;
}
public virtual void SetStop()
{
if (IsPlaying) NeedStop = true;
}
public virtual void Stop()
{
var animancer = VFXObject.GetComponent<AnimancerComponent>();
if (VFXAnim != null)
{
IsPlaying = false;
animancer.Stop(VFXAnim);
}
VFXObject.SetActive(false);
}
public virtual void Update()
{
if (NeedPlay)
{
NeedPlay = false;
Play();
}
if (NeedStop)
{
NeedStop = false;
Stop();
}
}
public virtual void UpdateSpecialParam(GridVFXParams param){}
public virtual void SetIntParam(int param)
{
}
};
public class FlagGridVFXRenderer : GridVFXRenderer
{
public uint CivId;
public FlagGridVFXRenderer(GridVFXParams param):base(param)
{
CivId = param.CivId;
}
public override void SetPlay()
{
if (IsPlaying) return;
if (VFXObject == null) return;
NeedPlay = true;
if (Table.Instance.PlayerDataAssets.GetPlayerInfoByCivId(CivId, CivId, out var playerInfo))
{
Sprite = playerInfo.FlagIcon;
VFXObject.transform.Find("FlagBG2").GetComponent<SpriteRenderer>().color = playerInfo.Color;
}
var t = VFXObject.transform.Find("FlagIcon").transform;
if (t == null) return;
t.GetComponent<SpriteRenderer>().sprite = Sprite;
}
public override void Play()
{
VFXObject.SetActive(true);
var animancer = VFXObject.GetComponent<AnimancerComponent>();
if (VFXAnim != null)
{
IsPlaying = true;
animancer.Play(VFXAnim);
Timer.Instance.TimerRegister(VFXObject, () =>
{
VFXObject.SetActive(false);
IsPlaying = false;
}, VFXAnim.length,"GridVFXRenderer_FlagGridVFXRendererPlay");
}
}
public override void UpdateSpecialParam(GridVFXParams param)
{
CivId = param.CivId;
}
}
public class DamageGridVFXRenderer : GridVFXRenderer
{
public int Damage;
public DamageGridVFXRenderer(GridVFXParams param):base(param)
{
Damage = param.Damage;
}
public override void SetPlay()
{
if (IsPlaying) return;
if (VFXObject == null) return;
NeedPlay = true;
var t = VFXObject.GetComponent<TextMeshPro>();
if (t == null) return;
t.text = Damage.ToString();
}
public override void Play()
{
VFXObject.SetActive(true);
var animancer = VFXObject.GetComponent<AnimancerComponent>();
if (VFXAnim != null)
{
IsPlaying = true;
animancer.Play(VFXAnim);
Timer.Instance.TimerRegister(VFXObject, () =>
{
VFXObject.SetActive(false);
IsPlaying = false;
}, VFXAnim.length,"GridVFXRenderer_Damage_Play");
}
}
public override void UpdateSpecialParam(GridVFXParams param)
{
Damage = param.Damage;
}
public override void SetIntParam(int param)
{
Damage = param;
}
}
public class ROYALFLAMESGridVFXRenderer : GridVFXRenderer
{
public int Damage;
public ROYALFLAMESGridVFXRenderer(GridVFXParams param):base(param)
{
}
public override void SetPlay()
{
if (IsPlaying) return;
if (VFXObject == null) return;
//TODO 临时用色彩赋予skillicon效果。要改掉
VFXObject.GetComponent<SpriteRenderer>().color = Color.yellow;
NeedPlay = true;
}
public override void Play()
{
VFXObject.SetActive(true);
var animancer = VFXObject.GetComponent<AnimancerComponent>();
if (VFXAnim != null)
{
IsPlaying = true;
animancer.Play(VFXAnim);
Timer.Instance.TimerRegister(VFXObject, () =>
{
VFXObject.SetActive(false);
IsPlaying = false;
}, VFXAnim.length,"GridVFXRenderer_ROYALFLAME_Play");
}
}
}
public class FogGridVFXRenderer : GridVFXRenderer
{
public FogGridVFXRenderer(GridVFXParams param):base(param)
{
}
public override void SetPlay()
{
if (IsPlaying) return;
if (VFXObject == null) return;
NeedPlay = true;
}
public override void Play()
{
VFXObject.SetActive(true);
var animancer = VFXObject.GetComponent<AnimancerComponent>();
if (VFXAnim != null)
{
IsPlaying = true;
animancer.Play(VFXAnim);
Timer.Instance.TimerRegister(VFXObject, () =>
{
VFXObject.SetActive(false);
IsPlaying = false;
}, VFXAnim.length,"GridVFXRenderer_Fog_Play");
}
}
}
}