Skip to content

Commit

Permalink
Add scaling and message display for treasure chest interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
HossamGouda committed Oct 14, 2024
1 parent 54fd644 commit c76f6bd
Showing 1 changed file with 23 additions and 45 deletions.
68 changes: 23 additions & 45 deletions src/gameObjects/map_start/treasureChest.gameObject.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,48 @@
import { scaleFactor } from '../../constants';

export const treasureChest = (k, map, spawnpoints) => {
// Load the sprite for the Open treasure chest
export const treasureChest = (k, map) => {
k.loadSprite(
'treasureChestClosed',
'./assets/sprites/treasure_chest_closed.png',
{
sliceX: 1,
sliceY: 1,
anims: {
idle: 0,
},
}
'./assets/sprites/treasure_chest_closed.png'
);
// Load the sprite for the Closed treasure chest open
k.loadSprite(
'treasureChestOpen',
'./assets/sprites/treasure_chest_open.png',
{
sliceX: 1,
sliceY: 1,
anims: {
idle: 0,
},
}
'./assets/sprites/treasure_chest_open.png'
);

// Load the chest opening sound
k.loadSound('chestOpen', './assets/sounds/chest_opening.wav');

const chestPosition = spawnpoints.treasureChest;
const chestPosition = { x: 200, y: 100 };

if (!chestPosition) {
return; // Prevent further execution
}

// Create a variable to track whether the chest is open
let isOpen = false;

// Create the treasure chest object
const chestObj = k.make([
k.sprite('treasureChestClosed', { anim: 'idle' }),
k.area({
shape: new k.Rect(k.vec2(0), 32, 32), // Adjust dimensions
}),
k.body({ isStatic: true }), // Set to static if it shouldn’t move
k.anchor('center'),
// make sure to define it in Tiled
k.sprite('treasureChestClosed'),
k.area(),
k.pos(
spawnpoints.treasureChest.x / scaleFactor,
spawnpoints.treasureChest.y / scaleFactor
map.pos.x + chestPosition.x / scaleFactor,
map.pos.y + chestPosition.y / scaleFactor
),
k.scale(1.0), // Adjust scale if necessary
k.offscreen({ hide: false, distance: 10 }), // Optional
'treasureChest', // Tag for identifying this object type
k.scale(0.5),
'treasureChest',
]);

// Add interaction logic to open the chest
chestObj.onCollide('player', () => {
if (!isOpen) {
isOpen = true;
// Change sprite to the open treasure chest
chestObj.useSprite('treasureChestOpen');

// Play the chest opening sound
k.playSound('chestOpen');
k.play('chestOpen');

chestObj.use(k.sprite('treasureChestOpen'));
const msg = k.add([
k.text('ohh you found the secret treasure!', {
size: 15,
}),
k.pos(chestObj.pos.x, chestObj.pos.y + 20),
]);

k.wait(4, () => {
msg.destroy();
});
}
});

Expand Down

0 comments on commit c76f6bd

Please sign in to comment.