Skip to content

Commit

Permalink
render item count over hotbar selector
Browse files Browse the repository at this point in the history
  • Loading branch information
j0code committed Mar 14, 2024
1 parent 798bd8f commit bf184d8
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions src/util/Hotbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,71 @@ const itemSize = scale * 12

export default class Hotbar {

static loadTexture() {
widgets = getTexture("tiny/textures/gui/widgets.png")
leftSlot = widgets.getSubtexture(0, 0, 21, 22)
middleSlot = widgets.getSubtexture(21, 0, 20, 22)
rightSlot = widgets.getSubtexture(161, 0, 21, 22)
selector = widgets.getSubtexture(0, 22, 24, 24)
}
static loadTexture() {
widgets = getTexture("tiny/textures/gui/widgets.png")
leftSlot = widgets.getSubtexture(0, 0, 21, 22)
middleSlot = widgets.getSubtexture(21, 0, 20, 22)
rightSlot = widgets.getSubtexture(161, 0, 21, 22)
selector = widgets.getSubtexture(0, 22, 24, 24)
}

static drawHotbar(g: Graphics) {
static drawHotbar(g: Graphics) {
const ctx = g.ctx
let hotbar = player.hotbar
let hotbar = player.hotbar

ctx.save()
ctx.translate(-204, game.height/2 - 100)
ctx.save()
ctx.translate(-204, game.height/2 - 100)

ctx.save()
ctx.scale(scale, scale)
ctx.save()
ctx.scale(scale, scale)

// first and last are diff b/c black outline
ctx.save()
ctx.scale(21, 22)
leftSlot.draw(g)
ctx.restore()
ctx.save()
ctx.translate(21, 0)
ctx.scale(20, 22)
for (let i = 1; i < hotbar.size -1; i++) {
middleSlot.draw(g)
ctx.translate(1, 0)
}
ctx.restore()
// first and last are diff b/c black outline
ctx.save()
ctx.scale(21, 22)
leftSlot.draw(g)
ctx.restore()
ctx.save()
ctx.translate(21, 0)
ctx.scale(20, 22)
for (let i = 1; i < hotbar.size -1; i++) {
middleSlot.draw(g)
ctx.translate(1, 0)
}
ctx.restore()

ctx.save()
ctx.translate(21 + (hotbar.size-2) * 20, 0)
ctx.scale(21, 22)
rightSlot.draw(g)
ctx.restore()
ctx.save()
ctx.translate(21 + (hotbar.size-2) * 20, 0)
ctx.scale(21, 22)
rightSlot.draw(g)
ctx.restore()

ctx.restore()
ctx.restore()

for (let i = 0; i < hotbar.size; i++) {
let stack = hotbar.get(i)
if (stack.item.id == "tiny:air") continue
if (!stack.item.texture) continue
// selected slot
ctx.save()
ctx.scale(scale, scale)
ctx.translate(-1, -1)
ctx.translate(player.selectedItemSlot * 20, 0)
ctx.scale(24, 24)
selector.draw(g)
ctx.restore()

let cx = 22/2 * scale - itemSize/2 + i * 20 * scale
let cy = 22/2 * scale - itemSize/2
for (let i = 0; i < hotbar.size; i++) {
let stack = hotbar.get(i)
if (stack.item.id == "tiny:air") continue
if (!stack.item.texture) continue

ctx.save()
ctx.translate(cx, cy)
stack.draw(g, itemSize)
ctx.restore()
}
let cx = 22/2 * scale - itemSize/2 + i * 20 * scale
let cy = 22/2 * scale - itemSize/2

// selected slot
ctx.save()
ctx.scale(scale, scale)
ctx.translate(-1, -1)
ctx.translate(player.selectedItemSlot * 20, 0)
ctx.scale(24, 24)
selector.draw(g)
ctx.restore()

ctx.restore()
}
ctx.save()
ctx.translate(cx, cy)
stack.draw(g, itemSize)
ctx.restore()
}

ctx.restore()
}

}

0 comments on commit bf184d8

Please sign in to comment.