From c363f2dcd08e98983319b0edfca98b04a3b425f7 Mon Sep 17 00:00:00 2001 From: deathcap Date: Mon, 20 Jan 2014 20:55:58 -0800 Subject: [PATCH] Refactor into common/client classes for client/server support Example for https://github.com/deathcap/voxpopuli/issues/26 --- index.coffee | 49 ++++++++++++++++++-------- index.js | 99 ++++++++++++++++++++++++++++++++-------------------- 2 files changed, 96 insertions(+), 52 deletions(-) diff --git a/index.coffee b/index.coffee index 1a5589d..f4ab698 100644 --- a/index.coffee +++ b/index.coffee @@ -5,21 +5,43 @@ InventoryWindow = require 'inventory-window' ever = require 'ever' module.exports = (game, opts) -> - new InventoryHotbar(game, opts) + if game.isClient + new InventoryHotbarClient game, opts + else + new InventoryHotbarCommon game, opts module.exports.pluginInfo = loadAfter: ['voxel-carry', 'voxel-registry'] -class InventoryHotbar extends EventEmitter +class InventoryHotbarCommon extends EventEmitter constructor: (@game, opts) -> opts ?= {} @inventory = game.plugins?.get('voxel-carry')?.inventory ? opts.inventory ? throw 'voxel-inventory-hotbar requires "voxel-carry" plugin or "inventory" option set to inventory instance' - registry = game.plugins?.get('voxel-registry') + @selectedIndex = 0 + + enable: () -> + + disable: () -> + + give: (itemPile) -> @inventory.give itemPile + take: (itemPile) -> @inventory.take itemPile + + # take some items from the pile the player is currently holding + takeHeld: (count=1) -> @inventory.takeAt @selectedIndex, count + + # get the pile of items the player is currently holding + held: () -> + @inventory.get @selectedIndex +class InventoryHotbarClient extends InventoryHotbarCommon + constructor: (@game, opts) -> + super @game, opts + + registry = game.plugins?.get('voxel-registry') windowOpts = opts.windowOpts ? {} windowOpts.registry ?= registry if registry - windowOpts.inventory ?= @inventory + windowOpts.inventory ?= @inventory windowOpts.inventorySize ?= opts.inventorySize ? @inventory.size() windowOpts.width ?= opts.width ? windowOpts.inventorySize # default to one row @inventoryWindow = new InventoryWindow windowOpts @@ -40,28 +62,25 @@ class InventoryHotbar extends EventEmitter enable: () -> @inventoryWindow.container.style.visibility = '' - @keydown = (ev) => # TODO: disable whem gui open? + @keydown = (ev) => # TODO: disable when gui open? (or use kb-bindings../interact) if '0'.charCodeAt(0) <= ev.keyCode <= '9'.charCodeAt(0) slot = ev.keyCode - '0'.charCodeAt(0) if slot == 0 slot = 10 slot -= 1 - @inventoryWindow.setSelected(slot) + @selectedIndex = slot + @inventoryWindow.setSelected(@selectedIndex) ever(document.body).on 'keydown', @keydown + + super() disable: () -> @inventoryWindow.container.style.visibility = 'hidden' ever(document.body).removeListener 'keydown', @keydown - give: (itemPile) -> @inventory.give itemPile - take: (itemPile) -> @inventory.take itemPile - - # take some items from the pile the player is currently holding - takeHeld: (count=1) -> @inventory.takeAt @inventoryWindow.selectedIndex, count - - # get the pile of items the player is currently holding - held: () -> - @inventory.get @inventoryWindow.selectedIndex + super() refresh: () -> @inventoryWindow.refresh() + + diff --git a/index.js b/index.js index 0502552..26b35cf 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.6.3 (function() { - var EventEmitter, InventoryHotbar, InventoryWindow, ever, + var EventEmitter, InventoryHotbarClient, InventoryHotbarCommon, InventoryWindow, ever, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; @@ -11,18 +11,22 @@ ever = require('ever'); module.exports = function(game, opts) { - return new InventoryHotbar(game, opts); + if (game.isClient) { + return new InventoryHotbarClient(game, opts); + } else { + return new InventoryHotbarCommon(game, opts); + } }; module.exports.pluginInfo = { loadAfter: ['voxel-carry', 'voxel-registry'] }; - InventoryHotbar = (function(_super) { - __extends(InventoryHotbar, _super); + InventoryHotbarCommon = (function(_super) { + __extends(InventoryHotbarCommon, _super); - function InventoryHotbar(game, opts) { - var container, registry, windowOpts, _ref, _ref1, _ref2, _ref3, _ref4; + function InventoryHotbarCommon(game, opts) { + var _ref; this.game = game; if (opts == null) { opts = {}; @@ -35,8 +39,45 @@ throw 'voxel-inventory-hotbar requires "voxel-carry" plugin or "inventory" option set to inventory instance'; } })(); - registry = (_ref1 = game.plugins) != null ? _ref1.get('voxel-registry') : void 0; - windowOpts = (_ref2 = opts.windowOpts) != null ? _ref2 : {}; + this.selectedIndex = 0; + } + + InventoryHotbarCommon.prototype.enable = function() {}; + + InventoryHotbarCommon.prototype.disable = function() {}; + + InventoryHotbarCommon.prototype.give = function(itemPile) { + return this.inventory.give(itemPile); + }; + + InventoryHotbarCommon.prototype.take = function(itemPile) { + return this.inventory.take(itemPile); + }; + + InventoryHotbarCommon.prototype.takeHeld = function(count) { + if (count == null) { + count = 1; + } + return this.inventory.takeAt(this.selectedIndex, count); + }; + + InventoryHotbarCommon.prototype.held = function() { + return this.inventory.get(this.selectedIndex); + }; + + return InventoryHotbarCommon; + + })(EventEmitter); + + InventoryHotbarClient = (function(_super) { + __extends(InventoryHotbarClient, _super); + + function InventoryHotbarClient(game, opts) { + var container, registry, windowOpts, _ref, _ref1, _ref2, _ref3; + this.game = game; + InventoryHotbarClient.__super__.constructor.call(this, this.game, opts); + registry = (_ref = game.plugins) != null ? _ref.get('voxel-registry') : void 0; + windowOpts = (_ref1 = opts.windowOpts) != null ? _ref1 : {}; if (registry) { if (windowOpts.registry == null) { windowOpts.registry = registry; @@ -46,10 +87,10 @@ windowOpts.inventory = this.inventory; } if (windowOpts.inventorySize == null) { - windowOpts.inventorySize = (_ref3 = opts.inventorySize) != null ? _ref3 : this.inventory.size(); + windowOpts.inventorySize = (_ref2 = opts.inventorySize) != null ? _ref2 : this.inventory.size(); } if (windowOpts.width == null) { - windowOpts.width = (_ref4 = opts.width) != null ? _ref4 : windowOpts.inventorySize; + windowOpts.width = (_ref3 = opts.width) != null ? _ref3 : windowOpts.inventorySize; } this.inventoryWindow = new InventoryWindow(windowOpts); this.inventoryWindow.selectedIndex = 0; @@ -63,7 +104,7 @@ this.enable(); } - InventoryHotbar.prototype.enable = function() { + InventoryHotbarClient.prototype.enable = function() { var _this = this; this.inventoryWindow.container.style.visibility = ''; this.keydown = function(ev) { @@ -74,42 +115,26 @@ slot = 10; } slot -= 1; - return _this.inventoryWindow.setSelected(slot); + _this.selectedIndex = slot; + return _this.inventoryWindow.setSelected(_this.selectedIndex); } }; - return ever(document.body).on('keydown', this.keydown); + ever(document.body).on('keydown', this.keydown); + return InventoryHotbarClient.__super__.enable.call(this); }; - InventoryHotbar.prototype.disable = function() { + InventoryHotbarClient.prototype.disable = function() { this.inventoryWindow.container.style.visibility = 'hidden'; - return ever(document.body).removeListener('keydown', this.keydown); - }; - - InventoryHotbar.prototype.give = function(itemPile) { - return this.inventory.give(itemPile); - }; - - InventoryHotbar.prototype.take = function(itemPile) { - return this.inventory.take(itemPile); + ever(document.body).removeListener('keydown', this.keydown); + return InventoryHotbarClient.__super__.disable.call(this); }; - InventoryHotbar.prototype.takeHeld = function(count) { - if (count == null) { - count = 1; - } - return this.inventory.takeAt(this.inventoryWindow.selectedIndex, count); - }; - - InventoryHotbar.prototype.held = function() { - return this.inventory.get(this.inventoryWindow.selectedIndex); - }; - - InventoryHotbar.prototype.refresh = function() { + InventoryHotbarClient.prototype.refresh = function() { return this.inventoryWindow.refresh(); }; - return InventoryHotbar; + return InventoryHotbarClient; - })(EventEmitter); + })(InventoryHotbarCommon); }).call(this);