205 lines
6.1 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 float _fadeDistance;
[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>();
private float _fadeProgress;
private bool _isFading;
private int _fadeState;
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()
{
if (_isFading)
{
if (_fadeProgress < _fadeDistance && _fadeState == 0)
{
_movementVector.x = Time.deltaTime * Speed;
transform.position += (Vector3)_movementVector;
_fadeProgress += _movementVector.x / _fadeDistance;
foreach (SpriteRenderer spriteRenderer in SpriteRenderers)
{
spriteRenderer.color = new Color(1.0f, 1.0f, 1.0f, 1.0f - _fadeProgress);
}
}
else if (_fadeProgress > _fadeDistance && _fadeState == 0)
{
transform.position = new Vector3(_originalPos.x - _fadeDistance, _originalPos.y, _originalPos.z);
_fadeState = 1;
}
else if (_fadeProgress > _fadeDistance && _fadeState == 1 && _fadeProgress < _fadeDistance * 2)
{
_movementVector.x = Time.deltaTime * Speed;
transform.position += (Vector3)_movementVector;
_fadeProgress += _movementVector.x / _fadeDistance;
foreach (SpriteRenderer spriteRenderer in SpriteRenderers)
{
spriteRenderer.color = new Color(1.0f, 1.0f, 1.0f, _fadeProgress - _fadeDistance);
}
}
else
{
// disable fading
_isFading = false;
_fadeProgress = 0;
_fadeState = 0;
foreach (SpriteRenderer spriteRenderer in SpriteRenderers)
{
spriteRenderer.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
}
// reset position
_movementVector.x = 0;
transform.position = _originalPos;
// toggle detection
_detection.SetActive(true);
_chargeDetection.SetActive(false);
// toggle animation
Animation.ToggleChargeAnim(false);
}
return;
}
// 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)
{
// charge state
_isCharging = false;
_timeSinceLastCharge = 0;
_opponentsHit.Clear();
// detection state
IsEnemyDetected = false;
Enemy = null;
// toggle detection
_detection.SetActive(false);
_chargeDetection.SetActive(false);
// start fading
_isFading = true;
}
}
else
{
_timeSinceLastCharge += Time.deltaTime;
if (IsEnemyDetected)
{
AttackEnemy();
}
}
}
private void AttackEnemy()
{
//Attack Cooldown
if (AttackSpeedWait > AttackInterval)
{
AttackDamage = _originalAttackDamage;
Animation.PlayAttackAnim();
AttackSpeedWait = 0f;
}
AttackSpeedWait += Time.deltaTime;
}
private void AttackEnemyRiding()
{
AttackDamage = _chargeAttackDamage;
_rootScript.Attack();
_opponentsHit.Add(Enemy);
}
// 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)
{
if (!_opponentsHit.Contains(entity))
{
Enemy = entity;
AttackEnemyRiding();
}
}
if (_detectionScript.DetectedEntities.Count > 0)
{
_detection.SetActive(false);
}
}
}