123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- 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<GroundPlane>().gameObject;
- cameraShake = Camera.main.GetComponent<CameraShake>();
- }
- private bool IsPointerOverUIObject()
- {
- PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
- eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
-
- List<RaycastResult> results = new List<RaycastResult>();
- 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<BattleObject> list = battleController.GetMap().GetBattleObjectByRange(new Vector3(), float.MaxValue, battleController.GetMyPlayer().team);
- // list.Sort(CompareCraftId);
- //
- // for(int i=0; i<list.Count; i++)
- // {
- // BattleObject bo = list[i];
- // if(firstTarget == null)
- // {
- // firstTarget = bo;
- // }
- //
- // if(cameraLockedTarget != null && cameraLockedTarget is BattleObject)
- // {
- // BattleObject lockedObj = cameraLockedTarget as BattleObject;
- // if(bo.id > 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<CraftAI>().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<Craft>();
- if(craft == null)
- {
- Debuger.LogError("craft is null");
- return;
- }
- if (craft.team != battleController.GetCtrlCraft().team) {
- battleController.GetCtrlCraft().target = craft;
- battleController.GetCtrlCraft().GetComponent<CraftAI> ().followTarget = true;
- }
- }
- }
- }
- public void LockNextCraft()
- {
- if (state != State.Battle)
- return;
- if (cameraLockedTarget == null || !(cameraLockedTarget is Craft) || (cameraLockedTarget as Craft).IsDead ())
- cameraLockedTarget = null;
- List<Player> playerList = battleController.GetBattleSession ().GetPlayerList ();
- Craft firstCraft = null;
- for(int i=0; i<playerList.Count; i++)
- {
- Player player = playerList [i];
- if (player.GetHero ().GetCraft () == null)
- continue;
- if (firstCraft == null)
- firstCraft = player.GetHero ().GetCraft ();
- if (cameraLockedTarget != null) {
- if(cameraLockedTarget == player.GetHero().GetCraft())
- {
- cameraLockedTarget = null;
- }
- } else {
- cameraLockedTarget = player.GetHero ().GetCraft ();
- break;
- }
- }
- if (cameraLockedTarget == null) {
- cameraLockedTarget = firstCraft;
- }
- }
- private void zoom(float value)
- {
- if(state == State.Battle)
- {
- targetDistance += value*10f;
- targetDistance = NumberUtil.forceBetween(targetDistance, MIN_SIZE, MAX_SIZE);
- }
- }
-
- }
|