RandomAllMap.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, GameObjectPool> _allGameObjectPools = new Map<int, GameObjectPool>();
  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.own.transform.position;
  24. if (Vector3.Distance(pos, objetcPos) > 300)
  25. {
  26. removeKey.Add(_allGameObjectPools.Key);
  27. GObjectPool.Instance.Recycle(_allGameObjectPools.Value);
  28. // _allGameObjectPools.Value.SetActive(false);
  29. }
  30. }
  31. for (int i = 0; i < removeKey.Count; i++)
  32. {
  33. _allGameObjectPools.Remove(removeKey[i]);
  34. }
  35. float radius = sceneMonoConfig.radius;
  36. int x = (int)(pos.x / radius);
  37. int z = (int)(pos.z / radius);
  38. float xStartInit = x;
  39. float zStartInit = z;
  40. for (int i = -3; i <= 3; i++)
  41. {
  42. for (int j = -3; j <= 3; j++)
  43. {
  44. // if (i == 0 && j == 0)
  45. // {
  46. // continue;
  47. // }
  48. float currX = (xStartInit + i) * radius;
  49. float currZ = (zStartInit + j) * radius;
  50. int key = (int)(currX * 1000 + currZ);
  51. if (_allGameObjectPools.ContainsKey(key))
  52. {
  53. continue;
  54. }
  55. GameObjectPool gameObjectPool = GetObejct();
  56. float y = Random.Range(-2, 0);
  57. gameObjectPool.own.transform.position = new Vector3(currX, y, currZ);
  58. _allGameObjectPools.Add(key, gameObjectPool);
  59. }
  60. }
  61. }
  62. private GameObjectPool GetObejct()
  63. {
  64. int index = Random.Range(0, sceneMonoConfig.allObject.Length);
  65. GameObject gameObject = sceneMonoConfig.allObject[index];
  66. // GameObject go = GameObject.Instantiate(gameObject);
  67. GameObjectPool gameObjectPool =
  68. GObjectPool.Instance.FetchAsyncForGameObject<GameObjectPool>(gameObject, gameObject.name);
  69. return gameObjectPool;
  70. }
  71. }
  72. }