From 52d6584577a5fee01a66b986a83436bc92ca2cf0 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 30 Jun 2025 00:15:50 -0400 Subject: [PATCH] knightOnHorse fading implemented --- Assets/Prefabs/Sticks/knightOnHorse.prefab | 7 +- Assets/Scripts/Ally/Rider.cs | 126 +++++++++++++++------ Assets/Scripts/Entity.cs | 4 +- 3 files changed, 100 insertions(+), 37 deletions(-) diff --git a/Assets/Prefabs/Sticks/knightOnHorse.prefab b/Assets/Prefabs/Sticks/knightOnHorse.prefab index ba9c7d9..ba70fee 100644 --- a/Assets/Prefabs/Sticks/knightOnHorse.prefab +++ b/Assets/Prefabs/Sticks/knightOnHorse.prefab @@ -1010,7 +1010,8 @@ MonoBehaviour: _chargeAttackDamage: 3 _chargeCooldown: 10 _maxChargeHitCount: 3 - _maxChargeDistance: 10 + _maxChargeDistance: 7 + _fadeDistance: 1 _detection: {fileID: 9048754633958631738} _chargeDetection: {fileID: 3062706309015911873} _root: {fileID: 2230360378127571586} @@ -2776,7 +2777,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 9048754633958631738} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: -0.33, y: 0, z: 0} + m_LocalPosition: {x: -0.57, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] @@ -2796,7 +2797,7 @@ BoxCollider2D: m_IsTrigger: 1 m_UsedByEffector: 0 m_UsedByComposite: 0 - m_Offset: {x: 1.33, y: 0} + m_Offset: {x: 1.57, y: 0} m_SpriteTilingProperty: border: {x: 0, y: 0, z: 0, w: 0} pivot: {x: 0, y: 0} diff --git a/Assets/Scripts/Ally/Rider.cs b/Assets/Scripts/Ally/Rider.cs index 67eeec7..7809ac6 100644 --- a/Assets/Scripts/Ally/Rider.cs +++ b/Assets/Scripts/Ally/Rider.cs @@ -13,6 +13,8 @@ public class Rider : Ally [SerializeField] private int _maxChargeDistance; [SerializeField] + private float _fadeDistance; + [SerializeField] private GameObject _detection; [SerializeField] private GameObject _chargeDetection; @@ -25,10 +27,15 @@ public class Rider : Ally private int _originalAttackDamage; private Vector3 _originalPos; private Vector2 _movementVector = Vector2.zero; + private bool _isCharging; private float _timeSinceLastCharge; private List _opponentsHit = new List(); + private float _fadeProgress; + private bool _isFading; + private int _fadeState; + public override void Start() { base.Start(); @@ -44,6 +51,61 @@ public class Rider : Ally 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) { @@ -73,10 +135,6 @@ public class Rider : Ally // reset if (transform.position.x - _originalPos.x >= _maxChargeDistance || _opponentsHit.Count >= _maxChargeHitCount) { - // position - _movementVector = Vector2.zero; - transform.position = _originalPos; - // charge state _isCharging = false; _timeSinceLastCharge = 0; @@ -86,12 +144,12 @@ public class Rider : Ally IsEnemyDetected = false; Enemy = null; - // toggle animation - Animation.ToggleChargeAnim(false); - // toggle detection - _detection.SetActive(true); + _detection.SetActive(false); _chargeDetection.SetActive(false); + + // start fading + _isFading = true; } } else @@ -103,31 +161,6 @@ public class Rider : Ally } } } - - // 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 @@ -142,4 +175,31 @@ public class Rider : Ally 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); + } + } } \ No newline at end of file diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 1497423..943edda 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -33,7 +33,7 @@ public abstract class Entity : LevelObject public virtual void Start() { _maxHp = _hp; - _spriteRenderers = GetComponentsInChildren(); + _spriteRenderers = GetComponentsInChildren(true); _audioPlayerComponent = GetComponent(); Animation = gameObject.AddComponent(); } @@ -155,6 +155,8 @@ public abstract class Entity : LevelObject set { _animation = value; } } + public SpriteRenderer[] SpriteRenderers { get { return _spriteRenderers; } } + #region [LevelManager code] public override bool Equals(ILevelObject other) {