JumpAbility.cs (3243B)
1 using System; 2 using Slimecing.Abilities.AbilityEffects; 3 using Slimecing.Characters; 4 using Slimecing.Triggers; 5 using UnityEngine; 6 7 namespace Slimecing.Abilities.UseAbilities 8 { 9 [CreateAssetMenu(fileName = "JumpAbility", menuName = "Abilities/JumpAbility")] 10 public class JumpAbility : Ability 11 { 12 [SerializeField] private float jumpForceInitial; 13 [SerializeField] private float jumpForceDouble; 14 [SerializeField] private float jumpForceMultiplier; 15 [SerializeField] private float simulatedJumpUpMass; 16 [SerializeField] private float simulatedJumpFallMass; 17 [SerializeField] private int jumpUseAmount; 18 19 protected override void PutOnCooldown(AbilityUser aUser) 20 { 21 aUser.AddAbilityOnCooldown(new CooldownData(this, abilityCooldown, jumpUseAmount)); 22 } 23 public override void Use(AbilityUser aUser) 24 { 25 var characterMovementController = aUser.gameObject.GetComponent<CharacterMovementController>(); 26 if (characterMovementController == null) return; 27 28 var startJumpEffect = new JumpAbilityEffect(this, aUser, characterMovementController, 29 jumpForceInitial, jumpForceDouble, jumpForceMultiplier, simulatedJumpUpMass, 30 simulatedJumpFallMass, jumpUseAmount); 31 32 if (characterMovementController.IsGrounded()) 33 { 34 startJumpEffect.canJump = true; 35 aUser.AddAbilityEffect(startJumpEffect); 36 aUser.Audio.PlayOneShot(abilitySound); 37 } 38 else 39 { 40 foreach (var effect in aUser.currentAbilityEffects) 41 { 42 if (!(effect is JumpAbilityEffect jumpAbilityEffect)) continue; 43 if (jumpAbilityEffect.BeginDouble()) 44 { 45 aUser.Audio.PlayOneShot(abilitySound); 46 } 47 return; 48 } 49 } 50 } 51 52 public override void CheckActivation(AbilityUser aUser, TriggerState state) 53 { 54 if (runtimeAbilityTrigger == null) return; 55 switch (state) 56 { 57 case TriggerState.Started: 58 StartAbility(aUser); 59 break; 60 case TriggerState.Performed: 61 SetCanJump(aUser, false); 62 break; 63 case TriggerState.Canceled: 64 SetCanJump(aUser, false); 65 break; 66 default: 67 throw new ArgumentOutOfRangeException(); 68 } 69 } 70 71 private static void SetCanJump(AbilityUser aUser, bool can) 72 { 73 foreach (var effect in aUser.currentAbilityEffects) 74 { 75 switch (effect) 76 { 77 case JumpAbilityEffect jumpAbilityEffect: 78 jumpAbilityEffect.canJump = can; 79 return; 80 case SimpleJumpAbilityEffect simpleJumpAbilityEffect: 81 simpleJumpAbilityEffect.canJump = can; 82 return; 83 } 84 } 85 } 86 } 87 }