You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using LzFramework.FSM.Msg;
using LzFramework;
namespace DisComputer
{
/*
* 模型控制
* @author lz
*/
public class ModelControl : Singleton<ModelControl>
{
public LayerMask clickableLayer;
//模糊后背景
public SpriteRenderer vagueMask_spr;
// Start is called before the first frame update
void Start()
{
curRotPlatform = destopPlatform;
InitMsg();
}
// Update is called once per frame
void Update()
{
if (MouseControl.Instance.isFocuseState)
return;
MouseThroughCamera();
RotationTarget();
}
/// <summary>
/// 模型角度复原
/// </summary>
public void RewindTargetRoatation()
{
curRotPlatform.transform.DORotate(new Vector3(0, 0, 0), 0.5f);
}
/// <summary>
/// 注册消息
/// </summary>
void InitMsg()
{
MessageDispatcher.AddListener(MsgName.ModelControlShowCenter, OnRecShowPlatform);
MessageDispatcher.AddListener(MsgName.ModelControlShowDesktop, OnRecDesktopPlatform);
}
/// <summary>
/// 展示中心平台
/// </summary>
/// <param name="message"></param>
void OnRecShowPlatform(IMessage message)
{
StartCoroutine(MoveToCenter(dragTarget.transform));
}
/// <summary>
/// 展示桌面平台
/// </summary>
/// <param name="message"></param>
void OnRecDesktopPlatform(IMessage message)
{
StartCoroutine(MoveToDesktop(dragTarget.transform));
}
//拖拽物体
public GameObject dragTarget;
//是否点击到物体
private bool isClickedObj;
private Vector3 screenSpace;
private Vector3 offset;
/// <summary>
/// 鼠标控制
/// </summary>
void MouseThroughCamera()
{
if (Input.GetMouseButtonDown(0))
{
if (ChectClickTarget())
{
screenSpace = Camera.main.WorldToScreenPoint(dragTarget.transform.position);
offset = dragTarget.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
}
}
if (isClickedObj)
{
Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
dragTarget.transform.position = curPosition;
}
if (Input.GetMouseButtonUp(0))
{
isClickedObj = false;
}
}
/// <summary>
/// 检测是否点击目标物体
/// </summary>
/// <returns></returns>
bool ChectClickTarget()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 50, clickableLayer.value))
{
dragTarget = hit.collider.gameObject;
isClickedObj = true;
return true;
}
return false;
}
//旋转速度
public float rotationSpeed;
//桌面旋转平台
public Transform destopPlatform;
//拆装旋转平台
public Transform showPlatform;
//当前旋转平台
private Transform curRotPlatform;
private bool isFree = false;
/// <summary>
/// 旋转目标
/// </summary>
void RotationTarget()
{
if (curRotPlatform == null)
return;
Vector3 fwd = Camera.main.transform.forward;
fwd.Normalize();
if (Input.GetMouseButton(1))
{
Vector3 vaxis = Vector3.Cross(fwd, Vector3.right);
curRotPlatform.Rotate(Vector3.up, -Input.GetAxis("Mouse X")* rotationSpeed, Space.World);
if (isFree)
{
Vector3 haxis = Vector3.Cross(fwd, Vector3.up);
curRotPlatform.Rotate(haxis, -Input.GetAxis("Mouse Y") * rotationSpeed, Space.World);
}
}
}
/// <summary>
/// 移动到中间位置
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
IEnumerator MoveToCenter(Transform target)
{
yield return new WaitForEndOfFrame();
Tween tw_move = target.DOMove(showPlatform.position, 0.5f);
tw_move.SetEase(Ease.OutQuart);
vagueMask_spr.DOFade(1.0f, 0.5f);
tw_move.OnComplete(()=> {
isFree = true;
target.SetParent(showPlatform,true);
curRotPlatform = showPlatform;
destopPlatform.localEulerAngles = Vector3.zero;
});
target.DORotate(new Vector3(0, 300, 0), 0.5f);
}
/// <summary>
/// 移动到桌面位置
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
IEnumerator MoveToDesktop(Transform target)
{
yield return new WaitForEndOfFrame();
Tween tw_move = target.DOMove(destopPlatform.position, 0.5f);
vagueMask_spr.DOFade(0.0f, 0.5f);
tw_move.SetEase(Ease.InSine);
tw_move.OnComplete(() => {
isFree = false;
target.SetParent(destopPlatform, true);
curRotPlatform = destopPlatform;
showPlatform.localEulerAngles = Vector3.zero;
});
target.DORotate(new Vector3(0, -40, 0), 0.5f);
}
}
}