123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- using System.Collections.Generic;
- using UnityEngine;
- using SwipeSystems;
- public class Navigation : MonoBehaviour
- {
- public static Navigation Instance = null;
- [SerializeField] private Transform mainCamera;
- [SerializeField] private Transform cameraPositionTarget;
- [SerializeField] private float lLimit;
- [SerializeField] private float rLimit;
- [SerializeField] private float speed;
- [SerializeField] private float slerpSpeed;
- [SerializeField] private Vector2 offSet;
- public SwipeSystem swipeSystem;
- public Material mat;
- [SerializeField] private List<Transform> currentNpcs = new List<Transform>();
- [SerializeField] private float ZoomTarget = 2.5f;
- private bool ZoomCam;
- private bool isZoom = false;
- private float ZoomDefault
- {
- get
- {
- return 4.5f;
- }
- }
- private float currZoom;
- private float cameraZoom;
- private float time;
- private Vector3 startPos;
- [SerializeField] private float fadeBackGround = 0.001f;
- private float fadeDefault
- {
- get
- {
- return 0;
- }
- }
- private float currFade;
- private float offSetHeight;
- private float offSetWidth = 1;
- private float scale;
- public Vector3 StartPos
- {
- get
- {
- return startPos;
- }
- }
- public bool IsZoom
- {
- get
- {
- return isZoom;
- }
- }
- private void Awake()
- {
- Instance = this;
- float h = Screen.height;
- float w = Screen.width;
- scale = w / h;
- }
- private void Start()
- {
- ConversationManager.instance.onChangeDialog += GetDialogNpc;
- startPos = cameraPositionTarget.localPosition;
- }
- private void OnDisable()
- {
- ConversationManager.instance.onChangeDialog -= GetDialogNpc;
- }
- public void SetLimitLocation(Transform _L, Transform _R, float _startPostX)
- {
- if (scale < 1.4f)
- {
- offSetWidth = 2.8f / scale;
- }
- else if (scale > 1.8f)
- {
- offSetWidth = -2.3f / scale;
- _startPostX = 0;
- }
- else
- {
- offSetWidth = 0;
- }
- lLimit = _L.position.x - (offSetWidth);
- rLimit = _R.position.x + offSetWidth;
- if (lLimit > 0)
- {
- lLimit = 0;
- }
- if (rLimit < 0)
- {
- rLimit = 0;
- }
- cameraPositionTarget.position = new Vector3(_startPostX, cameraPositionTarget.position.y, cameraPositionTarget.position.z); ;
- }
- void Update ()
- {
- if (swipeSystem.Dragged())
- {
- cameraPositionTarget.Translate(new Vector3(-swipeSystem.SwipeDirection().x * speed, 0, 0));
- if (cameraPositionTarget.position.x < lLimit)
- {
- cameraPositionTarget.position = new Vector3(lLimit, cameraPositionTarget.position.y, cameraPositionTarget.position.z);
- }
- if (cameraPositionTarget.position.x > rLimit)
- {
- cameraPositionTarget.position = new Vector3(rLimit, cameraPositionTarget.position.y, cameraPositionTarget.position.z);
- }
- }
- ZoomCamera();
- SetMaterial();
- mainCamera.position = Vector3.Lerp(mainCamera.position, cameraPositionTarget.position, Time.deltaTime * slerpSpeed);
- }
- private void ZoomCamera()
- {
- if(ZoomCam)
- {
- time += Time.deltaTime / 0.5f;
- mainCamera.GetComponent<Camera>().orthographicSize = Mathf.SmoothStep(cameraZoom, currZoom, time);
- if(time > 1)
- {
- ZoomCam = false;
- }
- }
- }
- private void GetDialogNpc(DataTools.Dialog _dialog)
- {
- SetCameraPosition(Characters.Instance.GetCurrentNpc(_dialog.NpcId));
- }
- private void SetCameraPosition(Transform _posNpc)
- {
- if(!isZoom)
- {
- startPos = cameraPositionTarget.localPosition;
- }
- cameraZoom = ZoomDefault;
- if (_posNpc != null)
- {
- currZoom = ZoomTarget * _posNpc.localScale.y;
- cameraPositionTarget.position = new Vector3((_posNpc.position.x + (offSet.x * _posNpc.localScale.y)), (_posNpc.position.y + (offSet.y * _posNpc.localScale.y)), cameraPositionTarget.position.z);
- ZoomCam = true;
- isZoom = true;
- currFade = fadeBackGround;
- }
- else
- {
- FinishCam();
- }
- }
- void SetMaterial()
- {
- mat.SetFloat("_BlurDistance", currFade);
- }
- public void FinishCam()
- {
- time = 0;
- cameraPositionTarget.localPosition = startPos;
- cameraZoom = currZoom;
- currZoom = ZoomDefault;
- ZoomCam = true;
- isZoom = false;
- currFade = fadeDefault;
- }
- public void SetCurrentNpcs(Transform _Npc)
- {
- currentNpcs.Add(_Npc);
- }
- public void ClearCurrentNpcs()
- {
- currentNpcs.Clear();
- }
- }
|