SpatialHashBroadphase.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using UnityEngine;
  5. namespace DunGen.Collision
  6. {
  7. [Serializable]
  8. [DisplayName("Spatial Hashing")]
  9. public class SpatialHashBroadphaseSettings : BroadphaseSettings
  10. {
  11. public float CellSize = 40.0f;
  12. public override ICollisionBroadphase Create()
  13. {
  14. return new SpatialHashBroadphase();
  15. }
  16. }
  17. public class SpatialHashBroadphase : ICollisionBroadphase
  18. {
  19. public SpatialHashGrid<Bounds> SpatialHashGrid { get; private set; }
  20. public void Init(BroadphaseSettings settings, DungeonGenerator dungeonGenerator)
  21. {
  22. if (!(settings is SpatialHashBroadphaseSettings spatialHashSettings))
  23. return;
  24. SpatialHashGrid = new SpatialHashGrid<Bounds>(
  25. spatialHashSettings.CellSize,
  26. (b) => b,
  27. dungeonGenerator.UpDirection);
  28. }
  29. public void Insert(Bounds bounds)
  30. {
  31. SpatialHashGrid.Insert(bounds);
  32. }
  33. public void Query(Bounds bounds, ref List<Bounds> results)
  34. {
  35. SpatialHashGrid.Query(bounds, ref results);
  36. }
  37. public void Remove(Bounds bounds)
  38. {
  39. SpatialHashGrid.Remove(bounds);
  40. }
  41. public void DrawDebug(float duration = 0)
  42. {
  43. SpatialHashGrid.DrawDebug(duration);
  44. }
  45. }
  46. }