Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/zero-to-mastery/ZTM-Quest i…
Browse files Browse the repository at this point in the history
…nto arcade-game
  • Loading branch information
BrianCheung1 committed Oct 14, 2024
2 parents 61769d3 + 5037bde commit fe03664
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
79 changes: 47 additions & 32 deletions src/interactions/map_arcade/game_machine_crawl.interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function startCrawlGame(k) {
title: 'Crawl Game',
gameRestartSceneName: 'crawlGame',
gameExitSceneName: 'arcade',
score: timeLabel.text
score: timeLabel.text,
});
});

Expand All @@ -132,33 +132,47 @@ function startCrawlGame(k) {
k.rect(30, 30),
// Calculate the position of each item, spacing them evenly
k.pos(
(k.width() / (numItems + 1) * (i + 1)), // Spacing items evenly between width
k.height() - 200 // Y position (fixed 200 units from the bottom)
(k.width() / (numItems + 1)) * (i + 1), // Spacing items evenly between width
k.height() - 200 // Y position (fixed 200 units from the bottom)
),
k.area(),
k.body({ isStatic: true }),
"item",
k.body({ isStatic: true }), // Ensure items don't move
'item',
]);
}

// Default Controls
// Crane movement left
k.onButtonDown(["left"], () => {
if (!crane.isMovingDown && !crane.isMovingUp && !crane.isMovingHorizontal && crane.pos.x > 0) {
k.onButtonDown(['left'], () => {
if (
!crane.isMovingDown &&
!crane.isMovingUp &&
!crane.isMovingHorizontal &&
crane.pos.x > 0
) {
crane.move(-crane.speed, 0);
}
});

// Crane movement right
k.onButtonDown(["right"], () => {
if (!crane.isMovingDown && !crane.isMovingUp && !crane.isMovingHorizontal && crane.pos.x < width - 40) {
k.onButtonDown(['right'], () => {
if (
!crane.isMovingDown &&
!crane.isMovingUp &&
!crane.isMovingHorizontal &&
crane.pos.x < width - 40
) {
crane.move(crane.speed, 0);
}
});

// Drop the crane using the spacebar
k.onKeyPress(["space", "down"], () => {
if (!crane.isMovingDown && !crane.isMovingUp && !crane.isMovingHorizontal) {
k.onKeyPress(['space', 'down'], () => {
if (
!crane.isMovingDown &&
!crane.isMovingUp &&
!crane.isMovingHorizontal
) {
crane.isMovingDown = true;
}
});
Expand Down Expand Up @@ -277,19 +291,21 @@ function startCrawlGame(k) {
if (crane.isMovingDown) {
crane.move(0, crane.speed);
// Check for collision with any items
const item = k.get("item").find(item => crane.isColliding(item));
const item = k
.get('item')
.find((item) => crane.isColliding(item));
if (item && !crane.grabbedItem) {
crane.grabbedItem = item; // Attach the item to the crane
k.debug.log("Item grabbed!");
timeRemaining += 10000
crane.grabbedItem = item; // Attach the item to the crane
k.debug.log('Item grabbed!');
timeRemaining += 10000;
// Reset crane position after reaching item
crane.isMovingDown = false; // Reset the flag so the crane can move again
crane.isMovingDown = false; // Reset the flag so the crane can move again
crane.isMovingUp = true;
}
if (crane.pos.y > k.height() - 150) {
// Reset crane position after reaching bottom
timeRemaining -= 5000
crane.isMovingDown = false; // Reset the flag so the crane can move again
timeRemaining -= 5000;
crane.isMovingDown = false; // Reset the flag so the crane can move again
crane.isMovingUp = true;
}
}
Expand All @@ -298,49 +314,48 @@ function startCrawlGame(k) {
crane.move(0, -crane.speed);
// Move the grabbed item with the crane
if (crane.grabbedItem) {
crane.grabbedItem.pos.x = crane.pos.x; // Align item's x position with the crane
crane.grabbedItem.pos.y = crane.pos.y + 20; // Adjust y position (offset for the crane's size)
crane.grabbedItem.pos.x = crane.pos.x; // Align item's x position with the crane
crane.grabbedItem.pos.y = crane.pos.y + 20; // Adjust y position (offset for the crane's size)
}

// Check if crane has reached its starting position
if (crane.pos.y <= crane.startPos.y) {
crane.isMovingHorizontal = true
crane.isMovingUp = false; // Stop moving up
crane.isMovingHorizontal = true;
crane.isMovingUp = false; // Stop moving up
}
}
// Move crane horizontally back to the starting x position
if (crane.isMovingHorizontal) {
const moveDirection = crane.pos.x > crane.startPos.x ? -1 : 1; // Determine direction
const moveDirection = crane.pos.x > crane.startPos.x ? -1 : 1; // Determine direction
crane.move(moveDirection * crane.speed, 0);

// Move the grabbed item with the crane horizontally
if (crane.grabbedItem) {
crane.grabbedItem.pos.x = crane.pos.x; // Align item's x position with the crane
crane.grabbedItem.pos.x = crane.pos.x; // Align item's x position with the crane
}

// Check if crane has reached its starting x position
if (Math.abs(crane.pos.x - crane.startPos.x) <= 1) { // Small tolerance for precision
crane.pos.x = crane.startPos.x; // Ensure exact x positioning
crane.isMovingHorizontal = false; // Stop movement
if (Math.abs(crane.pos.x - crane.startPos.x) <= 1) {
// Small tolerance for precision
crane.pos.x = crane.startPos.x; // Ensure exact x positioning
crane.isMovingHorizontal = false; // Stop movement

// Drop the grabbed item after reaching the start position
if (crane.grabbedItem) {
crane.grabbedItem.destroy()
crane.grabbedItem = null; // Release the item
k.debug.log("Item released!");
crane.grabbedItem.destroy();
crane.grabbedItem = null; // Release the item
k.debug.log('Item released!');
}
}
}
});
});
}


// Helper function to format time as MM:SS
function formatTime(milliseconds) {
const totalSeconds = Math.floor(milliseconds / 1000);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
}

2 changes: 1 addition & 1 deletion src/interactions/map_arcade/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const interactions = [
interactionWithGameMachine10,
interactionWithGameMachine11,
interactionWithGameMachine12,
interactionWithGameMachineCrawl
interactionWithGameMachineCrawl,
];

export default interactions;

0 comments on commit fe03664

Please sign in to comment.