From 3b24ee97730e56582d435920e7a598ad0992a487 Mon Sep 17 00:00:00 2001 From: dsc Date: Tue, 9 Nov 2010 22:25:05 -0800 Subject: [PATCH] Reorg of packages. --- src/tanks/game/game-map.js | 158 ++++++++++++++++++++++++++++++++++++++++++++ src/tanks/main-ui.js | 62 +++++++++++++++++ 2 files changed, 220 insertions(+), 0 deletions(-) create mode 100644 src/tanks/game/game-map.js create mode 100644 src/tanks/main-ui.js diff --git a/src/tanks/game/game-map.js b/src/tanks/game/game-map.js new file mode 100644 index 0000000..a6dc4f7 --- /dev/null +++ b/src/tanks/game/game-map.js @@ -0,0 +1,158 @@ + +Y(Game.prototype).extend({ + + initMap : function initMap(){ + // var self = this; + + this.byId = {}; + this.units = new Y.YArray(); + this.bullets = new Y.YArray(); + this.blockers = new Y.YArray(); + + this.pathmap = new PathMap(0,0, COLUMNS*REF_SIZE, ROWS*REF_SIZE, CAPACITY); + + var root = + this.root = + this.grid = + new Grid(COLUMNS,ROWS, CELL_SIZE) + .appendTo(this.viewport); + this.level = + new Level(this, root.layerWidth, root.layerHeight ) + .appendTo(this.root); + + // Agent.addEventListener('create', function(evt){ + // self.addAgent(evt.instance); + // }); + // Agent.addEventListener('destroy', function(evt){ + // self.killAgent(evt.instance); + // }); + }, + + + // *** Path Map Management *** // + + addBlocker : function addBlocker(agent){ + var bb = agent.boundingBox; + if (agent.blocking && bb) + agent.region = this.pathmap.set(bb.x1,bb.y1, bb.x2,bb.y2, agent); + return agent; + }, + + removeBlocker : function removeBlocker(agent){ + if (agent.region) + this.pathmap.remove(agent.region); + return agent; + }, + + updateBlocker : function updateBlocker(agent){ + this.removeBlocker(agent); + this.addBlocker(agent); + }, + + + // *** Agent Management *** // + + addUnit : function addUnit(unit, col,row){ + unit.game = this; + + if (col !== undefined) { + // Center unit in square + var sqX = (col || 0) * REF_SIZE + , sqY = (row || 0) * REF_SIZE + , x = sqX + (REF_SIZE-unit.width) /2 + , y = sqY + (REF_SIZE-unit.height)/2 ; + + unit.setLocation(x,y); + unit.render( this.level ); + } + + this.addBlocker(unit); + + if ( !this.byId[unit.id] ) { + this.byId[unit.id] = unit; + this.units.push(unit); + } + + return unit; + }, + + addAgent : function addAgent(agent){ + agent.game = this; + if (agent.id === undefined) return agent; + + this.addBlocker(agent); + + if ( !this.byId[agent.id] ) { + this.byId[agent.id] = agent; + if (agent instanceof Ability) + this.abilities.push(agent); + else + this.units.push(agent); + } + + return agent; + }, + + killAgent : function killAgent(agent){ + delete this.byId[agent.id]; + if (agent instanceof Ability) + this.abilities.remove(agent); + else + this.units.remove(agent); + + this.removeBlocker(agent); + return agent; + }, + + moveAgentTo : function moveAgentTo(agent, x,y){ + this.removeBlocker(agent); + agent.setLocation(x,y); + this.addBlocker(agent); + return agent; + }, + + getUnitAt : function getUnitAt(x,y){ + return this.grid.get(x,y); + }, + + getUnitsAt : function getUnitsAt(x1,y1, x2,y2){ + return this.grid.get(x1,y1, x2,y2); + } + +}); + +Y(Game.prototype).extend({ + + resize : function resize(){ + var ratio = COLUMNS / ROWS + , el = this.el + , p = el.parent() + , pw = p.width(), ph = p.height() + , pRatio = pw / ph + ; + + if ( ratio > pRatio ) + CELL_SIZE = Math.floor((pw-GRID_OFFSET*2) / COLUMNS); + else + CELL_SIZE = Math.floor((ph-GRID_OFFSET*2) / ROWS); + + SCALE = CELL_SIZE/REF_SIZE; + + var w = COLUMNS*CELL_SIZE + , h = ROWS*CELL_SIZE + , canvas = this.canvas[0]; + + this.el.width(w).height(h); + this.canvas.width(w).height(h); + canvas.width = w; + canvas.height = h; + + this.el.offset({ + top : (ph - h) / 2, + left : (pw - w) / 2 + }); + + this.ctx.scale(SCALE,SCALE); + } + +}); diff --git a/src/tanks/main-ui.js b/src/tanks/main-ui.js new file mode 100644 index 0000000..b719b68 --- /dev/null +++ b/src/tanks/main-ui.js @@ -0,0 +1,62 @@ + +// Set up UI listeners +function setupUI(){ + spark = LBT.loop.spark = new FpsSparkline(LBT.loop, '.fps-sparkline', 0,0); + + // Draw grid, initial units + LBT.root.draw(); + + setInterval(updateInfo, 1000); + updateInfo(); + + // Start button (click or return key) + $(document).bind('keydown', 'return', toggleGame); + $(document).bind('keydown', 'ctrl+o', toggleOverlay); + + // Fix grid-size on resize + // $(window).bind('resize', resizeGame); +} + +// Update performance info periodically +function updateInfo(){ + var loop = LBT.loop + , fps = loop.fps() + , n_units = LBT.units.size() + , n_projs = LBT.bullets.size() + ; + + $('#info [name=fps]').val( fps.toFixed(2) + " / " + loop.framerate ); + $('#info [name=frame]').val( loop.frametime().toFixed(3)+" ms" ); + $('#info #state').text( loop.running ? 'Running!' : ('Paused (tick '+TICKS+')') ); + + $('#info [name=objects]').val( n_units+n_projs ); + $('#info [name=units]').val( n_units ); + $('#info [name=bullets]').val( n_projs ); + + spark.drawTimes(); + + return false; +} + +function toggleGame(evt){ + if (LBT.loop.running) + LBT.stop(); + else + LBT.start(); + + updateInfo(); +} + +function toggleOverlay(evt){ + LBT.showOverlay = !(LBT.showOverlay); +} + +function resizeGame(evt){ + LBT.resize(evt); + + if (!LBT.loop.running) { + LBT.start(); + LBT.stop(); + } +} + -- 1.7.0.4