123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610 |
- 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
- {
- /// <summary>
- /// 容器抽象类
- /// </summary>
- public abstract class ArtContainerHandler : MonoBehaviour, ArtContainer
- {
- public ArtType artType;
- private ArtInfo info;
- //private GoodState goodsState = GoodState.Init;
- /// <summary>
- /// 同步时不会传递初始化符合
- /// </summary>
- private TransferState transferState = TransferState.Init;
- public ArtInfo Info { get => info; set => info = value; }
- public float smoothTween = 3f;
- /// <summary>
- /// 保存的向服务器发送数据的对象
- /// </summary>
- 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; }
- /// <summary>
- /// 虚假的定义
- /// </summary>
- public virtual TransferCell Transfer { get => null; set { } }
- /// <summary>
- /// 物体的状态
- /// 同步服务器的状态
- /// </summary>
- 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();
- }
- /// <summary>
- /// 添加美术资源到容器中
- /// </summary>
- /// <param name="art"></param>
- /// <param name="info"></param>
- 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<ArtTaskChild>();
- //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<ArtTaskAction>();
- //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;
- //}
- }
- }
- /// <summary>
- /// 销毁此物体
- /// </summary>
- public virtual void DestroyArt()
- {
- if (ArtComponent != null)
- {
- ArtComponent.DestroyArt();
- }
- Destroy(gameObject);
- }
- /// <summary>
- /// 获取资源的类型
- /// </summary>
- /// <returns></returns>
- public virtual ArtType GetArtType()
- {
- return artType;
- }
- /// <summary>
- /// 获取此info的信息
- /// </summary>
- /// <returns></returns>
- public virtual GoodsInfo GetGoodsInfo()
- {
- return this.data;
- }
- /// <summary>
- /// 获取实例化的数据信息
- /// </summary>
- /// <returns></returns>
- public virtual GameObject GetInstace()
- {
- return gameObject;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="isShow"></param>
- 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<Tween>(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<Tween>(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<Tween>(Ease.OutCubic);
- }
- if (!isClone)
- {
- Vector3 startScale = endScale / 2f;
- transform.localScale = startScale;
- }
- transform.DOScale(endScale, 0.8f).SetEase<Tween>(Ease.OutCubic).SetDelay(0.2f).OnComplete(() =>
- {
- TransferState = TransferState.Over;
- TransferSyn(data, false);
- // transform.DOKill();
- }).OnKill(() =>
- {
- TransferState = TransferState.Over;
- TransferSyn(data, false);
- });
- }
- }
- /// <summary>
- /// 传输同步信息
- /// </summary>
- /// <param name="state"></param>
- /// <param name="status"></param>
- 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);
- }
- }
- }
- }
- /// <summary>
- /// 同步物体状态
- /// 初始化状态时,不同步
- /// 当物体处于catch或者release状态时进行同步
- /// </summary>
- /// <param name="goods"></param>
- /// <param name="smooth"></param>
- 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
- // {
- // }
- //}
- }
- /// <summary>
- /// 锁定此容器
- /// 将此容器设置为不可编辑状态,取消此容器的选中效果等等
- /// </summary>
- public virtual void LockContainer()
- {
- }
- /// <summary>
- /// 解锁此容器
- /// 将此容器设置为可编辑状态
- /// </summary>
- public virtual void UnLockContainer()
- {
- }
- /// <summary>
- /// 当处于Release状态时可被编辑(删除)
- /// </summary>
- /// <returns></returns>
- 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<Texture> load)
- {
- if (handler != null)
- {
- handler.GetIcon(load);
- }
- else
- {
- load?.Invoke(null);
- }
- }
- public virtual void SetArtHandler(ArtHandler handler)
- {
- this.handler = handler;
- }
- /// <summary>
- /// 设置此容器的选中状态
- /// </summary>
- 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;
- }
- /// <summary>
- /// 计算位置信息
- /// </summary>
- public virtual void ResetEditorPosition()
- {
- }
- /// <summary>
- /// 后退
- /// </summary>
- /// <returns></returns>
- public virtual bool GoBack()
- {
- return false;
- }
- /// <summary>
- /// 前进
- /// </summary>
- /// <returns></returns>
- public virtual bool GoAhead()
- {
- return false;
- }
- /// <summary>
- /// 后退的步骤数量
- /// </summary>
- /// <returns></returns>
- public virtual int BackCount()
- {
- return 0;
- }
- /// <summary>
- /// 前进的步骤数量
- /// </summary>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 设置对应的组件
- /// </summary>
- /// <param name="component"></param>
- 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);
- }
- }
- }
|