Skip to content

Commit

Permalink
Merge pull request #4 from zheeeng/bump
Browse files Browse the repository at this point in the history
fix: prevent effect logic
  • Loading branch information
zheeeng authored Apr 22, 2024
2 parents d2c3a46 + d5622b2 commit 747f32a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/pointer-lock-movement/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pointer-lock-movement",
"version": "0.1.8",
"version": "0.1.9",
"author": "Zheeeng <hi@zheeeng.me>",
"description": "A pointer lock movement manager for customizing your own creative UI.",
"keywords": [
Expand Down
62 changes: 33 additions & 29 deletions packages/pointer-lock-movement/src/pointer-lock-movement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,50 +82,54 @@ export const pointerLockMovement = (
}

const move: CoData<MoveContext, PointerEvent> = (context, effect) => payload => {
if (payload.defaultPrevented) {
return move(context, effect)
const contextPatch: Pick<MoveContext, 'event' | 'movementX' | 'movementY' | 'x' | 'y' | 'status'> = {
event: payload,
movementX: payload.movementX,
movementY: payload.movementY,
x: context.x + context.movementX,
y: context.y + context.movementY,
status: 'moving',
}

context.event = payload
context.movementX = payload.movementX
context.movementY = payload.movementY
context.x += context.movementX
context.y += context.movementY
context.status = 'moving'

if (options.loopBehavior === 'loop') {
if (context.x > context.maxWidth) {
context.x -= context.maxWidth
} else if (context.x < 0) {
context.x += context.maxWidth
if (contextPatch.x > context.maxWidth) {
contextPatch.x -= context.maxWidth
} else if (contextPatch.x < 0) {
contextPatch.x += context.maxWidth
}

if (context.y > context.maxHeight) {
context.y -= context.maxHeight
} else if (context.y < 0) {
context.y += context.maxHeight
if (contextPatch.y > context.maxHeight) {
contextPatch.y -= context.maxHeight
} else if (contextPatch.y < 0) {
contextPatch.y += context.maxHeight
}
} else if (options.loopBehavior === 'stop') {
if (context.x > context.maxWidth) {
context.x = context.maxWidth
context.status = 'stopped'
} else if (context.x < 0) {
context.x = 0
if (contextPatch.x > context.maxWidth) {
contextPatch.x = context.maxWidth
context.status = 'stopped'
} else if (contextPatch.x < 0) {
contextPatch.x = 0
contextPatch.status = 'stopped'
}

if (context.y > context.maxHeight) {
context.y = context.maxHeight
context.status = 'stopped'
} else if (context.y < 0) {
context.y = 0
if (contextPatch.y > context.maxHeight) {
contextPatch.y = context.maxHeight
context.status = 'stopped'
} else if (contextPatch.y < 0) {
contextPatch.y = 0
contextPatch.status = 'stopped'
}
}

effect(context)
const newContext = { ...context, ...contextPatch }

effect(newContext)

if (newContext.event.defaultPrevented) {
return move(context, effect)
}

return move(context, effect)
return move(newContext, effect)
}

function startup () {
Expand Down

0 comments on commit 747f32a

Please sign in to comment.