Advertisement
Guest User

PlayerController.cs

a guest
Jan 19th, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.78 KB | None | 0 0
  1. using Behavior;
  2. using Duality;
  3. using Duality.Components.Renderers;
  4. using Duality.Input;
  5. using Duality.Resources;
  6. using Manager;
  7. using Misc;
  8.  
  9. namespace Player
  10. {
  11.     public enum FacingDirection
  12.     {
  13.         Left,
  14.         Right
  15.     }
  16.  
  17.     public enum PlayerAnimationState
  18.     {
  19.         Idle,
  20.         Running,
  21.         Jumping,
  22.         Falling,
  23.         Other
  24.     }
  25.  
  26.     /// <summary>
  27.     /// Utilizes Controller2D to perform calculations and take input from the player for movement.
  28.     /// </summary>
  29.     [RequiredComponent(typeof(Controller2D))]
  30.     public class PlayerController : Component, ICmpUpdatable, ICmpInitializable
  31.     {
  32.         public PlayerAnimationState PlayerAnimationState { get; private set; }
  33.  
  34.         public FacingDirection FacingDirection { get; private set; }
  35.  
  36.         public float FiringDelay { get; set; } = 10f;
  37.         public Vector3 BulletSpawnOffset { get; set; } = new Vector3(4, 0.1f, -0.1f);
  38.  
  39.         private float firingDelayCounter;
  40.         private float gravity;
  41.         private HeldWeapon heldWeapon;
  42.         private float jumpVelocity;
  43.         private SpriteRenderer spriteRenderer;
  44.         public Vector2 velocity;
  45.         private float MoveSpeed { get; } = 72;
  46.         private float JumpHeight { get; } = 45;
  47.         private float TimeToJumpApex { get; } = .45f;
  48.         private float AccelerationGrounded { get; } = 100;
  49.         private float AccelerationAirborne { get; set; } = 100;
  50.  
  51.         private Controller2D Controller
  52.         {
  53.             get { return GameObj.GetComponent<Controller2D>(); }
  54.         }
  55.  
  56.         private AnimationManager animationManager;
  57.  
  58.         public void OnInit(InitContext context)
  59.         {
  60.             if (context == InitContext.Activate)
  61.             {
  62.                 spriteRenderer = GameObj.GetComponent<SpriteRenderer>();
  63.                 heldWeapon = GameObj.ParentScene.FindGameObject<HeldWeapon>().GetComponent<HeldWeapon>();
  64.                 animationManager = GameObj.GetComponent<AnimationManager>();
  65.                 gravity = 2 * JumpHeight / TimeToJumpApex / TimeToJumpApex;
  66.                 jumpVelocity = MathF.Abs(gravity) * TimeToJumpApex;
  67.                 PlayerAnimationState = PlayerAnimationState.Idle;
  68.             }
  69.         }
  70.  
  71.         public void OnShutdown(ShutdownContext context)
  72.         {
  73.         }
  74.  
  75.         public void OnUpdate()
  76.         {
  77.             firingDelayCounter += Time.MsPFMult * Time.TimeMult;
  78.             if (Controller.Collisions.below || Controller.Collisions.above)
  79.                 velocity.Y = 0;
  80.  
  81.             if (DualityApp.Keyboard.KeyHit(Key.R))
  82.             {
  83.                 animationManager.PlayAnimationOnce("fall", "idle");
  84.                 PlayerAnimationState = PlayerAnimationState.Other;
  85.                 Log.Game.Write("PlayAnimationOnce was triggered!");
  86.             }
  87.                
  88.  
  89.             var _input = Vector2.Zero;
  90.             if (DualityApp.Keyboard[Key.Left])
  91.             {
  92.                 _input.X = -1;
  93.                 FacingDirection = FacingDirection.Left;
  94.                 PlayerAnimationState = PlayerAnimationState.Running;
  95.             }
  96.             else if (DualityApp.Keyboard[Key.Right])
  97.             {
  98.                 _input.X = 1;
  99.                 FacingDirection = FacingDirection.Right;
  100.                 PlayerAnimationState = PlayerAnimationState.Running;
  101.             }
  102.             else
  103.             {
  104.                 //No input, so player must not be moving.
  105.                 PlayerAnimationState = PlayerAnimationState.Idle;
  106.             }
  107.  
  108.             if (DualityApp.Keyboard[Key.Up])
  109.                 _input.Y = -1;
  110.             else if (DualityApp.Keyboard[Key.Down])
  111.                 _input.Y = 1;
  112.  
  113.             if (DualityApp.Keyboard.KeyHit(Key.Z) && Controller.Collisions.below)
  114.                 velocity.Y = -jumpVelocity;
  115.  
  116.             if (velocity.Y < 0) //Velocity means player is gaining height
  117.                 PlayerAnimationState = PlayerAnimationState.Jumping;
  118.             else if (velocity.Y > 0) //Velocity means player is losing height
  119.                 PlayerAnimationState = PlayerAnimationState.Falling;
  120.  
  121.             var _targetVelocityX = _input.X * MoveSpeed;
  122.             velocity.X = MathF.Lerp(velocity.X, _targetVelocityX, AccelerationGrounded / 100);
  123.             velocity.Y += gravity * Time.TimeMult / 60f;
  124.             Controller.Move(velocity * Time.TimeMult / 60f);
  125.             UpdateSprite();
  126.             UpdateAnimation();
  127.         }
  128.  
  129.         /// <summary>
  130.         /// Flips sprite based on arrow key input.
  131.         /// </summary>
  132.         private void UpdateSprite()
  133.         {
  134.             switch (FacingDirection)
  135.             {
  136.                 case FacingDirection.Left:
  137.                     spriteRenderer.Flip = SpriteRenderer.FlipMode.Horizontal;
  138.                     break;
  139.                 case FacingDirection.Right:
  140.                     spriteRenderer.Flip = SpriteRenderer.FlipMode.None;
  141.                     break;
  142.             }
  143.         }
  144.  
  145.         /// <summary>
  146.         /// Changes player's animation based on state machine.
  147.         /// </summary>
  148.         private void UpdateAnimation()
  149.         {
  150.             switch (PlayerAnimationState)
  151.             {
  152.                 case PlayerAnimationState.Idle:
  153.                     animationManager.PlayAnimationContinuously("idle");
  154.                     break;
  155.  
  156.                 case PlayerAnimationState.Running:
  157.                     animationManager.PlayAnimationContinuously("run");
  158.                     break;
  159.  
  160.                 case PlayerAnimationState.Jumping:
  161.                     animationManager.PlayAnimationContinuously("jump");
  162.                     break;
  163.  
  164.                 case PlayerAnimationState.Falling:
  165.                     animationManager.PlayAnimationContinuously("fall");
  166.                     break;
  167.             }
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement