134 lines
3.8 KiB
C#
134 lines
3.8 KiB
C#
|
using System;
|
||
|
using System.Threading;
|
||
|
using Cysharp.Threading.Tasks;
|
||
|
using DG.Tweening;
|
||
|
using UnityEngine;
|
||
|
|
||
|
/// <summary>
|
||
|
/// 团子在进入战斗的时候转换为战斗团子类
|
||
|
/// </summary>
|
||
|
public class BattleBlob: MonoBehaviour
|
||
|
{
|
||
|
[Header("兵种绑定的相关基础数据")]
|
||
|
public int MaxHP;
|
||
|
public float AttackDistance = 1;
|
||
|
public float AttactBackDistance = 2;
|
||
|
public float BasicAttackSpeed = 1;
|
||
|
public float BasicAttackDamage = 1;
|
||
|
public float HurtingBackDistance = 2;
|
||
|
public float HurtingRecoveringTime = 1;
|
||
|
|
||
|
[Header("倍率乘区")]
|
||
|
public float AttackSpeedMultiplier = 1f;
|
||
|
public float AttackDamageMultiplier = 1f;
|
||
|
|
||
|
[Header("可变属性")]
|
||
|
|
||
|
private float currentHP;
|
||
|
|
||
|
public float CurrentHP
|
||
|
{
|
||
|
get => currentHP;
|
||
|
set
|
||
|
{
|
||
|
currentHP = value;
|
||
|
if (currentHP == 0)
|
||
|
{
|
||
|
BattleManager.Instance.onBlobRetire?.Invoke(this);
|
||
|
Destroy(this.gameObject, 0.5f);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
public float AttackSpeed => BasicAttackSpeed * AttackSpeedMultiplier;
|
||
|
public float AttackDamage => BasicAttackDamage * AttackDamageMultiplier;
|
||
|
|
||
|
|
||
|
public Animator animator;
|
||
|
|
||
|
private float BackPositionX;
|
||
|
public Rigidbody rigid;
|
||
|
public float MovingSpeed = 2f;
|
||
|
|
||
|
public CancellationTokenSource movingCancelToken = new CancellationTokenSource();
|
||
|
|
||
|
|
||
|
private TriggerDetection hitbox;
|
||
|
private float AttackCooldownTime = 0;
|
||
|
public BattleBlobJob CurrentJob;
|
||
|
public BlobController originalBlob;
|
||
|
|
||
|
public Type Job;
|
||
|
|
||
|
public void Start()
|
||
|
{
|
||
|
animator = GetComponent<Animator>();
|
||
|
CurrentJob = GetComponent<BattleBlobJob>();
|
||
|
hitbox = GetComponentInChildren<TriggerDetection>();
|
||
|
rigid = GetComponent<Rigidbody>();
|
||
|
BackPositionX = transform.position.x;
|
||
|
movingCancelToken = new CancellationTokenSource();
|
||
|
|
||
|
hitbox.onTriggerEnter += OnHitBoxEnter;
|
||
|
hitbox.onTriggerStay += OnHitBoxStay;
|
||
|
hitbox.GetComponent<BoxCollider>().size = new Vector3(AttackDistance, 3, 1);
|
||
|
hitbox.GetComponent<BoxCollider>().center = new Vector3(AttackDistance/2, 3, 1);
|
||
|
}
|
||
|
|
||
|
public async UniTaskVoid GetHurt(float damage)
|
||
|
{
|
||
|
// BackPositionX = transform.position.x;
|
||
|
movingCancelToken.Cancel();
|
||
|
CurrentHP -= damage;
|
||
|
await transform.DOMoveX(transform.position.x - HurtingBackDistance, 0.1f);
|
||
|
await UniTask.WaitForSeconds(HurtingRecoveringTime);
|
||
|
movingCancelToken = new CancellationTokenSource();
|
||
|
TryBackToPosition().Forget();
|
||
|
}
|
||
|
|
||
|
public async UniTaskVoid TryBackToPosition()
|
||
|
{
|
||
|
await rigid.DOMoveX(BackPositionX, Mathf.Abs(transform.position.x - BackPositionX)/MovingSpeed).WithCancellation(movingCancelToken.Token);
|
||
|
}
|
||
|
|
||
|
|
||
|
private void OnHitBoxEnter(Collider other)
|
||
|
{
|
||
|
|
||
|
var enemy = other.gameObject.GetComponent<EnemyController>();
|
||
|
if (!enemy) return;
|
||
|
// print("enemy!");
|
||
|
movingCancelToken.Cancel();
|
||
|
//攻击部分代码
|
||
|
}
|
||
|
|
||
|
private void Update()
|
||
|
{
|
||
|
if(AttackCooldownTime>0)
|
||
|
AttackCooldownTime -= Time.deltaTime;
|
||
|
}
|
||
|
|
||
|
private EnemyController targetEnemy;
|
||
|
private void OnHitBoxStay(Collider other)
|
||
|
{
|
||
|
|
||
|
var enemy = other.gameObject.GetComponent<EnemyController>();
|
||
|
if (!enemy) return;
|
||
|
if (enemy.isDead) return;
|
||
|
// print("enemy!");
|
||
|
movingCancelToken.Cancel();
|
||
|
//攻击部分代码
|
||
|
if (AttackCooldownTime <= 0)
|
||
|
{
|
||
|
AttackCooldownTime = 1 / AttackSpeed;
|
||
|
targetEnemy = enemy;
|
||
|
animator.SetTrigger("Attack");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void OnAnimationAttack()
|
||
|
{
|
||
|
if(targetEnemy && !targetEnemy.isDead)
|
||
|
CurrentJob?.Attack(this, targetEnemy.gameObject).Forget();
|
||
|
}
|
||
|
}
|