tilegame/src/items.js

134 lines
3.2 KiB
JavaScript

import RES from './resource'
import { ItemEntity } from './entity'
import Input from './input'
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
}
}
class ItemTool extends Item {
use (dt, vp, world, player) {}
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)
}
}
class ItemStack {
static fromIString (str) {
if (typeof str !== 'string') return
let strpl = str.split(' ')
let iname = strpl[0]
let count = parseInt(strpl[1])
let item = ItemRegistry.get(iname)
let istack = new ItemStack()
istack.item = item
istack.count = isNaN(count) ? 1 : count
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
}
toString () {
return this.name + ' ' + this.count + (this.metadata ? ' ' + JSON.stringify(this.metadata) : '')
}
}
export { Item, ItemPlaceable, ItemTool, ItemMiningTool, ItemStack, ItemRegistry, MAX_STACK_SIZE }