Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class PlayerControls : MonoBehaviour
- {
- public bool grounded;
- public bool running;
- public bool speedCap;
- GameObject ground;
- Rigidbody2D rb;
- SpriteRenderer sprite;
- Animator anim;
- List<Vector2> gravityVectors;
- Vector2 gravityMagnitude;
- Vector2 currentGravityDirectionVector;
- Vector2 velocityCapVector; //defines how much speed can the player get from gravity
- float velocityDeathMagnitude;
- public float initialForceMagnitude;
- Vector3 currentControlsLeft;
- Vector3 currentControlsRight;
- Vector3 colliderRebaseVector;
- Vector2 lastVelocityBeforeGround;
- public float speed;
- float velocityCapSqrMagnitude;
- float airVelocityCapTime;
- float currentAirVelocityCapTime;
- float finishedAirVelocityCapTime;
- float damageFromGravity;
- float playerHitpoints;
- enum Direction
- {
- up,
- down,
- left,
- right
- }
- Direction currentGravityDirection;
- // Start is called before the first frame update
- void Start()
- {
- speed = 6.0f;
- grounded = false;
- speedCap = false;
- running = false;
- gravityMagnitude = new Vector2(9.8f, 9.8f);
- velocityCapSqrMagnitude = 81.0f;
- velocityCapVector = new Vector2(13.0f, 13.0f);
- velocityDeathMagnitude = 11.0f;
- airVelocityCapTime = 3.0f;
- initialForceMagnitude = 6.0f;
- playerHitpoints = 100.0f;
- rb = GetComponent<Rigidbody2D>();
- sprite = GameObject.Find("Player_Sprite").GetComponent<SpriteRenderer>();
- anim = GameObject.Find("Player_Sprite").GetComponent<Animator>();
- gravityVectors = new List<Vector2>();
- gravityVectors.Add(Vector2.down);
- gravityVectors.Add(Vector2.up);
- gravityVectors.Add(Vector2.right);
- gravityVectors.Add(Vector2.left);
- currentGravityDirectionVector = gravityMagnitude * gravityVectors[0];
- currentControlsLeft = -Vector3.right;
- currentControlsRight = Vector3.right;
- }
- // Update is called once per frame
- void Update()
- {
- PlayerInput();
- }
- private void FixedUpdate()
- {
- if (grounded)
- {
- currentAirVelocityCapTime = 0.0f;
- }
- else
- {
- if (rb.velocity.sqrMagnitude > velocityCapSqrMagnitude)
- {
- if (!speedCap)
- {
- speedCap = true;
- currentAirVelocityCapTime = Time.time;
- }
- rb.velocity = rb.velocity.normalized * velocityCapVector;
- } else
- {
- speedCap = false;
- }
- lastVelocityBeforeGround = rb.velocity;
- }
- if (currentGravityDirection == Direction.down || currentGravityDirection == Direction.up)
- {
- if (Input.GetKey(KeyCode.D))
- {
- transform.position += currentControlsRight * Time.deltaTime * speed;
- sprite.flipX = true && GetGravityDirection() != Direction.up;
- }
- if (Input.GetKey(KeyCode.A))
- {
- transform.position += currentControlsLeft * Time.deltaTime * speed;
- sprite.flipX = GetGravityDirection() == Direction.up;
- }
- }
- else if (currentGravityDirection == Direction.right)
- {
- if (Input.GetKey(KeyCode.W))
- {
- //rb.AddForce(new Vector2(20.0f, 20.0f));
- transform.position += currentControlsRight * Time.deltaTime * speed;
- sprite.flipX = true && GetGravityDirection() != Direction.up;
- }
- if (Input.GetKey(KeyCode.S))
- {
- transform.position += currentControlsLeft * Time.deltaTime * speed;
- sprite.flipX = GetGravityDirection() == Direction.up;
- }
- }
- else
- {
- if (Input.GetKey(KeyCode.S))
- {
- //rb.AddForce(new Vector2(20.0f, 20.0f));
- transform.position += currentControlsRight * Time.deltaTime * speed;
- sprite.flipX = true && GetGravityDirection() != Direction.up;
- }
- if (Input.GetKey(KeyCode.W))
- {
- transform.position += currentControlsLeft * Time.deltaTime * speed;
- sprite.flipX = GetGravityDirection() == Direction.up;
- }
- }
- }
- private bool IsVerticalGravity ()
- {
- this.currentGravityDirection = GetGravityDirection();
- return currentGravityDirection == Direction.down || currentGravityDirection == Direction.up;
- }
- private bool IsPressingMovementKeys()
- {
- return Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D);
- }
- private bool IsMovingX ()
- {
- return Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A);
- }
- private bool IsMovingY()
- {
- return Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S);
- }
- private void HandleFlipX()
- {
- if (Input.GetKey(KeyCode.D))
- {
- sprite.flipX = true && GetGravityDirection() != Direction.up;
- }
- else if (Input.GetKey(KeyCode.A))
- {
- sprite.flipX = GetGravityDirection() == Direction.up;
- }
- }
- void GetPlayerInput()
- {
- if (IsVerticalGravity())
- {
- if (IsMovingX())
- {
- running = true;
- HandleFlipX();
- }
- else
- {
- running = false;
- }
- }
- else
- {
- if (IsMovingY())
- {
- running = true;
- }
- else
- {
- running = false;
- }
- }
- }
- void PlayerInput()
- {
- GetPlayerInput();
- anim.SetBool("running", running);
- anim.SetBool("grounded", grounded);
- if (Input.GetKeyDown(KeyCode.DownArrow))
- {
- SetGravityDirection(Direction.down);
- SetControlsDirection(Direction.down);
- RotateToGravity(Direction.down);
- }
- if (Input.GetKeyDown(KeyCode.UpArrow))
- {
- SetGravityDirection(Direction.up);
- SetControlsDirection(Direction.up);
- RotateToGravity(Direction.up);
- }
- if (Input.GetKeyDown(KeyCode.RightArrow))
- {
- SetGravityDirection(Direction.right);
- SetControlsDirection(Direction.right);
- RotateToGravity(Direction.right);
- }
- if (Input.GetKeyDown(KeyCode.LeftArrow))
- {
- SetGravityDirection(Direction.left);
- SetControlsDirection(Direction.left);
- RotateToGravity(Direction.left);
- }
- GravityDirection();
- }
- void GravityDirection()
- {
- Physics2D.gravity = currentGravityDirectionVector;
- }
- Direction GetGravityDirection()
- {
- if (currentGravityDirectionVector.x > 0 || currentGravityDirectionVector.x < 0) //horizontal
- {
- if (currentGravityDirectionVector.x > 0) //right
- {
- return Direction.right;
- } else //left
- {
- return Direction.left;
- }
- } else //vertical
- {
- if (currentGravityDirectionVector.y < 0) //down
- {
- return Direction.down;
- } else //up
- {
- return Direction.up;
- }
- }
- }
- void SetGravityDirection(Direction direction)
- {
- //currentGravityDirectionVector -= currentGravityDirectionVector * 10;
- //Debug.Log(currentGravityDirectionVector);
- if (!grounded)
- {
- //rb.velocity = Vector2.zero; << this creates a nice slide bug, can be used in future
- }
- rb.velocity = Vector2.zero;
- switch(direction)
- {
- case Direction.down:
- currentGravityDirectionVector = gravityVectors[0];
- break;
- case Direction.up:
- currentGravityDirectionVector = gravityVectors[1];
- break;
- case Direction.right:
- currentGravityDirectionVector = gravityVectors[2];
- break;
- case Direction.left:
- currentGravityDirectionVector = gravityVectors[3];
- break;
- default:
- break;
- }
- }
- Vector3 GetDirectionAsVector(Direction direction)
- {
- switch (direction)
- {
- case Direction.down:
- colliderRebaseVector = -Vector3.up;
- break;
- case Direction.up:
- colliderRebaseVector = Vector3.up;
- break;
- case Direction.right:
- colliderRebaseVector = Vector3.right;
- break;
- case Direction.left:
- colliderRebaseVector = -Vector3.right;
- break;
- default:
- break;
- }
- return colliderRebaseVector;
- }
- void FixColliderRebase(Direction direction)
- {
- if (grounded)
- {
- transform.position += GetDirectionAsVector(direction) * 0.1f;
- }
- }
- void DoRotationBoost (Direction direction)
- {
- rb.AddForce(GetDirectionAsVector(direction) * initialForceMagnitude);
- }
- void RotateToGravity(Direction direction)
- {
- transform.rotation = Quaternion.LookRotation(Vector3.zero);
- FixColliderRebase(direction);
- //DoRotationBoost(direction);
- switch (direction)
- {
- case Direction.down:
- //camera.transform.Rotate(Vector3.forward, 0.0f);
- transform.Rotate(Vector3.forward, 0.0f);
- //transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0.0f, 0.0f, 0.0f), Time.deltaTime * speed);
- break;
- case Direction.up:
- //camera.transform.Rotate(Vector3.forward, 180.0f);
- transform.Rotate(Vector3.forward, 180.0f);
- //transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0.0f, 0.0f, 180.0f), Time.deltaTime);
- break;
- case Direction.right:
- //camera.transform.Rotate(Vector3.forward, 90.0f);
- transform.Rotate(Vector3.forward, 90.0f);
- break;
- case Direction.left:
- //camera.transform.Rotate(Vector3.forward, 270.0f);
- transform.Rotate(Vector3.forward, 270.0f);
- break;
- default:
- break;
- }
- }
- void SetControlsDirection(Direction direction)
- {
- switch (direction)
- {
- case Direction.up: case Direction.down:
- currentControlsLeft = -Vector3.right;
- currentControlsRight = Vector3.right;
- break;
- case Direction.right:
- currentControlsLeft = -Vector3.up;
- currentControlsRight = Vector3.up;
- break;
- case Direction.left:
- currentControlsLeft = Vector3.up;
- currentControlsRight = -Vector3.up;
- break;
- default:
- break;
- }
- }
- void ApplyDamageToPlayer(float damage)
- {
- playerHitpoints -= damage;
- if (playerHitpoints < 0.1f )
- {
- Debug.Log("you are dead =(");
- //SceneManager.GetActiveScene(); find a better way to start the scene
- playerHitpoints = 0.0f;
- }
- }
- float GetDamageFromGravity ()
- {
- return finishedAirVelocityCapTime * 100 / airVelocityCapTime;
- }
- bool IsPlayerDead ()
- {
- return false;
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (currentAirVelocityCapTime > 0)
- {
- finishedAirVelocityCapTime = Time.time - currentAirVelocityCapTime;
- ApplyDamageToPlayer(GetDamageFromGravity());
- //determine if player dead
- }
- currentAirVelocityCapTime = 0;
- }
- private void OnCollisionStay2D(Collision2D collision)
- {
- if (collision.gameObject.tag == "Ground")
- {
- if (collision.contacts[0].point.y != 0)
- {
- grounded = true;
- }
- }
- }
- private void OnCollisionExit2D(Collision2D collision)
- {
- if (collision.gameObject.tag == "Ground")
- grounded = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement