From 2a2f3bf04050d99b01abfc4d994ae35c07ad0898 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 29 Jun 2025 04:31:28 -0400 Subject: [PATCH] charging animation implemented --- .../BaseStick/animator_baseStick.controller | 2 +- Assets/Prefabs/Sticks/knightOnHorse.prefab | 10 ++-- Assets/Scripts/Ally/Rider.cs | 52 ++++++++++++++----- Assets/Scripts/AnimationEntity.cs | 47 ++++++++++++----- Assets/Scripts/Detection.cs | 1 + Assets/Scripts/Entity.cs | 34 ++++++------ Assets/Scripts/Root.cs | 12 ----- 7 files changed, 99 insertions(+), 59 deletions(-) diff --git a/Assets/Animations/Sticks/BaseStick/animator_baseStick.controller b/Assets/Animations/Sticks/BaseStick/animator_baseStick.controller index ebf8462..f058a8a 100644 --- a/Assets/Animations/Sticks/BaseStick/animator_baseStick.controller +++ b/Assets/Animations/Sticks/BaseStick/animator_baseStick.controller @@ -67,7 +67,7 @@ AnimatorStateTransition: m_TransitionDuration: 0.25 m_TransitionOffset: 0 m_ExitTime: 0.875 - m_HasExitTime: 1 + m_HasExitTime: 0 m_HasFixedDuration: 1 m_InterruptionSource: 0 m_OrderedInterruption: 1 diff --git a/Assets/Prefabs/Sticks/knightOnHorse.prefab b/Assets/Prefabs/Sticks/knightOnHorse.prefab index 77cfe19..ba9c7d9 100644 --- a/Assets/Prefabs/Sticks/knightOnHorse.prefab +++ b/Assets/Prefabs/Sticks/knightOnHorse.prefab @@ -1235,7 +1235,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 3062706309015911873} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalPosition: {x: 2.52, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] @@ -1255,7 +1255,7 @@ BoxCollider2D: m_IsTrigger: 1 m_UsedByEffector: 0 m_UsedByComposite: 0 - m_Offset: {x: 1.7083191, y: 0} + m_Offset: {x: -0.1805121, y: 0} m_SpriteTilingProperty: border: {x: 0, y: 0, z: 0, w: 0} pivot: {x: 0, y: 0} @@ -1266,7 +1266,7 @@ BoxCollider2D: adaptiveTiling: 0 m_AutoTiling: 0 serializedVersion: 2 - m_Size: {x: 0.5, y: 0.5} + m_Size: {x: 0.6224893, y: 0.5} m_EdgeRadius: 0 --- !u!114 &5850107427616902080 MonoBehaviour: @@ -2776,7 +2776,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 9048754633958631738} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalPosition: {x: -0.33, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] @@ -2796,7 +2796,7 @@ BoxCollider2D: m_IsTrigger: 1 m_UsedByEffector: 0 m_UsedByComposite: 0 - m_Offset: {x: 1, y: 0} + m_Offset: {x: 1.33, 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 bf59050..67eeec7 100644 --- a/Assets/Scripts/Ally/Rider.cs +++ b/Assets/Scripts/Ally/Rider.cs @@ -19,9 +19,10 @@ public class Rider : Ally [SerializeField] private GameObject _root; - private Detection _chargeDetectionScript; private Root _rootScript; + private Detection _detectionScript; + private int _originalAttackDamage; private Vector3 _originalPos; private Vector2 _movementVector = Vector2.zero; private bool _isCharging; @@ -32,33 +33,37 @@ public class Rider : Ally { base.Start(); - _chargeDetectionScript = _chargeDetection.GetComponent(); _rootScript = _root.GetComponent(); + _detectionScript = _detection.GetComponent(); + _originalAttackDamage = _chargeAttackDamage; _originalPos = transform.position; - _isCharging = true; + + _timeSinceLastCharge = _chargeCooldown + 1; } public override void Update() { - // check for charge cooldown - if (_timeSinceLastCharge > _chargeCooldown) + if (_timeSinceLastCharge > _chargeCooldown && !_isCharging) { + SweepAttack(); + _isCharging = true; + Animation.ToggleChargeAnim(true); } if (_isCharging) { + SweepAttack(); + // 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)) { @@ -76,11 +81,14 @@ public class Rider : Ally _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); @@ -96,21 +104,39 @@ public class Rider : Ally } } - void AttackEnemyRiding() + // 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() { - Debug.Log("Attack Riding"); - _rootScript.AttackWithCustomDamage(_chargeAttackDamage); + 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); } - void AttackEnemy() + private void AttackEnemy() { //Attack Cooldown if (AttackSpeedWait > AttackInterval) { - + AttackDamage = _originalAttackDamage; Animation.PlayAttackAnim(); + AttackSpeedWait = 0f; } diff --git a/Assets/Scripts/AnimationEntity.cs b/Assets/Scripts/AnimationEntity.cs index 5e83fa9..554f6b0 100644 --- a/Assets/Scripts/AnimationEntity.cs +++ b/Assets/Scripts/AnimationEntity.cs @@ -4,12 +4,13 @@ using UnityEngine; public class AnimationEntity : MonoBehaviour { - enum EntityAnimationState + enum EntityAnimationState { Idle = 0, Walking = 1, Attacking = 2, - Dying = 3 + Dying = 3, + Charging = 4 } private EntityAnimationState entityState; @@ -17,15 +18,15 @@ public class AnimationEntity : MonoBehaviour private bool _doSomething = false; private bool _isDead = false; private bool _isWalking = false; - - void Start() + + void Start() { AttackSpeedMultiplier = 10; SpeedMultiplier = 10; _animatorEntity = GetComponentInChildren(); } - void Update() + void Update() { if (!_animatorEntity) return; if (_doSomething && _animatorEntity.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1f) @@ -47,18 +48,20 @@ public class AnimationEntity : MonoBehaviour }; } - public void PlayIdleAnim() + public void PlayIdleAnim() { - if(!_isDead) { + if (!_isDead) + { _animatorEntity.speed = 1; _animatorEntity.Play("idle", 0, 0f); entityState = EntityAnimationState.Idle; } } - public void PlayWalkAnim() + public void PlayWalkAnim() { - if(!_isDead) { + if (!_isDead) + { _animatorEntity.speed = SpeedMultiplier; _animatorEntity.Play("walk", 0, 0f); entityState = EntityAnimationState.Walking; @@ -66,9 +69,10 @@ public class AnimationEntity : MonoBehaviour } } - public void PlayAttackAnim() + public void PlayAttackAnim() { - if(!_isDead && _animatorEntity != null) { + if (!_isDead && _animatorEntity != null) + { _animatorEntity.speed = AttackSpeedMultiplier; _animatorEntity.Play("attack", 0, 0f); entityState = EntityAnimationState.Attacking; @@ -76,7 +80,24 @@ public class AnimationEntity : MonoBehaviour } } - public void PlayDieAnim() + public void ToggleChargeAnim(bool isCharging) + { + if (!_isDead && _animatorEntity != null) + { + _animatorEntity.speed = AttackSpeedMultiplier; + _animatorEntity.SetBool("Charging", isCharging); + if (isCharging) + { + entityState = EntityAnimationState.Charging; + } + else + { + PlayIdleAnim(); + } + } + } + + public void PlayDieAnim() { // Not every entity needs an animator if (_animatorEntity != null) @@ -84,7 +105,7 @@ public class AnimationEntity : MonoBehaviour _animatorEntity.speed = 1; _animatorEntity.Play("die", 0, 0f); } - + entityState = EntityAnimationState.Dying; _doSomething = true; _isDead = true; diff --git a/Assets/Scripts/Detection.cs b/Assets/Scripts/Detection.cs index ef419d1..aefdfd8 100644 --- a/Assets/Scripts/Detection.cs +++ b/Assets/Scripts/Detection.cs @@ -178,4 +178,5 @@ public class Detection : MonoBehaviour return rect; } } + public List DetectedEntities { get { return detectedEntities; } } } diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 9ee431a..1497423 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -20,7 +20,7 @@ public abstract class Entity : LevelObject private float _attack_speed_wait = 0f; private AnimationEntity _animation; private Shader _shaderGUItext; - private Shader _shaderSpritesDefault; + private Shader _shaderSpritesDefault; private SpriteRenderer[] _spriteRenderers; private AudioPlayerComponent _audioPlayerComponent; @@ -45,41 +45,41 @@ public abstract class Entity : LevelObject _lifeBar.gameObject.SetActive(_lifeBar.value <= 0.99f); } //Start the animation of death and the fading of the entity - public virtual void Death() + public virtual void Death() { _animation.PlayDieAnim(); Invoke("Dying", 0.1f); } //Recursive method that fade the dying entity - private void Dying() + private void Dying() { foreach (SpriteRenderer renderer in _spriteRenderers) { Color currentColor = renderer.color; - currentColor.a = currentColor.a - 0.1f; + currentColor.a = currentColor.a - 0.1f; renderer.color = currentColor; } - if(_spriteRenderers[0].color.a > 0f) + if (_spriteRenderers[0].color.a > 0f) { Invoke("Dying", 0.1f); } - else + else { Destroy(gameObject); } - + } //When hit : get damage and start a flash of light - public void Hit(int damage) + public void Hit(int damage) { - _hp-=damage; + _hp -= damage; _lifeBar.value = _hp / (float)_maxHp; - _shaderGUItext = Shader.Find("GUI/Text Shader"); + _shaderGUItext = Shader.Find("GUI/Text Shader"); _shaderSpritesDefault = Shader.Find("Sprites/Default"); foreach (SpriteRenderer renderer in _spriteRenderers) @@ -90,7 +90,7 @@ public abstract class Entity : LevelObject } //End the flash of light from the method above - private void ReturnNormalColor() + private void ReturnNormalColor() { foreach (SpriteRenderer renderer in _spriteRenderers) { @@ -98,15 +98,15 @@ public abstract class Entity : LevelObject } } - public void Move() + public void Move() { - if(!_animation.IsWalking) + if (!_animation.IsWalking) { _animation.PlayWalkAnim(); } } - public void PlaySound(string soundName, float volumeMultiplier = 1f, float overrideVolume = -1f) + public void PlaySound(string soundName, float volumeMultiplier = 1f, float overrideVolume = -1f) { _audioPlayerComponent.PlaySound(soundName, volumeMultiplier, overrideVolume); } @@ -123,7 +123,11 @@ public abstract class Entity : LevelObject public float Speed => _speed * SpeedMultiplier; - public int AttackDamage => (int)(_attack_damage * DamageMultiplier); + public int AttackDamage + { + get { return (int)(_attack_damage * DamageMultiplier); } + set { _attack_damage = value; } + } public float AttackInterval => _attack_interval / AttackSpeedMultiplier; diff --git a/Assets/Scripts/Root.cs b/Assets/Scripts/Root.cs index 5578eb9..338cc8f 100644 --- a/Assets/Scripts/Root.cs +++ b/Assets/Scripts/Root.cs @@ -24,18 +24,6 @@ public class Root : MonoBehaviour } } - public void AttackWithCustomDamage(int damage) - { - if (_entity == null || _entity.Enemy == null) return; - - _entity.Enemy.Hit(damage); - if (_entity.Enemy.Hp <= 0) - { - _entity.Enemy.Death(); - _entity.IsEnemyDetected = false; - } - } - public void ShotProjectile() { Rigidbody2D _rigidbodyAlly;