UnityCommon/Role/Role.cs

327 lines
8.7 KiB
C#
Raw Normal View History

2024-12-04 05:50:39 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
2024-12-05 16:25:46 +08:00
using System.Drawing;
2024-12-04 05:50:39 +08:00
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
2024-12-05 16:25:46 +08:00
using UnityEngine.UIElements;
2024-12-04 05:50:39 +08:00
using Debug = UnityEngine.Debug;
2024-12-05 16:25:46 +08:00
using Image = UnityEngine.UI.Image;
2024-12-16 17:48:50 +08:00
using DG.Tweening;
2024-12-05 16:25:46 +08:00
public enum Camp
{/// <summary>
/// <20><><EFBFBD><EFBFBD>
/// </summary>
Player,
/// <summary>
/// <20><><EFBFBD><EFBFBD>
/// </summary>
Enemy,
/// <summary>
/// <20><><EFBFBD><EFBFBD>
/// </summary>
Neutral
}
2024-12-04 05:50:39 +08:00
/// <summary>
/// <20><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>,<2C><><EFBFBD>Ҵ<EFBFBD><D2B4><EFBFBD><EFBFBD>¼<EFBFBD>
/// </summary>
[ExecuteInEditMode]
public class Role : Fun
{
2024-12-11 16:51:55 +08:00
[Header("Id")] public int id;
2024-12-05 16:25:46 +08:00
[Header("<22><>Ӫ")] public Camp camp ;
2024-12-04 05:50:39 +08:00
[Header("Ѫ<><D1AA>")] public float hp = 100f;//Ѫ<><D1AA>
2024-12-14 21:15:33 +08:00
private float maxHp;
[Header(("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"))] public int dieIndex=-1;
[Header("hp<68><70><EFBFBD><EFBFBD>")] public Image Hpfiil;
[Header("<22><>Ѫ<EFBFBD><D1AA><EFBFBD><EFBFBD>")] public GameObject HpTextPrefab;
[Header("<22>Լ<EFBFBD><D4BC>Ļ<EFBFBD><C4BB><EFBFBD>")]public Canvas _Canvas;
2024-12-16 17:48:50 +08:00
[Header("<22>Լ<EFBFBD><D4BC><EFBFBD>ͼƬ")] public SpriteRenderer spriteRenderers; // <20><EFBFBD><E6B4A2><EFBFBD>е<EFBFBD>SpriteRenderer<65><72><EFBFBD><EFBFBD>
2024-12-04 05:50:39 +08:00
public float Hp
{
get => hp;
set
{
2024-12-14 21:15:33 +08:00
float temp = hp;
2024-12-04 05:50:39 +08:00
hp = value;
2024-12-14 21:15:33 +08:00
if (Hpfiil!=null)
{
Hpfiil.fillAmount = hp / maxHp;
}
if (HpTextPrefab != null&& hp<maxHp)
{
GameObject go = GameObject.Instantiate(HpTextPrefab, _Canvas.transform);
go.GetComponent<Text>().text= (temp - hp).ToString();
2024-12-16 17:48:50 +08:00
FlashRedEffect();
StopDoTween(0.2f);
2024-12-14 21:15:33 +08:00
}
2024-12-04 05:50:39 +08:00
if (hp <= 0)
{
die();
}
}
}
2024-12-05 16:25:46 +08:00
[Header("<22><><EFBFBD><EFBFBD>")] public float gold = 10f;
public float Gold
{
[DebuggerStepThrough]
get => gold;
[DebuggerStepThrough]
set => gold = value;
}
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")] private float attack = 10f;
public float Attack
{
get => attack;
set => attack = value;
}
[Header("<22>ȼ<EFBFBD>")] private int level = 1;
public int Level
{
[DebuggerStepThrough] get => level;
[DebuggerStepThrough] set => level = value;
}
2024-12-04 05:50:39 +08:00
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")] public int physicalArmor = 10;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
[Header("ħ<><C4A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")] public int magicArmor = 5;//ħ<><C4A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public List<BUff> buffList = new List<BUff>();
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")] public SimplePathfindingDoTween Navigation;
[System.Serializable ]
public class AnimationList//<2F><><EFBFBD><EFBFBD>list
{
//[Header("<22><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>ѭ<EFBFBD><D1AD>")] public bool isloop = false;
[Header("<22><><EFBFBD><EFBFBD>ͼƬ")] public List<Sprite> value; // <20>ֵ<EFBFBD><D6B5><EFBFBD>ֵ
[Header("<22><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD>)")] public int CharacterAnimationFrameInterval = 50;
}
[Header("<22><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>")] public List<AnimationList> AnimationTree = new List<AnimationList>();
[Header("<22><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")] public int animationHighlight = 0;
[Header("<22><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>")] public SpriteRenderer spriteRenderer;
[Header("<22><>ɫImageλ<65><CEBB>")] public Image image;
public delegate void AnimationItem(int AnimationItem);
public event AnimationItem OnAnimationStart;
public event AnimationItem OnAnimationIng;
public event AnimationItem OnAnimationEnd;
[Header("<22><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")] public bool isAnimationPlay = false;//<2F><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-12-05 16:25:46 +08:00
[Header("<22><>ײ<EFBFBD><D7B2>")]public CircleCollider2D mycollider;
2024-12-14 21:15:33 +08:00
[Header("<22><><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD>")] public Attack attackClass;
[Header("<22><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC><EFBFBD><EFBFBD>")] public int CurrentIndex;
public virtual async void Start()
2024-12-04 05:50:39 +08:00
{
2024-12-14 21:15:33 +08:00
maxHp = hp;
2024-12-04 05:50:39 +08:00
if (Application.isPlaying)
{
2024-12-05 16:25:46 +08:00
if (attackClass)
{
attackClass.role = this;
}
2024-12-04 05:50:39 +08:00
UpdateBuff();
2024-12-05 16:25:46 +08:00
//Navigation.MoveToNextWaypoint(gameObject);
2024-12-04 05:50:39 +08:00
}
2024-12-05 16:25:46 +08:00
tag = Enum.GetName(typeof(Camp), camp);
2024-12-04 05:50:39 +08:00
updateAnimation();
2024-12-05 16:25:46 +08:00
//AttackScope=1f;
2024-12-04 05:50:39 +08:00
}
/// <summary>
/// <20><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public async void updateAnimation()
{
while (true)
{
2024-12-04 06:08:11 +08:00
if (AnimationTree.Count == 0)
{
isAnimationPlay = false;
return;
}
isAnimationPlay = true;
if (animationHighlight >= AnimationTree.Count)
2024-12-04 05:50:39 +08:00
{
animationHighlight = AnimationTree.Count - 1;
}
else if (animationHighlight < 0) {
animationHighlight = 0;
}
2024-12-04 11:38:00 +08:00
//EditorUtility.SetDirty(this);
2024-12-04 05:50:39 +08:00
List<Sprite> LightSprite = AnimationTree[animationHighlight].value;
if (LightSprite == null )
{
2024-12-04 06:08:11 +08:00
isAnimationPlay = false;
2024-12-04 05:50:39 +08:00
return;
};
OnAnimationStart?.Invoke(animationHighlight);
2024-12-14 21:15:33 +08:00
var lsanimationHighlight = animationHighlight;
2024-12-04 05:50:39 +08:00
foreach (Sprite sprite in LightSprite)
{
2024-12-14 21:15:33 +08:00
if (lsanimationHighlight == animationHighlight)
2024-12-04 05:50:39 +08:00
{
2024-12-14 21:15:33 +08:00
if (image != null)
{
image.sprite = sprite;
}
else if (spriteRenderer != null)
{
spriteRenderer.sprite = sprite;
}
if (animationHighlight >= 0 && animationHighlight < AnimationTree.Count)
{
OnAnimationIng?.Invoke(animationHighlight);
await Task.Delay(AnimationTree[animationHighlight].CharacterAnimationFrameInterval);
}
2024-12-04 05:50:39 +08:00
}
2024-12-14 21:15:33 +08:00
else
2024-12-04 05:50:39 +08:00
{
2024-12-14 21:15:33 +08:00
break;
2024-12-04 05:50:39 +08:00
}
}
2024-12-14 21:15:33 +08:00
OnAnimationEnd?.Invoke(lsanimationHighlight);
if (lsanimationHighlight == dieIndex)
{
2024-12-14 23:20:36 +08:00
2024-12-14 21:15:33 +08:00
SpawnPool.intance.ReturnEnemyToPool(this.gameObject); // <20><><EFBFBD>յ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD>
ResetAllStatus();
}
2024-12-04 05:50:39 +08:00
}
}
2024-12-05 16:25:46 +08:00
2024-12-04 05:50:39 +08:00
/// <summary>
/// <20><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
/// </summary>
2024-12-14 21:15:33 +08:00
public virtual void die()
2024-12-04 05:50:39 +08:00
{
if (Application.isPlaying)
{
2024-12-14 21:15:33 +08:00
animationHighlight = dieIndex;
Navigation.PauseAnimation();
Debug.Log("die");
2024-12-04 05:50:39 +08:00
}
}
/// <summary>
/// <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>buff
/// </summary>
async void UpdateBuff()//<2F><>λbuff<66><66><EFBFBD><EFBFBD>
{
while (true)
{
List<BUff> deleteArr = new List<BUff>();
foreach (BUff buffItem in buffList)
{
if (buffItem.executionInterval <= 0)
{
buffItem.executionInterval = buffItem.executionInterval_max;
buffItem.Funaction.Invoke(buffItem.value);
}
buffItem.executionInterval -= 0.1f;
buffItem.timeLeft -= 0.1f;
if (buffItem.timeLeft <= 0)
{
deleteArr.Add(buffItem);
}
}
foreach (BUff item in deleteArr)
{
buffList.Remove(item);
}
await Task.Delay(100);//buff<66><66><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сʱ<D0A1><CAB1>0.1<EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>
}
}
2024-12-14 21:15:33 +08:00
/// <summary>
/// <20><><EFBFBD>ý<EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>״̬
/// </summary>
public void ResetAllStatus()
{
// <20><><EFBFBD><EFBFBD>Ѫ<EFBFBD><D1AA>
Hp = 100f;
// <20><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD>
Attack = 10f;
// <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD>
physicalArmor = 10;
magicArmor = 5;
// <20><><EFBFBD>õȼ<C3B5>
Level = 1;
// <20><><EFBFBD>ý<EFBFBD><C3BD><EFBFBD>
Gold = 10f;
// <20><><EFBFBD><EFBFBD> Buff <20>б<EFBFBD>
buffList.Clear();
// <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹͣ<CDA3><D6B9>ǰ<EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD>Ϊ
if (Navigation != null)
{
//Navigation.Stop(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Stop <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
}
// <20><><EFBFBD>ý<EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
animationHighlight = 0;
isAnimationPlay = false;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><EFBFBD><E5A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (mycollider != null)
{
mycollider.enabled = true; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>õ<EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
2024-12-16 17:48:50 +08:00
private void FlashRedEffect()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>SpriteRenderer<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DZ<EFBFBD><C7B1><EFBFBD>
if (spriteRenderer != null)
{
spriteRenderer.DOColor(UnityEngine.Color.red, 0.2f)
.OnComplete(() =>
{
// <20>ָ<EFBFBD>ԭʼ<D4AD><CABC>ɫ
spriteRenderer.DOColor(UnityEngine.Color.white, 0.2f);
});
}
}
public void StopDoTween(float stopTime)
{
// <20><>ͣ<EFBFBD><CDA3>ǰ<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
transform.DOPause();
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ٣<D3B3><D9A3><EFBFBD>ֱʱ<D6B1>
DOTween.To(() => 0f, x => { }, 0f, stopTime).OnKill(() =>
{
// <20><><EFBFBD>ӳٽ<D3B3><D9BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
transform.DOPlay();
});
}
2024-12-04 05:50:39 +08:00
}