123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- //==========================================================
- //
- // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd.
- // All rights reserved.
- //
- //==========================================================
- using System.Collections;
- using System.Collections.Generic;
- using GxrSdk;
- using UnityEngine;
- public class InteractionObjManager : MonoBehaviour
- {
- public static InteractionObjManager Instance { get; private set; }
- private Dictionary<int, GameObject> _dictionaryObj = null;
- private GameObject _Obj = null;
- public static Vector3 FirstV3 = new Vector3(-0.6f, 0.2f, 3);
- public static float IntervalX = 0.3f;
- public static float IntervalY = -0.28f;
- private int _rowNum = 2;
- private int _columnNum = 3;
- private int _curCreateIndex = 0;
- private void Awake()
- {
- Instance = this;
- }
- private void OnDestroy()
- {
- Instance = null;
- }
- void Start()
- {
- _dictionaryObj = new Dictionary<int, GameObject>();
- _Obj = Resources.Load("Prefabs/InteractionObj") as GameObject;
- if (_Obj == null)
- {
- GxrDebug.LogError("Cannot find Prefabs/InteractionObj.");
- }
- InitObjs();
- }
- private void InitObjs()
- {
- for (int i = 0; i < _rowNum; ++i)
- {
- for (int j = 0; j < _columnNum; ++j)
- {
- CreateObj(i, j);
- }
- }
- }
- public void DelayToCreateObj(int row, int column)
- {
- StartCoroutine(DelayToCreate(row, column));
- }
- private IEnumerator DelayToCreate(int row, int column)
- {
- yield return new WaitForSeconds(0.1f);
- CreateObj(row, column);
- }
- public void CreateObj(int row, int column)
- {
- if ((row < 0) || (column < 0))
- {
- return;
- }
- GameObject o = Instantiate(_Obj);
- o.transform.parent = transform;
- InteractionObj objCtrl = o.GetComponent<InteractionObj>();
- objCtrl.Init(row, column, _curCreateIndex);
- int key = _curCreateIndex;
- _dictionaryObj.Add(key, o);
- ++_curCreateIndex;
- }
- public void DestroyObj(InteractionObj obj)
- {
- GameObject o = null;
- int key = obj.Index;
- if (_dictionaryObj.TryGetValue(key, out o))
- {
- _dictionaryObj.Remove(key);
- o.transform.parent = null;
- Destroy(o);
- }
- }
- private int GetObjKey(int row, int column)
- {
- return (row * _columnNum + column);
- }
- public static Vector3 GetObjectLocalPosition(int row, int column)
- {
- float tmpX = FirstV3.x + IntervalX * column;
- float tmpY = FirstV3.y + IntervalY * row;
- return new Vector3(tmpX, tmpY, FirstV3.z);
- }
- public void ScaleAll(float scale)
- {
- float x = 1 + scale;
- Vector3 newScale = new Vector3(x, x, 1);
- transform.localScale = newScale;
- }
- public void RotateAll()
- {
- foreach (var item in _dictionaryObj)
- {
- InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
- if (objCtrl != null)
- {
- objCtrl.RotateCube();
- }
- }
- }
- public void ChangeColorAll()
- {
- foreach (var item in _dictionaryObj)
- {
- InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
- if (objCtrl != null)
- {
- objCtrl.ChangeColor();
- }
- }
- }
- public void LockColorAll(bool isLock)
- {
- foreach (var item in _dictionaryObj)
- {
- InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
- if (objCtrl != null)
- {
- objCtrl.LockColor(isLock);
- }
- }
- }
- public void AddAll()
- {
- foreach (var item in _dictionaryObj)
- {
- InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
- if (objCtrl != null)
- {
- objCtrl.Add();
- }
- }
- }
- public void SubAll()
- {
- foreach (var item in _dictionaryObj)
- {
- InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
- if (objCtrl != null)
- {
- objCtrl.Sub();
- }
- }
- }
- /// <summary>
- /// 松开时查找离得最近的对象,如果其距离小于0.1则交换位置,否则回到原位置
- /// </summary>
- /// <param name="obj"></param>
- public void DropObj(InteractionObj dropObj)
- {
- float maxDis = 1000;
- InteractionObj nearestObj = null;
- int dropRow = dropObj.Row;
- int dropColumn = dropObj.Column;
- foreach (var item in _dictionaryObj)
- {
- InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
- if ((objCtrl != null) && ((objCtrl.Row != dropRow) || (objCtrl.Column != dropColumn)))
- {
- float distance = Vector3.Distance(dropObj.transform.localPosition, objCtrl.transform.localPosition);
- if (distance < maxDis)
- {
- maxDis = distance;
- nearestObj = objCtrl;
- }
- }
- }
- if ((maxDis < 0.1f) && !nearestObj.IsMoving && !nearestObj.IsMoving)
- {
- if (nearestObj.Number == dropObj.Number)
- {
- DestroyObj(dropObj);
- DelayToCreateObj(dropRow, dropColumn);
- DestroyObj(nearestObj);
- DelayToCreateObj(nearestObj.Row, nearestObj.Column);
- }
- else
- {
- dropObj.SwapPosition(nearestObj.Row, nearestObj.Column);
- nearestObj.SwapPosition(dropRow, dropColumn);
- }
- }
- else
- {
- dropObj.SwapPosition(dropRow, dropColumn);
- }
- }
- }
|