From: dsc Date: Sat, 12 Feb 2011 08:57:44 +0000 (-0800) Subject: Checkpoint. Fixes updates to evt.Class init wrappers. X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=eefe0e16ea957afa6a21edec8c295522bae6ec8b;p=tanks.git Checkpoint. Fixes updates to evt.Class init wrappers. --- diff --git a/src/Y/utils.cjs b/src/Y/utils.cjs index 649becd..f517d16 100644 --- a/src/Y/utils.cjs +++ b/src/Y/utils.cjs @@ -23,7 +23,7 @@ function bindAll(o, names){ if (!o) return o; if (arguments.length === 1) - names = core.map(o, op.nth(1)); + names = Object.keys(o); else if ( !isArray(names) ) names = slice.call(arguments, 1); core.forEach(names, bindName, o); diff --git a/src/evt.cjs b/src/evt.cjs index 5ed4e9e..9636f40 100644 --- a/src/evt.cjs +++ b/src/evt.cjs @@ -73,6 +73,7 @@ function ConstructorTemplate() { , cls = A.callee , caller = unwrap(cls.caller) , args = (caller === instantiate) ? A[0] : A + , init = instance.init ; // Not subclassing @@ -88,9 +89,8 @@ function ConstructorTemplate() { 'args' : Y(args) }, instance); - var initialise = instance.__initialise__; - if ( typeof initialise == 'function' ) - initialise.apply(instance, args); + if ( typeof init == 'function' ) + init.apply(instance, args); cls.emit('created', instance, { 'instance' : instance, @@ -113,21 +113,20 @@ exports['createInitialise'] = function createInitialise(cls){ function initialise(){ var instance = this - , proto = cls.fn - , binds = proto.__bind__ // list of names to bind - , init = proto.init - , inits = proto.__inits__ + , proto = cls.fn // use prototype so super() calls pick up the right lists + , binds = proto.__bind__ // list of names to bind + , inits = proto.__inits__ // init functions + , super = cls.__super__ , args = Y(arguments) ; if (binds) Y.bindAll(instance, binds); - if ( typeof init == 'function' && init !== arguments.callee ) - init.apply(instance, args); - if ( inits && inits.length ) inits.invoke('apply', instance, args); + else if (super && super.init ) + super.init.apply(instance, args); instance.emit('init', instance, { 'instance' : instance, @@ -227,13 +226,14 @@ function Class(className, Parent, members){ ; // Fix Constructors - NewClass.__super__ = SuperClass; // don't override NewClass.constructor -- it should be Function - NewClass.__bases__ = NewClass.fn.__bases__ = Y(mixins).concat( [SuperClass], SuperClass.__bases__ || [ Class, Object ] ); + NewClass.__super__ = SuperClass; // don't override NewClass.constructor -- it should be Function + NewClass.__bases__ = prototype.__bases__ = Y(mixins).concat( [SuperClass], SuperClass.__bases__ || [ Class, Object ] ); prototype.constructor = prototype.__class__ = NewClass; - prototype.__inits__ = new Y.YArray(); - NewClass.init = prototype.__initialise__ = createInitialise(NewClass); - + var initialise = NewClass.init = prototype.init = createInitialise(NewClass) + , inits = NewClass.__inits__ = prototype.__inits__ = new Y.YArray(); + if (members.init) NewClass.__inits__.push(members.init); + delete members.init; // Record for metaprogramming KNOWN_CLASSES[className] = NewClass; @@ -265,7 +265,11 @@ function Class(className, Parent, members){ if ( typeof members == 'function' ) { // body fn is responsible for calling mixin, attaching statics, etc members.call(prototype, NewClass); - + var init = prototype.init; + if (init) { + NewClass.__inits__.remove(init).unshift(init); + prototype.init = initialise; + } // Or add new instance methods } else { for (var k in members) @@ -393,7 +397,7 @@ function mixin(cls, _mxn){ var mproto = (typeof mxn === "function") ? mxn.fn : mxn , statics = mxn.__static__ || {} , binds = mxn.__bind__ - , init = mproto.init + , inits = mproto.__inits__ , onCreate = mproto.onCreate , firstMix = (bases && bases.indexOf(mxn) === -1) ; @@ -416,8 +420,8 @@ function mixin(cls, _mxn){ cls.__bind__ = proto.__bind__ = cbinds.concat(binds).unique(); // Wrap init call - if ( typeof init == 'function' ) - cinits.remove(init).unshift(init); + if ( inits.length ) + cinits.extend(inits); // Only perform these actions the first time we mix into this class if (!firstMix) @@ -472,4 +476,4 @@ exports['instantiate'] = instantiate; exports['fabricate'] = Y.fabricate; exports['lookupClass'] = lookupClass; exports['mixin'] = mixin; -exports['aggregate'] = aggregate; \ No newline at end of file +exports['aggregate'] = aggregate; diff --git a/src/ezl/layer/layer.cjs b/src/ezl/layer/layer.cjs index 7db1b84..a17d7e6 100644 --- a/src/ezl/layer/layer.cjs +++ b/src/ezl/layer/layer.cjs @@ -11,8 +11,7 @@ evt.subclass('Layer', { init : function initLayer(props, attrs, html){ - this.setup(props, attrs, html); - + // Layer.init.call(this, props, attrs, html); } }) diff --git a/src/ezl/layer/layerable.cjs b/src/ezl/layer/layerable.cjs index 85141a6..64ec7f6 100644 --- a/src/ezl/layer/layerable.cjs +++ b/src/ezl/layer/layerable.cjs @@ -70,7 +70,7 @@ Mixin.subclass('Layerable', { /// Setup /// - setup : function setupLayer(props, attrs, html){ + init : function initLayerable(props, attrs, html){ if (props !== undefined && props !== null) { switch (typeof props) { case 'boolean' : this.hasCanvas = props; break; diff --git a/src/tanks/game.cjs b/src/tanks/game.cjs index a6ca65a..e587d9e 100644 --- a/src/tanks/game.cjs +++ b/src/tanks/game.cjs @@ -28,6 +28,8 @@ var Y = require('Y').Y Game = exports['Game'] = evt.subclass('Game', { + __bind__ : [ 'setGame', 'addUnit', 'killUnit', 'tick' ], + // Config gameoverDelay : null, @@ -39,7 +41,7 @@ evt.subclass('Game', { init : function initGame(levelId){ - Y.bindAll(this); + // Y.bindAll(this); if (levelId) this.levelId = levelId; diff --git a/src/tanks/inventory/bag.cjs b/src/tanks/inventory/bag.cjs index ee007ad..c65bd52 100644 --- a/src/tanks/inventory/bag.cjs +++ b/src/tanks/inventory/bag.cjs @@ -16,6 +16,8 @@ Item.subclass('Bag', { /// Setup /// hasUI : true, reqs : null, + max : 1, + cols : 4, // highlightIconOnOpen : true, // Whether to highlight the inventory icon when bag is open /// Bookkeeping /// @@ -30,24 +32,28 @@ Item.subclass('Bag', { Item.init.call(this); }, - /** * @return {this} */ - showUI : function showUI(){ - if (!this.hasUI) - return this; - if (!this.ui) + createUI : function createUI(){ + if (this.hasUI && !this.ui) this.ui = new ContainerUI(this.owner, this, { 'name':this.bagName }) + .hide() .appendTo(this.owner.game.root) .refresh(); - this.ui.show(); + return this; + }, + + /** + * @return {this} + */ + showUI : function showUI(){ + if (this.hasUI) this.createUI().ui.show(); return this; }, hideUI : function hideUI(){ - if (this.ui) - this.ui.hide(); + if (this.ui) this.ui.hide(); return this; }, @@ -59,8 +65,7 @@ Item.subclass('Bag', { onAcquired : function onAcquired(evt, container){ Item.fn.onAcquired.apply(this, arguments); - // this.parent = container; - // this.showUI(); + this.createUI(); }, onLost : function onLost(evt){ diff --git a/src/tanks/inventory/inventory.cjs b/src/tanks/inventory/inventory.cjs index 9ea2070..08146dd 100644 --- a/src/tanks/inventory/inventory.cjs +++ b/src/tanks/inventory/inventory.cjs @@ -60,13 +60,13 @@ evt.subclass('Inventory', { this.bagbag = new BagBag(this, owner); - // TODO: Add saved items - var backpack = tanks.data.items.backpack.instantiate(); - this.bagbag.addItem(backpack); + // TODO: Restore saved items + var backpack = this.defaultBag = tanks.data.items.backpack.instantiate(); + this.addBag(backpack); this.bagbag.ui.refresh(); // instantiate: - // - equip slots + // - equip slots (w bookkeeping: equipment, equipBags) // - belt }, @@ -103,9 +103,16 @@ evt.subclass('Inventory', { * @return {this} */ addItem : function addItem(item, bag){ + if (!item) + throw new Error('Cannot move item: no item supplied!'); + bag = this.getBag(bag, true); if ( !bag ) throw new Error('Cannot add item: unknown bag '+bag+'!'); + if ( !bag.checkReqs(item) ) + throw new Error('Cannot move item '+item+' to container '+bag+'!'); + + this.items[item.__id__] = item; return bag.addItem(item); }, @@ -125,6 +132,7 @@ evt.subclass('Inventory', { if ( !bag.checkReqs(item) ) throw new Error('Cannot move item '+item+' to container '+bag+'!'); + this.items[item.__id__] = item; idx = (idx !== undefined ? idx : bag._getEmptySlot()); return bag.moveItem(item, idx); }, @@ -138,10 +146,12 @@ evt.subclass('Inventory', { removeItem : function removeItem(item, bag, options){ if ( !this.hasItem(item, bag) ) return null; + item = this.getItem(item, bag); if ( !item ) return null; - + + delete this.items[item.__id__]; return item.inv.bag.removeItem(item, options); // TODO: Fire event? }, @@ -251,6 +261,13 @@ evt.subclass('Inventory', { }, addBag : function addBag(bag){ + if (!bag) // TODO: Errorz! + return this; + var id = bag.__id__; + this.items[id] = bag; + this.bags[id] = bag; + if (bag.bagName) + this.namedBags[bag.bagName] = bag; this.bagbag.addItem(bag); return this; }, diff --git a/src/tanks/map/level.cjs b/src/tanks/map/level.cjs index da9e1ae..c34779f 100644 --- a/src/tanks/map/level.cjs +++ b/src/tanks/map/level.cjs @@ -20,7 +20,7 @@ var Y = require('Y').Y Level = exports['Level'] = -new evt.Class('Level', { +Rect.subclass('Level', { __mixins__ : [ Speciated ], _layerClasses : 'ezl layer level', @@ -28,9 +28,11 @@ new evt.Class('Level', { this.game = game; this.map = new Map(0,0, this.width, this.height, capacity, buffer_size); - var shape = this.shape = new Rect(this.width,this.height).fill('transparent'); - shape.layer.attr('class', this._layerClasses); - this.bbox = shape.bbox; + Rect.init.call(this, this.width,this.height); + this.fill('transparent'); + // var shape = this.shape = new Rect(this.width,this.height).fill('transparent'); + // shape.layer.attr('class', this._layerClasses); + // this.bbox = shape.bbox; }, setup : function setup(repo){ diff --git a/src/tanks/thing/item.cjs b/src/tanks/thing/item.cjs index 860ee8a..58de5ea 100644 --- a/src/tanks/thing/item.cjs +++ b/src/tanks/thing/item.cjs @@ -63,7 +63,8 @@ Thing.subclass('Item', { this.currentPassive = new Y.YArray(); this.inv = new ContainerMeta(); - this.activateGauge = new CooldownGauge(this.cooldowns.activate, REF_SIZE+1,REF_SIZE+1); + if (this.cooldowns.activate) + this.activateGauge = new CooldownGauge(this.cooldowns.activate, REF_SIZE+1,REF_SIZE+1); this.on('collide', this.onCollide); this.on('item.acquire', this.onAcquired); @@ -126,11 +127,11 @@ Thing.subclass('Item', { return this; }, - // TODO: Add to correct container onCollide : function onCollide(evt){ - var unit = evt.data.unit; - if (unit.hasInventory && unit.canAddItem()) - unit.addItem(this); + var unit = evt.data.unit + , inv = unit.inventory + if ( inv && inv.canAddItem(this) ) + inv.addItem(this); }, onAcquired : function onAcquired(evt, container){ diff --git a/src/tanks/ui/inventory/containerui.cjs b/src/tanks/ui/inventory/containerui.cjs index 0848264..e632b5e 100644 --- a/src/tanks/ui/inventory/containerui.cjs +++ b/src/tanks/ui/inventory/containerui.cjs @@ -155,7 +155,7 @@ Layer.subclass('ContainerUISlot', { this.layer.addClass('occupied'); var icon = item.art && item.art.inv_icon - , src = this.itemEl = + , img = this.img = jQuery('') .width(this.artWidth) .height(this.artHeight) @@ -169,13 +169,15 @@ Layer.subclass('ContainerUISlot', { revert : 'invalid', zIndex : 10 }) - .attr('title', ''+item.name+'
'+item.desc) + .attr('title', ''+item.name+''+(item.desc ? '
'+item.desc : '')) .tipsy({ 'html':true, 'gravity':$.fn.tipsy.autoNS }) ; - if (icon) src.attr('src', icon); + if (icon) + img.attr('src', icon); - this.inner.append(item.activateGauge); + if (item.activateGauge) + this.inner.append(item.activateGauge); this.inner.layer.bind('click', this.onActivate); return this; @@ -198,7 +200,7 @@ Layer.subclass('ContainerUISlot', { } this.item = null; - this.itemEl = null; + this.img = null; return this; }, diff --git a/src/tanks/ui/inventory/equipslot.cjs b/src/tanks/ui/inventory/equipslot.cjs index bbce0a8..0056eaa 100644 --- a/src/tanks/ui/inventory/equipslot.cjs +++ b/src/tanks/ui/inventory/equipslot.cjs @@ -146,7 +146,8 @@ HtmlLayer.subclass('BackpackSlot', { if (icon) src.attr('src', icon); - this.inner.append(item.activateGauge); + if (item.activateGauge) + this.inner.append(item.activateGauge); this.inner.layer.bind('click', this.onActivate); return this; diff --git a/src/tanks/ui/main.cjs b/src/tanks/ui/main.cjs index f306a6b..e30b087 100644 --- a/src/tanks/ui/main.cjs +++ b/src/tanks/ui/main.cjs @@ -53,7 +53,7 @@ function main(){ $('#loading').clone() .attr('id', 'pause') .hide() - .css({ 'top':'1em', 'left':'1em', 'margin':0, 'width':'auto' }) + // .css({ 'top':'1em', 'left':'1em', 'margin':0, 'width':'auto' }) .appendTo( $('body') ) .find('.box').html('

Paused!

'); diff --git a/www/css/lttl.css b/www/css/lttl.css index 210cea6..55e1cb7 100644 --- a/www/css/lttl.css +++ b/www/css/lttl.css @@ -40,6 +40,7 @@ td { text-align:center; vertical-align:middle; } .countdown { position:fixed; overflow:hidden; z-index:2000; text-align:center; border:10px solid #fff; color:#fff; } #loading h2 { text-align:center; } #welcome .box { cursor:pointer; } +#pause { top:1em; left:1em; margin:0; width:auto; } #notes { /* position:fixed; top:4em; right:1em; color:#BFBFBF;*/ } #notes ul, #notes ol, #notes li { list-style:circle ! important; } #notes li { margin-left:1em; } @@ -61,7 +62,7 @@ td { text-align:center; vertical-align:middle; } .debug { z-index:5000; } .debug .inner { top:0; right:0; padding:1em; } -#info { position:absolute; top:1em; right:1em; } +#info { position:absolute; top:1em; left:1em; } #info .box { width:150px; } #info #state { font-weight:bold; } #info label { display:block; float:left; width:4em; margin-right:0.5em; color:#787878; }