Skip to content

Commit

Permalink
Tweak InputManager and clean up old files
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Feb 19, 2012
1 parent 0e59ecc commit ed0e5e2
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 2,814 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<script src="scripts/thirdparty/Box2d.min.js"></script>
<script src="scripts/thirdparty/stats.js"></script>
<script src="scripts/cannon.js"></script>-->
<script data-dojo-config="async: true, isDebug: 1" src="scripts/dojo/dojo.js"></script>
<script data-dojo-config="async: true, isDebug: 1" src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/dojo.js"></script>
<script>
require({
baseUrl: "scripts/",
Expand Down
68 changes: 28 additions & 40 deletions scripts/cannon.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ limitations under the License.
###

require [ 'dojo/dom', 'dojo/dom-geometry', 'mwe/GameCore', 'mwe/ResourceManager', 'mwe/CanvasManager', 'mwe/box2d/Box', 'mwe/box2d/CircleEntity', 'mwe/box2d/RectangleEntity', 'mwe/box2d/PolygonEntity', 'Marshmallow', 'thirdparty/stats', 'scripts/thirdparty/Box2d.min.js' ], (dom, domGeom, GameCore, ResourceManager, CanvasManager, Box, CircleEntity, RectangleEntity, PolygonEntity, Marshmallow, Stats) ->
require [ 'dojo/dom', 'dojo/dom-geometry', 'mwe/GameCore', 'mwe/ResourceManager', 'mwe/CanvasManager', 'mwe/InputManager', 'mwe/box2d/Box', 'mwe/box2d/CircleEntity', 'mwe/box2d/RectangleEntity', 'mwe/box2d/PolygonEntity', 'Marshmallow', 'thirdparty/stats', 'scripts/thirdparty/Box2d.min.js' ], (dom, domGeom, GameCore, ResourceManager, CanvasManager, InputManager, Box, CircleEntity, RectangleEntity, PolygonEntity, Marshmallow, Stats) ->
debug = if localStorage? and localStorage.debug is 'y' then true else false

SCALE = 30.0

geomId = 0
geomId = 11
millisToMarsh = 100
millisToMarshPassed = 0

Expand All @@ -33,15 +33,15 @@ require [ 'dojo/dom', 'dojo/dom-geometry', 'mwe/GameCore', 'mwe/ResourceManager'
stats.getDomElement().style.bottom = '0px'

world = {}
solids = [{"id":42,"x":0,"y":0,"points":[{"x":4,"y":19.666666666666668},{"x":4.233333333333333,"y":17.4},{"x":4.466666666666667,"y":16.866666666666667},{"x":5.166666666666667,"y":16.266666666666666},{"x":5.8,"y":16.266666666666666},{"x":6.1,"y":16.366666666666667}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":43,"x":0,"y":0,"points":[{"x":4.1,"y":19.666666666666668},{"x":6.333333333333333,"y":16},{"x":18.566666666666666,"y":15.8},{"x":18.77333335876465,"y":19.633333333333333}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":44,"x":0,"y":0,"points":[{"x":6.373333358764649,"y":15.973333740234375},{"x":5.973333358764648,"y":14.806667073567708},{"x":5.940000025431315,"y":13.573333740234375},{"x":6.606666692097982,"y":12.740000406901041},{"x":7.340000025431315,"y":12.506667073567709},{"x":7.906666692097982,"y":12.606667073567708}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":45,"x":0,"y":0,"points":[{"x":7.940000025431315,"y":12.606667073567708},{"x":8.606666692097981,"y":13.073333740234375},{"x":9.173333358764648,"y":13.740000406901041},{"x":9.140000025431315,"y":14.773333740234374},{"x":8.873333358764649,"y":15.940000406901042},{"x":6.440000025431315,"y":15.973333740234375}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":46,"x":0,"y":0,"points":[{"x":10.073333358764648,"y":15.960000610351562},{"x":11.073333358764648,"y":12.926667277018229},{"x":11.473333358764648,"y":12.72666727701823},{"x":13.406666692097982,"y":12.393333943684896},{"x":16.140000025431316,"y":12.660000610351563},{"x":18.27333335876465,"y":13.793333943684896}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":47,"x":0,"y":0,"points":[{"x":13.440000025431315,"y":12.353333536783854},{"x":13.940000025431315,"y":11.92000020345052},{"x":15.806666692097982,"y":12.02000020345052},{"x":16.10666669209798,"y":12.620000203450521}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":48,"x":0,"y":0,"points":[{"x":13.973333358764648,"y":11.886666870117187},{"x":13.673333358764648,"y":10.486666870117187},{"x":13.673333358764648,"y":9.786666870117188},{"x":13.873333358764649,"y":9.186666870117188},{"x":14.473333358764648,"y":8.653333536783855},{"x":14.840000025431316,"y":8.52000020345052}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":49,"x":0,"y":0,"points":[{"x":14.873333358764649,"y":8.553333536783855},{"x":15.306666692097982,"y":8.553333536783855},{"x":15.973333358764648,"y":8.953333536783854},{"x":16.373333358764647,"y":9.52000020345052},{"x":16.07333335876465,"y":11.220000203450521},{"x":15.806666692097982,"y":11.953333536783854}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":50,"x":0,"y":0,"points":[{"x":14.006666692097982,"y":11.886666870117187},{"x":14.873333358764649,"y":8.52000020345052},{"x":15.806666692097982,"y":12.02000020345052}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":51,"x":0,"y":0,"points":[{"x":10.140000025431315,"y":15.946666971842449},{"x":18.30666669209798,"y":13.780000305175781},{"x":18.540000025431315,"y":15.813333638509114}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":52,"x":0,"y":0,"points":[{"x":8.9,"y":15.958333333333334},{"x":9.166666666666666,"y":14.891666666666667},{"x":9.633333333333333,"y":15.025},{"x":9.933333333333334,"y":15.391666666666667},{"x":10.1,"y":15.725},{"x":10.066666666666666,"y":15.958333333333334}],"staticBody":true,"hidden":true,"type":"Polygon"}]

solids = [{"id":0,"x":0,"y":0,"points":[{"x":4,"y":19.666666666666668},{"x":4.233333333333333,"y":17.4},{"x":4.466666666666667,"y":16.866666666666667},{"x":5.166666666666667,"y":16.266666666666666},{"x":5.8,"y":16.266666666666666},{"x":6.1,"y":16.366666666666667}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":1,"x":0,"y":0,"points":[{"x":4.1,"y":19.666666666666668},{"x":6.333333333333333,"y":16},{"x":18.566666666666666,"y":15.8},{"x":18.77333335876465,"y":19.633333333333333}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":2,"x":0,"y":0,"points":[{"x":6.373333358764649,"y":15.973333740234375},{"x":5.973333358764648,"y":14.806667073567708},{"x":5.940000025431315,"y":13.573333740234375},{"x":6.606666692097982,"y":12.740000406901041},{"x":7.340000025431315,"y":12.506667073567709},{"x":7.906666692097982,"y":12.606667073567708}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":3,"x":0,"y":0,"points":[{"x":7.940000025431315,"y":12.606667073567708},{"x":8.606666692097981,"y":13.073333740234375},{"x":9.173333358764648,"y":13.740000406901041},{"x":9.140000025431315,"y":14.773333740234374},{"x":8.873333358764649,"y":15.940000406901042},{"x":6.440000025431315,"y":15.973333740234375}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":4,"x":0,"y":0,"points":[{"x":10.073333358764648,"y":15.960000610351562},{"x":11.073333358764648,"y":12.926667277018229},{"x":11.473333358764648,"y":12.72666727701823},{"x":13.406666692097982,"y":12.393333943684896},{"x":16.140000025431316,"y":12.660000610351563},{"x":18.27333335876465,"y":13.793333943684896}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":5,"x":0,"y":0,"points":[{"x":13.440000025431315,"y":12.353333536783854},{"x":13.940000025431315,"y":11.92000020345052},{"x":15.806666692097982,"y":12.02000020345052},{"x":16.10666669209798,"y":12.620000203450521}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":6,"x":0,"y":0,"points":[{"x":13.973333358764648,"y":11.886666870117187},{"x":13.673333358764648,"y":10.486666870117187},{"x":13.673333358764648,"y":9.786666870117188},{"x":13.873333358764649,"y":9.186666870117188},{"x":14.473333358764648,"y":8.653333536783855},{"x":14.840000025431316,"y":8.52000020345052}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":7,"x":0,"y":0,"points":[{"x":14.873333358764649,"y":8.553333536783855},{"x":15.306666692097982,"y":8.553333536783855},{"x":15.973333358764648,"y":8.953333536783854},{"x":16.373333358764647,"y":9.52000020345052},{"x":16.07333335876465,"y":11.220000203450521},{"x":15.806666692097982,"y":11.953333536783854}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":8,"x":0,"y":0,"points":[{"x":14.006666692097982,"y":11.886666870117187},{"x":14.873333358764649,"y":8.52000020345052},{"x":15.806666692097982,"y":12.02000020345052}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":9,"x":0,"y":0,"points":[{"x":10.140000025431315,"y":15.946666971842449},{"x":18.30666669209798,"y":13.780000305175781},{"x":18.540000025431315,"y":15.813333638509114}],"staticBody":true,"hidden":true,"type":"Polygon","_inherited":{"p":1}},{"id":10,"x":0,"y":0,"points":[{"x":8.9,"y":15.958333333333334},{"x":9.166666666666666,"y":14.891666666666667},{"x":9.633333333333333,"y":15.025},{"x":9.933333333333334,"y":15.391666666666667},{"x":10.1,"y":15.725},{"x":10.066666666666666,"y":15.958333333333334}],"staticBody":true,"hidden":true,"type":"Polygon"}]

# Resource Manager
rm = new ResourceManager imageDir: 'images/'
images = rm.loadFiles backImg: 'cannon.png', foreImg: 'foreground.png', marshImg: 'marsh32.png'

# Canvas Manager - Notice how this isn't inside a dojo/domReady! - It is because CanvasManager requires it before executing
cm = new CanvasManager {
cm = new CanvasManager {
canvasId: 'canvas'
height: 590
width: 620
Expand All @@ -50,7 +50,7 @@ require [ 'dojo/dom', 'dojo/dom-geometry', 'mwe/GameCore', 'mwe/ResourceManager'
entity.draw ctx for id, entity of world when not entity.hidden or showHidden
ctx.drawImage images.foreImg, 0, 0, @width, images.foreImg.height
}

# Box Wrapper for Box2d
box = new Box {
intervalRate: 60
Expand All @@ -60,18 +60,24 @@ require [ 'dojo/dom', 'dojo/dom-geometry', 'mwe/GameCore', 'mwe/ResourceManager'
scale: SCALE
gravityY: 9.8
}
box.setBodies solids

im = new InputManager {
box: box
canvasManager: cm
mouseUp: (event) ->
obj = getCollidedSprite @getMouseLoc event
@box.applyImpulse obj.id, Math.random() * 360, 100 if obj
touchEnd: (event) ->
@mouseUp event.changedTouches[0]
selectstart: (event) ->
event.preventDefault()
return false
}
im.bindMouse()
im.bindTouch()
im.bind document, 'selectstart'

addBodies = (shape) ->
console.log shape.type
geomId++
if shape.type is 'Polygon'
b2dshape = new PolygonEntity shape
b2dshape.id = geomId
box.addBody b2dshape
world[geomId] = b2dshape

addBodies shape for shape in solids

game = new GameCore {
canvasManager: cm
resourceManager: rm
Expand Down Expand Up @@ -116,11 +122,6 @@ require [ 'dojo/dom', 'dojo/dom-geometry', 'mwe/GameCore', 'mwe/ResourceManager'
console.log 'error in update', updateE
}

# Get coordinates in box2d space
getGfxMouse = (event) ->
coordsM = domGeom.position dom.byId 'canvas'
return x: (event.clientX - coordsM.x) / SCALE, y: (event.clientY - coordsM.y) / SCALE

intersect = (s1, s2, radiiSquared) ->
distance_squared = Math.pow(s1.x - s2.x,2) + Math.pow(s1.y - s2.y,2)
return distance_squared < radiiSquared # true if intersect
Expand All @@ -129,19 +130,6 @@ require [ 'dojo/dom', 'dojo/dom-geometry', 'mwe/GameCore', 'mwe/ResourceManager'
return sprite for spriteId, sprite of world when intersect mouse, sprite, 0.5
return null

mouseUpHandler = (event) ->
pt = getGfxMouse event
obj = getCollidedSprite pt
box.applyImpulse obj.id, Math.random() * 360, 100 if obj

require [ 'dojo/dom-construct', 'dojo/_base/window', 'dojo/on', 'dojo/touch', 'dojo/domReady!' ], (domConstruct, win, bind, touch) ->
require [ 'dojo/dom-construct', 'dojo/_base/window', 'dojo/domReady!' ], (domConstruct, win) ->
domConstruct.place stats.getDomElement(), win.body(), 'last' if debug

bind document, 'mouseup', mouseUpHandler
bind document, 'touchend', (event) ->
mouseUpHandler event.changedTouches[0]
bind document, 'selectstart', (event) ->
event.preventDefault()
return false

game.run()
game.run()
91 changes: 34 additions & 57 deletions scripts/cannon.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ limitations under the License.

(function() {

require(['dojo/dom', 'dojo/dom-geometry', 'mwe/GameCore', 'mwe/ResourceManager', 'mwe/CanvasManager', 'mwe/box2d/Box', 'mwe/box2d/CircleEntity', 'mwe/box2d/RectangleEntity', 'mwe/box2d/PolygonEntity', 'Marshmallow', 'thirdparty/stats', 'scripts/thirdparty/Box2d.min.js'], function(dom, domGeom, GameCore, ResourceManager, CanvasManager, Box, CircleEntity, RectangleEntity, PolygonEntity, Marshmallow, Stats) {
var SCALE, addBodies, box, cm, debug, game, geomId, getCollidedSprite, getGfxMouse, images, intersect, millisToMarsh, millisToMarshPassed, mouseUpHandler, rm, shape, showHidden, solids, stats, world, _i, _len;
require(['dojo/dom', 'dojo/dom-geometry', 'mwe/GameCore', 'mwe/ResourceManager', 'mwe/CanvasManager', 'mwe/InputManager', 'mwe/box2d/Box', 'mwe/box2d/CircleEntity', 'mwe/box2d/RectangleEntity', 'mwe/box2d/PolygonEntity', 'Marshmallow', 'thirdparty/stats', 'scripts/thirdparty/Box2d.min.js'], function(dom, domGeom, GameCore, ResourceManager, CanvasManager, InputManager, Box, CircleEntity, RectangleEntity, PolygonEntity, Marshmallow, Stats) {
var SCALE, box, cm, debug, game, geomId, getCollidedSprite, im, images, intersect, millisToMarsh, millisToMarshPassed, rm, showHidden, solids, stats, world;
debug = (typeof localStorage !== "undefined" && localStorage !== null) && localStorage.debug === 'y' ? true : false;
SCALE = 30.0;
geomId = 0;
geomId = 11;
millisToMarsh = 100;
millisToMarshPassed = 0;
showHidden = false;
Expand All @@ -33,7 +33,7 @@ limitations under the License.
world = {};
solids = [
{
"id": 42,
"id": 0,
"x": 0,
"y": 0,
"points": [
Expand Down Expand Up @@ -64,7 +64,7 @@ limitations under the License.
"p": 1
}
}, {
"id": 43,
"id": 1,
"x": 0,
"y": 0,
"points": [
Expand All @@ -89,7 +89,7 @@ limitations under the License.
"p": 1
}
}, {
"id": 44,
"id": 2,
"x": 0,
"y": 0,
"points": [
Expand Down Expand Up @@ -120,7 +120,7 @@ limitations under the License.
"p": 1
}
}, {
"id": 45,
"id": 3,
"x": 0,
"y": 0,
"points": [
Expand Down Expand Up @@ -151,7 +151,7 @@ limitations under the License.
"p": 1
}
}, {
"id": 46,
"id": 4,
"x": 0,
"y": 0,
"points": [
Expand Down Expand Up @@ -182,7 +182,7 @@ limitations under the License.
"p": 1
}
}, {
"id": 47,
"id": 5,
"x": 0,
"y": 0,
"points": [
Expand All @@ -207,7 +207,7 @@ limitations under the License.
"p": 1
}
}, {
"id": 48,
"id": 6,
"x": 0,
"y": 0,
"points": [
Expand Down Expand Up @@ -238,7 +238,7 @@ limitations under the License.
"p": 1
}
}, {
"id": 49,
"id": 7,
"x": 0,
"y": 0,
"points": [
Expand Down Expand Up @@ -269,7 +269,7 @@ limitations under the License.
"p": 1
}
}, {
"id": 50,
"id": 8,
"x": 0,
"y": 0,
"points": [
Expand All @@ -291,7 +291,7 @@ limitations under the License.
"p": 1
}
}, {
"id": 51,
"id": 9,
"x": 0,
"y": 0,
"points": [
Expand All @@ -313,7 +313,7 @@ limitations under the License.
"p": 1
}
}, {
"id": 52,
"id": 10,
"x": 0,
"y": 0,
"points": [
Expand Down Expand Up @@ -372,21 +372,26 @@ limitations under the License.
scale: SCALE,
gravityY: 9.8
});
addBodies = function(shape) {
var b2dshape;
console.log(shape.type);
geomId++;
if (shape.type === 'Polygon') {
b2dshape = new PolygonEntity(shape);
b2dshape.id = geomId;
box.addBody(b2dshape);
return world[geomId] = b2dshape;
box.setBodies(solids);
im = new InputManager({
box: box,
canvasManager: cm,
mouseUp: function(event) {
var obj;
obj = getCollidedSprite(this.getMouseLoc(event));
if (obj) return this.box.applyImpulse(obj.id, Math.random() * 360, 100);
},
touchEnd: function(event) {
return this.mouseUp(event.changedTouches[0]);
},
selectstart: function(event) {
event.preventDefault();
return false;
}
};
for (_i = 0, _len = solids.length; _i < _len; _i++) {
shape = solids[_i];
addBodies(shape);
}
});
im.bindMouse();
im.bindTouch();
im.bind(document, 'selectstart');
game = new GameCore({
canvasManager: cm,
resourceManager: rm,
Expand Down Expand Up @@ -434,14 +439,6 @@ limitations under the License.
}
}
});
getGfxMouse = function(event) {
var coordsM;
coordsM = domGeom.position(dom.byId('canvas'));
return {
x: (event.clientX - coordsM.x) / SCALE,
y: (event.clientY - coordsM.y) / SCALE
};
};
intersect = function(s1, s2, radiiSquared) {
var distance_squared;
distance_squared = Math.pow(s1.x - s2.x, 2) + Math.pow(s1.y - s2.y, 2);
Expand All @@ -455,28 +452,8 @@ limitations under the License.
}
return null;
};
mouseUpHandler = function(event) {
var mouseDownPt, obj, pt;
mouseDownPt = null;
pt = getGfxMouse(event);
console.log("mouse: " + pt);
obj = getCollidedSprite(pt);
console.log("sprite: " + obj);
if (obj) {
console.log("obj: " + obj);
return box.applyImpulse(obj.id, Math.random() * 360, 100);
}
};
return require(['dojo/dom-construct', 'dojo/_base/window', 'dojo/on', 'dojo/touch', 'dojo/domReady!'], function(domConstruct, win, bind, touch) {
return require(['dojo/dom-construct', 'dojo/_base/window', 'dojo/domReady!'], function(domConstruct, win) {
if (debug) domConstruct.place(stats.getDomElement(), win.body(), 'last');
bind(document, 'mouseup', mouseUpHandler);
bind(document, 'touchend', function(event) {
return mouseUpHandler(event.changedTouches[0]);
});
bind(document, 'selectstart', function(event) {
event.preventDefault();
return false;
});
return game.run();
});
});
Expand Down
Loading

0 comments on commit ed0e5e2

Please sign in to comment.