207 lines
6.1 KiB
Plaintext
207 lines
6.1 KiB
Plaintext
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using DragonLi.Core;
|
|
using DragonLi.Frame;
|
|
using InfiniteShooting;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
public class Esky : DragonLiAgent
|
|
{
|
|
//public LifeSlider LifeUIContoller;
|
|
public BloodSlider BloodSlider;
|
|
|
|
public AudioSource hitAudio;
|
|
public Transform hitPos;
|
|
|
|
|
|
public AudioClip hitClip;
|
|
|
|
int act = 0;
|
|
private bool isPerished = false;
|
|
private int needReset = 0;
|
|
|
|
private bool isInit = true;
|
|
private bool isDeath = false;
|
|
Transform player;
|
|
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
|
|
//transform.GetComponent<Rigidbody>().velocity = Vector3.zero;
|
|
if (IsAlive)
|
|
{
|
|
UpdateRotation();
|
|
//if (Data["isFly"].BooleanValue) {
|
|
// // 构建移动向量
|
|
// Vector3 movement = new Vector3(0, 1.0f, 0);
|
|
// transform.GetComponent<Rigidbody>().MovePosition(transform.position+ movement * Time.deltaTime);
|
|
//}
|
|
}
|
|
|
|
|
|
needReset++;
|
|
if (needReset >= 30 && AnimatorComponent.GetBool("BeHit"))
|
|
{
|
|
// Debug.Log("关闭");
|
|
AnimatorComponent.SetBool("BeHit", false);
|
|
}
|
|
}
|
|
void OnCollisionEnter(Collision collision)
|
|
{
|
|
// 在碰撞发生时执行的代码
|
|
if (collision.gameObject.tag == "Ground" && isDeath)
|
|
{
|
|
Transform explosion = SpawnManager.Instance.Spawn("Effect", "chanying_Explosion", transform.position);
|
|
if (explosion)
|
|
SpawnManager.Instance.Despawn(explosion, 4.0f);
|
|
// despawn self
|
|
SpawnManager.Instance.Despawn(transform, 0);
|
|
}
|
|
else if (collision.gameObject.tag == "Ground")
|
|
{
|
|
|
|
Data["isFly"].BooleanValue = true;
|
|
|
|
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
|
{
|
|
Data["isFly"].BooleanValue = false;
|
|
}, 2f, this);
|
|
}
|
|
}
|
|
private void Perish()
|
|
{
|
|
isPerished = true;
|
|
|
|
// we dont want to update behaviour anymore
|
|
IsUpdateBehaviour = false;
|
|
|
|
// we don't want to rigidbody to controller this eneity anymore
|
|
RigidbodyComponent.velocity = Vector3.zero;
|
|
|
|
AnimatorComponent.SetTrigger("Perish");
|
|
|
|
}
|
|
|
|
protected override void OnBorn()
|
|
{
|
|
EnemyList.GetInstance().AddEnemy(transform);
|
|
player = World.GetPlayer();
|
|
NavAgent.enabled = false;
|
|
Debug.Log("智能体出生");
|
|
Data["attackmode"].BooleanValue = false;
|
|
Data["isFly"].BooleanValue = false;
|
|
CloseCollider();
|
|
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
|
{
|
|
OpenCollider();
|
|
}, 2f, this);
|
|
EventDispatcher.TriggerEvent("SpwanPoint", transform);
|
|
}
|
|
|
|
protected override void OnDeath(object info, Transform _sender)
|
|
{
|
|
Debug.Log("智能体死亡");
|
|
// spawn explosion
|
|
//关闭动画
|
|
AnimatorComponent.speed = 0;
|
|
transform.GetComponent<Rigidbody>().useGravity = true;
|
|
isDeath = true;
|
|
EventDispatcher.TriggerEvent("DestoryPoint", transform);
|
|
EnemyList.GetInstance().RemoveEnemy(transform);
|
|
EnemyList.GetInstance().EskyAmount++;
|
|
Variables variables = SceneVariables.Instance(UnityEngine.SceneManagement.SceneManager.GetActiveScene()).variables;
|
|
GameObject target = variables.declarations.Get<GameObject>("Events");
|
|
CustomEvent.Trigger(target, "EskyDead", EnemyList.GetInstance().EskyAmount);
|
|
|
|
}
|
|
|
|
public void CloseCollider()
|
|
{
|
|
gameObject.GetComponent<SphereCollider>().enabled = false;
|
|
}
|
|
|
|
protected void OpenCollider()
|
|
{
|
|
gameObject.GetComponent<SphereCollider>().enabled = true;
|
|
//gameObject.GetComponent<SphereCollider>().isTrigger = true;
|
|
}
|
|
|
|
protected override void OnHeathChanged(float currentHealth)
|
|
{
|
|
base.OnHeathChanged(currentHealth);
|
|
|
|
if (isInit)
|
|
{
|
|
EnemyData data = GameMoudel.Ins.GetValue(EnemyNames.Esky);
|
|
Data["atk"].FloatValue = data.Atk;
|
|
Data["speed"].FloatValue = data.Speed;
|
|
Data["health"].FloatValue = currentHealth = data.Hp;
|
|
transform.localScale = new Vector3(data.Size, data.Size, data.Size);
|
|
isInit = false;
|
|
}
|
|
// set blood slider value
|
|
if (BloodSlider != null)
|
|
{
|
|
BloodSlider.SetValue2(currentHealth, Data["health"].FloatValue);
|
|
}
|
|
|
|
hitAudio.PlayOneShot(hitClip);
|
|
|
|
}
|
|
|
|
protected override float OnReceiveDamage(float value, object info, Transform _sender)
|
|
{
|
|
float res = base.OnReceiveDamage(50, info, _sender);
|
|
AnimatorComponent.Play("BeHit", 0, 0);
|
|
AnimatorComponent.SetBool("BeHit", true);
|
|
needReset = 0;
|
|
|
|
Transform explosion = SpawnManager.Instance.Spawn("Effect", "Blood_Smash_Medium_Green", hitPos.position);
|
|
if (explosion)
|
|
SpawnManager.Instance.Despawn(explosion, 2.0f);
|
|
return res;
|
|
}
|
|
|
|
protected override void OnReceiveMessage(string msg, object info)
|
|
{
|
|
// if we receive message from bahaviour tree tells us esky should perish, we perish
|
|
if (!isPerished && msg == "Perish")
|
|
{
|
|
Debug.Log("收到消息暴死");
|
|
Perish();
|
|
}
|
|
else if (msg == "DamageOnce")
|
|
{
|
|
if (player)
|
|
player.GetComponent<IAgent>().ApplyDamage(Data["atk"].FloatValue, null, transform);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update esky's rotation
|
|
/// </summary>
|
|
private void UpdateRotation()
|
|
{
|
|
if (RigidbodyComponent.velocity != Vector3.zero && !Data["attackmode"].BooleanValue)
|
|
{
|
|
transform.rotation = Quaternion.LookRotation(RigidbodyComponent.velocity);
|
|
}
|
|
else
|
|
{
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(player.position - transform.position), 20 * Time.deltaTime);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//private void UpdateRotation()
|
|
//{
|
|
// if (IsAlive && Data["attackmode"].BooleanValue)
|
|
//}
|
|
}
|