using DG.Tweening;
using LitJson;
using PublicTools.Unity;
using ShadowStudio.Model;
using ShadowStudio.Tool;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using XRTool.Util;
using static BoundingBox;
namespace ShadowStudio.Model
{
///
/// 容器抽象类
///
public abstract class ArtContainerHandler : MonoBehaviour, ArtContainer
{
public ArtType artType;
private ArtInfo info;
//private GoodState goodsState = GoodState.Init;
///
/// 同步时不会传递初始化符合
///
private TransferState transferState = TransferState.Init;
public ArtInfo Info { get => info; set => info = value; }
public float smoothTween = 3f;
///
/// 保存的向服务器发送数据的对象
///
public GoodsInfo data = new GoodsInfo();
private ArtHandler handler;
private Transform editorRoot;
public virtual Transform EditorRoot { get => editorRoot; set => editorRoot = value; }
public virtual Vector3 Position
{
get => transform.localPosition; set
{
transform.localPosition = value;
if (Transfer)
{
Transfer.OnRotateStopped();
}
}
}
public virtual Vector3 Angle
{
get => transform.localEulerAngles;
set
{
transform.localEulerAngles = value;
if (Transfer)
{
Transfer.OnRotateStopped();
}
}
}
public virtual Vector3 Scale
{
get => transform.localScale;
set
{
transform.localScale = value;
if (Transfer)
{
Transfer.OnScaleStopped();
}
//SendTransfer(2, (int)TransferState.Stop);
}
}
public virtual float Distance => Vector3.Distance(Camera.main.transform.position, transform.position);
private float alpha = 1;
public virtual float Alpha { get => 1; set => alpha = value; }
private Color goodsColor = Color.white;
private Timer autoUnLockTimer;
private ArtComponent artComponent;
public virtual Color GoodsColor { get => Color.white; set => goodsColor = value; }
///
/// 虚假的定义
///
public virtual TransferCell Transfer { get => null; set { } }
///
/// 物体的状态
/// 同步服务器的状态
///
public TransferState TransferState
{
get => transferState; set
{
if (transferState != value)
{
transferState = value;
///同步开始,锁定物体
if (transferState == TransferState.Start)
{
ClearTimer();
LockContainer();
}
///同步结束,结束锁定
else if (transferState == TransferState.Over)
{
ClearTimer();
UnLockContainer();
}
///姿态同步完成,自动倒计时解除锁定状态
else if (transferState == TransferState.Stop)
{
TransferSyn(data, false);
ClearTimer();
autoUnLockTimer = TimerMgr.Instance.CreateTimer(() =>
{
TransferState = TransferState.Over;
}, 7f);
}
}
}
}
public ArtComponent ArtComponent { get => artComponent; }
private void ClearTimer()
{
if (autoUnLockTimer != null && TimerMgr.Instance != null)
{
autoUnLockTimer = TimerMgr.Instance.DestroyTimer(autoUnLockTimer);
}
}
public virtual void OnDisable()
{
ClearTimer();
}
///
/// 添加美术资源到容器中
///
///
///
public virtual void AddToContainer(UnityEngine.Object art, ArtInfo info)
{
this.Info = info;
name = name + "_" + info.ArtName;
//if (ArtComponent != null)
//{
// ArtComponent.SetContainer(this, art, info);
//}
Debug.Log("添加AddToContainer");
if (art is GameObject)
{
GameObject obj = art as GameObject;
//artTaskChild = obj.GetComponentInChildren();
//if (artTaskChild != null)
//{
// XBoundingBox xBox = Transfer.BoundBox;
// xBox.ActiveHandle = HandleType.Rotation;
// xBox.ShowRotationHandleForX = false;
// xBox.ShowRotationHandleForY = true;
// xBox.ShowRotationHandleForZ = false;
// xBox.ActiveAxis = AxisType.None;
// artTaskChild.artContainerHandler = this;
//}
//artTaskAction = obj.GetComponentInChildren();
//if (artTaskAction != null)
//{
// XBoundingBox xBox = Transfer.BoundBox;
// xBox.ActiveHandle = HandleType.None;
// xBox.ShowRotationHandleForX = false;
// xBox.ShowRotationHandleForY = false;
// xBox.ShowRotationHandleForZ = false;
// xBox.ActiveAxis = AxisType.None;
// artTaskAction.artContainerHandler = this;
//}
}
}
///
/// 销毁此物体
///
public virtual void DestroyArt()
{
if (ArtComponent != null)
{
ArtComponent.DestroyArt();
}
Destroy(gameObject);
}
///
/// 获取资源的类型
///
///
public virtual ArtType GetArtType()
{
return artType;
}
///
/// 获取此info的信息
///
///
public virtual GoodsInfo GetGoodsInfo()
{
return this.data;
}
///
/// 获取实例化的数据信息
///
///
public virtual GameObject GetInstace()
{
return gameObject;
}
///
///
///
///
public virtual void SetArtActive(bool isShow)
{
gameObject.SetActive(isShow);
}
public virtual void PlayEffect()
{
StartCoroutine(ContainerEffect());
}
private IEnumerator ContainerEffect()
{
while (string.IsNullOrEmpty(data.goods_info))
{
yield return new WaitForFixedUpdate();
}
if (!string.IsNullOrEmpty(data.goods_info))
{
transform.DOKill();
Posture posture = UnityUtil.GetPosture(data.goods_info);
Vector3 endPos = Camera.main.transform.TransformPoint(posture.position);
bool isClone = false;
if (data.status == (int)TransferState.Clone)
{
isClone = true;
data.status = (int)TransferState.Init;
}
Vector3 endScale = posture.scale;
if (!isClone)
{
//if (IconItem.ItemPos != Vector3.zero)
//{
// // Vector3 startPos = GameSession.Instance.GetHeadForwadPos(info.Distance / 5f, false);
// Vector3 startPos = IconItem.ItemPos;
// transform.position = startPos;
// transform.DOMove(endPos, 1f).SetEase(Ease.OutCubic);
// IconItem.ItemPos = Vector3.zero;
//}
//else if (ItemView.itemPos != Vector3.zero)
//{
// // Vector3 startPos = GameSession.Instance.GetHeadForwadPos(info.Distance / 5f, false);
// Vector3 startPos = ItemView.itemPos;
// transform.position = startPos;
// transform.DOMove(endPos, 1f).SetEase(Ease.OutCubic);
// IconItem.ItemPos = Vector3.zero;
//}
//else
//{
transform.position = endPos;
//}
}
else
{
transform.localEulerAngles = posture.angle;
transform.localScale = posture.scale;
Vector3 startPos = endPos - Camera.main.transform.right * 0.5f;
transform.position = startPos;
transform.DOMove(endPos, 1f).SetEase(Ease.OutCubic);
}
if (!isClone)
{
Vector3 startScale = endScale / 2f;
transform.localScale = startScale;
}
transform.DOScale(endScale, 0.8f).SetEase(Ease.OutCubic).SetDelay(0.2f).OnComplete(() =>
{
TransferState = TransferState.Over;
TransferSyn(data, false);
// transform.DOKill();
}).OnKill(() =>
{
TransferState = TransferState.Over;
TransferSyn(data, false);
});
}
}
///
/// 传输同步信息
///
///
///
public virtual void SendTransfer(int state, int status)
{
if (!string.IsNullOrEmpty(data.goods_info))
{
data.goods_info = UnityUtil.TransferToString(transform, state);
data.status = status;
if (status == (int)TransferState.Stop)
{
ResetEditorPosition();
//GameNode.Instance.PageGoods[data.id] = data;
}
if (Info != null)
{
///单人模式下,减少数据传输
if (ArtInfoMgr.Instance.IsSingleMan() && status == (int)TransferState.Doing)
{
return;
}
if (Info.ImmediateSyn)
{
ArtInfoMgr.Instance.SendGoodsTransfer(data);
}
else
{
ArtInfoMgr.Instance.SendTransfer(data);
}
}
}
}
///
/// 同步物体状态
/// 初始化状态时,不同步
/// 当物体处于catch或者release状态时进行同步
///
///
///
public virtual void TransferSyn(GoodsInfo goods, bool smooth = true)
{
//if (!string.IsNullOrEmpty(goods.goods_info))
//{
// data = goods;
// Posture posture = UnityUtil.GetPosture(goods.goods_info);
// if (TransferState != TransferState.Init)
// {
// if ((TransferState)goods.status != TransferState.Init)
// {
// TransferState = (TransferState)goods.status;
// }
// if (TransferState == TransferState.Stop)
// {
// smooth = false;
// GameNode.Instance.PageGoods[data.id] = data;
// }
// else if (TransferState == TransferState.Over)
// {
// if (ArtComponent != null)
// {
// ArtComponent.TransferSyn(goods, smooth);
// }
// return;
// }
// if (smooth)
// {
// if (posture.count >= 0)
// {
// transform.localPosition = Vector3.Lerp(transform.localPosition, posture.position, (Time.deltaTime * smoothTween));
// }
// if (posture.count >= 1)
// {
// Quaternion q = Quaternion.Euler(posture.angle);
// transform.localRotation = Quaternion.Lerp(transform.localRotation, q, (Time.deltaTime * smoothTween));
// }
// if (posture.count >= 2)
// {
// transform.localScale = Vector3.Lerp(transform.localScale, posture.scale, (Time.deltaTime * smoothTween));
// }
// }
// else
// {
// if (posture.count >= 0)
// {
// transform.DOLocalMove(posture.position, Vector3.Distance(posture.position, transform.position) / (smoothTween * smoothTween));
// }
// if (posture.count >= 1)
// {
// transform.DOLocalRotate(posture.angle, 0.1f);
// }
// if (posture.count >= 2)
// {
// transform.DOScale(posture.scale, 0.1f);
// }
// }
// }
//}
//if (ArtComponent != null)
//{
// ArtComponent.TransferSyn(goods, smooth);
//}
//if (artTaskAction != null)
//{
// JsonData dataInfo = new JsonData();
// try
// {
// dataInfo = JsonMapper.ToObject(data.goods_extended);
// if (dataInfo["task"] != null)
// {
// artTaskAction.showType(dataInfo["task"].ToString());
// }
// }
// catch
// {
// }
//}
}
///
/// 锁定此容器
/// 将此容器设置为不可编辑状态,取消此容器的选中效果等等
///
public virtual void LockContainer()
{
}
///
/// 解锁此容器
/// 将此容器设置为可编辑状态
///
public virtual void UnLockContainer()
{
}
///
/// 当处于Release状态时可被编辑(删除)
///
///
public bool CanEditor()
{
return TransferState == TransferState.Over;
}
public void DelArtSync()
{
//WSHandler.Room.DeleteGood(new int[] { data.id });
}
public virtual Texture GetContainerIcon()
{
if (handler != null)
{
return handler.GetIcon();
}
return null;
}
public virtual void GetContainerIcon(Action load)
{
if (handler != null)
{
handler.GetIcon(load);
}
else
{
load?.Invoke(null);
}
}
public virtual void SetArtHandler(ArtHandler handler)
{
this.handler = handler;
}
///
/// 设置此容器的选中状态
///
public virtual void SetSelect()
{
}
public virtual void UnSetSelect()
{
}
public Transform GetEditorCenter()
{
if (!EditorRoot)
{
var obj = new GameObject(name + "_EditorRoot");
EditorRoot = obj.transform;
UnityUtil.SetParent(transform, EditorRoot);
}
ResetEditorPosition();
return EditorRoot;
}
///
/// 计算位置信息
///
public virtual void ResetEditorPosition()
{
}
///
/// 后退
///
///
public virtual bool GoBack()
{
return false;
}
///
/// 前进
///
///
public virtual bool GoAhead()
{
return false;
}
///
/// 后退的步骤数量
///
///
public virtual int BackCount()
{
return 0;
}
///
/// 前进的步骤数量
///
///
public virtual int AheadCount()
{
return 0;
}
public virtual Vector3 GetClonePoint()
{
var root = GetEditorCenter();
Vector3 point = transform.position;
point = root.InverseTransformPoint(point);
point += Vector3.right * 0.5f;
return root.TransformPoint(point);
}
public virtual void CloneContainer()
{
//Vector3 point = GetClonePoint();
//string datainfo = UnityUtil.TransferToString(GameNode.Instance.transform.InverseTransformPoint(point), transform.localEulerAngles, transform.localScale, 2);
//GoodsInfo tmp = data;
//tmp.id = 0;
//tmp.goods_info = datainfo;
//tmp.status = (int)TransferState.Clone;
//WSHandler.Room.CreateGood(tmp);
//Destroy(tmp);
}
///
/// 设置对应的组件
///
///
public virtual void AddArtComponent(ArtComponent component)
{
this.artComponent = component;
try
{
if (artComponent != null && artComponent.GetInstace())
{
Transform target = artComponent.GetInstace().transform;
if (target)
{
UnityUtil.SetParent(transform, target);
}
}
}
catch (Exception ex)
{
UnityLog.LogError(ex.ToString());
artComponent = null;
}
}
public virtual void UpdateProcess(float process)
{
}
public void ArtState(string type, string info)
{
JsonData dataInfo = new JsonData();
try
{
dataInfo = JsonMapper.ToObject(data.goods_extended);
}
catch
{
}
dataInfo[type] = info;
data.goods_extended = dataInfo.ToJson();
Debug.Log("Art状态信息改变" + data.goods_extended);
SendTransfer(0, (int)TransferState.Stop);
}
}
}