From 4a5313a9cff3d38ccdcfacc143e1b1c2675b1608 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Sun, 25 Jun 2023 18:14:45 -0400 Subject: [PATCH 1/4] animations walk/idle/death/move working --- .../Animations/MonsterBaseAnimator.controller | 85 +++++++++++++++++-- .../ClawClawRough/ClawClawRough_die.anim | 2 +- .../ClawClawRough/ClawClawRough_idle.anim | 2 +- .../ClawClawRough/ClawClawRough_walk.anim | 2 +- Assets/Scripts/Ally/Ally.cs | 5 +- Assets/Scripts/AnimationEntity.cs | 9 ++ Assets/Scripts/Detection.cs | 47 +++++----- Assets/Scripts/Entity.cs | 54 ++++++++++++ Assets/Scripts/Opponent/Opponent.cs | 7 +- 9 files changed, 181 insertions(+), 32 deletions(-) diff --git a/Assets/Animations/MonsterBaseAnimator.controller b/Assets/Animations/MonsterBaseAnimator.controller index d32d307..62f2cc8 100644 --- a/Assets/Animations/MonsterBaseAnimator.controller +++ b/Assets/Animations/MonsterBaseAnimator.controller @@ -1,5 +1,31 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: +--- !u!1102 &-5723871321714695223 +AnimatorState: + serializedVersion: 6 + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: New State + m_Speed: 1 + m_CycleOffset: 0 + m_Transitions: [] + m_StateMachineBehaviours: [] + m_Position: {x: 50, y: 50, z: 0} + m_IKOnFeet: 0 + m_WriteDefaultValues: 1 + m_Mirror: 0 + m_SpeedParameterActive: 0 + m_MirrorParameterActive: 0 + m_CycleOffsetParameterActive: 0 + m_TimeParameterActive: 0 + m_Motion: {fileID: 0} + m_Tag: + m_SpeedParameter: + m_MirrorParameter: + m_CycleOffsetParameter: + m_TimeParameter: --- !u!1102 &-5615459309368676638 AnimatorState: serializedVersion: 6 @@ -10,7 +36,8 @@ AnimatorState: m_Name: walk m_Speed: 1 m_CycleOffset: 0 - m_Transitions: [] + m_Transitions: + - {fileID: 6351061945338306534} m_StateMachineBehaviours: [] m_Position: {x: 50, y: 50, z: 0} m_IKOnFeet: 0 @@ -84,7 +111,8 @@ AnimatorState: m_Name: idle m_Speed: 1 m_CycleOffset: 0 - m_Transitions: [] + m_Transitions: + - {fileID: 1272373060093443428} m_StateMachineBehaviours: [] m_Position: {x: 50, y: 50, z: 0} m_IKOnFeet: 0 @@ -126,6 +154,28 @@ AnimatorState: m_MirrorParameter: m_CycleOffsetParameter: m_TimeParameter: +--- !u!1101 &1272373060093443428 +AnimatorStateTransition: + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: + m_Conditions: [] + m_DstStateMachine: {fileID: 0} + m_DstState: {fileID: 703164551053012985} + m_Solo: 0 + m_Mute: 0 + m_IsExit: 0 + serializedVersion: 3 + m_TransitionDuration: 0.25 + m_TransitionOffset: 0 + m_ExitTime: 0.75 + m_HasExitTime: 1 + m_HasFixedDuration: 1 + m_InterruptionSource: 0 + m_OrderedInterruption: 1 + m_CanTransitionToSelf: 1 --- !u!1107 &4042451101728204287 AnimatorStateMachine: serializedVersion: 6 @@ -137,16 +187,19 @@ AnimatorStateMachine: m_ChildStates: - serializedVersion: 1 m_State: {fileID: 703164551053012985} - m_Position: {x: 260, y: 110, z: 0} + m_Position: {x: 560, y: 160, z: 0} - serializedVersion: 1 m_State: {fileID: 1028912623824696338} - m_Position: {x: 260, y: 50, z: 0} + m_Position: {x: 260, y: 100, z: 0} - serializedVersion: 1 m_State: {fileID: -5615459309368676638} - m_Position: {x: 260, y: -10, z: 0} + m_Position: {x: 560, y: 90, z: 0} - serializedVersion: 1 m_State: {fileID: -4316900719387780663} m_Position: {x: 260, y: 170, z: 0} + - serializedVersion: 1 + m_State: {fileID: -5723871321714695223} + m_Position: {x: 704.1478, y: -596.24896, z: 0} m_ChildStateMachines: [] m_AnyStateTransitions: [] m_EntryTransitions: [] @@ -157,3 +210,25 @@ AnimatorStateMachine: m_ExitPosition: {x: 800, y: 120, z: 0} m_ParentStateMachinePosition: {x: 800, y: 20, z: 0} m_DefaultState: {fileID: -5615459309368676638} +--- !u!1101 &6351061945338306534 +AnimatorStateTransition: + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: + m_Conditions: [] + m_DstStateMachine: {fileID: 0} + m_DstState: {fileID: -5615459309368676638} + m_Solo: 0 + m_Mute: 0 + m_IsExit: 0 + serializedVersion: 3 + m_TransitionDuration: 0.25 + m_TransitionOffset: 0 + m_ExitTime: 0.75 + m_HasExitTime: 1 + m_HasFixedDuration: 1 + m_InterruptionSource: 0 + m_OrderedInterruption: 1 + m_CanTransitionToSelf: 1 diff --git a/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_die.anim b/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_die.anim index 1c2f3ec..99c5279 100644 --- a/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_die.anim +++ b/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_die.anim @@ -1705,7 +1705,7 @@ AnimationClip: m_Level: 0 m_CycleOffset: 0 m_HasAdditiveReferencePose: 0 - m_LoopTime: 1 + m_LoopTime: 0 m_LoopBlend: 0 m_LoopBlendOrientation: 0 m_LoopBlendPositionY: 0 diff --git a/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_idle.anim b/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_idle.anim index 0d1d26c..8095860 100644 --- a/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_idle.anim +++ b/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_idle.anim @@ -774,7 +774,7 @@ AnimationClip: m_Level: 0 m_CycleOffset: 0 m_HasAdditiveReferencePose: 0 - m_LoopTime: 1 + m_LoopTime: 0 m_LoopBlend: 0 m_LoopBlendOrientation: 0 m_LoopBlendPositionY: 0 diff --git a/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_walk.anim b/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_walk.anim index 499ebf7..93ee7e1 100644 --- a/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_walk.anim +++ b/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_walk.anim @@ -733,7 +733,7 @@ AnimationClip: m_Level: 0 m_CycleOffset: 0 m_HasAdditiveReferencePose: 0 - m_LoopTime: 1 + m_LoopTime: 0 m_LoopBlend: 0 m_LoopBlendOrientation: 0 m_LoopBlendPositionY: 0 diff --git a/Assets/Scripts/Ally/Ally.cs b/Assets/Scripts/Ally/Ally.cs index 55fa7b9..6cabeb3 100644 --- a/Assets/Scripts/Ally/Ally.cs +++ b/Assets/Scripts/Ally/Ally.cs @@ -17,12 +17,13 @@ public class Ally : Entity //Attack Cooldown if(AttackSpeed < AttackSpeedWait) { - Enemy.Hp-=AttackDamage; + Enemy.hit(AttackDamage); Debug.Log("Opponent Hp = " + Enemy.Hp); //Kill if no hp if(Enemy.Hp <= 0) { - Destroy(Enemy); + IsEnemyDetected = false; + Enemy.death(); } AttackSpeedWait = 0f; diff --git a/Assets/Scripts/AnimationEntity.cs b/Assets/Scripts/AnimationEntity.cs index 7d80a9f..97e59fd 100644 --- a/Assets/Scripts/AnimationEntity.cs +++ b/Assets/Scripts/AnimationEntity.cs @@ -25,9 +25,18 @@ public class AnimationEntity : MonoBehaviour _animator_entity.Play("idle", 0, 0f); } + public void Walk() { + _animator_entity.Play("walk", 0, 0f); + } + public void Attack() { _animator_entity.Play("attack", 0, 0f); _doSomething = true; } + public void Die() { + _animator_entity.Play("die", 0, 0f); + _doSomething = true; + } + } diff --git a/Assets/Scripts/Detection.cs b/Assets/Scripts/Detection.cs index 44adf33..be8b19e 100644 --- a/Assets/Scripts/Detection.cs +++ b/Assets/Scripts/Detection.cs @@ -22,30 +22,35 @@ public class Detection : MonoBehaviour void OnTriggerEnter2D(Collider2D other) { //Projectiles detection + damage deal - if(_projectileDamage > 0 && other.gameObject.GetComponent() == _entityLinked) { + if(_entityLinked is not null) { + if(_projectileDamage > 0 && other.gameObject.GetComponent() == _entityLinked) { + + _entityLinked.hit(_projectileDamage); + //Kill if no hp + if(other.gameObject.GetComponent().Hp <= 0) { + other.gameObject.GetComponent().death(); + _entityLinked = null; + } - other.gameObject.GetComponent().Hp -= _projectileDamage; - //Kill if no hp - if(other.gameObject.GetComponent().Hp <= 0) { - Destroy(other.gameObject); } - } + } void OnTriggerStay2D(Collider2D other) { + if(_entityLinked is not null) { + if(_entityLinked.Enemy == null) { + //Detect the enemy and inform the Ally + if (other.gameObject.tag == "Opponent" && _entityLinked is Ally) { + _entityLinked.IsEnemyDetected = true; + _entityLinked.Enemy = other.gameObject.GetComponent(); + } - if(_entityLinked.Enemy == null) { - //Detect the enemy and inform the Ally - if (other.gameObject.tag == "Opponent" && _entityLinked is Ally) { - _entityLinked.IsEnemyDetected = true; - _entityLinked.Enemy = other.gameObject.GetComponent(); - } - - //Detect the enemy and inform the Opponent - if (other.gameObject.tag == "Ally" && _entityLinked is Opponent) { - _entityLinked.IsEnemyDetected = true; - _entityLinked.Enemy = other.gameObject.GetComponent(); + //Detect the enemy and inform the Opponent + if (other.gameObject.tag == "Ally" && _entityLinked is Opponent) { + _entityLinked.IsEnemyDetected = true; + _entityLinked.Enemy = other.gameObject.GetComponent(); + } } } @@ -53,9 +58,11 @@ public class Detection : MonoBehaviour void OnTriggerExit2D(Collider2D other) { - if(_projectileDamage == 0) { - if ((other.gameObject.tag == "Opponent" && _entityLinked is Ally) || (other.gameObject.tag == "Ally" && _entityLinked is Opponent)) { - _entityLinked.IsEnemyDetected = false; + if(_entityLinked is not null) { + if(_projectileDamage == 0) { + if ((other.gameObject.tag == "Opponent" && _entityLinked is Ally) || (other.gameObject.tag == "Ally" && _entityLinked is Opponent)) { + _entityLinked.IsEnemyDetected = false; + } } } diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 3a8eee1..1c57bb0 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -16,11 +16,65 @@ public class Entity : LevelObject private float _attack_speed; private float _attack_speed_wait = 0f; private AnimationEntity _animation; + private Shader shaderGUItext; + private Shader shaderSpritesDefault; //Enemy Spotted private bool _isEnemyDetected = false; private Entity _enemy; + //Methods + public void death() { + _animation.Die(); + Invoke("dying", 0.1f); + } + + void dying() { + SpriteRenderer[] spriteRenderers; + spriteRenderers = GetComponentsInChildren(); + + + foreach (SpriteRenderer renderer in spriteRenderers) + { + Color currentColor = renderer.color; + currentColor.a = currentColor.a - 0.1f; + renderer.color = currentColor; + } + + if(spriteRenderers[0].color.a > 0f) { + Invoke("dying", 0.1f); + }else { + Destroy(gameObject); + } + + } + + public void hit(int damage) { + + _hp-=damage; + + SpriteRenderer[] spriteRenderers; + spriteRenderers = GetComponentsInChildren(); + shaderGUItext = Shader.Find("GUI/Text Shader"); + shaderSpritesDefault = Shader.Find("Sprites/Default"); + + foreach (SpriteRenderer renderer in spriteRenderers) + { + renderer.material.shader = shaderGUItext; + } + Invoke("returnNormalColor", 0.1f); + } + + void returnNormalColor() { + SpriteRenderer[] spriteRenderers; + spriteRenderers = GetComponentsInChildren(); + + foreach (SpriteRenderer renderer in spriteRenderers) + { + renderer.material.shader = shaderSpritesDefault; + } + } + //GETTERS AND SETTERS public int Hp diff --git a/Assets/Scripts/Opponent/Opponent.cs b/Assets/Scripts/Opponent/Opponent.cs index 822b8ad..ef8a095 100644 --- a/Assets/Scripts/Opponent/Opponent.cs +++ b/Assets/Scripts/Opponent/Opponent.cs @@ -11,6 +11,7 @@ public class Opponent : Entity void Start() { _rigidbody = GetComponent(); + Animation = gameObject.AddComponent(); } void Update() { @@ -29,12 +30,14 @@ public class Opponent : Entity //Attack Cooldown if(AttackSpeed < AttackSpeedWait) { - Enemy.Hp-=AttackDamage; + Animation.Attack(); + + Enemy.hit(AttackDamage); Debug.Log("Ally Hp = " + Enemy.Hp); //Kill if no hp if(Enemy.Hp <= 0) { - Destroy(Enemy); + Enemy.death(); } AttackSpeedWait = 0f; From 11d617db90899ec5928d42dc3b9051b6713d6253 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Sun, 25 Jun 2023 20:05:35 -0400 Subject: [PATCH 2/4] no message --- Assets/Animations/MonsterBaseAnimator.controller | 8 ++++---- Assets/Scripts/AnimationEntity.cs | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Assets/Animations/MonsterBaseAnimator.controller b/Assets/Animations/MonsterBaseAnimator.controller index 62f2cc8..3587c93 100644 --- a/Assets/Animations/MonsterBaseAnimator.controller +++ b/Assets/Animations/MonsterBaseAnimator.controller @@ -187,16 +187,16 @@ AnimatorStateMachine: m_ChildStates: - serializedVersion: 1 m_State: {fileID: 703164551053012985} - m_Position: {x: 560, y: 160, z: 0} + m_Position: {x: 260, y: 170, z: 0} - serializedVersion: 1 m_State: {fileID: 1028912623824696338} - m_Position: {x: 260, y: 100, z: 0} + m_Position: {x: 260, y: 240, z: 0} - serializedVersion: 1 m_State: {fileID: -5615459309368676638} - m_Position: {x: 560, y: 90, z: 0} + m_Position: {x: 260, y: 90, z: 0} - serializedVersion: 1 m_State: {fileID: -4316900719387780663} - m_Position: {x: 260, y: 170, z: 0} + m_Position: {x: 260, y: 310, z: 0} - serializedVersion: 1 m_State: {fileID: -5723871321714695223} m_Position: {x: 704.1478, y: -596.24896, z: 0} diff --git a/Assets/Scripts/AnimationEntity.cs b/Assets/Scripts/AnimationEntity.cs index 97e59fd..c906cde 100644 --- a/Assets/Scripts/AnimationEntity.cs +++ b/Assets/Scripts/AnimationEntity.cs @@ -7,6 +7,7 @@ public class AnimationEntity : MonoBehaviour private Animator _animator_entity; private bool _doSomething = false; + private bool _isDead = false; void Start() { _animator_entity = GetComponentInChildren(); @@ -22,21 +23,28 @@ public class AnimationEntity : MonoBehaviour } public void Idle() { - _animator_entity.Play("idle", 0, 0f); + if(!_isDead) { + _animator_entity.Play("idle", 0, 0f); + } } public void Walk() { - _animator_entity.Play("walk", 0, 0f); + if(!_isDead) { + _animator_entity.Play("walk", 0, 0f); + } } public void Attack() { - _animator_entity.Play("attack", 0, 0f); - _doSomething = true; + if(!_isDead) { + _animator_entity.Play("attack", 0, 0f); + _doSomething = true; + } } public void Die() { _animator_entity.Play("die", 0, 0f); _doSomething = true; + _isDead = true; } } From 5238cb2e8a4dc955d3a8e5305094535487e5bce7 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Fri, 7 Jul 2023 19:15:56 -0400 Subject: [PATCH 3/4] correction du merge request --- Assets/Scripts/Ally/Ally.cs | 16 +++++--- Assets/Scripts/Ally/Archer.cs | 13 +++++-- Assets/Scripts/AnimationEntity.cs | 34 ++++++++++------- Assets/Scripts/Detection.cs | 13 ++++--- Assets/Scripts/Entity.cs | 58 ++++++++++++++++------------- Assets/Scripts/Opponent/Opponent.cs | 17 +++++---- 6 files changed, 89 insertions(+), 62 deletions(-) diff --git a/Assets/Scripts/Ally/Ally.cs b/Assets/Scripts/Ally/Ally.cs index 6cabeb3..212691d 100644 --- a/Assets/Scripts/Ally/Ally.cs +++ b/Assets/Scripts/Ally/Ally.cs @@ -4,8 +4,14 @@ using UnityEngine; public class Ally : Entity { + public override void Start() + { + base.Start(); - void Update() { + } + + void Update() + { if(IsEnemyDetected) { AttackEnemy(); @@ -13,17 +19,17 @@ public class Ally : Entity } - void AttackEnemy() { + void AttackEnemy() + { //Attack Cooldown if(AttackSpeed < AttackSpeedWait) { - Enemy.hit(AttackDamage); - Debug.Log("Opponent Hp = " + Enemy.Hp); + Enemy.Hit(AttackDamage); //Kill if no hp if(Enemy.Hp <= 0) { IsEnemyDetected = false; - Enemy.death(); + Enemy.Death(); } AttackSpeedWait = 0f; diff --git a/Assets/Scripts/Ally/Archer.cs b/Assets/Scripts/Ally/Archer.cs index 8872636..d60b2e9 100644 --- a/Assets/Scripts/Ally/Archer.cs +++ b/Assets/Scripts/Ally/Archer.cs @@ -11,12 +11,16 @@ public class Archer : Ally private Rigidbody2D _rigidbodyAlly; private Rigidbody2D _rigidbodyOpponent; - void Start() { + public override void Start() + { + base.Start(); + _rigidbodyAlly = GetComponent(); Animation = gameObject.AddComponent(); } - void Update() { + void Update() + { if(IsEnemyDetected) { _rigidbodyOpponent = Enemy.GetComponent(); @@ -25,12 +29,13 @@ public class Archer : Ally } - void AttackEnemy() { + void AttackEnemy() + { //Attack Cooldown if(AttackSpeed < AttackSpeedWait) { - Animation.Attack(); + Animation.PlayAttackAnim(); GameObject _newArrow = Instantiate(_arrow, _rigidbodyAlly.position, Quaternion.identity); diff --git a/Assets/Scripts/AnimationEntity.cs b/Assets/Scripts/AnimationEntity.cs index c906cde..8890e82 100644 --- a/Assets/Scripts/AnimationEntity.cs +++ b/Assets/Scripts/AnimationEntity.cs @@ -5,44 +5,50 @@ using UnityEngine; public class AnimationEntity : MonoBehaviour { - private Animator _animator_entity; + private Animator _animatorEntity; private bool _doSomething = false; private bool _isDead = false; - void Start() { - _animator_entity = GetComponentInChildren(); + void Start() + { + _animatorEntity = GetComponentInChildren(); } - void Update() { + void Update() + { - if (_doSomething && _animator_entity.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1f) + if (_doSomething && _animatorEntity.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1f) { - Idle(); + PlayIdleAnim(); _doSomething = false; } } - public void Idle() { + public void PlayIdleAnim() + { if(!_isDead) { - _animator_entity.Play("idle", 0, 0f); + _animatorEntity.Play("idle", 0, 0f); } } - public void Walk() { + public void PlayWalkAnim() + { if(!_isDead) { - _animator_entity.Play("walk", 0, 0f); + _animatorEntity.Play("walk", 0, 0f); } } - public void Attack() { + public void PlayAttackAnim() + { if(!_isDead) { - _animator_entity.Play("attack", 0, 0f); + _animatorEntity.Play("attack", 0, 0f); _doSomething = true; } } - public void Die() { - _animator_entity.Play("die", 0, 0f); + public void PlayDieAnim() + { + _animatorEntity.Play("die", 0, 0f); _doSomething = true; _isDead = true; } diff --git a/Assets/Scripts/Detection.cs b/Assets/Scripts/Detection.cs index be8b19e..b5bab1c 100644 --- a/Assets/Scripts/Detection.cs +++ b/Assets/Scripts/Detection.cs @@ -22,13 +22,13 @@ public class Detection : MonoBehaviour void OnTriggerEnter2D(Collider2D other) { //Projectiles detection + damage deal - if(_entityLinked is not null) { + if(_entityLinked != null) { if(_projectileDamage > 0 && other.gameObject.GetComponent() == _entityLinked) { - _entityLinked.hit(_projectileDamage); + _entityLinked.Hit(_projectileDamage); //Kill if no hp if(other.gameObject.GetComponent().Hp <= 0) { - other.gameObject.GetComponent().death(); + other.gameObject.GetComponent().Death(); _entityLinked = null; } @@ -37,8 +37,9 @@ public class Detection : MonoBehaviour } - void OnTriggerStay2D(Collider2D other) { - if(_entityLinked is not null) { + void OnTriggerStay2D(Collider2D other) + { + if(_entityLinked != null) { if(_entityLinked.Enemy == null) { //Detect the enemy and inform the Ally if (other.gameObject.tag == "Opponent" && _entityLinked is Ally) { @@ -58,7 +59,7 @@ public class Detection : MonoBehaviour void OnTriggerExit2D(Collider2D other) { - if(_entityLinked is not null) { + if(_entityLinked != null) { if(_projectileDamage == 0) { if ((other.gameObject.tag == "Opponent" && _entityLinked is Ally) || (other.gameObject.tag == "Ally" && _entityLinked is Opponent)) { _entityLinked.IsEnemyDetected = false; diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 1c57bb0..7ba16df 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -16,62 +16,68 @@ public class Entity : LevelObject private float _attack_speed; private float _attack_speed_wait = 0f; private AnimationEntity _animation; - private Shader shaderGUItext; - private Shader shaderSpritesDefault; + private Shader _shaderGUItext; + private Shader _shaderSpritesDefault; + private SpriteRenderer[] _spriteRenderers; //Enemy Spotted private bool _isEnemyDetected = false; private Entity _enemy; //Methods - public void death() { - _animation.Die(); - Invoke("dying", 0.1f); + public virtual void Start() + { + _spriteRenderers = GetComponentsInChildren(); } - void dying() { - SpriteRenderer[] spriteRenderers; - spriteRenderers = GetComponentsInChildren(); + //Start the animation of death and the fading of the entity + public void Death() + { + _animation.PlayDieAnim(); + Invoke("Dying", 0.1f); + } - - foreach (SpriteRenderer renderer in spriteRenderers) + //Recursive method that fade the dying entity + void Dying() + { + foreach (SpriteRenderer renderer in _spriteRenderers) { Color currentColor = renderer.color; currentColor.a = currentColor.a - 0.1f; renderer.color = currentColor; } - if(spriteRenderers[0].color.a > 0f) { - Invoke("dying", 0.1f); + if(_spriteRenderers[0].color.a > 0f) { + Invoke("Dying", 0.1f); }else { Destroy(gameObject); } } - public void hit(int damage) { + //When hit : get damage and start a flash of light + public void Hit(int damage) + { _hp-=damage; - SpriteRenderer[] spriteRenderers; - spriteRenderers = GetComponentsInChildren(); - shaderGUItext = Shader.Find("GUI/Text Shader"); - shaderSpritesDefault = Shader.Find("Sprites/Default"); + _shaderGUItext = Shader.Find("GUI/Text Shader"); + _shaderSpritesDefault = Shader.Find("Sprites/Default"); - foreach (SpriteRenderer renderer in spriteRenderers) + foreach (SpriteRenderer renderer in _spriteRenderers) { - renderer.material.shader = shaderGUItext; + renderer.material.shader = _shaderGUItext; } - Invoke("returnNormalColor", 0.1f); + Invoke("ReturnNormalColor", 0.1f); } - void returnNormalColor() { - SpriteRenderer[] spriteRenderers; - spriteRenderers = GetComponentsInChildren(); - - foreach (SpriteRenderer renderer in spriteRenderers) + //End the flash of light from the method above + void ReturnNormalColor() + { + + foreach (SpriteRenderer renderer in _spriteRenderers) { - renderer.material.shader = shaderSpritesDefault; + renderer.material.shader = _shaderSpritesDefault; } } diff --git a/Assets/Scripts/Opponent/Opponent.cs b/Assets/Scripts/Opponent/Opponent.cs index ef8a095..0f23f7b 100644 --- a/Assets/Scripts/Opponent/Opponent.cs +++ b/Assets/Scripts/Opponent/Opponent.cs @@ -8,13 +8,16 @@ public class Opponent : Entity private Vector2 _movementVector = Vector2.zero; private Rigidbody2D _rigidbody; - void Start() + public override void Start() { + base.Start(); + _rigidbody = GetComponent(); Animation = gameObject.AddComponent(); } - void Update() { + void Update() + { _movementVector.x = -Time.deltaTime * Speed; @@ -26,18 +29,18 @@ public class Opponent : Entity } - void AttackEnemy() { + void AttackEnemy() + { //Attack Cooldown if(AttackSpeed < AttackSpeedWait) { - Animation.Attack(); + Animation.PlayAttackAnim(); - Enemy.hit(AttackDamage); - Debug.Log("Ally Hp = " + Enemy.Hp); + Enemy.Hit(AttackDamage); //Kill if no hp if(Enemy.Hp <= 0) { - Enemy.death(); + Enemy.Death(); } AttackSpeedWait = 0f; From e44264d66f2068dc53f341e5bb19684e2e3409db Mon Sep 17 00:00:00 2001 From: Baptiste Date: Sun, 9 Jul 2023 21:33:35 -0400 Subject: [PATCH 4/4] ajout event animation, ajout classe Root qui sert de transition entre la classe Entity et l'animator. --- .../BaseStick/baseStick_attack.anim | 9 ++++- .../ClawClawRough/ClawClawRough_attack.anim | 9 ++++- .../Monsters/ClawClawRough_monster.prefab | 14 +++++++ Assets/Prefabs/baseArcher.prefab | 16 +++++++- Assets/Prefabs/baseStick.prefab | 14 +++++++ Assets/Scripts/Ally/Ally.cs | 8 +--- Assets/Scripts/Ally/Archer.cs | 18 +-------- Assets/Scripts/Opponent/Opponent.cs | 7 ---- Assets/Scripts/Root.cs | 37 +++++++++++++++++++ Assets/Scripts/Root.cs.meta | 11 ++++++ 10 files changed, 109 insertions(+), 34 deletions(-) create mode 100644 Assets/Scripts/Root.cs create mode 100644 Assets/Scripts/Root.cs.meta diff --git a/Assets/Animations/BaseStick/baseStick_attack.anim b/Assets/Animations/BaseStick/baseStick_attack.anim index a92a0b4..22aa2e9 100644 --- a/Assets/Animations/BaseStick/baseStick_attack.anim +++ b/Assets/Animations/BaseStick/baseStick_attack.anim @@ -2066,4 +2066,11 @@ AnimationClip: script: {fileID: 0} m_HasGenericRootTransform: 1 m_HasMotionFloatCurves: 0 - m_Events: [] + m_Events: + - time: 0.6166667 + functionName: ShotProjectile + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 diff --git a/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_attack.anim b/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_attack.anim index 01ab6f5..42ba174 100644 --- a/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_attack.anim +++ b/Assets/Animations/Monsters/ClawClawRough/ClawClawRough_attack.anim @@ -4962,4 +4962,11 @@ AnimationClip: script: {fileID: 0} m_HasGenericRootTransform: 1 m_HasMotionFloatCurves: 0 - m_Events: [] + m_Events: + - time: 0.65 + functionName: Attack + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 diff --git a/Assets/Prefabs/Monsters/ClawClawRough_monster.prefab b/Assets/Prefabs/Monsters/ClawClawRough_monster.prefab index 7f38384..8264e43 100644 --- a/Assets/Prefabs/Monsters/ClawClawRough_monster.prefab +++ b/Assets/Prefabs/Monsters/ClawClawRough_monster.prefab @@ -497,6 +497,7 @@ GameObject: - component: {fileID: 4594931196062474192} - component: {fileID: 3579562406765713002} - component: {fileID: 2002611158446544964} + - component: {fileID: 7161153261079215068} m_Layer: 0 m_Name: root m_TagString: Untagged @@ -562,6 +563,19 @@ SortingGroup: m_SortingLayerID: 0 m_SortingLayer: 0 m_SortingOrder: 2 +--- !u!114 &7161153261079215068 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1531129202202440418} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c0fbd934c179894458914437255781c4, type: 3} + m_Name: + m_EditorClassIdentifier: + _entity: {fileID: 313037212318601125} --- !u!1 &1776168629768863343 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Prefabs/baseArcher.prefab b/Assets/Prefabs/baseArcher.prefab index e69e1fd..f2f1688 100644 --- a/Assets/Prefabs/baseArcher.prefab +++ b/Assets/Prefabs/baseArcher.prefab @@ -527,7 +527,6 @@ MonoBehaviour: _speed: 0 _attack_damage: 10 _attack_speed: 2 - _arrow: {fileID: 6962989255644195630, guid: 869a03bba705e8d4485aa73daad773dc, type: 3} --- !u!1 &6962989255710846697 GameObject: m_ObjectHideFlags: 0 @@ -686,6 +685,7 @@ GameObject: m_Component: - component: {fileID: 6962989255883535387} - component: {fileID: 6962989255883535384} + - component: {fileID: 8020346774049635274} m_Layer: 0 m_Name: root m_TagString: Untagged @@ -735,6 +735,20 @@ Animator: m_AllowConstantClipSamplingOptimization: 1 m_KeepAnimatorStateOnDisable: 0 m_WriteDefaultValuesOnDisable: 0 +--- !u!114 &8020346774049635274 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6962989255883535386} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c0fbd934c179894458914437255781c4, type: 3} + m_Name: + m_EditorClassIdentifier: + _entity: {fileID: 5998513223176943995} + _projectile: {fileID: 6962989255644195630, guid: 869a03bba705e8d4485aa73daad773dc, type: 3} --- !u!1 &6962989255885988245 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Prefabs/baseStick.prefab b/Assets/Prefabs/baseStick.prefab index 209a766..b1bfea4 100644 --- a/Assets/Prefabs/baseStick.prefab +++ b/Assets/Prefabs/baseStick.prefab @@ -685,6 +685,7 @@ GameObject: m_Component: - component: {fileID: 6962989255883535387} - component: {fileID: 6962989255883535384} + - component: {fileID: 5665233094155356672} m_Layer: 0 m_Name: root m_TagString: Untagged @@ -734,6 +735,19 @@ Animator: m_AllowConstantClipSamplingOptimization: 1 m_KeepAnimatorStateOnDisable: 0 m_WriteDefaultValuesOnDisable: 0 +--- !u!114 &5665233094155356672 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6962989255883535386} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c0fbd934c179894458914437255781c4, type: 3} + m_Name: + m_EditorClassIdentifier: + _entity: {fileID: 8585520847943034727} --- !u!1 &6962989255885988245 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Ally/Ally.cs b/Assets/Scripts/Ally/Ally.cs index 212691d..b5e2438 100644 --- a/Assets/Scripts/Ally/Ally.cs +++ b/Assets/Scripts/Ally/Ally.cs @@ -24,13 +24,7 @@ public class Ally : Entity //Attack Cooldown if(AttackSpeed < AttackSpeedWait) { - Enemy.Hit(AttackDamage); - - //Kill if no hp - if(Enemy.Hp <= 0) { - IsEnemyDetected = false; - Enemy.Death(); - } + Animation.PlayAttackAnim(); AttackSpeedWait = 0f; } diff --git a/Assets/Scripts/Ally/Archer.cs b/Assets/Scripts/Ally/Archer.cs index d60b2e9..9c82bc1 100644 --- a/Assets/Scripts/Ally/Archer.cs +++ b/Assets/Scripts/Ally/Archer.cs @@ -5,17 +5,9 @@ using UnityEngine; public class Archer : Ally { - [SerializeField] - private GameObject _arrow; - - private Rigidbody2D _rigidbodyAlly; - private Rigidbody2D _rigidbodyOpponent; - public override void Start() { base.Start(); - - _rigidbodyAlly = GetComponent(); Animation = gameObject.AddComponent(); } @@ -23,7 +15,6 @@ public class Archer : Ally { if(IsEnemyDetected) { - _rigidbodyOpponent = Enemy.GetComponent(); AttackEnemy(); } @@ -37,14 +28,7 @@ public class Archer : Ally Animation.PlayAttackAnim(); - GameObject _newArrow = Instantiate(_arrow, _rigidbodyAlly.position, Quaternion.identity); - - //Warning : the Speed of the arrow is equal to the speed of this unit, if this unit need to move, use an other variable ! - _newArrow.GetComponent().Damage = AttackDamage; - _newArrow.GetComponent().EnemySpeed = Enemy.Speed; - _newArrow.GetComponent().VectorStart = _rigidbodyAlly.position; - _newArrow.GetComponent().VectorEnd = _rigidbodyOpponent.position; - _newArrow.GetComponent().Target = Enemy; + AttackSpeedWait = 0f; diff --git a/Assets/Scripts/Opponent/Opponent.cs b/Assets/Scripts/Opponent/Opponent.cs index 0f23f7b..19e8221 100644 --- a/Assets/Scripts/Opponent/Opponent.cs +++ b/Assets/Scripts/Opponent/Opponent.cs @@ -36,13 +36,6 @@ public class Opponent : Entity { Animation.PlayAttackAnim(); - Enemy.Hit(AttackDamage); - - //Kill if no hp - if(Enemy.Hp <= 0) { - Enemy.Death(); - } - AttackSpeedWait = 0f; } diff --git a/Assets/Scripts/Root.cs b/Assets/Scripts/Root.cs new file mode 100644 index 0000000..6dacc7a --- /dev/null +++ b/Assets/Scripts/Root.cs @@ -0,0 +1,37 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Root : MonoBehaviour +{ + + [SerializeField] + private Entity _entity; + [SerializeField] + private GameObject _projectile; + + void Attack() { + _entity.Enemy.Hit( _entity.AttackDamage); + if(_entity.Enemy.Hp <= 0) { + _entity.Enemy.Death(); + _entity.IsEnemyDetected = false; + } + } + + void ShotProjectile() { + Rigidbody2D _rigidbodyAlly; + Rigidbody2D _rigidbodyOpponent; + _rigidbodyAlly = _entity.GetComponent(); + _rigidbodyOpponent = _entity.Enemy.GetComponent(); + + GameObject _newArrow = Instantiate(_projectile, _rigidbodyAlly.position, Quaternion.identity); + + //Warning : the Speed of the arrow is equal to the speed of this unit, if this unit need to move, use an other variable ! + _newArrow.GetComponent().Damage = _entity.AttackDamage; + _newArrow.GetComponent().EnemySpeed = _entity.Enemy.Speed; + _newArrow.GetComponent().VectorStart = _rigidbodyAlly.position; + _newArrow.GetComponent().VectorEnd = _rigidbodyOpponent.position; + _newArrow.GetComponent().Target = _entity.Enemy; + + } +} diff --git a/Assets/Scripts/Root.cs.meta b/Assets/Scripts/Root.cs.meta new file mode 100644 index 0000000..c14f8ac --- /dev/null +++ b/Assets/Scripts/Root.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c0fbd934c179894458914437255781c4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: