You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class LocalPlayer : Player {
  5. public CharacterController charControl;
  6. public Animator Animator;
  7. public float Speed = 5;
  8. private float animWalkSpeed = 0;
  9. public Recipe recipe;
  10. protected override void Update()
  11. {
  12. ApplyGravity();
  13. ApplyInput();
  14. Teleport();
  15. base.Update();
  16. }
  17. private void ApplyGravity()
  18. {
  19. if (!charControl.isGrounded)
  20. {
  21. charControl.SimpleMove(new Vector3(0.0f, -5.0f * Time.deltaTime, 0.0f));
  22. }
  23. }
  24. private void ApplyInput()
  25. {
  26. if (Input.GetMouseButton(0))
  27. {
  28. Vector3 mousePos = new Vector3(Input.mousePosition.x, 0.0f, Input.mousePosition.y);
  29. Vector3 screenCentre = new Vector3(Screen.width / 2, 0.0F, Screen.height / 2);
  30. Vector3 movePos = mousePos - screenCentre;
  31. movePos.x = movePos.x / Screen.width;
  32. movePos.z = movePos.z / Screen.height;
  33. charControl.Move(movePos * Speed * Time.deltaTime);
  34. animWalkSpeed = Mathf.Clamp01(movePos.magnitude * 3);
  35. playerModel.transform.LookAt(playerModel.transform.position + movePos, Vector3.up);
  36. }else if (animWalkSpeed > 0)
  37. {
  38. animWalkSpeed -= 0.1f;
  39. }
  40. Animator.SetFloat("WalkSpeed", animWalkSpeed);
  41. }
  42. void Teleport()
  43. {
  44. if (map == null)
  45. return;
  46. if (transform.position.x > map.maxX)
  47. {
  48. transform.position = new Vector3(map.minX, transform.position.y, transform.position.z);
  49. }
  50. else if (transform.position.x < map.minX)
  51. {
  52. transform.position = new Vector3(map.maxX, transform.position.y, transform.position.z);
  53. }
  54. if (transform.position.z > map.maxZ)
  55. {
  56. transform.position = new Vector3(transform.position.x, transform.position.y, map.minZ);
  57. }
  58. else if (transform.position.z < map.minZ)
  59. {
  60. transform.position = new Vector3(transform.position.x, transform.position.y, map.maxZ);
  61. }
  62. }
  63. public void PickupVeggie(Vegetable vegIn)
  64. {
  65. if (heldVeggie != null)
  66. {
  67. recipe.friendlyPickup(heldVeggie.Name, -1);
  68. }
  69. heldVeggie = vegIn;
  70. recipe.friendlyPickup(vegIn.Name, 1);
  71. }
  72. }