shaoxiadiablo/Assets/AGame/Scripts/Game/Skill/SkillSpiritSword.cs
2025-05-18 01:04:31 +08:00

182 lines
4.3 KiB
C#

// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2020-09-02
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "Skill_SpiritSword" company="Kimch"></copyright>
// <summary></summary>
// ***********************************************************************
namespace G
{
using UnityEngine;
/// <summary>
/// 幽灵剑
/// </summary>
public class SkillSpiritSword : MonoBehaviour
{
public Transform sword;
protected Transform _transform;
protected Collider _collider;
protected Transform _cha1;
protected Transform _oldTarget;
protected int _swordIndex;
protected float _startDelay;
protected int _createIndex;
protected bool _createFinish;
protected bool _homing = true;
protected float _power;
protected float _liftTime;
protected int _swordCount;
protected int _hitCount;
protected int _hitCountMax;
public float lifeTime
{
set { _liftTime = value; }
}
public int swordCount
{
get { return _swordCount; }
set { _swordCount = value; }
}
public int maxHitCount
{
set { _hitCountMax = value; }
}
public float power
{
set { _power = value; }
}
const int MAX_SWORD_COUNT = 7;
#region Field
private Transform[] _swords = new Transform[MAX_SWORD_COUNT];
#endregion
#region Unity
private void Awake()
{
_transform = this.transform;
_collider = this.GetComponent<Collider>();
_cha1 = GameObject.FindWithTag("Player").transform;
}
private void OnEnable()
{
Reset();
}
public void Reset()
{
_collider.enabled = false;
_startDelay = 0f;
_createIndex = 0;
_swordIndex = 0;
_createFinish = false;
_homing = false;
_hitCount = 0;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == GameLayer.UnitLayer && !_homing && (_oldTarget != other.transform))
{
_swords[_swordIndex].GetComponent<SpiritSword_p1>().FireSword(other.transform);
_oldTarget = other.transform;
_swordIndex++;
_collider.enabled = false;
_startDelay = 0f;
_homing = true;
if (_swordIndex >= _swordCount)
{
this.gameObject.SetActive(false);
_transform.position = Vector3.up * 31f;
}
}
}
private void Update()
{
if (!_createFinish)
{
if (_startDelay > 0.1f)
{
_startDelay = 0f;
if (_swords[_createIndex] == null)
{
Vector3 position = _transform.position;
float angle = 30 * _createIndex;
Vector3 eulerAngles = _transform.eulerAngles;
_swords[_createIndex] = Object.Instantiate(sword, position, Quaternion.Euler(0f, angle + eulerAngles.y - 270f, 0f));
_swords[_createIndex].GetComponent<Rigidbody>().mass = _power;
_swords[_createIndex].position += _swords[_createIndex].forward * 0.1f;
_swords[_createIndex].forward = Vector3.up + _transform.forward * 0.6f;
}
else
{
_swords[_createIndex].GetComponent<Rigidbody>().mass = _power;
_swords[_createIndex].position = _transform.position;
float angle = 30 * _createIndex;
Vector3 eulerAngles = _transform.eulerAngles;
_swords[_createIndex].rotation = Quaternion.Euler(0f, angle + eulerAngles.y - 270f, 0f);
_swords[_createIndex].gameObject.SetActive(true);
_swords[_createIndex].position += _swords[_createIndex].forward * 0.1f;
_swords[_createIndex].forward = Vector3.up + _transform.forward * 0.6f;
}
_swords[_createIndex].parent = _transform;
if (++_createIndex >= _swordCount)
{
_createFinish = true;
}
}
else
{
_startDelay += Time.deltaTime;
}
}
else if (_startDelay > 0.4f)
{
_homing = false;
_collider.enabled = true;
_startDelay = -1f;
}
else if (_startDelay > -1f)
{
_startDelay += Time.deltaTime;
}
else
{
_startDelay -= Time.deltaTime;
if (_startDelay < -2f)
{
_collider.enabled = false;
_collider.enabled = true;
_oldTarget = null;
}
}
_transform.position = _cha1.position + Vector3.up * 0.2f;
_transform.rotation = Quaternion.Lerp(_transform.rotation, _cha1.rotation, Time.deltaTime * 3f);
}
#endregion
}
}