2020-01-10 22:54:37 +00:00
|
|
|
import RES from './resource'
|
2020-01-14 19:33:17 +00:00
|
|
|
import { ItemEntity } from './entity'
|
|
|
|
import Input from './input'
|
2020-01-10 22:54:37 +00:00
|
|
|
|
|
|
|
const MAX_STACK_SIZE = 999
|
|
|
|
|
|
|
|
const ItemRegistry = new (class ItemRegistry {
|
|
|
|
constructor () {
|
|
|
|
this.items = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
register (name, item) {
|
|
|
|
this.items[name] = item
|
|
|
|
}
|
|
|
|
|
|
|
|
get (name) {
|
|
|
|
return this.items[name]
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
|
|
|
|
class Item {
|
|
|
|
constructor (name, img, description) {
|
|
|
|
this.name = name
|
|
|
|
this._img = img
|
|
|
|
this.description = description
|
|
|
|
ItemRegistry.register(name, this)
|
|
|
|
}
|
|
|
|
|
|
|
|
get image () {
|
|
|
|
return RES.loadImage(this._img, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ItemPlaceable extends Item {
|
|
|
|
constructor (tile, name, img, description) {
|
|
|
|
super(name, img, description)
|
|
|
|
this.placeable = tile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 11:11:34 +00:00
|
|
|
class ItemTool extends Item {
|
2020-01-14 19:33:17 +00:00
|
|
|
use (dt, vp, world, player) {}
|
2020-01-11 11:11:34 +00:00
|
|
|
|
2020-01-14 19:33:17 +00:00
|
|
|
useSecondary (dt, vp, world, player) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ItemMiningTool extends ItemTool {
|
|
|
|
constructor (name, img, power, description) {
|
|
|
|
super(name, img, description)
|
|
|
|
this.power = power
|
|
|
|
}
|
|
|
|
|
|
|
|
mine (l, dt, vp, world, player) {
|
|
|
|
let mabs = { x: Input.mouse.pos.x + vp.x, y: Input.mouse.pos.y + vp.y }
|
|
|
|
let mpin = world.gridPosition(mabs)
|
|
|
|
if (!mpin.chunk) return false
|
|
|
|
let layer = mpin.chunk.getLayer(l)
|
|
|
|
let tile = layer.tileAtXY(mpin.tile.x, mpin.tile.y)
|
|
|
|
if (tile === -1) return false
|
|
|
|
let broken = layer.damageTileAtXY(mpin.tile.x, mpin.tile.y, this.power)
|
|
|
|
if (!broken) return false
|
|
|
|
let itile = layer.map.getTileByID(tile)
|
|
|
|
let success = mpin.chunk.setTile(l, mpin.tile, layer.map.indexOf('AIR'))
|
|
|
|
if (success) {
|
|
|
|
let e = ItemEntity.new(ItemStack.new(itile.item, 1), mabs.x - 8, mabs.y - 8)
|
|
|
|
let p = world.getLayer('ents')
|
|
|
|
p.entities.push(e)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use (dt, vp, world, player) {
|
|
|
|
return this.mine('fg', dt, vp, world, player)
|
|
|
|
}
|
|
|
|
|
|
|
|
useSecondary (dt, vp, world, player) {
|
|
|
|
return this.mine('bg', dt, vp, world, player)
|
|
|
|
}
|
2020-01-11 11:11:34 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 22:54:37 +00:00
|
|
|
class ItemStack {
|
|
|
|
static fromIString (str) {
|
|
|
|
if (typeof str !== 'string') return
|
|
|
|
let strpl = str.split(' ')
|
|
|
|
let iname = strpl[0]
|
2020-01-11 11:11:34 +00:00
|
|
|
let count = parseInt(strpl[1])
|
2020-01-10 22:54:37 +00:00
|
|
|
let item = ItemRegistry.get(iname)
|
|
|
|
let istack = new ItemStack()
|
|
|
|
istack.item = item
|
2020-01-11 11:11:34 +00:00
|
|
|
istack.count = isNaN(count) ? 1 : count
|
2020-01-10 22:54:37 +00:00
|
|
|
return istack
|
|
|
|
}
|
|
|
|
|
|
|
|
static new (itemdef, count = 1, metadata) {
|
|
|
|
if (itemdef instanceof ItemStack) return itemdef.copy()
|
|
|
|
if (typeof itemdef === 'string') return ItemStack.fromIString(itemdef)
|
|
|
|
if (!(itemdef instanceof Item)) throw new Error('Invalid Item Definition!')
|
|
|
|
let istack = new ItemStack()
|
|
|
|
istack.item = itemdef
|
|
|
|
istack.count = count
|
|
|
|
istack.metadata = metadata
|
|
|
|
return istack
|
|
|
|
}
|
|
|
|
|
|
|
|
copy () {
|
|
|
|
return ItemStack.new(this.item, this.count, this.metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
get name () {
|
|
|
|
return this.item ? this.item.name : ''
|
|
|
|
}
|
|
|
|
|
|
|
|
isEmpty () {
|
|
|
|
return this.item === null || this.count === 0
|
|
|
|
}
|
|
|
|
|
|
|
|
takeItem (c) {
|
|
|
|
let a = this.copy()
|
|
|
|
if (c > this.count) {
|
|
|
|
this.count = 0
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
this.count -= c
|
|
|
|
a.count = c
|
|
|
|
return a
|
|
|
|
}
|
2020-01-11 11:11:34 +00:00
|
|
|
|
|
|
|
toString () {
|
|
|
|
return this.name + ' ' + this.count + (this.metadata ? ' ' + JSON.stringify(this.metadata) : '')
|
|
|
|
}
|
2020-01-10 22:54:37 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 19:33:17 +00:00
|
|
|
export { Item, ItemPlaceable, ItemTool, ItemMiningTool, ItemStack, ItemRegistry, MAX_STACK_SIZE }
|