Furry_02

Player.cs

Aug 8th, 2021
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.49 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Player : MonoBehaviour {
  7.  
  8.     public bool isGrounded;
  9.     public bool isSprinting;
  10.  
  11.     private Transform cam;
  12.     private World world;
  13.  
  14.     public float walkSpeed = 3f;
  15.     public float sprintSpeed = 6f;
  16.     public float jumpForce = 5f;
  17.     public float gravity = -9.8f;
  18.  
  19.     public float playerWidth = 0.15f;
  20.     public float boundsTolerance = 0.1f;
  21.  
  22.     private float horizontal;
  23.     private float vertical;
  24.     private float mouseHorizontal;
  25.     private float mouseVertical;
  26.     private Vector3 velocity;
  27.     private float verticalMomentum = 0;
  28.     private bool jumpRequest;
  29.  
  30.     public Transform highlightBlock;
  31.     public Transform placeBlock;
  32.     public float checkIncrement = 0.1f;
  33.     public float reach = 8f;
  34.  
  35.     public Toolbar toolbar;
  36.  
  37.     private void Start() {
  38.  
  39.         cam = GameObject.Find("Main Camera").transform;
  40.         world = GameObject.Find("World").GetComponent<World>();
  41.  
  42.         world.inUI = false;
  43.  
  44.     }
  45.  
  46.     private void FixedUpdate() {
  47.        
  48.         if (!world.inUI) {
  49.  
  50.             CalculateVelocity();
  51.             if (jumpRequest)
  52.                 Jump();
  53.  
  54.             transform.Translate(velocity, Space.World);
  55.  
  56.         }
  57.  
  58.     }
  59.  
  60.     private void Update() {
  61.  
  62.         if (Input.GetKeyDown(KeyCode.I)) {
  63.  
  64.             world.inUI = !world.inUI;
  65.  
  66.         }
  67.  
  68.         if (!world.inUI) {
  69.             GetPlayerInputs();
  70.             placeCursorBlocks();
  71.         }
  72.  
  73.     }
  74.  
  75.     void Jump () {
  76.  
  77.         verticalMomentum = jumpForce;
  78.         isGrounded = false;
  79.         jumpRequest = false;
  80.  
  81.     }
  82.  
  83.     private void CalculateVelocity () {
  84.  
  85.         // Affect vertical momentum with gravity.
  86.         if (verticalMomentum > gravity)
  87.             verticalMomentum += Time.fixedDeltaTime * gravity;
  88.  
  89.         // if we're sprinting, use the sprint multiplier.
  90.         if (isSprinting)
  91.             velocity = ((transform.forward * vertical) + (transform.right * horizontal)) * Time.fixedDeltaTime * sprintSpeed;
  92.         else
  93.             velocity = ((transform.forward * vertical) + (transform.right * horizontal)) * Time.fixedDeltaTime * walkSpeed;
  94.  
  95.         // Apply vertical momentum (falling/jumping).
  96.         velocity += Vector3.up * verticalMomentum * Time.fixedDeltaTime;
  97.  
  98.         if ((velocity.z > 0 && front) || (velocity.z < 0 && back))
  99.             velocity.z = 0;
  100.         if ((velocity.x > 0 && right) || (velocity.x < 0 && left))
  101.             velocity.x = 0;
  102.  
  103.         if (velocity.y < 0)
  104.             velocity.y = checkDownSpeed(velocity.y);
  105.         else if (velocity.y > 0)
  106.             velocity.y = checkUpSpeed(velocity.y);
  107.  
  108.         transform.Rotate(Vector3.up * mouseHorizontal * world.settings.mouseSensitivity);
  109.         cam.Rotate(Vector3.right * -mouseVertical * world.settings.mouseSensitivity);
  110.     }
  111.  
  112.     private void GetPlayerInputs () {
  113.  
  114.         horizontal = Input.GetAxis("Horizontal");
  115.         vertical = Input.GetAxis("Vertical");
  116.         mouseHorizontal = Input.GetAxis("Mouse X");
  117.         mouseVertical = Input.GetAxis("Mouse Y");
  118.  
  119.         if (Input.GetKeyDown(KeyCode.Escape))
  120.         {
  121.         #if UNITY_EDITOR
  122.             UnityEditor.EditorApplication.isPlaying = false;
  123.         #else
  124.             Application.Quit();
  125.         #endif
  126.         }
  127.  
  128.         if (Input.GetButtonDown("Sprint"))
  129.             isSprinting = true;
  130.         if (Input.GetButtonUp("Sprint"))
  131.             isSprinting = false;
  132.  
  133.         if (isGrounded && Input.GetButtonDown("Jump"))
  134.             jumpRequest = true;
  135.  
  136.         if (highlightBlock.gameObject.activeSelf) {
  137.  
  138.             // Destroy block.
  139.             if (Input.GetMouseButtonDown(0))
  140.                 world.GetChunkFromVector3(highlightBlock.position).EditVoxel(highlightBlock.position, 0);
  141.  
  142.             // Place block.
  143.             if (Input.GetMouseButtonDown(1)) {
  144.                 if (toolbar.slots[toolbar.slotIndex].HasItem) {
  145.                     world.GetChunkFromVector3(placeBlock.position).EditVoxel(placeBlock.position, toolbar.slots[toolbar.slotIndex].itemSlot.stack.id);
  146.                     toolbar.slots[toolbar.slotIndex].itemSlot.Take(1);
  147.                 }
  148.             }
  149.         }
  150.  
  151.     }
  152.  
  153.     private void placeCursorBlocks () {
  154.  
  155.         float step = checkIncrement;
  156.         Vector3 lastPos = new Vector3();
  157.  
  158.         while (step < reach) {
  159.  
  160.             Vector3 pos = cam.position + (cam.forward * step);
  161.  
  162.             if (world.CheckForVoxel(pos)) {
  163.  
  164.                 highlightBlock.position = new Vector3(Mathf.FloorToInt(pos.x), Mathf.FloorToInt(pos.y), Mathf.FloorToInt(pos.z));
  165.                 placeBlock.position = lastPos;
  166.  
  167.                 highlightBlock.gameObject.SetActive(true);
  168.                 placeBlock.gameObject.SetActive(true);
  169.  
  170.                 return;
  171.  
  172.             }
  173.  
  174.             lastPos = new Vector3(Mathf.FloorToInt(pos.x), Mathf.FloorToInt(pos.y), Mathf.FloorToInt(pos.z));
  175.  
  176.             step += checkIncrement;
  177.  
  178.         }
  179.  
  180.         highlightBlock.gameObject.SetActive(false);
  181.         placeBlock.gameObject.SetActive(false);
  182.  
  183.     }
  184.  
  185.     private float checkDownSpeed (float downSpeed) {
  186.  
  187.         if (
  188.             world.CheckForVoxel(new Vector3(transform.position.x - playerWidth, transform.position.y + downSpeed, transform.position.z - playerWidth)) ||
  189.             world.CheckForVoxel(new Vector3(transform.position.x + playerWidth, transform.position.y + downSpeed, transform.position.z - playerWidth)) ||
  190.             world.CheckForVoxel(new Vector3(transform.position.x + playerWidth, transform.position.y + downSpeed, transform.position.z + playerWidth)) ||
  191.             world.CheckForVoxel(new Vector3(transform.position.x - playerWidth, transform.position.y + downSpeed, transform.position.z + playerWidth))
  192.            ) {
  193.  
  194.             isGrounded = true;
  195.             return 0;
  196.  
  197.         } else {
  198.  
  199.             isGrounded = false;
  200.             return downSpeed;
  201.  
  202.         }
  203.  
  204.     }
  205.  
  206.     private float checkUpSpeed (float upSpeed) {
  207.  
  208.         if (
  209.             world.CheckForVoxel(new Vector3(transform.position.x - playerWidth, transform.position.y + 2f + upSpeed, transform.position.z - playerWidth)) ||
  210.             world.CheckForVoxel(new Vector3(transform.position.x + playerWidth, transform.position.y + 2f + upSpeed, transform.position.z - playerWidth)) ||
  211.             world.CheckForVoxel(new Vector3(transform.position.x + playerWidth, transform.position.y + 2f + upSpeed, transform.position.z + playerWidth)) ||
  212.             world.CheckForVoxel(new Vector3(transform.position.x - playerWidth, transform.position.y + 2f + upSpeed, transform.position.z + playerWidth))
  213.            ) {
  214.  
  215.             return 0;
  216.  
  217.         } else {
  218.  
  219.             return upSpeed;
  220.  
  221.         }
  222.  
  223.     }
  224.  
  225.     public bool front {
  226.  
  227.         get {
  228.             if (
  229.                 world.CheckForVoxel(new Vector3(transform.position.x, transform.position.y, transform.position.z + playerWidth)) ||
  230.                 world.CheckForVoxel(new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z + playerWidth))
  231.                 )
  232.                 return true;
  233.             else
  234.                 return false;
  235.         }
  236.  
  237.     }
  238.     public bool back {
  239.  
  240.         get {
  241.             if (
  242.                 world.CheckForVoxel(new Vector3(transform.position.x, transform.position.y, transform.position.z - playerWidth)) ||
  243.                 world.CheckForVoxel(new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z - playerWidth))
  244.                 )
  245.                 return true;
  246.             else
  247.                 return false;
  248.         }
  249.  
  250.     }
  251.     public bool left {
  252.  
  253.         get {
  254.             if (
  255.                 world.CheckForVoxel(new Vector3(transform.position.x - playerWidth, transform.position.y, transform.position.z)) ||
  256.                 world.CheckForVoxel(new Vector3(transform.position.x - playerWidth, transform.position.y + 1f, transform.position.z))
  257.                 )
  258.                 return true;
  259.             else
  260.                 return false;
  261.         }
  262.  
  263.     }
  264.     public bool right {
  265.  
  266.         get {
  267.             if (
  268.                 world.CheckForVoxel(new Vector3(transform.position.x + playerWidth, transform.position.y, transform.position.z)) ||
  269.                 world.CheckForVoxel(new Vector3(transform.position.x + playerWidth, transform.position.y + 1f, transform.position.z))
  270.                 )
  271.                 return true;
  272.             else
  273.                 return false;
  274.         }
  275.  
  276.     }
  277.  
  278. }
Add Comment
Please, Sign In to add comment