sp.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // no optimizations, just gitrdone
  2. var GridSP = function(options) {
  3. var defaults = {
  4. worldSize: pt(256,256),
  5. cellSize: 8,
  6. gridSize: pt(32,32),
  7. points: [],
  8. bounds: [],
  9. objs: Object.create(null),
  10. };
  11. var e = $.extend({}, defaults, options);
  12. for(x in e) this[x] = e[x];
  13. this.init();
  14. }
  15. GridSP.prototype.init = function() {
  16. var sz = this.gridSize.x * this.gridSize.y;
  17. for(var i = 0; i < sz; i++) {
  18. // create(null) is to obviate hasOwnProperty
  19. this.bounds[i] = Object.create(null);
  20. this.points[i] = Object.create(null);
  21. };
  22. }
  23. // removes and entity from the grid
  24. GridSP.prototype.removeObj = function( eid) {
  25. var l = this.objs[eid];
  26. for(var j = 0; j < l.length; j++) {
  27. var i = l[j];
  28. delete this.points[i][eid];
  29. delete this.bounds[i][eid];
  30. }
  31. delete this.objs[eid];
  32. }
  33. // use this one to add an entity
  34. GridSP.prototype.addObj = function(eid) {
  35. var aabb = game.getComp(eid, 'aabb');
  36. var pos = game.getComp(eid, 'position');
  37. this.insert(translateRect(aabb, pos), ptc(pos), eid);
  38. }
  39. // blindly adds, internal use only
  40. GridSP.prototype.insert = function(rect, p, eid) {
  41. // FIXME: min/max tests for edges
  42. var minx = floor(min(rect.l, rect.r) / this.cellSize);
  43. var maxx = ceil(max(rect.l, rect.r) / this.cellSize);
  44. var miny = floor(min(rect.t, rect.b) / this.cellSize);
  45. var maxy = ceil(max(rect.t, rect.b) / this.cellSize);
  46. var l = [];
  47. for(var y = miny; y <= maxy; y++) {
  48. for(var x = minx; x <= maxx; x++) {
  49. var i = x + y * this.gridSize.x;
  50. this.points[i][eid] = p;
  51. this.bounds[i][eid] = rect;
  52. l.push(i);
  53. }
  54. }
  55. this.objs[eid] = l;
  56. }
  57. // center to center, for things that are small enough for the bounds to not matter
  58. GridSP.prototype.rangeRadiusFast = function(qp, rad) {
  59. // FIXME: min/max tests for edges
  60. var minx = floor((qp.x - rad) / this.cellSize);
  61. var maxx = ceil((qp.x + rad) / this.cellSize);
  62. var miny = floor((qp.y - rad) / this.cellSize);
  63. var maxy = ceil((qp.y + rad) / this.cellSize);
  64. var l = [];
  65. for(var y = miny; y <= maxy; y++) {
  66. for(var x = minx; x <= maxx; x++) {
  67. var i = x + y * this.gridSize.x;
  68. for(eid in this.points[i]) {
  69. if(circleIntersect(qp, rad, this.points[i][eid], 0)) {
  70. l.push(eid);
  71. }
  72. }
  73. }
  74. }
  75. return l;
  76. }
  77. // this one runs as a system
  78. GridSP.prototype.updatePositions = function() {
  79. var comps = ['position', 'nextpos'];
  80. runSystem(game.c, comps, function(ent) {
  81. // there should be some extra cleverness in this function. simplicity for now.
  82. // nothing in the grid actually moves atm
  83. });
  84. }