Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Room.prototype.getCenter = function () {
- var i, j,
- range = 20,
- distance = 1,
- collMap = this.getCollision(),
- x = this.x * 5 + this.xsize / 2,
- y = this.y * 5 + this.ysize / 2,
- checkSpot = function (x, y, room, map) {
- var a, b,
- j = x - room.x * 5,
- i = y - room.y * 5;
- if (isNaN(map[i][j])) {
- return false;
- }
- for (a = -1; a < 2; a += 1) {
- for (b = -1; b < 2; b += 1) {
- if (map[i+a][j+b] !== 0 && map[i+a][j+b] !== 16) {
- return false;
- }
- }
- }
- return true;
- };
- // Check if the original spot is valid
- if (checkSpot(x, y, this, collMap)) {
- return {x: x, y: y};
- }
- while (distance < range) {
- for (i = -distance; i <= distance; i += 1) {
- for (j = -distance; j <= distance; j += 1) {
- // Check outer layer only (skip previously checked)
- if (Math.abs(i) < Math.abs(distance) && Math.abs(j) < Math.abs(distance)) {
- continue;
- }
- if (checkSpot(x + i, y + j, this, collMap)) {
- return {x: x + i, y: y + j};
- }
- }
- }
- distance += 1;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement