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(); health = MaxHealth; SpriteRenderer ??= GetComponent(); FlashDamageMaterial = SpriteRenderer.material; animator = GetComponent(); } 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(); 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(); if (battleBlob) { // print("blob"); animator.SetTrigger("Attack"); isFighting = true; speed = 0; _battleBlob = battleBlob; return; } } private void OnTriggerStay(Collider other) { if (other.GetComponent() == _battleBlob) { animator.SetTrigger("Attack"); } if (other.GetComponent() == _wall) { animator.SetTrigger("Attack"); } } private void OnTriggerExit(Collider other) { if (other.GetComponent() == _battleBlob) { _battleBlob = null; isFighting = false; } if (other.GetComponent() == _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("Ѫ����"+_wall.CurrentWallHP); } }