Skip to content

Commit

Permalink
apply block interaction range
Browse files Browse the repository at this point in the history
  • Loading branch information
j0code committed Feb 15, 2024
1 parent aaf54ee commit 345b609
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/dim/Dim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default interface Dim {
sqMag(): number
mag(): number,
normalize(): Dim,
distanceTo(other: Dim): number
floor(): Dim,
copy(): Dim

Expand Down
4 changes: 4 additions & 0 deletions src/dim/Dim2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export default class Dim2 implements Dim {
return this.scale(1/mag)
}

distanceTo(other: Dim2 | Dim3): number {
return this.copy().sub(other).mag()
}

floor(): Dim2 {
this.x = Math.floor(this.x)
this.y = Math.floor(this.y)
Expand Down
4 changes: 4 additions & 0 deletions src/dim/Dim3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export default class Dim3 implements Dim {
return this.scale(1/mag)
}

distanceTo(other: Dim2 | Dim3): number {
return this.copy().sub(other).mag()
}

floor(): Dim3 {
this.x = Math.floor(this.x)
this.y = Math.floor(this.y)
Expand Down
13 changes: 9 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,15 @@ function draw() {

// block highlight
{
let mousePos = getMouseBlock().floor()
const mousePos = getMouseBlock().floor()
const reachable = mousePos.distanceTo(player.position) <= player.attributes.get("player.block_interaction_range")!

graphics.save()
graphics.translate(mousePos.x, mousePos.y)

graphics.fillStyle = "transparent"
graphics.strokeStyle = "white"
graphics.lineWidth = 2
graphics.strokeStyle = reachable ? "white" : "#707070"
graphics.lineWidth = reachable ? 2 : 1
graphics.strokeRect(1, 1)

graphics.restore()
Expand Down Expand Up @@ -270,12 +271,15 @@ input.on("click", (button: number) => {
if (Container.onClick(button)) return
}

const {x, y} = getMouseBlock()
const mouseBlock = getMouseBlock()
const {x, y} = mouseBlock
let z = input.pressed("ShiftLeft") ? -1 : 0
const {block: frontBlock, z: frontZ} = getFirstBlock(world, x, y)
const reachable = mouseBlock.distanceTo(player.position) <= player.attributes.get("player.block_interaction_range")!

switch (button) {
case 0:
if (!reachable) break
if (z < frontZ && frontBlock?.full) break // inaccessible
world.clearBlock(x, y, z)
break
Expand All @@ -284,6 +288,7 @@ input.on("click", (button: number) => {
player.pickBlock(frontBlock)
break
case 2:
if (!reachable) break
const stack = player.selectedItem
if (z != 0 && stack.item.getBlock().mainLayerOnly()) z = 0
if (z < frontZ && frontBlock?.full) break // inaccessible
Expand Down

0 comments on commit 345b609

Please sign in to comment.