Skip to content

Commit

Permalink
Refactor into common/client classes for client/server support
Browse files Browse the repository at this point in the history
  • Loading branch information
deathcap committed Jan 21, 2014
1 parent 7108712 commit c363f2d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 52 deletions.
49 changes: 34 additions & 15 deletions index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()


99 changes: 62 additions & 37 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c363f2d

Please sign in to comment.