145 lines
4.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 Root _rootScript;
private Detection _detectionScript;
private int _originalAttackDamage;
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();
_rootScript = _root.GetComponent<Root>();
_detectionScript = _detection.GetComponent<Detection>();
_originalAttackDamage = _chargeAttackDamage;
_originalPos = transform.position;
_timeSinceLastCharge = _chargeCooldown + 1;
}
public override void Update()
{
// check for charge cooldown
if (_timeSinceLastCharge > _chargeCooldown && !_isCharging)
{
SweepAttack();
_isCharging = true;
Animation.ToggleChargeAnim(true);
}
if (_isCharging)
{
SweepAttack();
// toggle charge detection
_chargeDetection.SetActive(true);
// movement
_movementVector.x = Time.deltaTime * Speed;
transform.position += (Vector3)_movementVector;
// 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 animation
Animation.ToggleChargeAnim(false);
// toggle detection
_detection.SetActive(true);
_chargeDetection.SetActive(false);
}
}
else
{
_timeSinceLastCharge += Time.deltaTime;
if (IsEnemyDetected)
{
AttackEnemy();
}
}
}
// attacks all enemies already in the default detection box at start of the charge.
// charge uses a different detection due to Enemy being the oldest opponent to enter the hitbox.
// therefore enemies would only get hit when the previous Enemy exits, which is at the handle instead of the tip`.
// to cover for the enemies behind the charge detection which is at the tip, we hit them all as the charge starts
private void SweepAttack()
{
foreach (Entity entity in _detectionScript.DetectedEntities)
{
Enemy = entity;
AttackEnemyRiding();
}
if (_detectionScript.DetectedEntities.Count > 0)
{
_detection.SetActive(false);
}
}
private void AttackEnemyRiding()
{
AttackDamage = _chargeAttackDamage;
_rootScript.Attack();
_opponentsHit.Add(Enemy);
}
private void AttackEnemy()
{
//Attack Cooldown
if (AttackSpeedWait > AttackInterval)
{
AttackDamage = _originalAttackDamage;
Animation.PlayAttackAnim();
AttackSpeedWait = 0f;
}
AttackSpeedWait += Time.deltaTime;
}
}