RandomAllMap.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections.Generic;
  2. using Fort23.UTool;
  3. using UnityEngine;
  4. using Utility;
  5. namespace GameLogic.Combat.CombatTool.SceneTool
  6. {
  7. public class RandomAllMap
  8. {
  9. private Transform root;
  10. private SceneMonoConfig sceneMonoConfig;
  11. private Map<int, GameObject> _allGameObjectPools = new Map<int, GameObject>();
  12. public void Init(Transform root, SceneMonoConfig sceneMonoConfig)
  13. {
  14. this.root = root;
  15. this.sceneMonoConfig = sceneMonoConfig;
  16. }
  17. public void Update()
  18. {
  19. Vector3 pos = root.position;
  20. List<int> removeKey = new List<int>();
  21. for (_allGameObjectPools.Begin(); _allGameObjectPools.Next();)
  22. {
  23. Vector3 objetcPos = _allGameObjectPools.Value.transform.position;
  24. if (Vector3.Distance(pos, objetcPos) > 300)
  25. {
  26. removeKey.Add(_allGameObjectPools.Key);
  27. _allGameObjectPools.Value.SetActive(false);
  28. }
  29. }
  30. for (int i = 0; i < removeKey.Count; i++)
  31. {
  32. _allGameObjectPools.Remove(removeKey[i]);
  33. }
  34. float radius = sceneMonoConfig.radius;
  35. int x = (int)(pos.x / radius);
  36. int z = (int)(pos.z / radius);
  37. float xStartInit = x;
  38. float zStartInit = z;
  39. for (int i = -3; i <= 3; i++)
  40. {
  41. for (int j = -3; j <= 3; j++)
  42. {
  43. // if (i == 0 && j == 0)
  44. // {
  45. // continue;
  46. // }
  47. float currX = (xStartInit + i) * radius;
  48. float currZ = (zStartInit + j) * radius;
  49. int key = (int)(currX * 1000 + currZ);
  50. if (_allGameObjectPools.ContainsKey(key))
  51. {
  52. continue;
  53. }
  54. GameObject gameObjectPool = GetObejct();
  55. float y = Random.Range(-2, 0);
  56. gameObjectPool.transform.position = new Vector3(currX, y, currZ);
  57. _allGameObjectPools.Add(key, gameObjectPool);
  58. }
  59. }
  60. }
  61. private GameObject GetObejct()
  62. {
  63. int index = Random.Range(0, sceneMonoConfig.allObject.Length);
  64. GameObject gameObject = sceneMonoConfig.allObject[index];
  65. GameObject go = GameObject.Instantiate(gameObject);
  66. // GameObjectPool gameObjectPool =
  67. // GObjectPool.Instance.FetchAsyncForGameObject<GameObjectPool>(gameObject, gameObject.name);
  68. return go;
  69. }
  70. }
  71. }