%%js
handleKeyDown({ key }) {
switch (key) {
case 'e':
if(this.hasCollided("Hello, please help me escape this prison.")){
levelData.setPlayerItem();
this.destroy();
}
break;
}
}
class Data {
constructor() {
this.itemsCollected = 0; // Initialize itemsCollected as an instance property
this.keys = 0; // Add property to track if player has received the key
this.displayStatus();
}
setPlayerItem() {
this.itemsCollected++;
addItemToInventory("spoon");
if (this.itemsCollected === 2) {
const questGiver = GameEnv.gameObjects.find(obj => obj.canvas?.id === 'Questgiver');
if (questGiver) {
if (this.keys == 0) {
questGiver.spriteData.greeting = "Here's the key to escape. Use it wisely!";
console.clear();
} else {
questGiver.spriteData.greeting = "You already have the key. Use it to escape!";
this.displayStatus();
}
}
} else {
this.displayStatus();
}
}
removePlayerItem(item){
this.itemsCollected--;
removeItemFromInventory(item);
}
getPlayerItem() {
return this.itemsCollected;
}
hasEscapeKey() {
this.displayStatus();
return this.keys;
}
addKey(){
this.keys++;
addItemToInventory("key");
}
}
export default Data;
- This code is used to register if the player is touching the item by matching the greeting
- It then calls a function in a different script, which is passed to all objects, allowed each script to talk to each other easily
%%js
normalizeMovement(speed){
let magnitude;
if(Math.abs(this.velocity.x == playerSpeed) && Math.abs(this.velocity.y) == playerSpeed){
magnitude = Math.sqrt((this.velocity.x*this.velocity.x) + (this.velocity.y*this.velocity.y));
speed = speed / magnitude;
}
return speed;
}
switch (keyCode) {
case this.keypress.up:
this.velocity.y = this.normalizeMovement(-1*playerSpeed);
this.direction = 'up';
break;
}
- This works by called the normalize movement funciton
- basically uses pythagorean theorem to make diagaonal movement the same as horizontal movement
- if this didnt exist, player would move faster diagonal
%%js
export function addItemToInventory(itemName) {
if (inventoryItems.length < 8) { // Limit inventory slots
inventoryItems.push(itemName);
localStorage.setItem("inventoryItems", JSON.stringify(inventoryItems));
} else {
alert("Inventory full!");
}
updateInventory();
}
function updateInventory() {
const inventory = document.getElementById("inventory");
if (!inventory) return;
inventory.innerHTML = ""; // Clear old items
inventoryItems.forEach(item => {
const slot = document.createElement("div");
slot.className = "inventory-slot";
const img = document.createElement("img");
img.src = imagePath + itemImages[item] || imagePath + "/assets/images/default.png"; // Fallback image
img.alt = item;
img.style.width = "40px";
img.style.height = "40px";
slot.appendChild(img);
inventory.appendChild(slot);
});
}
- Uses an array to store items
- calls a function that updates the markdown to display image and grid
setPrompt(answers){
for (let i = 0; i < answers.length; i++) {
console.log('Index:', i); // The index of the array
console.log('Question Index:', answers[i].questionIndex);
console.log('Answer:', answers[i].answer);
if(answers[i].questionIndex == 0 && answers[i].answer == "15"){
console.log("correct");
if(!hasGottenQuizKey)
{
this.addKey();
hasGottenQuizKey = true;
}
console.log("keys obtained: " + this.keys);
}
}
}
just checks if the answer is equal to correct.