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.

97 lines
2.5 KiB

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 CharacterMovement : MonoBehaviour {
  5. public float Speed;
  6. GameObject localPlayer;
  7. public List<GameObject> Players = new List<GameObject>();
  8. public List<bool> Nearby = new List<bool>();
  9. public float DistanceToWin;
  10. Camera cam;
  11. private void Start()
  12. {
  13. localPlayer = Multiplayer.PlayersManager.Instance.LocalPlayer;
  14. Players.Add(localPlayer);
  15. foreach (GameObject curPlayer in Multiplayer.PlayersManager.Instance.RemotePlayers.Values)
  16. {
  17. Players.Add(curPlayer);
  18. }
  19. cam = localPlayer.GetComponentInChildren<Camera>();
  20. }
  21. //Public movement functions :)
  22. public void MoveLeft()
  23. {
  24. localPlayer.GetComponent<Rigidbody>().AddForce(new Vector3(-Speed, 0.0f, 0.0f));
  25. }
  26. public void MoveRight()
  27. {
  28. localPlayer.GetComponent<Rigidbody>().AddForce(new Vector3(Speed, 0.0f, 0.0f));
  29. }
  30. public void MoveUp()
  31. {
  32. localPlayer.GetComponent<Rigidbody>().AddForce(new Vector3(0.0f, 0.0f, Speed));
  33. }
  34. public void MoveDown()
  35. {
  36. localPlayer.GetComponent<Rigidbody>().AddForce(new Vector3(0.0f, 0.0f, -Speed));
  37. }
  38. public void mouseClick()
  39. {
  40. Vector3 screenMouse = Input.mousePosition;
  41. Vector3 mousePos = cam.ScreenToWorldPoint(screenMouse);
  42. Vector3 moveVec = new Vector3(mousePos.x, 0.0f, mousePos.z) * Speed;
  43. localPlayer.GetComponent<Rigidbody>().AddForce(moveVec);
  44. }
  45. public bool CheckNearby()
  46. {
  47. for (int i = 0; i < Players.Count; i++)
  48. {
  49. if ((localPlayer.transform.position - Players[i].transform.position).magnitude < DistanceToWin)
  50. {
  51. Greet();
  52. }
  53. else
  54. {
  55. Player playerScript = Players[i].GetComponent<Player>();
  56. foreach (GameObject dummy in playerScript.dummies)
  57. {
  58. if ((localPlayer.transform.position - dummy.transform.position).magnitude < DistanceToWin)
  59. {
  60. Greet();
  61. }
  62. }
  63. }
  64. }
  65. return CheckWin();
  66. }
  67. bool CheckWin()
  68. {
  69. for (int i = 0; i < Nearby.Count; i++)
  70. {
  71. if (Nearby[i] == false)
  72. {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. public void Greet()
  79. {
  80. //Play audio
  81. //Trigger animation
  82. }
  83. }