InputController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. public class InputController : MonoBehaviour {
  7. private const float ORIGIN_SIZE = 25f;
  8. private const float MIN_SIZE = 10f;
  9. private const float MAX_SIZE = 25f;
  10. private const float MIN_ANGLE = 30f;
  11. private const float MAX_ANGLE = 80f;
  12. private const float FAR_AWAY_SIZE = 100f;
  13. public enum State
  14. {
  15. Faraway,
  16. ZoomIn,
  17. Battle,
  18. }
  19. public enum CameraMode
  20. {
  21. LockCraft = 0,
  22. FreeLook = 1
  23. }
  24. public State state;
  25. public CameraMode cameraMode;
  26. private BattleController battleController;
  27. private GameObject ground;
  28. private Vector3 lastMousePosition;
  29. public GameObject moveMarkPrefab;
  30. public bool lockCamera = true;
  31. private float distance = FAR_AWAY_SIZE;
  32. private float targetDistance = FAR_AWAY_SIZE;
  33. private float targetAngle = MAX_ANGLE;
  34. public ITarget cameraLockedTarget;
  35. private Vector3 lookAtPos;
  36. public CameraShake cameraShake;
  37. void Start ()
  38. {
  39. ground = GameObject.FindObjectOfType<GroundPlane>().gameObject;
  40. cameraShake = Camera.main.GetComponent<CameraShake>();
  41. }
  42. private bool IsPointerOverUIObject()
  43. {
  44. PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
  45. eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
  46. List<RaycastResult> results = new List<RaycastResult>();
  47. EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
  48. return results.Count > 0;
  49. }
  50. public void SetBattleController(BattleController battleController)
  51. {
  52. this.battleController = battleController;
  53. }
  54. public Vector3 cameraPosition
  55. {
  56. get{
  57. return lookAtPos;
  58. }
  59. }
  60. // private void findCameraLockTarget()
  61. // {
  62. // if(battleController.ctrlCraft == null)
  63. // {
  64. // if(battleController.GetMyPlayer() == null || battleController.isGameOver || battleController.GetMyPlayer().CanRelive())
  65. // {
  66. // return;
  67. // }
  68. //
  69. // bool findTarget = false;
  70. // ITarget firstTarget = null;
  71. // List<BattleObject> list = battleController.GetMap().GetBattleObjectByRange(new Vector3(), float.MaxValue, battleController.GetMyPlayer().team);
  72. // list.Sort(CompareCraftId);
  73. //
  74. // for(int i=0; i<list.Count; i++)
  75. // {
  76. // BattleObject bo = list[i];
  77. // if(firstTarget == null)
  78. // {
  79. // firstTarget = bo;
  80. // }
  81. //
  82. // if(cameraLockedTarget != null && cameraLockedTarget is BattleObject)
  83. // {
  84. // BattleObject lockedObj = cameraLockedTarget as BattleObject;
  85. // if(bo.id > lockedObj.id)
  86. // {
  87. // findTarget = true;
  88. // cameraLockedTarget = bo;
  89. // break;
  90. // }
  91. // }
  92. // }
  93. //
  94. // if(!findTarget)
  95. // {
  96. // cameraLockedTarget = firstTarget;
  97. // }
  98. // }
  99. // else
  100. // {
  101. // cameraLockedTarget = battleController.ctrlCraft;
  102. // }
  103. // }
  104. public static int CompareCraftId(BattleObject a, BattleObject b)
  105. {
  106. if (a == null)
  107. {
  108. if (b == null)
  109. {
  110. return 0;
  111. }
  112. else
  113. {
  114. return -1;
  115. }
  116. }
  117. else
  118. {
  119. if (b == null)
  120. {
  121. return 1;
  122. }
  123. else
  124. {
  125. if(a.id > b.id)
  126. {
  127. return 1;
  128. }
  129. else if(a.id < b.id)
  130. {
  131. return -1;
  132. }
  133. else
  134. {
  135. return 0;
  136. }
  137. }
  138. }
  139. }
  140. public void SetCameraTo(Vector3 pos)
  141. {
  142. lookAtPos = pos;
  143. Vector3 cameraPosition = Camera.main.transform.position;
  144. Vector3 cameraRotation = Camera.main.transform.eulerAngles;
  145. float theta = NumberUtil.angleToRadian (cameraRotation.x);
  146. pos.y += distance * Mathf.Sin(theta);
  147. pos.z -= distance * Mathf.Cos(theta);
  148. Camera.main.transform.position = pos;
  149. }
  150. private void updateCamera ()
  151. {
  152. if (cameraMode != CameraMode.LockCraft)
  153. return;
  154. if(lockCamera)
  155. {
  156. if(cameraLockedTarget != null)
  157. {
  158. if(state == State.ZoomIn)
  159. {
  160. targetDistance -= GameTime.deltaTime*(targetDistance - MAX_SIZE) * 3f;
  161. if(targetDistance < MAX_SIZE+0.1f)
  162. {
  163. targetDistance = MAX_SIZE;
  164. state = State.Battle;
  165. battleController.ReadyToAction();
  166. }
  167. }
  168. Camera camera = battleController.gameCamera;
  169. Vector3 cameraPosition = camera.transform.position;
  170. Vector3 ctrlCraftPosition = cameraLockedTarget.position;
  171. lookAtPos = ctrlCraftPosition;
  172. distance = targetDistance;
  173. targetAngle = MIN_ANGLE + (MAX_ANGLE - MIN_ANGLE) * (targetDistance - MIN_SIZE) / (MAX_SIZE - MIN_SIZE);
  174. targetAngle = NumberUtil.forceBetween(targetAngle, MIN_ANGLE, MAX_ANGLE);
  175. Quaternion cameraRotation = Quaternion.Euler(targetAngle, 0f, 0f); ;
  176. float theta = NumberUtil.angleToRadian (targetAngle);
  177. // distance += (targetDistance - distance)*5f*GameTime.deltaTime;
  178. ctrlCraftPosition.y += distance * Mathf.Sin(theta);
  179. ctrlCraftPosition.z -= distance * Mathf.Cos(theta);
  180. if (battleController.GetCtrlCraft () != null) {
  181. cameraPosition.x += (ctrlCraftPosition.x - cameraPosition.x) * 5f * GameTime.deltaTime + cameraShake.offsetX;
  182. cameraPosition.z += (ctrlCraftPosition.z - cameraPosition.z) * 5f * GameTime.deltaTime + cameraShake.offsetZ;
  183. // cameraPosition.y += (ctrlCraftPosition.y-cameraPosition.y)*5f*GameTime.deltaTime;
  184. } else {
  185. cameraPosition.x = ctrlCraftPosition.x + cameraShake.offsetX;
  186. cameraPosition.z = ctrlCraftPosition.z + cameraShake.offsetZ;
  187. }
  188. cameraPosition.y = ctrlCraftPosition.y;
  189. camera.transform.position = Vector3.Lerp(cameraPosition, camera.transform.position, GameTime.deltaTime * 5f);
  190. //camera.transform.localRotation = Quaternion.Slerp(camera.transform.localRotation, cameraRotation, GameTime.deltaTime * 100f);
  191. camera.transform.localRotation = cameraRotation;
  192. }
  193. }
  194. else
  195. {
  196. if(Input.GetMouseButtonDown(0))
  197. {
  198. lastMousePosition = Input.mousePosition;
  199. }
  200. else if(Input.GetMouseButton(0))
  201. {
  202. Vector3 currentMousePosition = Input.mousePosition;
  203. Vector3 lastWorldPosition = Camera.main.ScreenToWorldPoint(lastMousePosition);
  204. Vector3 currentWorldPosition = Camera.main.ScreenToWorldPoint(currentMousePosition);
  205. float deltaX = currentWorldPosition.x - lastWorldPosition.x;
  206. float deltaY = (currentWorldPosition.z - lastWorldPosition.z)/Mathf.Cos(Mathf.PI*10/180);
  207. Vector3 cameraPosition = Camera.main.transform.position;
  208. cameraPosition.x -= deltaX;
  209. cameraPosition.z -= deltaY;
  210. Camera.main.transform.position = cameraPosition;
  211. lastMousePosition = currentMousePosition;
  212. }
  213. }
  214. }
  215. void LateUpdate()
  216. {
  217. updateCamera ();
  218. }
  219. // Update is called once per frame
  220. void Update ()
  221. {
  222. float mouseScrollWheel = Input.GetAxis("Mouse ScrollWheel");
  223. if(mouseScrollWheel != 0)
  224. {
  225. zoom(-mouseScrollWheel*2);
  226. }
  227. if(battleController.isGameOver || BattleController.battleType == BattleController.BattleType.Edit)
  228. {
  229. return;
  230. }
  231. if (cameraMode == CameraMode.LockCraft)
  232. CameraLockCraft ();
  233. else if (cameraMode == CameraMode.FreeLook)
  234. CameraFreeLook ();
  235. }
  236. private Vector3 mouseDownPos;
  237. private Vector3 mouseDownCameraPos;
  238. private void CameraFreeLook()
  239. {
  240. if (Input.GetMouseButtonDown (0)) {
  241. mouseDownPos = Input.mousePosition;
  242. mouseDownCameraPos = Camera.main.transform.position;
  243. }
  244. if(Input.GetMouseButton(0))
  245. {
  246. Vector3 currentMousePos = Input.mousePosition;
  247. Vector3 pos = Camera.main.transform.position;
  248. float deltaX = (currentMousePos.x - mouseDownPos.x) / Screen.width;
  249. float deltaY = (currentMousePos.y - mouseDownPos.y) / Screen.height;
  250. pos.x = mouseDownCameraPos.x - deltaX * 20f;
  251. pos.z = mouseDownCameraPos.z - deltaY * 20f;
  252. Camera.main.transform.position = Vector3.Lerp (pos, Camera.main.transform.position, GameTime.deltaTime * 10f);
  253. }
  254. }
  255. private void CameraLockCraft()
  256. {
  257. Craft ctrlCraft = battleController.GetCtrlCraft();
  258. if (ctrlCraft != null) {
  259. if (Input.GetMouseButtonDown (0) && !IsPointerOverUIObject ()) {
  260. doClick ();
  261. }
  262. if (Input.GetKeyDown (KeyCode.Q)) {
  263. battleController.AttempUsePower (ctrlCraft.GetPowerManager ().GetPowers () [1].GetId ());
  264. } else if (Input.GetKeyDown (KeyCode.W)) {
  265. battleController.AttempUsePower (ctrlCraft.GetPowerManager ().GetPowers () [2].GetId ());
  266. } else if (Input.GetKeyDown (KeyCode.E)) {
  267. battleController.AttempUsePower (ctrlCraft.GetPowerManager ().GetPowers () [3].GetId ());
  268. } else if (Input.GetKeyDown (KeyCode.A)) {
  269. battleController.AttempUsePower (ctrlCraft.GetPowerManager ().GetCrystalPowers () [0].GetId ());
  270. } else if (Input.GetKeyDown (KeyCode.S)) {
  271. battleController.AttempUsePower (ctrlCraft.GetPowerManager ().GetCrystalPowers () [1].GetId ());
  272. }
  273. } else {
  274. if (Input.GetMouseButtonDown (0) && !IsPointerOverUIObject ()) {
  275. LockNextCraft ();
  276. }
  277. }
  278. }
  279. private void doClick()
  280. {
  281. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  282. RaycastHit hitInfo;
  283. if(Physics.Raycast(ray, out hitInfo))
  284. {
  285. if(hitInfo.collider.gameObject.Equals(ground))
  286. {
  287. int hitCol = (int)(hitInfo.point.x/Map.TILE_WIDTH);
  288. int hitRow = (int)(hitInfo.point.z/Map.TILE_LENGTH);
  289. Map map = battleController.GetMap();
  290. hitCol = NumberUtil.forceBetween(hitCol, 0, map.columns-1);
  291. hitRow = NumberUtil.forceBetween(hitRow, 0, map.rows-1);
  292. GameObject moveMark = Instantiate(moveMarkPrefab) as GameObject;
  293. Vector3 moveMarkPosition = new Vector3(hitInfo.point.x, 0.01f, hitInfo.point.z);
  294. moveMark.transform.position = moveMarkPosition;
  295. battleController.GetCtrlCraft().GetComponent<CraftAI>().followTarget = false;
  296. battleController.GetCtrlCraft().forceMove = true;
  297. battleController.GetMessageManager().Move(battleController.GetCtrlCraft(), hitCol, hitRow);
  298. // if(battleController.ctrlCraft.target == null)
  299. // {
  300. // float theta = NumberUtil.getRadianByATan(moveMarkPosition.x, moveMarkPosition.z, battleController.ctrlCraft.position.x, battleController.ctrlCraft.position.z);
  301. // float rotateAngle = 90-NumberUtil.radianToAngle(theta);
  302. // battleController.ctrlCraft.setRotateTargetAngle(rotateAngle);
  303. // }
  304. }
  305. else
  306. {
  307. Craft craft = hitInfo.collider.GetComponent<Craft>();
  308. if(craft == null)
  309. {
  310. Debuger.LogError("craft is null");
  311. return;
  312. }
  313. if (craft.team != battleController.GetCtrlCraft().team) {
  314. battleController.GetCtrlCraft().target = craft;
  315. battleController.GetCtrlCraft().GetComponent<CraftAI> ().followTarget = true;
  316. }
  317. }
  318. }
  319. }
  320. public void LockNextCraft()
  321. {
  322. if (state != State.Battle)
  323. return;
  324. if (cameraLockedTarget == null || !(cameraLockedTarget is Craft) || (cameraLockedTarget as Craft).IsDead ())
  325. cameraLockedTarget = null;
  326. List<Player> playerList = battleController.GetBattleSession ().GetPlayerList ();
  327. Craft firstCraft = null;
  328. for(int i=0; i<playerList.Count; i++)
  329. {
  330. Player player = playerList [i];
  331. if (player.GetHero ().GetCraft () == null)
  332. continue;
  333. if (firstCraft == null)
  334. firstCraft = player.GetHero ().GetCraft ();
  335. if (cameraLockedTarget != null) {
  336. if(cameraLockedTarget == player.GetHero().GetCraft())
  337. {
  338. cameraLockedTarget = null;
  339. }
  340. } else {
  341. cameraLockedTarget = player.GetHero ().GetCraft ();
  342. break;
  343. }
  344. }
  345. if (cameraLockedTarget == null) {
  346. cameraLockedTarget = firstCraft;
  347. }
  348. }
  349. private void zoom(float value)
  350. {
  351. if(state == State.Battle)
  352. {
  353. targetDistance += value*10f;
  354. targetDistance = NumberUtil.forceBetween(targetDistance, MIN_SIZE, MAX_SIZE);
  355. }
  356. }
  357. }