slimecing

a fighting game featuring slimes and swords
Log | Files | Refs | README

SimpleJumpAbilityEffect.cs (2339B)


      1 using Slimecing.Abilities.UseAbilities;
      2 using Slimecing.Characters;
      3 using UnityEngine;
      4 
      5 namespace Slimecing.Abilities.AbilityEffects
      6 {
      7     public class SimpleJumpAbilityEffect : AbilityEffect
      8     {
      9         private readonly JumpAbility _ability;
     10         private readonly AbilityUser _aUser;
     11         private readonly CharacterMovementController _controller;
     12         private readonly float _jumpForce;
     13         private readonly float _simulatedJumpUpMass;
     14         private readonly float _simulatedFallingMass;
     15 
     16         private Vector3 _jumpDir;
     17         private float _lastJumpMass;
     18         private bool _fallMassSet;
     19        
     20         public bool canJump { get; set; }
     21 
     22         public SimpleJumpAbilityEffect(Ability ability, AbilityUser aUser, CharacterMovementController controller, 
     23             float jumpForce, float simulatedJumpUpMass, float simulatedFallingUpMass) : base(ability, aUser)
     24         {
     25             _ability = (JumpAbility)ability;
     26             _aUser = aUser;
     27             _controller = controller;
     28             _jumpForce = jumpForce;
     29             _simulatedJumpUpMass = simulatedJumpUpMass;
     30             _simulatedFallingMass = simulatedFallingUpMass;
     31         }
     32 
     33         public override void Activate()
     34         {
     35             _jumpDir = -_controller.GravityDirection;
     36             _lastJumpMass = _controller.SimulatedFallingMass;
     37             _controller.SimulatedFallingMass = _simulatedJumpUpMass;
     38             _controller.AddForceTo(-_jumpDir, _controller.playerRigidbody.velocity.y);
     39             BeginJump();
     40         }
     41 
     42         public override void DoUpdate()
     43         {
     44             if (canJump)
     45             {
     46                 Debug.Log("ayy");
     47                 return;
     48             }
     49             if (!_fallMassSet)
     50             {
     51                 _controller.SimulatedFallingMass = _simulatedFallingMass;
     52                 _fallMassSet = true;
     53             }
     54 
     55             if (_controller.IsGrounded())
     56             {
     57                 End();
     58             }
     59         }
     60 
     61         private void BeginJump()
     62         {
     63             _controller.AddForceTo(_jumpDir, _jumpForce);
     64         }
     65 
     66         public override bool DoesUpdate() => true;
     67 
     68         public override void End()
     69         {
     70             _fallMassSet = false;
     71             _controller.SimulatedFallingMass = _lastJumpMass;
     72             _aUser.RemoveAbilityEffect(this);
     73         }
     74     }
     75 }