Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

100 lignes
2.0 KiB

  1. define([
  2. 'js/misc/distanceToPolygon',
  3. 'js/system/events'
  4. ], function (
  5. distanceToPolygon,
  6. events
  7. ) {
  8. return {
  9. grid: null,
  10. width: 0,
  11. height: 0,
  12. init: function (collisionMap) {
  13. events.on('resetPhysics', this.reset.bind(this));
  14. this.width = collisionMap.length;
  15. this.height = collisionMap[0].length;
  16. let grid = this.grid = _.get2dArray(this.width, this.height, false);
  17. for (let i = 0; i < this.width; i++) {
  18. let row = grid[i];
  19. let collisionRow = collisionMap[i];
  20. for (let j = 0; j < this.height; j++)
  21. row[j] = collisionRow[j];
  22. }
  23. },
  24. reset: function () {
  25. this.width = 0;
  26. this.height = 0;
  27. this.grid = [];
  28. },
  29. isTileBlocking: function (x, y, mob, obj) {
  30. if ((x < 0) || (y < 0) || (x >= this.width) | (y >= this.height))
  31. return true;
  32. x = ~~x;
  33. y = ~~y;
  34. return this.grid[x][y];
  35. },
  36. setCollision: function (config) {
  37. const x = config.x;
  38. const y = config.y;
  39. const collides = config.collides;
  40. this.grid[x][y] = collides;
  41. },
  42. isInPolygon: function (x, y, verts) {
  43. let inside = false;
  44. let vLen = verts.length;
  45. for (let i = 0, j = vLen - 1; i < vLen; j = i++) {
  46. let vi = verts[i];
  47. let vj = verts[j];
  48. let xi = vi[0];
  49. let yi = vi[1];
  50. let xj = vj[0];
  51. let yj = vj[1];
  52. let doesIntersect = (
  53. ((yi > y) !== (yj > y)) &&
  54. (x < ((((xj - xi) * (y - yi)) / (yj - yi)) + xi))
  55. );
  56. if (doesIntersect)
  57. inside = !inside;
  58. }
  59. return inside;
  60. },
  61. //Helper function to check if a point is inside an area
  62. // This function is optimized to check if the point is outside the rect first
  63. // and if it is not, we do the more expensive isInPolygon check
  64. isInArea: function (x, y, { x: ax, y: ay, width, height, area }) {
  65. //Outside rect
  66. if (
  67. x < ax ||
  68. x >= ax + width ||
  69. y < ay ||
  70. y >= ay + height
  71. )
  72. return false;
  73. return this.isInPolygon(x, y, area);
  74. },
  75. distanceToPolygon: function (p, verts) {
  76. return distanceToPolygon.calculate(p, verts);
  77. }
  78. };
  79. });