Skip to content

Commit

Permalink
Input class upgrade
Browse files Browse the repository at this point in the history
- keydown only fires when key is pressed
- keypress fires when keydown previously fired
- pressed() renamed to keyPressed()
- added mousePressed(button)
- keys and mouseButtons are now Sets instead of Maps
  • Loading branch information
j0code committed Mar 1, 2024
1 parent ef0de94 commit 1266231
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 43 deletions.
36 changes: 28 additions & 8 deletions src/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,57 @@ import EventEmitter from "./util/EventEmitter.js"

export default class Input extends EventEmitter {

private keys: Map<string, boolean>
private mousePos: Dim2
private readonly keys: Set<string>
private readonly mouseButtons: Set<number>
private readonly mousePos: Dim2

constructor() {
super()
this.keys = new Map()
this.keys = new Set()
this.mouseButtons = new Set()
this.mousePos = new Dim2()

window.addEventListener("keydown", e => {
if (e.code == "F12") return // open devtools
if (e.code == "F5") {
// TODO: save game beforehand!
return // default (reload) not prevented
}

this.keys.set(e.code, true)
this.emit("keydown", e.code)
const before = this.keys.has(e.code)
this.keys.add(e.code)
if (!before) this.emit("keydown", e.code)
this.emit("keypress", e.code)
e.preventDefault()
})

window.addEventListener("keyup", e => {
this.keys.set(e.code, false)
this.keys.delete(e.code)
this.emit("keyup", e.code)
e.preventDefault()
})

window.addEventListener("mousemove", e => {
this.mousePos.x = e.x
this.mousePos.y = e.y
this.emit("mousemove")
})

window.addEventListener("mousedown", e => {
this.mouseButtons.add(e.button)
this.emit("click", e.button)
e.preventDefault()
})

window.addEventListener("mouseup", e => {
this.mouseButtons.delete(e.button)
e.preventDefault()
})

window.addEventListener("contextmenu", e => {
e.preventDefault()
})

window.addEventListener("wheel", e => {
//e.preventDefault()
})
Expand All @@ -55,8 +71,12 @@ export default class Input extends EventEmitter {
return this.mousePos.copy()
}

pressed(code: string) {
return this.keys.get(code) || false
keyPressed(code: string) {
return this.keys.has(code)
}

mousePressed(button: number) {
return this.mouseButtons.has(button)
}

}
68 changes: 35 additions & 33 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@ export function getTexture(path: string) {
}

function tick() {
player.motion.x = (Number(input.pressed("KeyD")) - Number(input.pressed("KeyA"))) * 0.15
//player.motion.y = (Number(input.pressed("KeyW")) - Number(input.pressed("KeyS"))) * 0.25
if (player.inFluid && input.pressed("Space")) player.motion.y = Entity.TERMINAL_FLUID_VELOCITY
if (input.keyPressed("Space")) {
if (player.inFluid) player.motion.y = Entity.TERMINAL_FLUID_VELOCITY
else if (player.onGround) player.motion.y = 0.35
}

player.motion.x = (Number(input.keyPressed("KeyD")) - Number(input.keyPressed("KeyA"))) * 0.15

world.tick()
}

Expand Down Expand Up @@ -239,7 +243,7 @@ function draw() {

const floatingStack = Container.floatingStack()
const {block: frontBlock, z: frontZ} = getFirstBlock(world, x, y)
const z = input.pressed("ShiftLeft") ? -1 : 0
const z = input.keyPressed("ShiftLeft") ? -1 : 0
const targetBlock = world.getBlock(x, y, z)
const inaccessible = z < frontZ && frontBlock?.full

Expand Down Expand Up @@ -300,28 +304,6 @@ input.on("keydown", (key: string) => {
if (key == "Digit4") player.selectedItemSlot = 3
if (key == "Digit5") player.selectedItemSlot = 4

if (key == "KeyQ") {
const stack = player.selectedItem
const index = player.selectedItemSlot
if (stack.item.id == "tiny:air") return

const entityData = {
position: player.position.asArray(),
motion: getMousePos().sub(player.position).normalize().scale(0.6).asArray()
}
let dropStack = stack

if (!input.pressed("ControlLeft")) {
dropStack = new ItemStack(stack.item.id)
}

if (input.pressed("ControlLeft") || stack.amount <= 1) {
player.hotbar.set(index, new ItemStack("tiny:air"))
} else stack.amount--

world.spawn<ItemEntityData>("tiny:item", { ...entityData, item: dropStack })
}

inv: if (key == "KeyE") { // open inventory under mouse
if (Container.showingInventory()) {
Container.setInventory()
Expand All @@ -341,7 +323,7 @@ input.on("keydown", (key: string) => {
debug.showDebugScreen = !debug.showDebugScreen
}

if (input.pressed("F3") && debug.showDebugScreen) {
if (input.keyPressed("F3") && debug.showDebugScreen) {

if (key == "KeyN") {
debug.showRange = !debug.showRange
Expand All @@ -358,10 +340,6 @@ input.on("keydown", (key: string) => {

}

if (key == "Space") {
if (!player.inFluid && player.onGround) player.motion.y = 0.35
}

if (key == "F11" || key == "F1") {
if (document.fullscreenElement) document.exitFullscreen()
else game.requestFullscreen()
Expand All @@ -374,7 +352,31 @@ input.on("keydown", (key: string) => {
console.log("blockData:", worldSave.blockData)
world = World.load(worldSave.stringBlocks, worldSave.blockData, worldSave.dims, worldSave.entities) as World
world.spawn(player)
}*/
}*/2
})

input.on("keypress", (key: string) => {
if (input.keyPressed("KeyQ")) {
const stack = player.selectedItem
const index = player.selectedItemSlot
if (stack.item.id == "tiny:air") return

const entityData = {
position: player.position.asArray(),
motion: getMousePos().sub(player.position).normalize().scale(0.6).asArray()
}
let dropStack = stack

if (!input.keyPressed("ControlLeft")) {
dropStack = new ItemStack(stack.item.id)
}

if (input.keyPressed("ControlLeft") || stack.amount <= 1) {
player.hotbar.set(index, new ItemStack("tiny:air"))
} else stack.amount--

world.spawn<ItemEntityData>("tiny:item", { ...entityData, item: dropStack })
}
})

input.on("click", (button: number) => {
Expand All @@ -384,7 +386,7 @@ input.on("click", (button: number) => {

const mouseBlock = getMouseBlock()
const {x, y} = mouseBlock
let z = input.pressed("ShiftLeft") ? -1 : 0
let z = input.keyPressed("ShiftLeft") ? -1 : 0
const {block: frontBlock, z: frontZ} = getFirstBlock(world, x, y)
const reachable = isBlockReachable(mouseBlock)

Expand Down
2 changes: 1 addition & 1 deletion src/util/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class Container {
if (!mouseSlot) return false

if (button == 0) {
if (input.pressed("ShiftLeft")) {
if (input.keyPressed("ShiftLeft")) {
const stack = inventory.get(mouseSlot.slotIndex)
if (stack.item.id == "tiny:air") return false
const leftOver = player.hotbar.addItems(stack)
Expand Down
2 changes: 1 addition & 1 deletion src/util/DebugScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function gameInfo(g: Graphics, world: World, player: Player) {
lines.push(`${perf.tps.toFixed(1)}/${tickTarget} tps (${perf.mspt.toFixed(2)}ms / tick)`)
lines.push(`entities: ${world.getAllEntities().length}`)
lines.push(`world size: ${world.minX}..${world.maxX}; ${world.minY}..${world.maxY}; ${world.minZ}..${world.maxZ}`)
lines.push(`shift: ${input.pressed("ShiftLeft")}`)
lines.push(`shift: ${input.keyPressed("ShiftLeft")}`)

lines.push(``)
lines.push(`player: (${player.position.x},${player.position.y})`)
Expand Down

0 comments on commit 1266231

Please sign in to comment.