119 lines
3.0 KiB
C#
119 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Rider : Ally
|
|
{
|
|
[SerializeField]
|
|
private int _chargeAttackDamage;
|
|
[SerializeField]
|
|
private int _chargeCooldown;
|
|
[SerializeField]
|
|
private int _maxChargeHitCount;
|
|
[SerializeField]
|
|
private int _maxChargeDistance;
|
|
[SerializeField]
|
|
private GameObject _detection;
|
|
[SerializeField]
|
|
private GameObject _chargeDetection;
|
|
[SerializeField]
|
|
private GameObject _root;
|
|
|
|
private Detection _chargeDetectionScript;
|
|
private Root _rootScript;
|
|
|
|
private Vector3 _originalPos;
|
|
private Vector2 _movementVector = Vector2.zero;
|
|
private bool _isCharging;
|
|
private float _timeSinceLastCharge;
|
|
private List<Entity> _opponentsHit = new List<Entity>();
|
|
|
|
public override void Start()
|
|
{
|
|
base.Start();
|
|
|
|
_chargeDetectionScript = _chargeDetection.GetComponent<Detection>();
|
|
_rootScript = _root.GetComponent<Root>();
|
|
|
|
_originalPos = transform.position;
|
|
_isCharging = true;
|
|
}
|
|
|
|
public override void Update()
|
|
{
|
|
|
|
// check for charge cooldown
|
|
if (_timeSinceLastCharge > _chargeCooldown)
|
|
{
|
|
_isCharging = true;
|
|
}
|
|
|
|
if (_isCharging)
|
|
{
|
|
// toggle charge detection
|
|
_detection.SetActive(false);
|
|
_chargeDetection.SetActive(true);
|
|
|
|
// movement
|
|
_movementVector.x = Time.deltaTime * Speed;
|
|
transform.position += (Vector3)_movementVector;
|
|
|
|
Debug.Log(IsEnemyDetected);
|
|
// attack
|
|
if (IsEnemyDetected && !_opponentsHit.Contains(Enemy))
|
|
{
|
|
AttackEnemyRiding();
|
|
}
|
|
|
|
// reset
|
|
if (transform.position.x - _originalPos.x >= _maxChargeDistance || _opponentsHit.Count >= _maxChargeHitCount)
|
|
{
|
|
// position
|
|
_movementVector = Vector2.zero;
|
|
transform.position = _originalPos;
|
|
|
|
// charge state
|
|
_isCharging = false;
|
|
_timeSinceLastCharge = 0;
|
|
_opponentsHit.Clear();
|
|
|
|
// detection state
|
|
IsEnemyDetected = false;
|
|
Enemy = null;
|
|
|
|
// toggle detection
|
|
_detection.SetActive(true);
|
|
_chargeDetection.SetActive(false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_timeSinceLastCharge += Time.deltaTime;
|
|
if (IsEnemyDetected)
|
|
{
|
|
AttackEnemy();
|
|
}
|
|
}
|
|
}
|
|
|
|
void AttackEnemyRiding()
|
|
{
|
|
Debug.Log("Attack Riding");
|
|
_rootScript.AttackWithCustomDamage(_chargeAttackDamage);
|
|
_opponentsHit.Add(Enemy);
|
|
}
|
|
|
|
void AttackEnemy()
|
|
{
|
|
//Attack Cooldown
|
|
if (AttackSpeedWait > AttackInterval)
|
|
{
|
|
|
|
Animation.PlayAttackAnim();
|
|
|
|
AttackSpeedWait = 0f;
|
|
}
|
|
|
|
AttackSpeedWait += Time.deltaTime;
|
|
}
|
|
} |