//========================================================== // // 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 _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(); _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(); 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(); if (objCtrl != null) { objCtrl.RotateCube(); } } } public void ChangeColorAll() { foreach (var item in _dictionaryObj) { InteractionObj objCtrl = item.Value.GetComponent(); if (objCtrl != null) { objCtrl.ChangeColor(); } } } public void LockColorAll(bool isLock) { foreach (var item in _dictionaryObj) { InteractionObj objCtrl = item.Value.GetComponent(); if (objCtrl != null) { objCtrl.LockColor(isLock); } } } public void AddAll() { foreach (var item in _dictionaryObj) { InteractionObj objCtrl = item.Value.GetComponent(); if (objCtrl != null) { objCtrl.Add(); } } } public void SubAll() { foreach (var item in _dictionaryObj) { InteractionObj objCtrl = item.Value.GetComponent(); if (objCtrl != null) { objCtrl.Sub(); } } } /// /// 松开时查找离得最近的对象,如果其距离小于0.1则交换位置,否则回到原位置 /// /// 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(); 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); } } }