Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cleanup

  • Loading branch information...
commit 96c534859dbe2b84ece18e8c2302aef4b8792452 1 parent aaa4abf
@Raynos authored
Showing with 8 additions and 9 deletions.
  1. +7 −8 map/index.js
  2. +1 −1  static/bundle.js
View
15 map/index.js
@@ -36,9 +36,8 @@ function Map() {
, newX = (entity.x += diffX)
, newY = (entity.y += diffY)
, loc = getGrid(newX, newY)
- , list = blocks[loc]
- var isValid = !collision(entity, list)
+ var isValid = !collision(entity, loc)
if (isValid) {
var previousLoc = getGrid(entity.x, entity.y)
@@ -62,18 +61,18 @@ function Map() {
}
/*
+ Should do a proper spatial query instead of just blocks[loc]
+
Returns true if there is a collison
*/
- function collision(block, list) {
+ function collision(block, loc) {
+ var list = blocks[loc]
+
if (!list) {
return false
}
- return list.some(checkCollision, block)
- }
-
- function checkCollision(other) {
- if (this === other) {
+ if (list.length === 1 && list[0] === block) {
return false
}
View
2  static/bundle.js
@@ -202,7 +202,7 @@ require.define("/node_modules/vm-browserify/package.json",Function(['require','m
require.define("/node_modules/vm-browserify/index.js",Function(['require','module','exports','__dirname','__filename','process'],"var Object_keys = function (obj) {\n if (Object.keys) return Object.keys(obj)\n else {\n var res = [];\n for (var key in obj) res.push(key)\n return res;\n }\n};\n\nvar forEach = function (xs, fn) {\n if (xs.forEach) return xs.forEach(fn)\n else for (var i = 0; i < xs.length; i++) {\n fn(xs[i], i, xs);\n }\n};\n\nvar Script = exports.Script = function NodeScript (code) {\n if (!(this instanceof Script)) return new Script(code);\n this.code = code;\n};\n\nScript.prototype.runInNewContext = function (context) {\n if (!context) context = {};\n \n var iframe = document.createElement('iframe');\n if (!iframe.style) iframe.style = {};\n iframe.style.display = 'none';\n \n document.body.appendChild(iframe);\n \n var win = iframe.contentWindow;\n \n forEach(Object_keys(context), function (key) {\n win[key] = context[key];\n });\n \n if (!win.eval && win.execScript) {\n // win.eval() magically appears when this is called in IE:\n win.execScript('null');\n }\n \n var res = win.eval(this.code);\n \n forEach(Object_keys(win), function (key) {\n context[key] = win[key];\n });\n \n document.body.removeChild(iframe);\n \n return res;\n};\n\nScript.prototype.runInThisContext = function () {\n return eval(this.code); // maybe...\n};\n\nScript.prototype.runInContext = function (context) {\n // seems to be just runInNewContext on magical context objects which are\n // otherwise indistinguishable from objects except plain old objects\n // for the parameter segfaults node\n return this.runInNewContext(context);\n};\n\nforEach(Object_keys(Script.prototype), function (name) {\n exports[name] = Script[name] = function (code) {\n var s = Script(code);\n return s[name].apply(s, [].slice.call(arguments, 1));\n };\n});\n\nexports.createScript = function (code) {\n return exports.Script(code);\n};\n\nexports.createContext = Script.createContext = function (context) {\n // not really sure what this one does\n // seems to just make a shallow copy\n var copy = {};\n if(typeof context === 'object') {\n forEach(Object_keys(context), function (key) {\n copy[key] = context[key];\n });\n }\n return copy;\n};\n\n//@ sourceURL=/node_modules/vm-browserify/index.js"));
-require.define("/map/index.js",Function(['require','module','exports','__dirname','__filename','process'],"var Widget = require(\"./widget\")\n , through = require(\"through\")\n\nmodule.exports = Map\n\nfunction Map() {\n var widget = Widget(600, 400)\n , blocks = {}\n , map = {}\n\n map.addEntity = addEntity\n map.addBlock = addBlock\n map.appendTo = widget.appendTo\n\n return map\n\n /*\n When an entity emit deltas in position we pipe it through a\n validator which checks collisions and then\n pipe it back into the entity if the validator thinks there are\n no collisions\n */\n function addEntity(entity) {\n entity.pipe(through(detectCollisions)).pipe(entity)\n\n addBlock(entity)\n\n /*\n\n */\n function detectCollisions(data) {\n //console.log(\"got changes\", data)\n var changes = data[0]\n , diffX = changes.x || 0\n , diffY = changes.y || 0\n , newX = (entity.x += diffX)\n , newY = (entity.y += diffY)\n , loc = getGrid(newX, newY)\n , list = blocks[loc]\n\n var isValid = !collision(entity, list)\n\n if (isValid) {\n var previousLoc = getGrid(entity.x, entity.y)\n\n if (previousLoc !== loc) {\n delete blocks[previousLoc]\n blocks[loc] = entity\n }\n\n entity.x = newX\n entity.y = newY\n\n this.emit(\"data\", [{\n x: newX\n , y: newY\n }, data[1], data[2]])\n } else {\n entity.x -= diffX\n entity.y -= diffY\n }\n }\n\n /*\n Returns true if there is a collison\n */\n function collision(block, list) {\n if (!list) {\n return false\n }\n\n return list.some(checkCollision, block)\n }\n\n function checkCollision(other) {\n if (this === other) {\n return false\n }\n\n return true\n }\n }\n\n function addBlock(block) {\n var loc = getGrid(block.x, block.y)\n , list = blocks[loc]\n\n if (list) {\n list.push(block)\n } else {\n blocks[loc] = [block]\n }\n\n widget.addBlock(block)\n }\n}\n\nfunction getGrid(x, y) {\n var diffX = x % 20\n , diffY = y % 20\n\n return (x - diffX) + \":\" + (y - diffY)\n}\n//@ sourceURL=/map/index.js"));
+require.define("/map/index.js",Function(['require','module','exports','__dirname','__filename','process'],"var Widget = require(\"./widget\")\n , through = require(\"through\")\n\nmodule.exports = Map\n\nfunction Map() {\n var widget = Widget(600, 400)\n , blocks = {}\n , map = {}\n\n map.addEntity = addEntity\n map.addBlock = addBlock\n map.appendTo = widget.appendTo\n\n return map\n\n /*\n When an entity emit deltas in position we pipe it through a\n validator which checks collisions and then\n pipe it back into the entity if the validator thinks there are\n no collisions\n */\n function addEntity(entity) {\n entity.pipe(through(detectCollisions)).pipe(entity)\n\n addBlock(entity)\n\n /*\n\n */\n function detectCollisions(data) {\n //console.log(\"got changes\", data)\n var changes = data[0]\n , diffX = changes.x || 0\n , diffY = changes.y || 0\n , newX = (entity.x += diffX)\n , newY = (entity.y += diffY)\n , loc = getGrid(newX, newY)\n\n var isValid = !collision(entity, loc)\n\n if (isValid) {\n var previousLoc = getGrid(entity.x, entity.y)\n\n if (previousLoc !== loc) {\n delete blocks[previousLoc]\n blocks[loc] = entity\n }\n\n entity.x = newX\n entity.y = newY\n\n this.emit(\"data\", [{\n x: newX\n , y: newY\n }, data[1], data[2]])\n } else {\n entity.x -= diffX\n entity.y -= diffY\n }\n }\n\n /*\n Should do a proper spatial query instead of just blocks[loc]\n\n Returns true if there is a collison\n */\n function collision(block, loc) {\n var list = blocks[loc]\n\n if (!list) {\n return false\n }\n\n if (list.length === 1 && list[0] === block) {\n return false\n }\n\n return true\n }\n }\n\n function addBlock(block) {\n var loc = getGrid(block.x, block.y)\n , list = blocks[loc]\n\n if (list) {\n list.push(block)\n } else {\n blocks[loc] = [block]\n }\n\n widget.addBlock(block)\n }\n}\n\nfunction getGrid(x, y) {\n var diffX = x % 20\n , diffY = y % 20\n\n return (x - diffX) + \":\" + (y - diffY)\n}\n//@ sourceURL=/map/index.js"));
require.define("/map/widget.js",Function(['require','module','exports','__dirname','__filename','process'],"var svg = require(\"./map.svg\")\n , Fragment = require(\"fragment\")\n\nmodule.exports = Widget\n\nfunction Widget(width, height) {\n var elem = Fragment(svg).firstChild\n elem.setAttribute(\"width\", width)\n elem.setAttribute(\"height\", height)\n\n return {\n appendTo: appendTo\n , addBlock: addBlock\n }\n \n function appendTo(other) {\n other.appendChild(elem)\n }\n\n function addBlock(block) {\n block.appendTo(elem)\n }\n}\n//@ sourceURL=/map/widget.js"));
Please sign in to comment.
Something went wrong with that request. Please try again.