170 lines
4.6 KiB
C#
170 lines
4.6 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Cysharp.Threading.Tasks;
|
||
using Cysharp.Threading.Tasks.Triggers;
|
||
using DG.Tweening;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
|
||
public class EnemyController : MonoBehaviour, IPointerDownHandler, IPointerEnterHandler, IPointerExitHandler
|
||
{
|
||
[SerializeField]private int health;
|
||
public int MaxHealth;
|
||
public SpriteRenderer SpriteRenderer;
|
||
public Material FlashDamageMaterial;
|
||
|
||
public int Damage = 3;
|
||
|
||
private Animator animator;
|
||
|
||
|
||
public float initSpeed = 1.0f;
|
||
public float speed = 1.0f;
|
||
|
||
public Rigidbody rigid;
|
||
|
||
public bool isDead = false;
|
||
public bool isFighting = false;
|
||
public int Health
|
||
{
|
||
get => health;
|
||
set
|
||
{
|
||
if (health == 0) return;
|
||
value = Mathf.Clamp(value, 0, MaxHealth);
|
||
// print(health);
|
||
// print(value);
|
||
if (health > value || value <= 0)
|
||
{
|
||
|
||
DOTween.Complete(this);
|
||
DOTween.To(
|
||
() => FlashDamageMaterial.GetFloat("_Intensity"),
|
||
x => FlashDamageMaterial.SetFloat("_Intensity", x),
|
||
1f, 0.1f).onComplete = () =>
|
||
DOTween.To(
|
||
() => FlashDamageMaterial.GetFloat("_Intensity"),
|
||
x => FlashDamageMaterial.SetFloat("_Intensity", x),
|
||
0f, 0.1f);
|
||
// print("punch");
|
||
transform.DOPunchScale(new Vector3(-0.3f, 0.5f), 0.2f).onComplete += () =>
|
||
{
|
||
if (value <= 0)
|
||
{
|
||
SpriteRenderer.DOColor(Color.black, 0.3f);
|
||
isDead = true;
|
||
Destroy(this.gameObject);
|
||
}
|
||
};
|
||
}
|
||
health = value;
|
||
}
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
rigid = GetComponent<Rigidbody>();
|
||
health = MaxHealth;
|
||
SpriteRenderer ??= GetComponent<SpriteRenderer>();
|
||
FlashDamageMaterial = SpriteRenderer.material;
|
||
animator = GetComponent<Animator>();
|
||
}
|
||
|
||
public void Update()
|
||
{
|
||
if (isDead) return;
|
||
// if (isFighting)
|
||
// {
|
||
// if (_battleBlob != null) _battleBlob.CurrentHP -= Time.deltaTime;
|
||
// if (_wall != null) _wall.CurrentWallHP -= Time.deltaTime;
|
||
// }
|
||
|
||
rigid.position += Vector3.left * (Time.deltaTime * speed);
|
||
}
|
||
|
||
public void OnPointerDown(PointerEventData eventData)
|
||
{
|
||
Health -= (1 + GlobalBuffSystem.Instance.ClickAdder);
|
||
}
|
||
|
||
public void OnPointerEnter(PointerEventData eventData)
|
||
{
|
||
transform.localScale *= 1.2f;
|
||
}
|
||
|
||
public void OnPointerExit(PointerEventData eventData)
|
||
{
|
||
transform.localScale /= 1.2f;
|
||
}
|
||
|
||
private BattleBlob _battleBlob;
|
||
private BattleWall _wall;
|
||
private void OnTriggerEnter(Collider other)
|
||
{
|
||
|
||
var battleWall = other.gameObject.GetComponent<BattleWall>();
|
||
if (battleWall)
|
||
{
|
||
// print("wall");
|
||
animator.SetTrigger("Attack");
|
||
_wall = battleWall;
|
||
isFighting = true;
|
||
speed = 0;
|
||
//battleWall.CurrentWallHP -= Damage;
|
||
//Destroy(this.gameObject);
|
||
return;
|
||
}
|
||
|
||
var battleBlob = other.gameObject.GetComponent<BattleBlob>();
|
||
if (battleBlob)
|
||
{
|
||
// print("blob");
|
||
animator.SetTrigger("Attack");
|
||
isFighting = true;
|
||
speed = 0;
|
||
_battleBlob = battleBlob;
|
||
return;
|
||
}
|
||
}
|
||
|
||
private void OnTriggerStay(Collider other)
|
||
{
|
||
if (other.GetComponent<BattleBlob>() == _battleBlob)
|
||
{
|
||
animator.SetTrigger("Attack");
|
||
}
|
||
|
||
if (other.GetComponent<BattleWall>() == _wall)
|
||
{
|
||
animator.SetTrigger("Attack");
|
||
}
|
||
}
|
||
|
||
private void OnTriggerExit(Collider other)
|
||
{
|
||
if (other.GetComponent<BattleBlob>() == _battleBlob)
|
||
{
|
||
_battleBlob = null;
|
||
isFighting = false;
|
||
}
|
||
|
||
if (other.GetComponent<BattleWall>() == _wall)
|
||
{
|
||
_wall = null;
|
||
isFighting = false;
|
||
speed = initSpeed;
|
||
}
|
||
}
|
||
|
||
public void Attack()
|
||
{
|
||
// _battleBlob?.GetHurt(Damage).Forget();
|
||
AudioManager.Instance.PlayEnemyChopWoodSFX();
|
||
if (_battleBlob) _battleBlob.CurrentHP -= Damage;
|
||
if (_wall) _wall.CurrentWallHP -= Damage;
|
||
Debug.Log("Ѫ<><D1AA><EFBFBD><EFBFBD>"+_wall.CurrentWallHP);
|
||
}
|
||
|
||
}
|