Skip to content

Commit

Permalink
update npcInteractionHandler for mobile (#75)
Browse files Browse the repository at this point in the history
Fixes #66
  • Loading branch information
r4pt0s authored Oct 3, 2024
2 parents fd652b8 + 5934b86 commit fc116d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/interactions/handler.interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ export const npcInteractionHandler = (
onCollideEnd
) => {
let keyHandler;
let clickHandler;

player.onCollide(target, (sprite) => {
// Display the modal prompting the user to press 't'
const { actionModal, actionLabel } = buildActionModal(sprite, k);

keyHandler = k.onKeyPress('t', () => {
function handleOnCollide() {
k.destroy(actionModal);
k.destroy(actionLabel);
onCollide();
clickHandler.cancel();
keyHandler.cancel();
});
}

clickHandler = k.onClick('action-modal', () => handleOnCollide());
keyHandler = k.onKeyPress('t', () => handleOnCollide());
});

player.onCollideEnd(target, () => {
Expand All @@ -30,6 +36,7 @@ export const npcInteractionHandler = (
actionLabel.destroy();
}

clickHandler.cancel();
keyHandler.cancel();

if (onCollideEnd) {
Expand Down
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@ export function setCamScale(k) {
}
}

// NOTE: sprite must be an npc not an object like mailbox
export function buildActionModal(sprite, k) {
const spritePos = sprite.pos;

const actionModal = k.add([
k.rect(20, 20),
k.area(),
k.color(255, 255, 255),
k.outline(2, k.Color.BLACK),
k.pos(spritePos.x - 10, spritePos.y - sprite.height - 30),
Expand Down

0 comments on commit fc116d0

Please sign in to comment.