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.

103 lines
2.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 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. public AudioSource WalkingSounds;
  11. protected override void Update()
  12. {
  13. ApplyGravity();
  14. ApplyInput();
  15. Teleport();
  16. base.Update();
  17. }
  18. private void ApplyGravity()
  19. {
  20. if (!charControl.isGrounded)
  21. {
  22. charControl.SimpleMove(new Vector3(0.0f, -5.0f * Time.deltaTime, 0.0f));
  23. }
  24. }
  25. private void ApplyInput()
  26. {
  27. if (Input.GetMouseButton(0))
  28. {
  29. Vector3 mousePos = new Vector3(Input.mousePosition.x, 0.0f, Input.mousePosition.y);
  30. Vector3 screenCentre = new Vector3(Screen.width / 2, 0.0F, Screen.height / 2);
  31. Vector3 movePos = mousePos - screenCentre;
  32. movePos.x = movePos.x / Screen.width;
  33. movePos.z = movePos.z / Screen.height;
  34. charControl.Move(movePos * Speed * Time.deltaTime);
  35. animWalkSpeed = Mathf.Clamp01(movePos.magnitude * 3);
  36. playerModel.transform.LookAt(playerModel.transform.position + movePos, Vector3.up);
  37. }else if (animWalkSpeed > 0)
  38. {
  39. animWalkSpeed -= 0.1f;
  40. }
  41. Animator.SetFloat("WalkSpeed", animWalkSpeed);
  42. if ( WalkingSounds != null && WalkingSounds.isPlaying != true)
  43. {
  44. WalkingSounds.Play();
  45. }
  46. }
  47. void Teleport()
  48. {
  49. if (map == null)
  50. return;
  51. if (transform.position.x > map.maxX)
  52. {
  53. transform.position = new Vector3(map.minX, transform.position.y, transform.position.z);
  54. }
  55. else if (transform.position.x < map.minX)
  56. {
  57. transform.position = new Vector3(map.maxX, transform.position.y, transform.position.z);
  58. }
  59. if (transform.position.z > map.maxZ)
  60. {
  61. transform.position = new Vector3(transform.position.x, transform.position.y, map.minZ);
  62. }
  63. else if (transform.position.z < map.minZ)
  64. {
  65. transform.position = new Vector3(transform.position.x, transform.position.y, map.maxZ);
  66. }
  67. }
  68. public void PickupVeggie(Vegetable vegIn)
  69. {
  70. if (heldVeggie != null)
  71. {
  72. recipe.friendlyPickup(heldVeggie.Name, -1);
  73. }
  74. heldVeggie = vegIn;
  75. recipe.friendlyPickup(vegIn.Name, 1);
  76. recipe.pickupItem(vegIn);
  77. }
  78. public void OnControllerColliderHit(ControllerColliderHit hit)
  79. {
  80. if (hit.gameObject.tag == "Vegetable")
  81. {
  82. PickupVeggie(hit.gameObject.GetComponent<VegetableSpawner>().thisVeg);
  83. }
  84. }
  85. }