collisions.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // all this shit has to change to handle the tile-based approach
  2. Systems.checkCollisions = function() {
  3. var colliders = game.components.collides;
  4. var collidees = game.components.collidable;
  5. // old ugly way... need to update
  6. for(var eid in colliders) { if(colliders.hasOwnProperty(eid)) {
  7. var oldpos = game.getComp(eid, 'position');
  8. var nextpos = game.getComp(eid, 'nextpos');
  9. var aabb_rel = game.getComp(eid, 'aabb');
  10. // world coord aabb
  11. var aabb = {
  12. t: aabb_rel.t + nextpos.y,
  13. b: aabb_rel.b + nextpos.y,
  14. l: aabb_rel.l + nextpos.x,
  15. r: aabb_rel.r + nextpos.x,
  16. };
  17. for(var ceid in collidees) { if(collidees.hasOwnProperty(ceid)) {
  18. var pos2 = game.getComp(ceid, 'position');
  19. var aabb_rel2 = game.getComp(ceid, 'aabb');
  20. var aabb2 = {
  21. t: aabb_rel2.t + pos2.y,
  22. b: aabb_rel2.b + pos2.y,
  23. l: aabb_rel2.l + pos2.x,
  24. r: aabb_rel2.r + pos2.x,
  25. };
  26. // check box collision
  27. if(intersectRect(aabb, aabb2)) {
  28. //console.log('collided');
  29. // just don't let it move for now...
  30. nextpos.x = oldpos.x;
  31. nextpos.y = oldpos.y;
  32. }
  33. }}
  34. }}
  35. }