Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class PlayerControls : MonoBehaviour
  7. {
  8.  
  9.     public bool grounded;
  10.     public bool running;
  11.     public bool speedCap;
  12.     GameObject ground;
  13.     Rigidbody2D rb;
  14.     SpriteRenderer sprite;
  15.     Animator anim;
  16.     List<Vector2> gravityVectors;
  17.     Vector2 gravityMagnitude;
  18.     Vector2 currentGravityDirectionVector;
  19.     Vector2 velocityCapVector; //defines how much speed can the player get from gravity
  20.     float velocityDeathMagnitude;
  21.     public float initialForceMagnitude;
  22.     Vector3 currentControlsLeft;
  23.     Vector3 currentControlsRight;
  24.     Vector3 colliderRebaseVector;
  25.     Vector2 lastVelocityBeforeGround;
  26.     public float speed;
  27.     float velocityCapSqrMagnitude;
  28.     float airVelocityCapTime;
  29.     float currentAirVelocityCapTime;
  30.     float finishedAirVelocityCapTime;
  31.     float damageFromGravity;
  32.     float playerHitpoints;
  33.  
  34.  
  35.     enum Direction
  36.     {
  37.         up,
  38.         down,
  39.         left,
  40.         right
  41.     }
  42.     Direction currentGravityDirection;
  43.     // Start is called before the first frame update
  44.     void Start()
  45.     {
  46.         speed = 6.0f;
  47.         grounded = false;
  48.         speedCap = false;
  49.         running = false;
  50.         gravityMagnitude = new Vector2(9.8f, 9.8f);
  51.         velocityCapSqrMagnitude = 81.0f;
  52.         velocityCapVector = new Vector2(13.0f, 13.0f);
  53.         velocityDeathMagnitude = 11.0f;
  54.         airVelocityCapTime = 3.0f;
  55.         initialForceMagnitude = 6.0f;
  56.         playerHitpoints = 100.0f;
  57.         rb = GetComponent<Rigidbody2D>();
  58.         sprite = GameObject.Find("Player_Sprite").GetComponent<SpriteRenderer>();
  59.         anim = GameObject.Find("Player_Sprite").GetComponent<Animator>();
  60.  
  61.         gravityVectors = new List<Vector2>();
  62.         gravityVectors.Add(Vector2.down);
  63.         gravityVectors.Add(Vector2.up);
  64.         gravityVectors.Add(Vector2.right);
  65.         gravityVectors.Add(Vector2.left);
  66.  
  67.         currentGravityDirectionVector = gravityMagnitude * gravityVectors[0];
  68.         currentControlsLeft = -Vector3.right;
  69.         currentControlsRight = Vector3.right;
  70.        
  71.     }
  72.  
  73.     // Update is called once per frame
  74.     void Update()
  75.     {
  76.  
  77.         PlayerInput();
  78.  
  79.     }
  80.  
  81.     private void FixedUpdate()
  82.     {
  83.         if (grounded)
  84.         {
  85.             currentAirVelocityCapTime = 0.0f;
  86.         }
  87.         else
  88.         {
  89.             if (rb.velocity.sqrMagnitude > velocityCapSqrMagnitude)
  90.             {
  91.                 if (!speedCap)
  92.                 {
  93.                     speedCap = true;
  94.                     currentAirVelocityCapTime = Time.time;
  95.                 }
  96.                 rb.velocity = rb.velocity.normalized * velocityCapVector;
  97.             } else
  98.             {
  99.                 speedCap = false;
  100.             }
  101.             lastVelocityBeforeGround = rb.velocity;
  102.         }
  103.  
  104.         if (currentGravityDirection == Direction.down || currentGravityDirection == Direction.up)
  105.         {
  106.             if (Input.GetKey(KeyCode.D))
  107.             {
  108.                 transform.position += currentControlsRight * Time.deltaTime * speed;
  109.                 sprite.flipX = true && GetGravityDirection() != Direction.up;
  110.             }
  111.             if (Input.GetKey(KeyCode.A))
  112.             {
  113.                 transform.position += currentControlsLeft * Time.deltaTime * speed;
  114.                 sprite.flipX = GetGravityDirection() == Direction.up;
  115.             }
  116.         }
  117.         else if (currentGravityDirection == Direction.right)
  118.         {
  119.             if (Input.GetKey(KeyCode.W))
  120.             {
  121.                 //rb.AddForce(new Vector2(20.0f, 20.0f));
  122.                 transform.position += currentControlsRight * Time.deltaTime * speed;
  123.                 sprite.flipX = true && GetGravityDirection() != Direction.up;
  124.             }
  125.             if (Input.GetKey(KeyCode.S))
  126.             {
  127.                 transform.position += currentControlsLeft * Time.deltaTime * speed;
  128.                 sprite.flipX = GetGravityDirection() == Direction.up;
  129.             }
  130.         }
  131.         else
  132.         {
  133.             if (Input.GetKey(KeyCode.S))
  134.             {
  135.                 //rb.AddForce(new Vector2(20.0f, 20.0f));
  136.                 transform.position += currentControlsRight * Time.deltaTime * speed;
  137.                 sprite.flipX = true && GetGravityDirection() != Direction.up;
  138.             }
  139.             if (Input.GetKey(KeyCode.W))
  140.             {
  141.                 transform.position += currentControlsLeft * Time.deltaTime * speed;
  142.                 sprite.flipX = GetGravityDirection() == Direction.up;
  143.             }
  144.         }
  145.     }
  146.  
  147.     private bool IsVerticalGravity ()
  148.     {
  149.         this.currentGravityDirection = GetGravityDirection();
  150.         return currentGravityDirection == Direction.down || currentGravityDirection == Direction.up;
  151.     }
  152.  
  153.     private bool IsPressingMovementKeys()
  154.     {
  155.         return Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D);
  156.     }
  157.  
  158.     private bool IsMovingX ()
  159.     {
  160.         return Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A);
  161.     }
  162.  
  163.     private bool IsMovingY()
  164.     {
  165.         return Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S);
  166.     }
  167.  
  168.     private void HandleFlipX()
  169.     {
  170.         if (Input.GetKey(KeyCode.D))
  171.         {
  172.             sprite.flipX = true && GetGravityDirection() != Direction.up;
  173.         }
  174.         else if (Input.GetKey(KeyCode.A))
  175.         {
  176.             sprite.flipX = GetGravityDirection() == Direction.up;
  177.         }
  178.     }
  179.  
  180.     void GetPlayerInput()
  181.     {
  182.  
  183.         if (IsVerticalGravity())
  184.         {
  185.             if (IsMovingX())
  186.             {
  187.                 running = true;
  188.                 HandleFlipX();
  189.             }
  190.             else
  191.             {
  192.                 running = false;
  193.             }
  194.         }
  195.         else
  196.         {
  197.             if (IsMovingY())
  198.             {
  199.                 running = true;
  200.             }
  201.             else
  202.             {
  203.                 running = false;
  204.             }
  205.         }
  206.     }
  207.  
  208.     void PlayerInput()
  209.     {
  210.  
  211.         GetPlayerInput();
  212.        
  213.         anim.SetBool("running", running);
  214.         anim.SetBool("grounded", grounded);
  215.        
  216.  
  217.  
  218.        
  219.         if (Input.GetKeyDown(KeyCode.DownArrow))
  220.         {
  221.             SetGravityDirection(Direction.down);
  222.             SetControlsDirection(Direction.down);
  223.             RotateToGravity(Direction.down);
  224.         }
  225.         if (Input.GetKeyDown(KeyCode.UpArrow))
  226.         {
  227.             SetGravityDirection(Direction.up);
  228.             SetControlsDirection(Direction.up);
  229.             RotateToGravity(Direction.up);
  230.         }
  231.         if (Input.GetKeyDown(KeyCode.RightArrow))
  232.         {
  233.             SetGravityDirection(Direction.right);
  234.             SetControlsDirection(Direction.right);
  235.             RotateToGravity(Direction.right);
  236.         }
  237.         if (Input.GetKeyDown(KeyCode.LeftArrow))
  238.         {
  239.             SetGravityDirection(Direction.left);
  240.             SetControlsDirection(Direction.left);
  241.             RotateToGravity(Direction.left);
  242.         }
  243.         GravityDirection();
  244.     }
  245.  
  246.     void GravityDirection()
  247.     {
  248.         Physics2D.gravity = currentGravityDirectionVector;
  249.     }
  250.  
  251.     Direction GetGravityDirection()
  252.     {
  253.         if (currentGravityDirectionVector.x > 0 || currentGravityDirectionVector.x < 0) //horizontal
  254.         {
  255.             if (currentGravityDirectionVector.x > 0) //right
  256.             {
  257.                 return Direction.right;
  258.             } else //left
  259.             {
  260.                 return Direction.left;
  261.             }
  262.         } else //vertical
  263.         {
  264.             if (currentGravityDirectionVector.y < 0) //down
  265.             {
  266.                 return Direction.down;
  267.             } else //up
  268.             {
  269.                 return Direction.up;
  270.             }
  271.  
  272.         }
  273.     }
  274.  
  275.     void SetGravityDirection(Direction direction)
  276.     {
  277.         //currentGravityDirectionVector -= currentGravityDirectionVector * 10;
  278.         //Debug.Log(currentGravityDirectionVector);
  279.         if (!grounded)
  280.         {
  281.             //rb.velocity = Vector2.zero; << this creates a nice slide bug, can be used in future
  282.         }
  283.         rb.velocity = Vector2.zero;
  284.         switch(direction)
  285.         {
  286.             case Direction.down:
  287.                 currentGravityDirectionVector = gravityVectors[0];
  288.                 break;
  289.             case Direction.up:
  290.                 currentGravityDirectionVector = gravityVectors[1];
  291.                 break;
  292.             case Direction.right:
  293.                 currentGravityDirectionVector = gravityVectors[2];
  294.                 break;
  295.             case Direction.left:
  296.                 currentGravityDirectionVector = gravityVectors[3];
  297.                 break;
  298.             default:
  299.                 break;
  300.         }
  301.     }
  302.  
  303.  
  304.     Vector3 GetDirectionAsVector(Direction direction)
  305.     {
  306.         switch (direction)
  307.         {
  308.             case Direction.down:
  309.                 colliderRebaseVector = -Vector3.up;
  310.                 break;
  311.             case Direction.up:
  312.                 colliderRebaseVector = Vector3.up;
  313.                 break;
  314.             case Direction.right:
  315.                 colliderRebaseVector = Vector3.right;
  316.                 break;
  317.             case Direction.left:
  318.                 colliderRebaseVector = -Vector3.right;
  319.                 break;
  320.             default:
  321.                 break;
  322.         }
  323.  
  324.         return colliderRebaseVector;
  325.     }
  326.  
  327.     void FixColliderRebase(Direction direction)
  328.     {
  329.         if (grounded)
  330.         {
  331.             transform.position += GetDirectionAsVector(direction) * 0.1f;
  332.         }
  333.     }
  334.  
  335.     void DoRotationBoost (Direction direction)
  336.     {
  337.         rb.AddForce(GetDirectionAsVector(direction) * initialForceMagnitude);
  338.     }
  339.  
  340.     void RotateToGravity(Direction direction)
  341.     {
  342.         transform.rotation = Quaternion.LookRotation(Vector3.zero);
  343.         FixColliderRebase(direction);
  344.         //DoRotationBoost(direction);
  345.         switch (direction)
  346.         {
  347.             case Direction.down:
  348.                 //camera.transform.Rotate(Vector3.forward, 0.0f);
  349.                 transform.Rotate(Vector3.forward, 0.0f);
  350.  
  351.                 //transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0.0f, 0.0f, 0.0f), Time.deltaTime * speed);
  352.                 break;
  353.             case Direction.up:
  354.                 //camera.transform.Rotate(Vector3.forward, 180.0f);
  355.                 transform.Rotate(Vector3.forward, 180.0f);
  356.                 //transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0.0f, 0.0f, 180.0f), Time.deltaTime);
  357.                 break;
  358.             case Direction.right:
  359.                 //camera.transform.Rotate(Vector3.forward, 90.0f);
  360.                 transform.Rotate(Vector3.forward, 90.0f);
  361.                 break;
  362.             case Direction.left:
  363.                 //camera.transform.Rotate(Vector3.forward, 270.0f);
  364.                 transform.Rotate(Vector3.forward, 270.0f);
  365.                 break;
  366.             default:
  367.                 break;
  368.         }
  369.     }
  370.  
  371.     void SetControlsDirection(Direction direction)
  372.     {
  373.         switch (direction)
  374.         {
  375.             case Direction.up: case Direction.down:
  376.                 currentControlsLeft = -Vector3.right;
  377.                 currentControlsRight = Vector3.right;
  378.                 break;
  379.             case Direction.right:
  380.                 currentControlsLeft = -Vector3.up;
  381.                 currentControlsRight = Vector3.up;
  382.                 break;
  383.             case Direction.left:
  384.                 currentControlsLeft = Vector3.up;
  385.                 currentControlsRight = -Vector3.up;
  386.                 break;
  387.             default:
  388.                 break;
  389.         }
  390.     }
  391.  
  392.     void ApplyDamageToPlayer(float damage)
  393.     {
  394.         playerHitpoints -= damage;
  395.         if (playerHitpoints < 0.1f )
  396.         {
  397.             Debug.Log("you are dead =(");
  398.             //SceneManager.GetActiveScene(); find a better way to start the scene
  399.             playerHitpoints = 0.0f;
  400.         }
  401.     }
  402.  
  403.     float GetDamageFromGravity ()
  404.     {
  405.         return finishedAirVelocityCapTime * 100 / airVelocityCapTime;
  406.     }
  407.  
  408.     bool IsPlayerDead ()
  409.     {
  410.         return false;
  411.     }
  412.  
  413.     private void OnCollisionEnter2D(Collision2D collision)
  414.     {
  415.         if (currentAirVelocityCapTime > 0)
  416.         {
  417.             finishedAirVelocityCapTime = Time.time - currentAirVelocityCapTime;
  418.             ApplyDamageToPlayer(GetDamageFromGravity());
  419.             //determine if player dead
  420.         }
  421.         currentAirVelocityCapTime = 0;
  422.     }
  423.  
  424.     private void OnCollisionStay2D(Collision2D collision)
  425.     {
  426.         if (collision.gameObject.tag == "Ground")
  427.         {
  428.             if (collision.contacts[0].point.y != 0)
  429.             {
  430.                 grounded = true;
  431.             }
  432.         }
  433.     }
  434.  
  435.     private void OnCollisionExit2D(Collision2D collision)
  436.     {
  437.         if (collision.gameObject.tag == "Ground")
  438.             grounded = false;
  439.     }
  440.    
  441.  
  442.  
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement