goopler/src/ReplicatedStorage/ecs/systems/client/inputMapper.ts

44 lines
No EOL
1.4 KiB
TypeScript

import { useDeltaTime, useEvent, useThrottle, World } from "@rbxts/matter"
import { UserInputService } from "@rbxts/services"
import { clientState } from "ReplicatedStorage/ecs/state"
import { InputKind } from "ReplicatedStorage/inputKind"
let holdDuration = 0
function inputMapper(_: World, client: clientState): void {
for (const [, input, gpe] of useEvent(UserInputService, "InputBegan")) {
if (gpe) return undefined
if (input.KeyCode !== Enum.KeyCode.Unknown) {
client.lastProcessedCommand = InputKind.KeyDown(input.KeyCode)
} else if (input.UserInputType === Enum.UserInputType.MouseButton1) {
if (useThrottle(0.5)) {
client.lastProcessedCommand = InputKind.PointerClick
return undefined
}
client.lastProcessedCommand = InputKind.DoubleClick
}
return undefined
}
for (const [, input, gpe] of useEvent(UserInputService, "InputEnded")) {
if (gpe) return undefined
if (input.KeyCode !== Enum.KeyCode.Unknown) {
client.lastProcessedCommand = InputKind.KeyUp(input.KeyCode)
} else if (input.UserInputType === Enum.UserInputType.MouseButton1) {
client.lastProcessedCommand = InputKind.HoldRelease
}
return undefined
}
if (UserInputService.IsMouseButtonPressed(Enum.UserInputType.MouseButton1)) {
holdDuration += useDeltaTime()
client.lastProcessedCommand = InputKind.Hold(holdDuration)
return
}
holdDuration = 0
client.lastProcessedCommand = undefined
return
}
export = inputMapper