BoxList.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package me.hammerle.snuviengine.game;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. public class BoxList
  5. {
  6. private static final double MAX_SIZE = 1;
  7. public static class Node
  8. {
  9. private Node next;
  10. private Node previous;
  11. private Entity ent;
  12. private int x;
  13. private int y;
  14. }
  15. private final double boxMinX;
  16. private final double boxMinY;
  17. private final double boxMaxX;
  18. private final double boxMaxY;
  19. private final double size;
  20. private final Node[][] nodes;
  21. private Node freeNodes = null;
  22. private final int partionX;
  23. private final int partionY;
  24. public int amount = 0;
  25. public BoxList(double x1, double y1, double x2, double y2, double size)
  26. {
  27. this.size = size;
  28. boxMinX = Math.min(x1, x2);
  29. boxMinY = Math.min(y1, y2);
  30. partionX = (int) Math.ceil((Math.max(x1, x2) - boxMinX) / size);
  31. partionY = (int) Math.ceil((Math.max(y1, y2) - boxMinX) / size);
  32. boxMaxX = partionX * size;
  33. boxMaxY = partionY * size;
  34. System.out.println(boxMinX + " " + boxMaxX);
  35. System.out.println(boxMinY + " " + boxMaxY);
  36. System.out.println(partionX + " " + partionY);
  37. nodes = new Node[partionX][partionY];
  38. for(int i = 0; i < 10000; i++)
  39. {
  40. Node n = new Node();
  41. n.next = freeNodes;
  42. freeNodes = n;
  43. }
  44. }
  45. public Node add(Entity ent)
  46. {
  47. int x = Math.max(Math.min((int) (((ent.xPos + ent.width / 2) + boxMinX) / size), partionX - 1), 0);
  48. int y = Math.max(Math.min((int) (((ent.yPos + ent.height / 2) + boxMinY) / size), partionY - 1), 0);
  49. amount++;
  50. Node n = freeNodes;
  51. freeNodes = n.next;
  52. n.next = null;
  53. n.ent = ent;
  54. n.x = x;
  55. n.y = y;
  56. if(nodes[x][y] == null)
  57. {
  58. nodes[x][y] = n;
  59. }
  60. else
  61. {
  62. n.next = nodes[x][y];
  63. nodes[x][y].previous = n;
  64. nodes[x][y] = n;
  65. }
  66. return n;
  67. }
  68. public void remove(Node n)
  69. {
  70. if(n.previous != null)
  71. {
  72. n.previous.next = n.next;
  73. }
  74. else
  75. {
  76. nodes[n.x][n.y] = n.next;
  77. }
  78. if(n.next != null)
  79. {
  80. n.next.previous = n.previous;
  81. }
  82. n.previous = null;
  83. n.ent = null;
  84. n.next = freeNodes;
  85. freeNodes = n;
  86. amount--;
  87. }
  88. public List<Entity> getAllEntitiesAt(Entity not, double minX, double minY, double maxX, double maxY)
  89. {
  90. int sx = Math.max(Math.min((int) ((minX + boxMinX - MAX_SIZE) / size), partionX - 1), 0);
  91. int sy = Math.max(Math.min((int) ((minY + boxMinY - MAX_SIZE) / size), partionY - 1), 0);
  92. int ex = Math.max(Math.min((int) Math.ceil((maxX + boxMinX + MAX_SIZE) / size), partionX - 1), 0);
  93. int ey = Math.max(Math.min((int) Math.ceil((maxY + boxMinY + MAX_SIZE) / size), partionY - 1), 0);
  94. List<Entity> list = new LinkedList<>();
  95. for(int x = sx; x <= ex; x++)
  96. {
  97. for(int y = sy; y <= ey; y++)
  98. {
  99. Node n = nodes[x][y];
  100. while(n != null)
  101. {
  102. if(not != n.ent && n.ent.isColliding(minX, minY, maxX, maxY))
  103. {
  104. list.add(n.ent);
  105. }
  106. n = n.next;
  107. }
  108. }
  109. }
  110. return list;
  111. }
  112. }