Advertisement
kolton

Untitled

Dec 20th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. Room.prototype.getCenter = function () {
  2. var i, j,
  3. range = 20,
  4. distance = 1,
  5. collMap = this.getCollision(),
  6. x = this.x * 5 + this.xsize / 2,
  7. y = this.y * 5 + this.ysize / 2,
  8. checkSpot = function (x, y, room, map) {
  9. var a, b,
  10. j = x - room.x * 5,
  11. i = y - room.y * 5;
  12.  
  13. if (isNaN(map[i][j])) {
  14. return false;
  15. }
  16.  
  17. for (a = -1; a < 2; a += 1) {
  18. for (b = -1; b < 2; b += 1) {
  19. if (map[i+a][j+b] !== 0 && map[i+a][j+b] !== 16) {
  20. return false;
  21. }
  22. }
  23. }
  24.  
  25. return true;
  26. };
  27.  
  28. // Check if the original spot is valid
  29. if (checkSpot(x, y, this, collMap)) {
  30. return {x: x, y: y};
  31. }
  32.  
  33. while (distance < range) {
  34. for (i = -distance; i <= distance; i += 1) {
  35. for (j = -distance; j <= distance; j += 1) {
  36. // Check outer layer only (skip previously checked)
  37. if (Math.abs(i) < Math.abs(distance) && Math.abs(j) < Math.abs(distance)) {
  38. continue;
  39. }
  40.  
  41. if (checkSpot(x + i, y + j, this, collMap)) {
  42. return {x: x + i, y: y + j};
  43. }
  44. }
  45. }
  46.  
  47. distance += 1;
  48. }
  49.  
  50. return false;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement