224 lines
5.9 KiB
C#
224 lines
5.9 KiB
C#
// ***********************************************************************
|
|
// Assembly : Game
|
|
// Author : Kimch
|
|
// Created : 2020-09-02
|
|
// Description :
|
|
// Last Modified By :
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "EntityCart" company="Kimch"></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
using UnityEngine;
|
|
|
|
namespace G
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class EntityCart : Entity
|
|
{
|
|
#region Field
|
|
|
|
public Transform horse;
|
|
public AudioClip snd_break;
|
|
public Transform pt_hit;
|
|
public Transform cart_destroy;
|
|
|
|
private Animation _myAnimation;
|
|
private Animation _horseAnimation;
|
|
|
|
private float _delay;
|
|
private int _maxHp;
|
|
private int _hp;
|
|
private bool _life = true;
|
|
private bool _super;
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private BloodBoard _hpBar;
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private AudioSource _audioSource;
|
|
|
|
#endregion
|
|
|
|
#region Method
|
|
|
|
public bool Init(EntityInfo info)
|
|
{
|
|
if (info != null)
|
|
{
|
|
this.id = info.guid;
|
|
this.position = info.position;
|
|
}
|
|
_maxHp = 30;
|
|
_hp = _maxHp;
|
|
|
|
this.InitView();
|
|
return true;
|
|
}
|
|
|
|
protected override void OnViewInitCompleted()
|
|
{
|
|
|
|
}
|
|
|
|
public void ShowDistance()
|
|
{
|
|
Vector3 position = transform.position;
|
|
var endPoint = GameLevel.Instance.endPosition;
|
|
GameLevel.Instance.stageProgress = position.z / endPoint.z;
|
|
if (position.z > endPoint.z)
|
|
{
|
|
GameLevel.Instance.MissionSuccess();
|
|
Instance = null;
|
|
Destroy(this.gameObject, 2f);
|
|
|
|
_super = true;
|
|
CancelInvoke(nameof(ShowDistance));
|
|
DestoryHpBar();
|
|
}
|
|
}
|
|
|
|
public void Damaged(Vector3 _damageVector)
|
|
{
|
|
if (_life && !_super)
|
|
{
|
|
_delay = 2f;
|
|
|
|
_hp = Mathf.Max(0, _hp - 1);
|
|
|
|
SetHp();
|
|
|
|
_myAnimation.Stop();
|
|
_myAnimation.Play("cart_damage");
|
|
_horseAnimation.Stop();
|
|
|
|
_audioSource.Stop();
|
|
_audioSource.PlayOneShot(snd_break);
|
|
pt_hit.GetComponent<Pt_hit>().ParticleOn();
|
|
|
|
if (_hp <= 0)
|
|
{
|
|
Transform destory = Object.Instantiate(cart_destroy, transform.position, transform.rotation);
|
|
destory.GetComponent<Animation>()["cart_destroy"].speed = 0.2f;
|
|
Destroy(destory.gameObject, 5f);
|
|
|
|
horse.parent = null;
|
|
_horseAnimation["horse_run"].speed = 0.6f;
|
|
_horseAnimation.Play("horse_run");
|
|
this.transform.position = Vector3.up * 16f;
|
|
_life = false;
|
|
DestoryHpBar();
|
|
//GameCamera.Instance.LookTarget(destory, 30, 0f);
|
|
Invoke(nameof(FailStage), 1.5f);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void FailStage()
|
|
{
|
|
Instance = null;
|
|
Destroy(this.gameObject, 2f);
|
|
|
|
GameLevel.Instance.MissionFail();
|
|
}
|
|
|
|
private void CreateHpBar()
|
|
{
|
|
_hpBar = BoardManager.Instance.CreateBloodBoard("BloodBoard2");
|
|
_hpBar.SetTarget(this.transform, Vector3.up * 0.3f);
|
|
_hpBar.HideSlider();
|
|
}
|
|
|
|
private void DestoryHpBar()
|
|
{
|
|
if (_hpBar)
|
|
{
|
|
_hpBar.Release();
|
|
_hpBar = null;
|
|
}
|
|
}
|
|
|
|
private void SetHp()
|
|
{
|
|
if (_hpBar)
|
|
{
|
|
_hpBar.SetHpValue(_hp, _maxHp, 0.3f, 0);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Unity
|
|
|
|
public static EntityCart Instance;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
|
|
_myAnimation = this.GetComponent<Animation>();
|
|
_horseAnimation = horse.GetComponent<Animation>();
|
|
|
|
_audioSource = GetComponent<AudioSource>();
|
|
SoundProxy.Instance.SetAudioMixer(_audioSource, SoundProxy.FX_LAYER_INDEX);
|
|
this.transform.position = Vector3.forward * 0.5f;
|
|
}
|
|
|
|
private System.Collections.IEnumerator Start()
|
|
{
|
|
_hp = _maxHp;
|
|
SetHp();
|
|
_myAnimation["cart_run"].speed = 0.3f;
|
|
_myAnimation["cart_damage"].speed = 0.3f;
|
|
_horseAnimation["horse_walk"].speed = 0.4f;
|
|
|
|
_audioSource.Play();
|
|
InvokeRepeating(nameof(ShowDistance), 0.1f, 1f);
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
|
CreateHpBar();
|
|
SetHp();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (horse)
|
|
{
|
|
Destroy(horse.gameObject);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!_life)
|
|
{
|
|
horse.position += Vector3.forward * (Time.deltaTime * 1f);
|
|
}
|
|
else if (_delay > 0f)
|
|
{
|
|
_delay -= Time.deltaTime;
|
|
if (_delay <= 0f)
|
|
{
|
|
_myAnimation.CrossFade("cart_run");
|
|
_horseAnimation.CrossFade("horse_walk");
|
|
_audioSource.Play();
|
|
}
|
|
else if (_delay < 1.5f)
|
|
{
|
|
pt_hit.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
transform.position += Vector3.forward * (Time.deltaTime * 0.1f);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|