GameController.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameController : MonoBehaviour {
  5. [Tooltip("玩家预设")]
  6. public GameObject prefabP;
  7. [Tooltip("怪物预设")]
  8. public GameObject prefabM;
  9. public GameObject[] sceneEnity;
  10. public GameObject[] player;
  11. public GameObject[] moster;
  12. private List<Player> playerS;
  13. private List<Moster> mosterS;
  14. public Transform Bullet;
  15. public Transform Moster;
  16. public Transform Player;
  17. public GameObject bulletG;
  18. public GameObject bulletR;
  19. private static GameController instance;
  20. void Awake()
  21. {
  22. if (instance==null)
  23. {
  24. instance = this;
  25. }
  26. playerS = new List<Player>();
  27. mosterS = new List<Moster>();
  28. for (int i = 0; i < player.Length; i++)
  29. {
  30. var ps= player[i].GetComponent<Player>();
  31. if (ps)
  32. {
  33. playerS.Add(ps);
  34. }
  35. }
  36. for (int i = 0; i < moster.Length; i++)
  37. {
  38. var ms = moster[i].GetComponent<Moster>();
  39. if (ms)
  40. {
  41. mosterS.Add(ms);
  42. }
  43. }
  44. }
  45. public static GameController GetInstance()
  46. {
  47. return instance;
  48. }
  49. private Vector3 nowPos;
  50. private Vector3 oldPos;
  51. private bool isClick = false;
  52. private float length = 1;
  53. //private void OnMouseUp()
  54. //{
  55. // isClick = false;
  56. // print(2);
  57. //}
  58. //private void OnMouseDown()
  59. //{
  60. // isClick = true;
  61. // print(3);
  62. //}
  63. private void Update()
  64. {
  65. CloneRole();
  66. WriteFile();
  67. if (Input.GetMouseButtonDown(0))
  68. {
  69. isClick = true;
  70. }
  71. else if(Input.GetMouseButtonUp(0))
  72. {
  73. isClick = false;
  74. }
  75. nowPos = Input.mousePosition;
  76. if (isClick == true)
  77. {
  78. Vector3 offset = nowPos - oldPos;
  79. if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y) && Mathf.Abs(offset.x) > length)
  80. {
  81. // transform.Rotate(Vector3.up, -offset.x);
  82. Rotation(-offset.x/10);
  83. print(111);
  84. }
  85. }
  86. oldPos = Input.mousePosition;
  87. }
  88. private void Rotation(float offset)
  89. {
  90. for (int i = 0; i < playerS.Count; i++)
  91. {
  92. playerS[i].RotationSelf(offset);
  93. }
  94. for (int i = 0; i < mosterS.Count; i++)
  95. {
  96. mosterS[i].RotationSelf(offset);
  97. }
  98. }
  99. private void CloneRole()
  100. {
  101. if (Input.GetKeyDown(KeyCode.P))
  102. {
  103. var go = GameObject.Instantiate(prefabP);
  104. go.transform.SetParent(Player);
  105. go.name = prefabP.name;
  106. var gs= go.AddComponent<Player>();
  107. playerS.Add(gs);
  108. }
  109. if (Input.GetKeyDown(KeyCode.M))
  110. {
  111. var go = GameObject.Instantiate(prefabM);
  112. go.transform.SetParent(Moster);
  113. go.name = prefabM.name;
  114. var gs = go.AddComponent<Moster>();
  115. mosterS.Add(gs);
  116. }
  117. }
  118. private void WriteFile()
  119. {
  120. if (Input.GetKeyDown(KeyCode.C))
  121. {
  122. OutPointManager.GetInstance().StartWriteFile();
  123. }
  124. }
  125. public float GetAng(Vector3 dir)
  126. {
  127. var xx = Mathf.Abs(dir.x);
  128. var zz = Mathf.Abs(dir.z);
  129. var obl = Mathf.Sqrt(Mathf.Pow(dir.x, 2) + Mathf.Pow(dir.z, 2));
  130. return 180 / Mathf.PI * Mathf.Acos(xx / obl);
  131. }
  132. }