Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Made hexmap a Crafty component and moved it out of the global namespace.

  • Loading branch information...
commit 446b3229c598406cf2ad5dca21e0c6d6524a4847 1 parent f9adc15
Øyvind Ingvaldsen authored
Showing with 181 additions and 196 deletions.
  1. +181 −196 src/hexmap.js
377 src/hexmap.js
View
@@ -1,43 +1,51 @@
-if(Array.prototype.addElement == null) {
- Array.prototype.addElement = function(element) {
- this.push(element);
- return this;
- }
-}
-
-if(Array.prototype.removeElement == null) {
- Array.prototype.removeElement = function(element) {
- var i = this.length;
- while(i--) {
- if(this[i] === element) {
- this.splice(i, 1);
+var Helper = {
+
+ arrays: {
+
+ addElement: function(array, element) {
+ array.push(element);
+ },
+
+ removeElement: function(array, element, firstOccurrence) {
+ firstOccurrence = typeof firstOccurrence != 'undefined' ? firstOccurrence : false;
+ var i = array.length;
+ while(i--) {
+ if(array[i] === element) {
+ array.splice(i, 1);
+ if(firstOccurrence) return;
+ }
}
- }
- return this;
- }
-}
-
-if(Array.prototype.contains == null) {
- Array.prototype.contains = function(element) {
- var i = this.length;
- while(i--) {
- if(this[i] === element) {
- return true;
+ },
+
+ contains: function(array, element) {
+ var i = array.length;
+ while(i--) {
+ if(array[i] === element) return true;
}
+ return false;
+ }
+
+ },
+
+ objects: {
+ defineGetter: function(obj, prop, getFunc) {
+ if(Object.defineProperty) return Object.defineProperty(obj, prop, { get: getFunc, configurable: true, enumerable: true });
+ if(Object.prototype.__defineGetter__) return obj.__defineGetter__(prop, getFunc);
+ },
+
+ defineSetter: function(obj, prop, setFunc) {
+ if(Object.defineProperty) return Object.defineProperty(obj, prop, { set: setFunc, configurable: true, enumerable: true });
+ if(Object.prototype.__defineSetter__) return obj.__defineSetter__(prop, setFunc);
+ },
+
+ defineAccessors: function(obj, prop, getFunc, setFunc) {
+ this.defineGetter(obj, prop, getFunc);
+ this.defineSetter(obj, prop, setFunc);
}
- return false;
}
-}
+};
-function defineGetter(obj, prop, getFunc) {
- if(Object.defineProperty) return Object.defineProperty(obj, prop, { get: getFunc, configurable: true, enumerable: true });
- if(Object.prototype.__defineGetter__) return obj.__defineGetter__(prop, getFunc);
-}
-function defineSetter(obj, prop, setFunc) {
- if(Object.defineProperty) return Object.defineProperty(obj, prop, { set: setFunc, configurable: true, enumerable: true });
- if(Object.prototype.__defineSetter__) return obj.__defineSetter__(prop, setFunc);
-}
Crafty.c("HexmapNode", {
_i: 0,
@@ -46,198 +54,175 @@ Crafty.c("HexmapNode", {
_G: 0,
_H: 0,
- init: function() {
- defineGetter(this, "i", function() { return this._i; });
- defineSetter(this, "i", function(v) { this._i = v; });
- defineGetter(this, "j", function() { return this._j; });
- defineSetter(this, "j", function(v) { this._j = v; });
- defineGetter(this, "parentNode", function() { return this._parentNode; });
- defineSetter(this, "parentNode", function(v) { this._parentNode = v; });
- defineGetter(this, "G", function() { return this._G; });
- defineSetter(this, "G", function(v) { this._G = v; });
- defineGetter(this, "H", function() { return this._H; });
- defineSetter(this, "H", function(v) { this._H = v; });
- defineGetter(this, "F", function() { return this._G + this._H; });
+ init: function() {
+ Helper.objects.defineAccessors(this, "i", function() { return this._i; }, function(v) { this._i = v; });
+ Helper.objects.defineAccessors(this, "j", function() { return this._j; }, function(v) { this._j = v; });
+ Helper.objects.defineAccessors(this, "parentNode", function() { return this._parentNode; }, function(v) { this._parentNode = v; });
+ Helper.objects.defineAccessors(this, "G", function() { return this._G; }, function(v) { this._G = v; });
+ Helper.objects.defineAccessors(this, "H", function() { return this._H; }, function(v) { this._H = v; });
+ Helper.objects.defineGetter(this, "F", function() { return this._G + this._H; });
},
hexmapNode: function(i, j) {
- this.i = i;
- this.j = j;
+ this._i = i;
+ this._j = j;
return this;
}
});
-Crafty.extend({
-
- hexmap: {
-
- _map: {
- _width: 0,
- _height: 0,
- _tiles: new Array()
- },
+Crafty.c("Hexmap", {
+ _mapWidth: 0,
+ _mapHeight: 0,
+ _tiles: new Array(),
+ _tileMeta: {
+ _width: 0,
+ _height: 0,
+ _r: 0,
+ _s: 0,
+ _h: 0
+ },
+
+ init: function() {
- setMapSize: function(width, height) {
- this._map._width = width;
- this._map._height = height;
- },
+ },
- setTile: function(i, j, tile) {
- if(i >= this._map._width || j >= this._map._height || i < 0 || j < 0) return;
- this._map._tiles[(j * this._map._height) + i] = tile;
- },
+ hexmap: function(tileWidth, tileHeight) {
+ this._tileMeta._width = tileWidth;
+ this._tileMeta._height = tileHeight;
+ this._tileMeta._r = this._tileMeta._width * 0.5;
+ this._tileMeta._s = this._tileMeta._r / Math.cos(20 * (Math.PI / 180));
+ this._tileMeta._h = (this._tileMeta._height - this._tileMeta._s) * 0.5;
- getTile: function(i, j) {
- if(i >= this._map._width || j >= this._map._height || i < 0 || j < 0) return null;
- return this._map._tiles[(j * this._map._height) + i];
- },
-
- getTiles: function() {
- return this._map._tiles;
- },
-
- getAdjacentTiles: function(tile) {
- var i = tile.i;
- var j = tile.j;
-
- if(j % 2 != 0) {
- return [
- this.getTile(i + 1, j),
- this.getTile(i - 1, j),
- this.getTile(i, j - 1),
- this.getTile(i, j + 1),
- this.getTile(i - 1, j - 1),
- this.getTile(i - 1, j + 1)
- ];
- }
- else {
- return [
- this.getTile(i + 1, j),
- this.getTile(i - 1, j),
- this.getTile(i, j - 1),
- this.getTile(i, j + 1),
- this.getTile(i + 1, j - 1),
- this.getTile(i + 1, j + 1),
- ];
- }
- },
+ return this;
+ },
- _tile: {
- _width: 0,
- _height: 0,
- _r: 0,
- _s: 0,
- _h: 0,
- },
+ setTile: function(i, j, tile) {
+ if(i >= this._mapWidth || i < 0 || j >= this._mapHeight || j < 0) return;
+ this._tiles[(j * this._mapHeight) + i] = tile;
+ },
- setTileSize: function(width, height) {
- this._tile._width = width;
- this._tile._height = height;
- this._tile._r = width * 0.5;
- this._tile._s = this._tile._r / Math.cos(20 * (Math.PI / 180));
- this._tile._h = (this._tile._height - this._tile._s) * 0.5;
- return this;
- },
+ getTile: function(i, j) {
+ if(i >= this._mapWidth || i < 0 || j >= this._mapHeight || j < 0) return null;
+ return this._tiles[(j * this._mapHeight) + i];
+ },
- placeTile: function(i, j, tile) {
- tile.x = i * 2 * this._tile._r + ((j % 2 == 0) * this._tile._r);
- tile.y = j * (this._tile._h + this._tile._s);
+ getAdjacentTiles: function(i, j) {
+ var adjacentTiles = new Array();
+
+ if(this.getTile(i + 1, j)) adjacentTiles.push(this.getTile(i + 1, j));
+ if(this.getTile(i - 1, j)) adjacentTiles.push(this.getTile(i - 1, j));
+ if(this.getTile(i, j - 1)) adjacentTiles.push(this.getTile(i, j - 1));
+ if(this.getTile(i, j + 1)) adjacentTiles.push(this.getTile(i, j + 1));
+
+ if(j % 2 != 0) {
+ if(this.getTile(i - 1, j - 1)) adjacentTiles.push(this.getTile(i - 1, j - 1));
+ if(this.getTile(i - 1, j + 1)) adjacentTiles.push(this.getTile(i - 1, j + 1));
+ }
+ else {
+ if(this.getTile(i + 1, j - 1)) adjacentTiles.push(this.getTile(i + 1, j - 1));
+ if(this.getTile(i + 1, j + 1)) adjacentTiles.push(this.getTile(i + 1, j + 1));
+ }
- tile.areaMap(
- [this._tile._width * 0.5, 0],
- [this._tile._width, this._tile._height * 0.25],
- [this._tile._width, this._tile._height * 0.75],
- [this._tile._width * 0.5, this._tile._height],
- [0, this._tile._height * 0.75],
- [0, this._tile._height * 0.25]
- );
+ return adjacentTiles;
+ },
- return this;
- },
+ placeTile: function(i, j, tile) {
+ tile.x = i * 2 * this._tileMeta._r + ((j % 2 == 0) * this._tileMeta._r);
+ tile.y = j * (this._tileMeta._h + this._tileMeta._s);
- create: function(width, height, creationFunc) {
- this.setMapSize(width, height);
+ tile.areaMap(
+ [this._tileMeta._width * 0.5, 0],
+ [this._tileMeta._width, this._tileMeta._height * 0.25],
+ [this._tileMeta._width, this._tileMeta._height * 0.75],
+ [this._tileMeta._width * 0.5, this._tileMeta._height],
+ [0, this._tileMeta._height * 0.75],
+ [0, this._tileMeta._height * 0.25]
+ );
- for(y = 0; y < height; y++) {
- for(x = 0; x < width; x++) {
+ return this;
+ },
- tile = creationFunc();
- tile.addComponent("HexmapNode").hexmapNode(x, y);
+ create: function(mapWidth, mapHeight, creationFunc) {
+ this._mapWidth = mapWidth;
+ this._mapHeight = mapHeight;
- this.placeTile(x, y, tile).setTile(x, y, tile);
- }
+ for(j = 0; j < this._mapHeight; j++) {
+ for(i = 0; i < this._mapWidth; i++) {
+ tile = creationFunc();
+ tile.addComponent("HexmapNode").hexmapNode(i, j);
+ this.placeTile(i, j, tile).setTile(i, j, tile);
}
+ }
+ },
- return this;
- },
+ findPath: function(startingNode, targetNode) {
+ var path = new Array();
+ var openList = new Array();
+ var closedList = new Array();
+
+ var H = function(u, v) {
+ dx = v.x - u.x;
+ dy = v.y - u.y;
+ return dx * dx + dy * dy;
+ }
- astar: function(startingNode, targetNode) {
- var path = new Array();
- var openList = new Array();
- var closedList = new Array();
+ startingNode.G = 100;
+ startingNode.parentNode = null;
+ openList.push(startingNode);
- var H = function(u, v) {
- dx = v.x - u.x;
- dy = v.y - u.y;
- return dx * dx + dy * dy;
- }
+ while(true) {
+ var bestNode = null;
+
+ // find the best node (lowest F) in the open list
+ openList.forEach(function(node) {
+ if(bestNode == null) {
+ bestNode = node;
+ }
+ else if(node != null) {
+ node.H = H(node, targetNode);
+ bestNode.H = H(bestNode, targetNode);
+ if(node.F < bestNode.F) bestNode = node;
+ }
+ });
- startingNode.G = 100;
- startingNode.parentNode = null;
- openList.push(startingNode);
+ // if we have found the target or the open list is empty we are done
+ if(bestNode == targetNode || bestNode == null) break;
- while(true) {
- var bestNode = null;
+ // move the best node to the closed list
+ Helper.arrays.removeElement(openList, bestNode);
+ Helper.arrays.addElement(closedList, bestNode);
- // find the best node (lowest F) in the open list
- openList.forEach(function(node) {
- if(bestNode == null) {
- bestNode = node;
+ // consider all the adjacent tiles
+ this.getAdjacentTiles(bestNode.i, bestNode.j).forEach(function(node) {
+ if(node != null) {
+ if(Helper.arrays.contains(closedList, node)) {
+ // ignore if it's in the closed list
}
- else if(node != null) {
+ else if(!Helper.arrays.contains(openList, node)) {
+ // add to the open list if it's not already in it
+ Helper.arrays.addElement(openList, node);
+ node.parentNode = bestNode;
+ node.G = bestNode.G + 100;
node.H = H(node, targetNode);
- bestNode.H = H(bestNode, targetNode);
- if(node.F < bestNode.F) bestNode = node;
}
- });
-
- // if we have found the target or the open list is empty we are done
- if(bestNode == targetNode || bestNode == null) break;
-
- // move the best node to the closed list
- openList.removeElement(bestNode);
- closedList.addElement(bestNode);
-
- // consider all the adjacent tiles
- this.getAdjacentTiles(bestNode).forEach(function(node) {
- if(node != null) {
- if(closedList.contains(node)) {
- // ignore if it's in the closed list
- }
- else if(!openList.contains(node)) {
- // add to the open list if it's not already in it
- openList.addElement(node);
- node.parentNode = bestNode;
- node.G = bestNode.G + 100;
- node.H = H(node, targetNode);
- }
- else if(openList.contains(node) && node.G < bestNode.G) {
- // if it is in the open list and its G-value is lower, update parent and G-value
- node.parentNode = bestNode;
- node.G = bestNode.G + 100;
- node.H = H(node, targetNode);
- }
+ else if(Helper.arrays.contains(openList, node) && node.G < bestNode.G) {
+ // if it is in the open list and its G-value is lower, update parent and G-value
+ node.parentNode = bestNode;
+ node.G = bestNode.G + 100;
+ node.H = H(node, targetNode);
}
- });
- }
-
- // go backward from the last best node using the parent-attribute to find the path
- while(bestNode.parentNode != null) {
- path.push(bestNode);
- bestNode = bestNode._parentNode;
- }
+ }
+ });
+ }
- return path;
+ // go backward from the last best node using the parent-attribute to find the path
+ while(bestNode.parentNode != null) {
+ path.push(bestNode);
+ bestNode = bestNode._parentNode;
}
+
+ return path;
+
}
-});
+
+});
Please sign in to comment.
Something went wrong with that request. Please try again.