From: dsc Date: Sun, 9 Jan 2011 15:22:23 +0000 (-0800) Subject: Adds destructable walls. X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=978c353d2a3000bf01a7686e588f56365c48afe6;p=tanks.git Adds destructable walls. --- diff --git a/data/config.yaml b/data/config.yaml index 6b4336d..a52c410 100644 --- a/data/config.yaml +++ b/data/config.yaml @@ -1,18 +1,20 @@ game: timeDilation : 1.0 gameoverDelay : 1000 - refSize : &ref_size 50 debug: showFpsGraph : false # createGridTable : false # showGridCoords : false +map: + refSize : &ref_size 50 + minSplitSize : 12.5 +pathing: + pathSquare : *ref_size + overlayAiPaths : false + overlayPathmap : false + traceTrajectories : false ui: createGridCanvas : true overlayOnPause : true showAttackCooldown : false showCountdown : true -pathing: - gridSquare : *ref_size - overlayAiPaths : false - overlayPathmap : false - traceTrajectories : false diff --git a/data/types/levels.yaml b/data/types/levels.yaml index 86b4a1f..dc0aa88 100644 --- a/data/types/levels.yaml +++ b/data/types/levels.yaml @@ -25,10 +25,6 @@ types: - type: wall args: [300,350, 50,100] - type: wall - args: [50,350, 100,100] - - type: wall - args: [150,300, 50,50] - - type: wall args: [100,100, 50,50] - type: fence args: [360,210, 130,30] @@ -38,16 +34,20 @@ types: args: [110,210, 30,30] - type: fence args: [210,210, 30,30] + - type: drywall + args: [50,350, 100,100] + - type: drywall + args: [150,300, 50,50] units: - type: player align: 1 - loc: [325,475] + loc: [175,475] - type: blue align: 1 - loc: [175,475] - - type: green - align: 2 - loc: [25,375] + loc: [325,475] + # - type: green + # align: 2 + # loc: [25,375] - type: green align: 2 loc: [75,25] diff --git a/src/Y/types/string.cjs b/src/Y/types/string.cjs index da020f3..676a1a9 100644 --- a/src/Y/types/string.cjs +++ b/src/Y/types/string.cjs @@ -123,7 +123,7 @@ YCollection.subclass('YString', function(YString){ function(out, ch, i){ if ( !nameUnsafe && symbolPat.test(ch) ) return out; - if ( upperPat.test(ch) ) + if ( upperPat.test(ch) && i !== 0 ) out += '_'; return out + ch.toLowerCase(); }, ''); diff --git a/src/ezl/loc/boundingbox.cjs b/src/ezl/loc/boundingbox.cjs index 2317789..995649b 100644 --- a/src/ezl/loc/boundingbox.cjs +++ b/src/ezl/loc/boundingbox.cjs @@ -1,9 +1,6 @@ var Y = require('Y').Y , Loc = require('ezl/loc/loc').Loc -, math = require('ezl/math') -, Vec = math.Vec -, Line = math.Line -, Rect = math.Rect +, Rect = require('ezl/math/rect').Rect , X1 = 0, Y1 = 1 , X2 = 2, Y2 = 3 diff --git a/src/ezl/math/index.cjs b/src/ezl/math/index.cjs index 57fe480..a891b65 100644 --- a/src/ezl/math/index.cjs +++ b/src/ezl/math/index.cjs @@ -1,11 +1,6 @@ -var Y = require('Y').Y -, vec = require('ezl/math/vec') -; - -Y(exports, { - 'Vec' : vec.Vec, - 'Line' : require('ezl/math/line').Line, - 'Rect' : require('ezl/math/rect').Rect, +//#exports clamp lerp +require('Y').Y.core +.extend(exports, { 'clamp' : function clamp(value, min, max) { return Math.min(Math.max(value, min), max); @@ -13,15 +8,6 @@ Y(exports, { 'lerp' : function lerp(x, a, b) { return a + x*(b - a); - }, - - 'reflect' : function reflect(v, line){ - var dot = vec.dot - , basev = vec.difference(v, line.p1); - return line.vec() - .scale(2 * dot(basev,line) / dot(line,line)) - .subtract(basev) - .add(line.p1); } }); diff --git a/src/ezl/math/line.cjs b/src/ezl/math/line.cjs index a31eb95..68d1236 100644 --- a/src/ezl/math/line.cjs +++ b/src/ezl/math/line.cjs @@ -116,6 +116,26 @@ Vec.subclass('Line', { return this.calcY(x) === y; }, + intersection : function intersection(line){ + var pt + , mA = this.slope, mB = line.slope + , xA = this.xint, xB = line.xint + , yA = this.yint, yB = line.yint + ; + + if ( mA === mB ) return undefined; + if ( this.equals(line) ) return null; + if ( mA === 0 ) return line.pointAtY(yA); + if ( mB === 0 ) return this.pointAtY(yB); + if ( !isFinite(mA) ) return line.pointAtX(xA); + if ( !isFinite(mB) ) return this.pointAtX(xB); + + var x = (yB - yA) / (mA - mB); + return this.pointAtX(x); + // var y = (xB - xA) * dSlope; + // return this.pointAtY(y); + }, + isWithin : function isWithin(pt, w,h){ if ( !Y.isNumber(w) ){ h = w.height; w = w.width; @@ -153,7 +173,8 @@ Vec.subclass('Line', { }, toString : function(){ - return '['+this.p1+', '+this.p2+', slope='+this.slope.toFixed(3)+']'; + // return '['+this.p1+', '+this.p2+', slope='+this.slope.toFixed(3)+']'; + return this.className+'(slope='+this.slope.toFixed(3)+', xint='+this.xint.toFixed(3)+', yint='+this.yint.toFixed(3)+')'; } }); diff --git a/src/ezl/math/vec.cjs b/src/ezl/math/vec.cjs index 3a9deb0..6c9f154 100644 --- a/src/ezl/math/vec.cjs +++ b/src/ezl/math/vec.cjs @@ -1,4 +1,6 @@ -var Y = require('Y').Y +var Y = require('Y').Y +, math = require('ezl/math') + , _X = 0, _Y = 1 , @@ -104,10 +106,7 @@ new Y.Class('Vec', [], { }); -// Can't import from math due to deps -function lerp(x, a, b) { return a + x*(b - a); } - -Y.extend(exports, { +var vec = { sum : function sum(a, b) { return new Vec(a[_X]+b[_X], a[_Y]+b[_Y]); }, @@ -121,8 +120,8 @@ Y.extend(exports, { }, lerp : function vecLerp(x, a, b) { - return new Vec( lerp(x, a[_X], b[_X]), - lerp(x, a[_Y], b[_Y]) ); + return new Vec( math.lerp(x, a[_X], b[_X]), + math.lerp(x, a[_Y], b[_Y]) ); }, manhattan: function manhattan(x1,y1, x2,y2) { @@ -133,6 +132,18 @@ Y.extend(exports, { var d1 = Math.abs(x2 - x1) , d2 = Math.abs(y2 - y1) ; return d1 + d2; + }, + + reflect : function reflect(v, line){ + var basev = vec.difference(v, line.p1); + return line.vec() + .scale(2 * vec.dot(basev,line) / vec.dot(line,line)) + .subtract(basev) + .add(line.p1); } -}); +}; + +//#exports sum difference dot lerp manhattan reflect +Y.extend(exports, vec); + diff --git a/src/ezl/shape/line.cjs b/src/ezl/shape/line.cjs index d347c7a..3d592ea 100644 --- a/src/ezl/shape/line.cjs +++ b/src/ezl/shape/line.cjs @@ -1,7 +1,7 @@ //#ensure "jquery" - var Y = require('Y').Y -, math = require('ezl/math') + +, line = require('ezl/math/line') , Layer = require('ezl/layer/layer').Layer , Shape = require('ezl/shape/shape').Shape , @@ -40,7 +40,7 @@ Shape.subclass('Line', { this.x1 = pos.left; this.x2 += this.x1; this.y1 = pos.top; this.y2 += this.y1; - this.line = new math.Line(this.x1,this.y1, this.x2,this.y2, (this.line||{}).tdist); + this.line = new line.Line(this.x1,this.y1, this.x2,this.y2, (this.line||{}).tdist); return this; }, diff --git a/src/tanks/config.cjs b/src/tanks/config.cjs index b128344..a30c26f 100644 --- a/src/tanks/config.cjs +++ b/src/tanks/config.cjs @@ -4,7 +4,7 @@ var Y = require('Y').Y , ensure = require('Y/types/object').ensure , Config = require('Y/modules/y.config').Config -, Vec = require('ezl/math').Vec +, Vec = require('ezl/math/vec').Vec , DataFile = require('ezl/util/data/datafile').DataFile , Loader = require('ezl/util/data/loader').Loader , @@ -21,11 +21,11 @@ exports['config'] = new Config(defaults) ; // Updates the midpoint square when square-size changes -config.addEventListener('set:pathing.gridSquare', function(evt){ +config.addEventListener('set:pathing.pathSquare', function(evt){ var sq = evt.data.newval; - config.set('pathing.gridSquareMid', new Vec(sq/2, sq/2)); + config.set('pathing.pathSquareMid', new Vec(sq/2, sq/2)); }); -config.set('pathing.gridSquare', config.get('pathing.gridSquare')); +config.set('pathing.pathSquare', config.get('pathing.pathSquare')); /// Load Data Files /// diff --git a/src/tanks/effects/stat.cjs b/src/tanks/effects/stat.cjs index 33ec019..aa3f972 100644 --- a/src/tanks/effects/stat.cjs +++ b/src/tanks/effects/stat.cjs @@ -19,12 +19,14 @@ Y.subclass('Stat', { base : 0, val : 0, max : 0, + ratio : 1.0, init : function initStat(base, val, max, isInteger){ this.base = base; this.val = val !== undefined ? val : base; this.max = max !== undefined ? max : base; + this.ratio = this.val / this.max; this.integer = !!isInteger; }, @@ -48,6 +50,7 @@ Y.subclass('Stat', { modify : function modify(dv){ var v = Math.min(this.max, this.val+dv); this.val = (this.integer ? Math.round(v) : v); + this.ratio = this.val / this.max; return this; }, @@ -84,6 +87,7 @@ Y.subclass('Stat', { break; } this.val = (this.integer ? Math.round(v) : v); + this.ratio = this.val / this.max; return this; }, diff --git a/src/tanks/game.cjs b/src/tanks/game.cjs index b31c13d..8dfac07 100644 --- a/src/tanks/game.cjs +++ b/src/tanks/game.cjs @@ -146,20 +146,9 @@ Y.subclass('Game', { return animation; }, - addThing : function addThing(unit, x,y){ - if (unit instanceof Event) - unit = unit.trigger; - + noteThing : function noteThing(unit){ unit.game = this; - - if (x !== undefined) { - unit.position(x,y); - unit.render( this.level ); - } else if ( unit.isRenderable ) { - unit.render( this.level ); - } - - this.pathmap.addBlocker(unit); + if (unit.dead) return unit; if ( unit.active && !this.byId[unit.__id__] ) { this.byId[unit.__id__] = unit; @@ -180,6 +169,26 @@ Y.subclass('Game', { return unit; }, + addThing : function addThing(unit, x,y){ + if (unit instanceof Event) + unit = unit.trigger; + + if (unit.dead) return unit; + + this.noteThing(unit); + + if (x !== undefined) { + unit.position(x,y); + unit.render( this.level ); + } else if ( unit.isRenderable ) { + unit.render( this.level ); + } + + this.pathmap.addBlocker(unit); + + return unit; + }, + killThing : function killThing(unit){ if (unit instanceof Event) unit = unit.data.unit; diff --git a/src/tanks/map/drywall.cjs b/src/tanks/map/drywall.cjs new file mode 100644 index 0000000..d4f8156 --- /dev/null +++ b/src/tanks/map/drywall.cjs @@ -0,0 +1,112 @@ +var Y = require('Y').Y +, clamp = require('ezl/math').clamp +, Vec = require('ezl/math/vec').Vec + +, config = require('tanks/config').config +, Wall = require('tanks/map/wall').Wall +, + + +Drywall = +exports['Drywall'] = +Wall.subclass('Drywall', { + minSplitSize : 25, + + isReflective : false, + isBoundary : false, + isSplittable : true, + + fillColors : [[139,49,20], [245,154,180], [255,246,174]], + + fillStyle : 'rgba(249,190,0, 0.25)', + strokeStyle : 'rgba(249,190,0, 0.5)', + lineWidth : 1, + + stats : { + hp : 3, + }, + + + init : function initDrywall(x,y, w,h, hp){ + this.stats = Y.extend({}, this.stats); + this.stats.hp = hp || this.stats.hp; + + var line = Math.round(Math.min(w,h) * 0.2); + this.lineWidth = clamp(line, 1, 3); + + Wall.init.call(this, x,y, w,h, this.isBoundary); + this.updateColor(); + }, + + updateColor : function updateColor(){ + var L = this.fillColors.length-1 + , i = clamp(Math.floor(this.stats.hp.ratio*L), 0, L) + , color = this.fillColors[i] + ; + this.fillStyle = 'rgba('+color.join(',')+', 0.25)'; + this.strokeStyle = 'rgba('+color.join(',')+', 0.50)'; + console.log(this+'color = '+this.fillStyle+' ('+i+')'); + return this; + }, + + dealDamage : function dealDamage(d, source){ + Wall.fn.dealDamage.apply(this, arguments); + if (!this.dead) { + var old = this.fillStyle; + if (old !== this.updateColor().fillStyle) + this.render(); + } + return this; + }, + + split : function split(gap){ + if (!this.isSplittable || this.width <= this.minSplitSize) + return this; + + var _x = this.loc.x, _y = this.loc.y + , mx = _x+this.width, my = _y+this.height + , size = this.width * 0.5 + , gapX = gap ? gap.x : Infinity + , gapY = gap ? gap.y : Infinity + , hp = Math.max(Math.round(this.stats.hp.max * 0.5), 1.0) + + , x, y, nextX, nextY, wall + ; + + // console.group(this+'.split()'); + // console.log('gap: ('+gapX+','+gapY+')'); + // console.log('loc: '+this.loc); + // console.log('dim: w='+this.width+'; h='+this.height+'; size='+size); + // console.log('max: x='+mx+'; y='+my); + for (x = _x; x= bsize ) + if ( unit.dead && !unit.isWall && asize >= bsize ) end = asize; var dur = ex.timeBase + ex.timeInc*end; diff --git a/src/tanks/thing/thing.cjs b/src/tanks/thing/thing.cjs index 975e3d7..5487ab5 100644 --- a/src/tanks/thing/thing.cjs +++ b/src/tanks/thing/thing.cjs @@ -88,7 +88,7 @@ new evt.Class('Thing', { init : function init(align){ - this.align = align || 0; + this.align = align || this.align; this.createBoundingBox(); this.createStats(); this.createCooldowns(); @@ -111,10 +111,10 @@ new evt.Class('Thing', { return this; }, - destroy : function destroy(){ + destroy : function destroy(killer, side){ if (this.dead) return this; this.dead = true; - return this.remove().fire('destroy', this, { 'unit':this }); + return this.remove().fire('destroy', this, { 'unit':this, 'killer':killer, 'side':side }); }, remove : function remove(){ @@ -122,10 +122,10 @@ new evt.Class('Thing', { return this; }, - dealDamage : function dealDamage(d, source){ + dealDamage : function dealDamage(d, source, side){ this.stats.hp.modify(-d); if (this.stats.hp.val <= 0) - this.destroy(); + this.destroy(source, side); return this; }, diff --git a/src/tanks/ui/pathmapui.cjs b/src/tanks/ui/pathmapui.cjs index ff188e2..cf27951 100644 --- a/src/tanks/ui/pathmapui.cjs +++ b/src/tanks/ui/pathmapui.cjs @@ -15,7 +15,7 @@ PathMapUI = exports['PathMapUI'] = Rect.subclass('PathMapUI', { // Config - gridSquare : null, + pathSquare : null, overlayPathmap : null, overlayAiPaths : null, @@ -109,7 +109,7 @@ Rect.subclass('PathMapUI', { pathWithUI : function pathWithUI(agent, x,y){ var pm = this.pathmap , path = this._path.call(pm, agent, x,y) - , start = vec.sum(pm._getSquare(agent.loc), pm.gridSquareMid) + , start = vec.sum(pm._getSquare(agent.loc), pm.pathSquareMid) ; if (this.overlayAiPaths) this.drawPath(agent, start, Y(path)); @@ -175,7 +175,7 @@ Rect.subclass('PathMapUI', { }, drawPathStep : function drawPathStep(ctx, p, idx){ - var SIZE = this.gridSquare + var SIZE = this.pathSquare , off = SIZE*(idx === -1 ? 0.15 : 0.3) , r = SIZE/2 - off ; @@ -215,6 +215,6 @@ Rect.subclass('PathMapUI', { config.updateOnChange( - ['pathing.gridSquare', 'pathing.overlayPathmap', 'pathing.overlayAiPaths'], + ['pathing.pathSquare', 'pathing.overlayPathmap', 'pathing.overlayAiPaths'], PathMapUI.fn); diff --git a/www/test/math/test-math.js b/www/test/math/test-math.js index 8a59783..1d6db3f 100644 --- a/www/test/math/test-math.js +++ b/www/test/math/test-math.js @@ -107,7 +107,7 @@ function addPoint(x,y){ , c = drawPoint(v, null, '#552F74'); } - var rv = math.reflect(c.vec,line) + var rv = vec.reflect(c.vec,line) , rc = c.reflected = drawPoint(rv, null, '#F25522'); points.push(c); return c;