From ec9f635c6b6d9c63599dc184038e0871ccc93e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20H=C3=A9liot?= Date: Wed, 26 Apr 2023 09:03:15 +0200 Subject: [PATCH] Add Attack Movement --- .../Combat/Base/Sword/BaseSword1.anim | 28 +++++++++++++ .../Combat/Base/Sword/BaseSword2.anim | 28 +++++++++++++ .../Combat/Base/Sword/BaseSword3.anim | 28 +++++++++++++ PlatformerController/Assets/Data/Weapons.meta | 8 ++++ .../Assets/Data/Weapons/SwordData.asset | 18 ++++++++ .../Assets/Data/Weapons/SwordData.asset.meta | 8 ++++ .../Assets/Scenes/MainScene.unity | 1 + .../Intermediaries/WeaponAnimationToWeapon.cs | 20 +++++++++ .../States/Sub States/PlayerAttackState.cs | 41 ++++++++++++++++++- .../Assets/Scripts/ScriptableObjects.meta | 8 ++++ .../Scripts/ScriptableObjects/Weapons.meta | 8 ++++ .../ScriptableObjects/Weapons/WeaponData.cs | 7 ++++ .../Weapons/WeaponData.cs.meta | 11 +++++ .../Assets/Scripts/Weapons/Weapon.cs | 34 +++++++++++++++ 14 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 PlatformerController/Assets/Data/Weapons.meta create mode 100644 PlatformerController/Assets/Data/Weapons/SwordData.asset create mode 100644 PlatformerController/Assets/Data/Weapons/SwordData.asset.meta create mode 100644 PlatformerController/Assets/Scripts/ScriptableObjects.meta create mode 100644 PlatformerController/Assets/Scripts/ScriptableObjects/Weapons.meta create mode 100644 PlatformerController/Assets/Scripts/ScriptableObjects/Weapons/WeaponData.cs create mode 100644 PlatformerController/Assets/Scripts/ScriptableObjects/Weapons/WeaponData.cs.meta diff --git a/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword1.anim b/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword1.anim index e1f6ef5..c341c65 100644 --- a/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword1.anim +++ b/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword1.anim @@ -82,6 +82,34 @@ AnimationClip: m_HasGenericRootTransform: 0 m_HasMotionFloatCurves: 0 m_Events: + - time: 0.06666667 + functionName: AnimationStartMovementTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 + - time: 0.06666667 + functionName: AnimationTurnOffFlipTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 + - time: 0.2 + functionName: AnimationStopMovementTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 + - time: 0.26666668 + functionName: AnimationTrunOnFlipTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 - time: 0.33333334 functionName: AnimationFinishTrigger data: diff --git a/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword2.anim b/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword2.anim index ce0e397..c748fac 100644 --- a/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword2.anim +++ b/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword2.anim @@ -82,6 +82,34 @@ AnimationClip: m_HasGenericRootTransform: 0 m_HasMotionFloatCurves: 0 m_Events: + - time: 0.06666667 + functionName: AnimationStartMovementTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 + - time: 0.06666667 + functionName: AnimationTurnOffFlipTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 + - time: 0.2 + functionName: AnimationStopMovementTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 + - time: 0.26666668 + functionName: AnimationTrunOnFlipTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 - time: 0.33333334 functionName: AnimationFinishTrigger data: diff --git a/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword3.anim b/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword3.anim index 1cdb25d..07898a3 100644 --- a/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword3.anim +++ b/PlatformerController/Assets/Animation/Animations/Combat/Base/Sword/BaseSword3.anim @@ -82,6 +82,34 @@ AnimationClip: m_HasGenericRootTransform: 0 m_HasMotionFloatCurves: 0 m_Events: + - time: 0.06666667 + functionName: AnimationStartMovementTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 + - time: 0.06666667 + functionName: AnimationTurnOffFlipTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 + - time: 0.2 + functionName: AnimationStopMovementTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 + - time: 0.26666668 + functionName: AnimationTrunOnFlipTrigger + data: + objectReferenceParameter: {fileID: 0} + floatParameter: 0 + intParameter: 0 + messageOptions: 0 - time: 0.33333334 functionName: AnimationFinishTrigger data: diff --git a/PlatformerController/Assets/Data/Weapons.meta b/PlatformerController/Assets/Data/Weapons.meta new file mode 100644 index 0000000..c3e2ce4 --- /dev/null +++ b/PlatformerController/Assets/Data/Weapons.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 61996f67d40a44c4f9365cdf92c2ff8b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/PlatformerController/Assets/Data/Weapons/SwordData.asset b/PlatformerController/Assets/Data/Weapons/SwordData.asset new file mode 100644 index 0000000..f0e18f3 --- /dev/null +++ b/PlatformerController/Assets/Data/Weapons/SwordData.asset @@ -0,0 +1,18 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 04aafc5ec35034941bb80fe99b1f925a, type: 3} + m_Name: SwordData + m_EditorClassIdentifier: + movementSpeed: + - 3 + - 4 + - 2 diff --git a/PlatformerController/Assets/Data/Weapons/SwordData.asset.meta b/PlatformerController/Assets/Data/Weapons/SwordData.asset.meta new file mode 100644 index 0000000..3440c16 --- /dev/null +++ b/PlatformerController/Assets/Data/Weapons/SwordData.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 348c44c3fa52c50439d4a4275d846945 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/PlatformerController/Assets/Scenes/MainScene.unity b/PlatformerController/Assets/Scenes/MainScene.unity index 77d8808..39f5c96 100644 --- a/PlatformerController/Assets/Scenes/MainScene.unity +++ b/PlatformerController/Assets/Scenes/MainScene.unity @@ -767,6 +767,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: f76bcdf370a40c346b14e7917c47dea9, type: 3} m_Name: m_EditorClassIdentifier: + weaponData: {fileID: 11400000, guid: 348c44c3fa52c50439d4a4275d846945, type: 2} --- !u!1 &1258763309 GameObject: m_ObjectHideFlags: 0 diff --git a/PlatformerController/Assets/Scripts/Intermediaries/WeaponAnimationToWeapon.cs b/PlatformerController/Assets/Scripts/Intermediaries/WeaponAnimationToWeapon.cs index b54c9d2..fd8db68 100644 --- a/PlatformerController/Assets/Scripts/Intermediaries/WeaponAnimationToWeapon.cs +++ b/PlatformerController/Assets/Scripts/Intermediaries/WeaponAnimationToWeapon.cs @@ -13,4 +13,24 @@ private void AnimationFinishTrigger() { _weapon.AnimationFinishTrigger(); } + + private void AnimationStartMovementTrigger() + { + _weapon.AnimationStartMovementTrigger(); + } + + private void AnimationStopMovementTrigger() + { + _weapon.AnimationStopMovementTrigger(); + } + + private void AnimationTurnOffFlipTrigger() + { + _weapon.AnimationTurnOffFlipTrigger(); + } + + private void AnimationTrunOnFlipTrigger() + { + _weapon.AnimationTurnOnFlipTrigger(); + } } diff --git a/PlatformerController/Assets/Scripts/Player/States/Sub States/PlayerAttackState.cs b/PlatformerController/Assets/Scripts/Player/States/Sub States/PlayerAttackState.cs index 3d7f816..7eac484 100644 --- a/PlatformerController/Assets/Scripts/Player/States/Sub States/PlayerAttackState.cs +++ b/PlatformerController/Assets/Scripts/Player/States/Sub States/PlayerAttackState.cs @@ -1,7 +1,14 @@ public class PlayerAttackState : PlayerAbilityState { - private Weapon _weapon; + private bool _setVelocity; + private bool _shouldCheckFlip; + + private int _xInput; + private float _velocityToSet; + + private Weapon _weapon; + public PlayerAttackState(Player player, PlayerStateMachine stateMachine, PlayerData playerData, string animationBoolName) : base(player, stateMachine, playerData, animationBoolName) {} @@ -9,6 +16,8 @@ public PlayerAttackState(Player player, PlayerStateMachine stateMachine, PlayerD public override void Enter() { base.Enter(); + + _setVelocity = false; _weapon.EnterWeapon(); } @@ -20,12 +29,42 @@ public override void Exit() _weapon.ExitWeapon(); } + public override void LogicUpdate() + { + base.LogicUpdate(); + + _xInput = _player.InputHandler.NormalizedInputX; + + if (_shouldCheckFlip) + { + _player.CheckIfShouldFlip(_xInput); + } + + if (_setVelocity) + { + _player.SetVelocityX(_velocityToSet * _player.FacingDirection); + } + } + public void SetWeapon(Weapon weapon) { _weapon = weapon; _weapon.InitializeWeapon(this); } + public void SetPlayerVelocity(float velocity) + { + _player.SetVelocityX(velocity * _player.FacingDirection); + + _velocityToSet = velocity; + _setVelocity = true; + } + + public void SetFlipCheck(bool value) + { + _shouldCheckFlip = value; + } + #region Animation Triggers public override void AnimationFinishTrigger() diff --git a/PlatformerController/Assets/Scripts/ScriptableObjects.meta b/PlatformerController/Assets/Scripts/ScriptableObjects.meta new file mode 100644 index 0000000..219644d --- /dev/null +++ b/PlatformerController/Assets/Scripts/ScriptableObjects.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: da1ddf312fdafa94fbab105df0a6da74 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/PlatformerController/Assets/Scripts/ScriptableObjects/Weapons.meta b/PlatformerController/Assets/Scripts/ScriptableObjects/Weapons.meta new file mode 100644 index 0000000..ece3e4a --- /dev/null +++ b/PlatformerController/Assets/Scripts/ScriptableObjects/Weapons.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 21dfbf72ad7d233438e97117ad717a8b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/PlatformerController/Assets/Scripts/ScriptableObjects/Weapons/WeaponData.cs b/PlatformerController/Assets/Scripts/ScriptableObjects/Weapons/WeaponData.cs new file mode 100644 index 0000000..72f5e19 --- /dev/null +++ b/PlatformerController/Assets/Scripts/ScriptableObjects/Weapons/WeaponData.cs @@ -0,0 +1,7 @@ +using UnityEngine; + +[CreateAssetMenu(fileName = "newWeaponData", menuName = "Data/Weapon Data/Weapon")] +public class WeaponData : ScriptableObject +{ + public float[] movementSpeed; +} diff --git a/PlatformerController/Assets/Scripts/ScriptableObjects/Weapons/WeaponData.cs.meta b/PlatformerController/Assets/Scripts/ScriptableObjects/Weapons/WeaponData.cs.meta new file mode 100644 index 0000000..15d72d8 --- /dev/null +++ b/PlatformerController/Assets/Scripts/ScriptableObjects/Weapons/WeaponData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 04aafc5ec35034941bb80fe99b1f925a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/PlatformerController/Assets/Scripts/Weapons/Weapon.cs b/PlatformerController/Assets/Scripts/Weapons/Weapon.cs index 3df3071..05d1729 100644 --- a/PlatformerController/Assets/Scripts/Weapons/Weapon.cs +++ b/PlatformerController/Assets/Scripts/Weapons/Weapon.cs @@ -2,6 +2,10 @@ public class Weapon : MonoBehaviour { + [SerializeField] private WeaponData weaponData; + + protected int _attackCounter; + protected Animator _baseAnimator; protected Animator _weaponAnimator; @@ -23,15 +27,25 @@ public void InitializeWeapon(PlayerAttackState state) public virtual void EnterWeapon() { gameObject.SetActive(true); + + if (_attackCounter >= weaponData.movementSpeed.Length) + { + _attackCounter = 0; + } _baseAnimator.SetBool("attack", true); _weaponAnimator.SetBool("attack", true); + + _baseAnimator.SetInteger("attackCounter", _attackCounter); + _weaponAnimator.SetInteger("attackCounter", _attackCounter); } public virtual void ExitWeapon() { _baseAnimator.SetBool("attack", false); _weaponAnimator.SetBool("attack", false); + + _attackCounter++; gameObject.SetActive(false); } @@ -43,5 +57,25 @@ public virtual void AnimationFinishTrigger() _state.AnimationFinishTrigger(); } + public virtual void AnimationStartMovementTrigger() + { + _state.SetPlayerVelocity(weaponData.movementSpeed[_attackCounter]); + } + + public virtual void AnimationStopMovementTrigger() + { + _state.SetPlayerVelocity(0); + } + + public virtual void AnimationTurnOffFlipTrigger() + { + _state.SetFlipCheck(false); + } + + public virtual void AnimationTurnOnFlipTrigger() + { + _state.SetFlipCheck(true); + } + #endregion }