collisions.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var COL_LEFT = 0x0001;
  2. var COL_RIGHT = 0x0002;
  3. var COL_TOP = 0x0004;
  4. var COL_BOTTOM = 0x0008;
  5. var COL_X = COL_LEFT | COL_RIGHT;
  6. var COL_Y = COL_TOP | COL_BOTTOM;
  7. var Collisions = function(options) {
  8. var defaults = {
  9. emptyRects: [],
  10. actors: [],
  11. };
  12. var e = $.extend({}, defaults, options);
  13. for(x in e) this[x] = e[x];
  14. }
  15. // swap the positions
  16. Collisions.prototype.frameMove = function(te) {
  17. for(var i = 0; i < this.actors.length; i++){
  18. this.actors[i].position.x = this.actors[i].nextpos.x;
  19. this.actors[i].position.y = this.actors[i].nextpos.y;
  20. }
  21. };
  22. Collisions.prototype.checkActorCollisions = function() {
  23. // ugly as ugly gets; optimize later
  24. for(var i = 0; i < this.actors.length; i++) {
  25. for(var j = i + 1; j < this.actors.length; j++) {
  26. var a = this.actors[i];
  27. var b = this.actors[j];
  28. if(a.ghost || b.ghost) continue;
  29. if(areColliding(a.nextpos, b.nextpos, a.minBound, b.minBound)) {
  30. a.colliders.push(b);
  31. b.colliders.push(a);
  32. // console.log(a.type + " collides with " + b.type);
  33. }
  34. }
  35. }
  36. };
  37. function areColliding(a, b, ar, br) {
  38. var dx2 = (a.x - b.x) * (a.x - b.x);
  39. var dy2 = (a.y - b.y) * (a.y - b.y);
  40. var dr2 = (ar + br) * (ar + br);
  41. return (dx2 + dy2 > dr2) ? false : true;
  42. }
  43. Collisions.prototype.checkActorBounds = function() {
  44. for(var i = 0; i < this.actors.length; i++) {
  45. var a = this.actors[i];
  46. var res = this.inBounds(a.nextpos, a.minBound);
  47. // console.log(res);
  48. a.bumping = res;
  49. if(res != 0) {
  50. // fix actor...
  51. // console.log(a.type + ' is out of bounds');
  52. // really dumb, just don't let them move...
  53. if(res & COL_X) a.nextpos.x = a.position.x;
  54. if(res & COL_Y) a.nextpos.y = a.position.y;
  55. };
  56. };
  57. }
  58. // be really dumb for now
  59. Collisions.prototype.inBounds = function(pt, r) {
  60. //console.log(this.emptyRects.length);
  61. var res = 0;
  62. for(var i = 0; i < this.emptyRects.length; i++) {
  63. var bb = this.emptyRects[i];
  64. // if(log) console.log(pt.x + ' ' + pt.y + ' ' + r);
  65. if(pt.x - r < bb.l) res |= COL_LEFT;
  66. if(pt.x + r > bb.r) res |= COL_RIGHT;
  67. if(pt.y - r < bb.t) res |= COL_TOP;
  68. if(pt.y + r > bb.b) res |= COL_BOTTOM;
  69. }
  70. return res;
  71. };
  72. // be really dumb for now
  73. Collisions.prototype.subtractStaticRect = function(r) {
  74. this.emptyRects.push(rectc(r));
  75. };
  76. Collisions.prototype.addActor = function(obj) {
  77. if(typeof obj != 'object')
  78. return this.actors.push(obj);
  79. this.actors = this.actors.concat(obj);
  80. };