171 lines
4.8 KiB
C#
Raw Normal View History

2025-05-18 01:04:31 +08:00
// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2020-09-02
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "AI_Ride_Enemy" company="Kimch"></copyright>
// <summary></summary>
// ***********************************************************************
using UnityEngine;
namespace G
{
public class AI_Ride_Enemy : MonoBehaviour
{
public Transform mon;
private Transform cha1;
private Vector3 _rndPosition = Vector3.zero;
private bool _life = true;
private float _attackDelay;
private Transform _efHit;
private Cam_Move_ride _scriptCam;
private Cha_Control_ride_cha _scriptCha1;
private AI_Ride_Enemy2 _scriptMon;
private BloodBoard _hpBar;
private int _hp = 2;
#region Method
void PlayAnim(string anim)
{
this.GetComponent<Animation>().Play(anim);
}
void PlaySound()
{
var sound = GameObject.FindGameObjectWithTag("sound");
if (sound)
sound.GetComponent<AudioSource>().Play();
}
#endregion
private void Awake()
{
_scriptMon = mon.GetComponent<AI_Ride_Enemy2>();
}
private System.Collections.IEnumerator Start()
{
cha1 = GameObject.FindWithTag("Player").transform;
_scriptCha1 = cha1.GetComponent<Cha_Control_ride_cha>();
this.GetComponent<Animation>()["down"].speed = 0.3f;
PlayAnim("move");
InvokeRepeating(nameof(SetRndPosition), Random.Range(0.1f, 3f), Random.Range(1f, 2f));
_scriptMon.Defeat();
_scriptCam = Camera.main.GetComponent<Cam_Move_ride>();
var hitFxGO = Map_Compose.Instance.enemyHitFx;// GameObject.FindWithTag("icon_skill");
_efHit = hitFxGO.transform;
yield return new WaitForSeconds(0.5f);
CreatePropBar();
this.gameObject.SetActive(false);
}
private void OnEnable()
{
PlayAnim("move");
_life = true;
_scriptMon.Wake();
_hp = Random.Range(3, 5);
if (_hpBar)
{
_hpBar.SetMultProp(_hp);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == GameLayer.NormalAttack)
{
_hpBar.HidePropValue();
_life = false;
PlayAnim("down");
PlaySound();
//EnemyHelper.Instance.SoundOn(3);
_scriptCam.Hitcam();
_efHit.gameObject.SetActive(true);
_efHit.position = transform.position + Vector3.up * 0.05f;
_scriptCha1.GetSoulStone(_hp);
_scriptMon.Defeat();
}
}
public void SetRndPosition()
{
_rndPosition = new Vector3(Random.Range(-1f, 1f), 0f, Random.Range(-0.4f, 0.6f));
}
private void Update()
{
if (_life)
{
transform.position = Vector3.Lerp(transform.position, _rndPosition, Time.deltaTime * 0.5f);
}
else
{
transform.position -= Vector3.forward * Time.deltaTime * 1f;
Vector3 position = transform.position;
if (position.z < -1f)
{
transform.position = Vector3.one * 4f;
this.gameObject.SetActive(false);
}
}
if (_attackDelay > 0f)
{
_attackDelay -= Time.deltaTime;
}
else
{
Vector3 myPos = transform.position;
Vector3 chaPos = cha1.position;
var attackrangeZ = myPos.z - chaPos.z;
if (Mathf.Abs(attackrangeZ) >= 0.18f)
{
return;
}
var attackrangeX = myPos.x - chaPos.x;
if (Mathf.Abs(attackrangeX) < 0.34f)
{
if (attackrangeX > 0f)
{
_scriptCha1.Attack(true);
}
else
{
_scriptCha1.Attack(false);
}
_attackDelay = 0.2f;
}
}
}
private void CreatePropBar()
{
_hpBar = BoardManager.Instance.CreateBloodBoard("BloodBoard3");
_hpBar.SetTarget(this.transform, Vector3.up * 0.3f);
_hpBar.SetMultProp(_hp);
}
}
}