Skip to content

Commit

Permalink
Game machine 13 interaction (#203)
Browse files Browse the repository at this point in the history
Fixes #139
  • Loading branch information
r4pt0s authored Nov 1, 2024
2 parents 7aa17f0 + a99942b commit c03dcd9
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 1 deletion.
8 changes: 7 additions & 1 deletion asset_credits.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ https://kenney.nl/assets/puzzle-pack - sprites for Break Brick Game on Machine #
https://free-game-assets.itch.io/free-street-animal-pixel-art-asset-pack - crow sprite for city map

https://craftpix.net/freebies/city-man-pixel-art-character-sprite-sheets/ - sprite for Runner Quest Game on Machine 7(runner)
https://openart.ai/ - other sprites for Runner Quest Game on Machine 7
https://openart.ai/ - other sprites for Runner Quest Game on Machine 7

https://www.freepik.com/free-vector/dessert-icons-pixel-art_29012360.htm#fromView=search&page=6&position=19&uuid=bc602f54-b53e-4d17-aa10-b3fa525a67d5 dessert sprites for Eat All Cake Game on Machine #13

https://totuslotus.itch.io/characterpack - man sprite for Eat All Cake Game on Machine #13 (@lotus_totus)


Binary file added public/assets/sprites/desserts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/sprites/man.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
188 changes: 188 additions & 0 deletions src/interactions/map_arcade/game_machine_13.interactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { displayDialogue } from '../../utils';

export const interactionWithGameMachine13 = (player, k, map) => {
player.onCollide('game_machine_13', () => {
// Trigger the custom prompt when the player collides with the game machine
showCustomPrompt(
'Do you want to play an Eat All Cake Game?', // Updated Prompt message
['Yes', 'No'], // Options for the game prompt
(selectedOption) => {
// Logic based on the selected option
if (selectedOption === 'Yes') {
displayDialogue({
k,
player,
text: ['Starting Eat All Cake Game... Good luck!'],
onDisplayEnd: () => {
startEatAllCakeGame(k); // Pass k to the game start function
},
});
} else {
displayDialogue({
k,
player,
text: ['Maybe next time!'],
});
}
}
);
});
};

function showCustomPrompt(message, options, callback) {
// Set the prompt message
document.getElementById('prompt-message').textContent = message;

// Clear any existing options in the container
const optionsContainer = document.getElementById('options-container');
optionsContainer.innerHTML = '';

// Create buttons for each option
options.forEach((option) => {
const button = document.createElement('button');
button.textContent = option;
button.classList.add('option-btn');
button.setAttribute('tabindex', '0'); // Make the button focusable

// Add click event for mouse interactions
button.onclick = function () {
callback(option);
closeCustomPrompt();
};

// Add keyboard event listener for accessibility
button.addEventListener('keydown', function (e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault(); // Prevent the default behavior
callback(option);
closeCustomPrompt();
}
});

optionsContainer.appendChild(button);
});

// Display the custom prompt
document.getElementById('custom-prompt').style.display = 'flex';

// Set focus on the first button
if (optionsContainer.children.length > 0) {
optionsContainer.children[0].focus();
}
}

// Function to close the custom prompt
function closeCustomPrompt() {
// Hide the custom prompt
document.getElementById('custom-prompt').style.display = 'none';
}

export function startEatAllCakeGame(k) {
k.debug.log('Eat All Cake Game Started!');

k.go('startScreen', {
title: 'Eat All Cake Game',
gameSceneName: 'eatAllCakeGame',
});

// Load sprites with frame slicing
k.loadSprite('man', './assets/sprites/man.png', {
sliceX: 3, // 3 frames per row horizontally
sliceY: 4, // 4 rows, totaling 12 frames
});

k.loadSprite('desserts', './assets/sprites/desserts.png', {
sliceX: 4, // 4 frames horizontally
sliceY: 2, // 2 rows, total of 8 frames
});

// Main Game Scene
k.scene('eatAllCakeGame', () => {
const SPEED_MIN = 120;
const SPEED_MAX = 640;
let score = 0;
const otherDessertsFrameIndex = [0, 2, 3, 4, 5, 6, 7];

// Calculate scale based on screen size
const scaleFactor = Math.min(k.width() / 800, k.height() / 600);

// Add player character
const player = k.add([
k.sprite('man', { frame: 2, animSpeed: 0.1 }),
k.pos(40, 20),
k.scale(2.5 * scaleFactor),
k.area(),
k.anchor('center'),
'player',
]);

// Display score
const scoreLabel = k.add([
k.text(`Cake Eaten: ${score}`, 32 * scaleFactor),
k.pos(12 * scaleFactor, 12 * scaleFactor),
]);

// Update player position to follow mouse
player.onUpdate(() => {
const mousePosition = k.mousePos();
player.pos.x = Math.min(mousePosition.x, k.width() - player.width);
player.pos.y = Math.min(
mousePosition.y,
k.height() - player.height
);
});

// Loop to spawn desserts and cakes
k.loop(0.9, () => {
const x = k.width() + 24;
const y = k.rand(0, k.height());
const speed = k.rand(SPEED_MIN, SPEED_MAX);
const isCake = k.chance(0.5); // 60% chance for cake

const frameIndex = isCake ? 1 : k.choose(otherDessertsFrameIndex); // Frame 1 is "cake", others are "desserts"

k.add([
k.sprite('desserts', { frame: frameIndex }),
k.pos(x, y),
k.scale(0.25 * scaleFactor), // Adjusted scale for better visibility
k.area(), // Ensure collision detection works
k.anchor('center'),
isCake ? 'cake' : 'dessert', // Assign tag for collision
{ speed: speed },
]);
});

// When player collides with cake, increase score and destroy the cake
k.onCollide('player', 'cake', (player, cake) => {
score += 1;
k.destroy(cake); // Remove cake once eaten
scoreLabel.text = `Cake Eaten: ${score}`;
});

// When player collides with dessert, end game
k.onCollide('player', 'dessert', (player, dessert) => {
k.go('lose', {
title: 'Eat All Cake Game',
gameRestartSceneName: 'eatAllCakeGame',
gameExitSceneName: 'arcade',
score,
}); // Go to lose scene
score = 0;
});

// Move desserts and cakes, destroy them if off-screen
k.onUpdate('dessert', (dessert) => {
dessert.move(-dessert.speed, 0);
if (dessert.pos.x < -120) {
k.destroy(dessert);
}
});

k.onUpdate('cake', (cake) => {
cake.move(-cake.speed, 0);
if (cake.pos.x < -120) {
k.destroy(cake);
}
});
});
}
2 changes: 2 additions & 0 deletions src/interactions/map_arcade/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { interactionWithGameMachineCrawl } from './game_machine_crawl.interactio
import { interactionWithGameMachine9 } from './game_machine_9.interactions';
import { interactionWithGameMachine5 } from './game_machine_5.interactions';
import { interactionWithGameMachine7 } from './game_machine_7.interaction';
import { interactionWithGameMachine13 } from './game_machine_13.interactions';

const interactions = [
enterMapCityInteraction,
Expand All @@ -38,6 +39,7 @@ const interactions = [
interactionWithGameMachine12,
interactionWithGameMachineCrawl,
interactionWithGameMachine5,
interactionWithGameMachine13,
];

export default interactions;

0 comments on commit c03dcd9

Please sign in to comment.