QuadtreeBroadphase.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #if DUNGEN_QUADTREE
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using UnityEngine;
  6. namespace DunGen.Collision
  7. {
  8. [Serializable]
  9. [DisplayName("Quadtree")]
  10. public class QuadtreeBroadphaseSettings : BroadphaseSettings
  11. {
  12. public Bounds InitialBounds = new Bounds(Vector3.zero, new Vector3(100, 10, 100));
  13. public override ICollisionBroadphase Create()
  14. {
  15. return new QuadtreeBroadphase();
  16. }
  17. }
  18. public class QuadtreeBroadphase : ICollisionBroadphase
  19. {
  20. public Quadtree<Bounds> Quadtree { get; private set; }
  21. public void Init(BroadphaseSettings settings, DungeonGenerator dungeonGenerator)
  22. {
  23. if (!(settings is QuadtreeBroadphaseSettings quadtreeSettings))
  24. throw new ArgumentException("Settings must be of type QuadtreeSettings");
  25. Bounds initialBounds = quadtreeSettings.InitialBounds;
  26. // Override initial bounds to be the maximum bounds of the dungeon if applicable
  27. if (dungeonGenerator.RestrictDungeonToBounds)
  28. initialBounds = dungeonGenerator.TilePlacementBounds;
  29. Quadtree = new Quadtree<Bounds>(initialBounds, (b) => b);
  30. }
  31. public void Insert(Bounds bounds)
  32. {
  33. Quadtree.Insert(bounds);
  34. }
  35. public void Query(Bounds bounds, ref List<Bounds> results)
  36. {
  37. Quadtree.Query(bounds, ref results);
  38. }
  39. public void Remove(Bounds bounds)
  40. {
  41. Quadtree.Remove(bounds);
  42. }
  43. public void DrawDebug(float duration = 0)
  44. {
  45. Quadtree.DrawDebug(duration);
  46. }
  47. }
  48. }
  49. #endif