Checkpoint. Fixes updates to evt.Class init wrappers.
authordsc <david.schoonover@gmail.com>
Sat, 12 Feb 2011 08:57:44 +0000 (00:57 -0800)
committerdsc <david.schoonover@gmail.com>
Sat, 12 Feb 2011 08:57:44 +0000 (00:57 -0800)
13 files changed:
src/Y/utils.cjs
src/evt.cjs
src/ezl/layer/layer.cjs
src/ezl/layer/layerable.cjs
src/tanks/game.cjs
src/tanks/inventory/bag.cjs
src/tanks/inventory/inventory.cjs
src/tanks/map/level.cjs
src/tanks/thing/item.cjs
src/tanks/ui/inventory/containerui.cjs
src/tanks/ui/inventory/equipslot.cjs
src/tanks/ui/main.cjs
www/css/lttl.css

index 649becd..f517d16 100644 (file)
@@ -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);
index 5ed4e9e..9636f40 100644 (file)
@@ -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;
index 7db1b84..a17d7e6 100644 (file)
@@ -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);
     }
     
 })
index 85141a6..64ec7f6 100644 (file)
@@ -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;
index a6ca65a..e587d9e 100644 (file)
@@ -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;
         
index ee007ad..c65bd52 100644 (file)
@@ -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){
index 9ea2070..08146dd 100644 (file)
@@ -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;
     },
index da9e1ae..c34779f 100644 (file)
@@ -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){
index 860ee8a..58de5ea 100644 (file)
@@ -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){
index 0848264..e632b5e 100644 (file)
@@ -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('<img />')
                 .width(this.artWidth)
                 .height(this.artHeight)
@@ -169,13 +169,15 @@ Layer.subclass('ContainerUISlot', {
                 revert : 'invalid',
                 zIndex : 10
             })
-            .attr('title', '<b>'+item.name+'</b><br/>'+item.desc)
+            .attr('title', '<b>'+item.name+'</b>'+(item.desc ? '<br/>'+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;
     },
     
index bbce0a8..0056eaa 100644 (file)
@@ -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;
index f306a6b..e30b087 100644 (file)
@@ -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('<h1>Paused!</h1>');
     
index 210cea6..55e1cb7 100644 (file)
@@ -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; }