using UnityEngine; using UnityEngine.EventSystems; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; public class InputController : MonoBehaviour { private const float ORIGIN_SIZE = 25f; private const float MIN_SIZE = 10f; private const float MAX_SIZE = 25f; private const float MIN_ANGLE = 30f; private const float MAX_ANGLE = 80f; private const float FAR_AWAY_SIZE = 100f; public enum State { Faraway, ZoomIn, Battle, } public enum CameraMode { LockCraft = 0, FreeLook = 1 } public State state; public CameraMode cameraMode; private BattleController battleController; private GameObject ground; private Vector3 lastMousePosition; public GameObject moveMarkPrefab; public bool lockCamera = true; private float distance = FAR_AWAY_SIZE; private float targetDistance = FAR_AWAY_SIZE; private float targetAngle = MAX_ANGLE; public ITarget cameraLockedTarget; private Vector3 lookAtPos; public CameraShake cameraShake; void Start () { ground = GameObject.FindObjectOfType().gameObject; cameraShake = Camera.main.GetComponent(); } private bool IsPointerOverUIObject() { PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y); List results = new List(); EventSystem.current.RaycastAll(eventDataCurrentPosition, results); return results.Count > 0; } public void SetBattleController(BattleController battleController) { this.battleController = battleController; } public Vector3 cameraPosition { get{ return lookAtPos; } } // private void findCameraLockTarget() // { // if(battleController.ctrlCraft == null) // { // if(battleController.GetMyPlayer() == null || battleController.isGameOver || battleController.GetMyPlayer().CanRelive()) // { // return; // } // // bool findTarget = false; // ITarget firstTarget = null; // List list = battleController.GetMap().GetBattleObjectByRange(new Vector3(), float.MaxValue, battleController.GetMyPlayer().team); // list.Sort(CompareCraftId); // // for(int i=0; i lockedObj.id) // { // findTarget = true; // cameraLockedTarget = bo; // break; // } // } // } // // if(!findTarget) // { // cameraLockedTarget = firstTarget; // } // } // else // { // cameraLockedTarget = battleController.ctrlCraft; // } // } public static int CompareCraftId(BattleObject a, BattleObject b) { if (a == null) { if (b == null) { return 0; } else { return -1; } } else { if (b == null) { return 1; } else { if(a.id > b.id) { return 1; } else if(a.id < b.id) { return -1; } else { return 0; } } } } public void SetCameraTo(Vector3 pos) { lookAtPos = pos; Vector3 cameraPosition = Camera.main.transform.position; Vector3 cameraRotation = Camera.main.transform.eulerAngles; float theta = NumberUtil.angleToRadian (cameraRotation.x); pos.y += distance * Mathf.Sin(theta); pos.z -= distance * Mathf.Cos(theta); Camera.main.transform.position = pos; } private void updateCamera () { if (cameraMode != CameraMode.LockCraft) return; if(lockCamera) { if(cameraLockedTarget != null) { if(state == State.ZoomIn) { targetDistance -= GameTime.deltaTime*(targetDistance - MAX_SIZE) * 3f; if(targetDistance < MAX_SIZE+0.1f) { targetDistance = MAX_SIZE; state = State.Battle; battleController.ReadyToAction(); } } Camera camera = battleController.gameCamera; Vector3 cameraPosition = camera.transform.position; Vector3 ctrlCraftPosition = cameraLockedTarget.position; lookAtPos = ctrlCraftPosition; distance = targetDistance; targetAngle = MIN_ANGLE + (MAX_ANGLE - MIN_ANGLE) * (targetDistance - MIN_SIZE) / (MAX_SIZE - MIN_SIZE); targetAngle = NumberUtil.forceBetween(targetAngle, MIN_ANGLE, MAX_ANGLE); Quaternion cameraRotation = Quaternion.Euler(targetAngle, 0f, 0f); ; float theta = NumberUtil.angleToRadian (targetAngle); // distance += (targetDistance - distance)*5f*GameTime.deltaTime; ctrlCraftPosition.y += distance * Mathf.Sin(theta); ctrlCraftPosition.z -= distance * Mathf.Cos(theta); if (battleController.GetCtrlCraft () != null) { cameraPosition.x += (ctrlCraftPosition.x - cameraPosition.x) * 5f * GameTime.deltaTime + cameraShake.offsetX; cameraPosition.z += (ctrlCraftPosition.z - cameraPosition.z) * 5f * GameTime.deltaTime + cameraShake.offsetZ; // cameraPosition.y += (ctrlCraftPosition.y-cameraPosition.y)*5f*GameTime.deltaTime; } else { cameraPosition.x = ctrlCraftPosition.x + cameraShake.offsetX; cameraPosition.z = ctrlCraftPosition.z + cameraShake.offsetZ; } cameraPosition.y = ctrlCraftPosition.y; camera.transform.position = Vector3.Lerp(cameraPosition, camera.transform.position, GameTime.deltaTime * 5f); //camera.transform.localRotation = Quaternion.Slerp(camera.transform.localRotation, cameraRotation, GameTime.deltaTime * 100f); camera.transform.localRotation = cameraRotation; } } else { if(Input.GetMouseButtonDown(0)) { lastMousePosition = Input.mousePosition; } else if(Input.GetMouseButton(0)) { Vector3 currentMousePosition = Input.mousePosition; Vector3 lastWorldPosition = Camera.main.ScreenToWorldPoint(lastMousePosition); Vector3 currentWorldPosition = Camera.main.ScreenToWorldPoint(currentMousePosition); float deltaX = currentWorldPosition.x - lastWorldPosition.x; float deltaY = (currentWorldPosition.z - lastWorldPosition.z)/Mathf.Cos(Mathf.PI*10/180); Vector3 cameraPosition = Camera.main.transform.position; cameraPosition.x -= deltaX; cameraPosition.z -= deltaY; Camera.main.transform.position = cameraPosition; lastMousePosition = currentMousePosition; } } } void LateUpdate() { updateCamera (); } // Update is called once per frame void Update () { float mouseScrollWheel = Input.GetAxis("Mouse ScrollWheel"); if(mouseScrollWheel != 0) { zoom(-mouseScrollWheel*2); } if(battleController.isGameOver || BattleController.battleType == BattleController.BattleType.Edit) { return; } if (cameraMode == CameraMode.LockCraft) CameraLockCraft (); else if (cameraMode == CameraMode.FreeLook) CameraFreeLook (); } private Vector3 mouseDownPos; private Vector3 mouseDownCameraPos; private void CameraFreeLook() { if (Input.GetMouseButtonDown (0)) { mouseDownPos = Input.mousePosition; mouseDownCameraPos = Camera.main.transform.position; } if(Input.GetMouseButton(0)) { Vector3 currentMousePos = Input.mousePosition; Vector3 pos = Camera.main.transform.position; float deltaX = (currentMousePos.x - mouseDownPos.x) / Screen.width; float deltaY = (currentMousePos.y - mouseDownPos.y) / Screen.height; pos.x = mouseDownCameraPos.x - deltaX * 20f; pos.z = mouseDownCameraPos.z - deltaY * 20f; Camera.main.transform.position = Vector3.Lerp (pos, Camera.main.transform.position, GameTime.deltaTime * 10f); } } private void CameraLockCraft() { Craft ctrlCraft = battleController.GetCtrlCraft(); if (ctrlCraft != null) { if (Input.GetMouseButtonDown (0) && !IsPointerOverUIObject ()) { doClick (); } if (Input.GetKeyDown (KeyCode.Q)) { battleController.AttempUsePower (ctrlCraft.GetPowerManager ().GetPowers () [1].GetId ()); } else if (Input.GetKeyDown (KeyCode.W)) { battleController.AttempUsePower (ctrlCraft.GetPowerManager ().GetPowers () [2].GetId ()); } else if (Input.GetKeyDown (KeyCode.E)) { battleController.AttempUsePower (ctrlCraft.GetPowerManager ().GetPowers () [3].GetId ()); } else if (Input.GetKeyDown (KeyCode.A)) { battleController.AttempUsePower (ctrlCraft.GetPowerManager ().GetCrystalPowers () [0].GetId ()); } else if (Input.GetKeyDown (KeyCode.S)) { battleController.AttempUsePower (ctrlCraft.GetPowerManager ().GetCrystalPowers () [1].GetId ()); } } else { if (Input.GetMouseButtonDown (0) && !IsPointerOverUIObject ()) { LockNextCraft (); } } } private void doClick() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if(Physics.Raycast(ray, out hitInfo)) { if(hitInfo.collider.gameObject.Equals(ground)) { int hitCol = (int)(hitInfo.point.x/Map.TILE_WIDTH); int hitRow = (int)(hitInfo.point.z/Map.TILE_LENGTH); Map map = battleController.GetMap(); hitCol = NumberUtil.forceBetween(hitCol, 0, map.columns-1); hitRow = NumberUtil.forceBetween(hitRow, 0, map.rows-1); GameObject moveMark = Instantiate(moveMarkPrefab) as GameObject; Vector3 moveMarkPosition = new Vector3(hitInfo.point.x, 0.01f, hitInfo.point.z); moveMark.transform.position = moveMarkPosition; battleController.GetCtrlCraft().GetComponent().followTarget = false; battleController.GetCtrlCraft().forceMove = true; battleController.GetMessageManager().Move(battleController.GetCtrlCraft(), hitCol, hitRow); // if(battleController.ctrlCraft.target == null) // { // float theta = NumberUtil.getRadianByATan(moveMarkPosition.x, moveMarkPosition.z, battleController.ctrlCraft.position.x, battleController.ctrlCraft.position.z); // float rotateAngle = 90-NumberUtil.radianToAngle(theta); // battleController.ctrlCraft.setRotateTargetAngle(rotateAngle); // } } else { Craft craft = hitInfo.collider.GetComponent(); if(craft == null) { Debuger.LogError("craft is null"); return; } if (craft.team != battleController.GetCtrlCraft().team) { battleController.GetCtrlCraft().target = craft; battleController.GetCtrlCraft().GetComponent ().followTarget = true; } } } } public void LockNextCraft() { if (state != State.Battle) return; if (cameraLockedTarget == null || !(cameraLockedTarget is Craft) || (cameraLockedTarget as Craft).IsDead ()) cameraLockedTarget = null; List playerList = battleController.GetBattleSession ().GetPlayerList (); Craft firstCraft = null; for(int i=0; i