From 3160e254d83dcb7dccd08c236ee409ce3f60f5f0 Mon Sep 17 00:00:00 2001 From: Jacob Wilson Date: Fri, 25 Oct 2024 21:06:02 -0500 Subject: [PATCH 1/3] Added quest capability --- src/core/kaplay/quests/index.js | 13 +++++ src/factories/player.factory.js | 13 +++-- src/factories/quest.factory.js | 6 ++ .../enterMapStartRight.interactions.js | 1 - .../map_start/bruno.interaction.js | 9 ++- .../map_start/enterMapCityLeft.interaction.js | 36 ++++++++++-- .../enterMapCityRight.interactions.js | 35 ++++++++++-- .../map_start/restroom.interactions.js | 15 +++-- src/interactions/quests/constants.quests.js | 13 +++++ src/interactions/quests/map_start/index.js | 5 ++ .../map_start/startInteracting.quests.js | 10 ++++ src/scenes/bootstrap.js | 4 +- src/utils/gameState.js | 2 + src/utils/questHandler.js | 55 +++++++++++++++++++ 14 files changed, 192 insertions(+), 25 deletions(-) create mode 100644 src/core/kaplay/quests/index.js create mode 100644 src/factories/quest.factory.js create mode 100644 src/interactions/quests/constants.quests.js create mode 100644 src/interactions/quests/map_start/index.js create mode 100644 src/interactions/quests/map_start/startInteracting.quests.js create mode 100644 src/utils/questHandler.js diff --git a/src/core/kaplay/quests/index.js b/src/core/kaplay/quests/index.js new file mode 100644 index 0000000..ab8e36d --- /dev/null +++ b/src/core/kaplay/quests/index.js @@ -0,0 +1,13 @@ +import { setGameState } from '../../../utils/gameState'; + +export const addQuests = (player, quests) => { + if (quests === undefined) { + return; + } + quests.forEach((quest) => { + if (!Object.keys(player.state.quests).includes(Object.keys(quest)[0])) { + player.state.quests = { ...player.state.quests, ...quest }; + setGameState({ player: { ...player.state } }); + } + }); +}; diff --git a/src/factories/player.factory.js b/src/factories/player.factory.js index 2ac0469..3dd135b 100644 --- a/src/factories/player.factory.js +++ b/src/factories/player.factory.js @@ -2,7 +2,7 @@ import { k } from '../kplayCtx'; import { scaleFactor } from '../constants'; import { getGameState, setGameState } from '../utils/gameState'; -export function makePlayer(playerProps = {}, customScale = scaleFactor) { +export function makePlayer(playerState = {}, customScale = scaleFactor) { if (!k.getSprite('player')) { k.loadSprite('player', './assets/sprites/characters.png', { sliceX: 10, @@ -35,8 +35,9 @@ export function makePlayer(playerProps = {}, customScale = scaleFactor) { ); } - const playerState = { + const playerStateHandler = { set: function (target, key, value) { + console.log('hit'); const gameState = getGameState(); gameState.player[key] = value; setGameState(gameState); @@ -48,10 +49,10 @@ export function makePlayer(playerProps = {}, customScale = scaleFactor) { }, }; const state = new Proxy( - { - ...playerProps, - }, - playerState + // target + playerState, + // handler + playerStateHandler ); const player = k.make([ diff --git a/src/factories/quest.factory.js b/src/factories/quest.factory.js new file mode 100644 index 0000000..7d53cc2 --- /dev/null +++ b/src/factories/quest.factory.js @@ -0,0 +1,6 @@ +export const makeQuest = (name, description, objectives) => { + const newQuest = {}; + const questDetails = { description, objectives, done: false }; + newQuest[name] = questDetails; + return newQuest; +}; diff --git a/src/interactions/map_city/enterMapStartRight.interactions.js b/src/interactions/map_city/enterMapStartRight.interactions.js index fe0e773..a8c25c6 100644 --- a/src/interactions/map_city/enterMapStartRight.interactions.js +++ b/src/interactions/map_city/enterMapStartRight.interactions.js @@ -8,7 +8,6 @@ export const enterMapStartRightInteraction = (player, k) => { 'You are about to go back to the start. Are you sure you want to proceed?', ], }); - // console.log(granted); if (granted) { k.go('start', 'spawn_right'); } diff --git a/src/interactions/map_start/bruno.interaction.js b/src/interactions/map_start/bruno.interaction.js index c4d0601..0e63eb5 100644 --- a/src/interactions/map_start/bruno.interaction.js +++ b/src/interactions/map_start/bruno.interaction.js @@ -1,16 +1,23 @@ import { interactionHandler } from '../handler.interactions'; import { displayDialogue } from '../../utils'; import { conversationBruno, bruno } from '../../constants'; +import { completeQuestObjective, recieveQuest } from '../../utils/questHandler'; +import { map_start_quests } from '../quests/constants.quests'; export const interactionWithBruno = (player, k, map) => { interactionHandler(player, bruno.name, k, () => { + recieveQuest(player, map_start_quests['Start Interacting!']); displayDialogue({ k, player, characterName: bruno.name, text: conversationBruno, onDisplayEnd: () => { - player.state.hasTalkedToBruno = true; + completeQuestObjective( + player, + 'Start Interacting!', + 'hasTalkedToBruno' + ); }, }); }); diff --git a/src/interactions/map_start/enterMapCityLeft.interaction.js b/src/interactions/map_start/enterMapCityLeft.interaction.js index b9e27ea..4e03c31 100644 --- a/src/interactions/map_start/enterMapCityLeft.interaction.js +++ b/src/interactions/map_start/enterMapCityLeft.interaction.js @@ -1,15 +1,39 @@ import { displayDialogue } from '../../utils'; +import { + recieveQuest, + retrieveQuestObjectiveStatus, +} from '../../utils/questHandler'; +import { map_start_quests } from '../quests/constants.quests'; export const enterMapCityLeftInteraction = (player, k, map) => { + const questName = 'Start Interacting!'; + const hasTalkedToBruno = retrieveQuestObjectiveStatus( + player, + questName, + 'hasTalkedToBruno' + ); + const wasInRestroom = retrieveQuestObjectiveStatus( + player, + questName, + 'wasInRestroom' + ); + const hasWashedHands = retrieveQuestObjectiveStatus( + player, + questName, + 'hasWashedHands' + ); player.onCollide('enter_map_left', () => { if ( - player.state.hasTalkedToBruno && - player.state.wasInRestroom && - player.state.hasHandsWashed + hasTalkedToBruno && + wasInRestroom && + hasWashedHands && + questName in player.state.quests ) { k.go('city', 'spawn_office_left'); } else { - if (!player.state.hasTalkedToBruno) { + console.log(map_start_quests); + recieveQuest(player, map_start_quests['Start Interacting!']); + if (!hasTalkedToBruno) { displayDialogue({ k, player, @@ -21,7 +45,7 @@ export const enterMapCityLeftInteraction = (player, k, map) => { return; } else { - if (!player.state.wasInRestroom) { + if (!wasInRestroom) { displayDialogue({ k, player, @@ -34,7 +58,7 @@ export const enterMapCityLeftInteraction = (player, k, map) => { return; } - if (!player.state.hasHandsWashed) { + if (!hasWashedHands) { displayDialogue({ k, player, diff --git a/src/interactions/map_start/enterMapCityRight.interactions.js b/src/interactions/map_start/enterMapCityRight.interactions.js index 42f939d..0597dba 100644 --- a/src/interactions/map_start/enterMapCityRight.interactions.js +++ b/src/interactions/map_start/enterMapCityRight.interactions.js @@ -1,16 +1,39 @@ import { displayDialogue } from '../../utils'; +import { + recieveQuest, + retrieveQuestObjectiveStatus, +} from '../../utils/questHandler'; +import { map_start_quests } from '../quests/constants.quests'; export const enterMapCityRightInteraction = (player, k, map) => { + const questName = 'Start Interacting!'; + const hasTalkedToBruno = retrieveQuestObjectiveStatus( + player, + questName, + 'hasTalkedToBruno' + ); + const wasInRestroom = retrieveQuestObjectiveStatus( + player, + questName, + 'wasInRestroom' + ); + const hasWashedHands = retrieveQuestObjectiveStatus( + player, + questName, + 'hasWashedHands' + ); player.onCollide('enter_map_right', () => { // Collision point (Enter map boundary) //! NOT THE SPAWNPOINT if ( - player.state.hasTalkedToBruno && - player.state.wasInRestroom && - player.state.hasHandsWashed + hasTalkedToBruno && + wasInRestroom && + hasWashedHands && + questName in player.state.quests ) { k.go('city', 'spawn_office_right'); // City spawn point } else { - if (!player.state.hasTalkedToBruno) { + recieveQuest(player, map_start_quests['Start Interacting!']); + if (!hasTalkedToBruno) { displayDialogue({ k, player, @@ -22,7 +45,7 @@ export const enterMapCityRightInteraction = (player, k, map) => { return; } else { - if (!player.state.wasInRestroom) { + if (!wasInRestroom) { displayDialogue({ k, player, @@ -35,7 +58,7 @@ export const enterMapCityRightInteraction = (player, k, map) => { return; } - if (!player.state.hasHandsWashed) { + if (!hasWashedHands) { displayDialogue({ k, player, diff --git a/src/interactions/map_start/restroom.interactions.js b/src/interactions/map_start/restroom.interactions.js index b5298a5..dcf3432 100644 --- a/src/interactions/map_start/restroom.interactions.js +++ b/src/interactions/map_start/restroom.interactions.js @@ -1,12 +1,18 @@ import { displayDialogue } from '../../utils'; +import { + completeQuestObjective, + retrieveQuestObjectiveStatus, +} from '../../utils/questHandler'; export const restroomInteractions = (player, k, map) => { + const questName = 'Start Interacting!'; player.onCollide('restroom_toilet', () => { - player.state.wasInRestroom = true; - player.state.hasHandsWashed = false; + completeQuestObjective(player, questName, 'wasInRestroom'); const dialogue = ['You feel refreshed now.', 'Ready for the ride.']; - if (!player.state.hasTalkedToBruno) { + if ( + !retrieveQuestObjectiveStatus(player, questName, 'hasTalkedToBruno') + ) { dialogue.push('You should talk to Bruno first.'); } displayDialogue({ @@ -17,12 +23,13 @@ export const restroomInteractions = (player, k, map) => { }); player.onCollide('restroom_sink', () => { + const sinkObjective = 'hasWashedHands'; displayDialogue({ k, player, text: ['You washed your hands. Good job!'], onDisplayEnd: () => { - player.state.hasHandsWashed = true; + completeQuestObjective(player, questName, sinkObjective); }, }); }); diff --git a/src/interactions/quests/constants.quests.js b/src/interactions/quests/constants.quests.js new file mode 100644 index 0000000..feb90e1 --- /dev/null +++ b/src/interactions/quests/constants.quests.js @@ -0,0 +1,13 @@ +import { makeQuest } from '../../factories/quest.factory'; + +export const map_start_quests = { + 'Start Interacting!': makeQuest( + 'Start Interacting!', + 'Start interacting with the environment!', + { + hasTalkedToBruno: false, + hasWashedHands: false, + wasInRestroom: false, + } + ), +}; diff --git a/src/interactions/quests/map_start/index.js b/src/interactions/quests/map_start/index.js new file mode 100644 index 0000000..065c961 --- /dev/null +++ b/src/interactions/quests/map_start/index.js @@ -0,0 +1,5 @@ +import { startInteractingQuest } from './startInteracting.quests'; + +const quests = [startInteractingQuest]; + +export default quests; diff --git a/src/interactions/quests/map_start/startInteracting.quests.js b/src/interactions/quests/map_start/startInteracting.quests.js new file mode 100644 index 0000000..1ccbf86 --- /dev/null +++ b/src/interactions/quests/map_start/startInteracting.quests.js @@ -0,0 +1,10 @@ +export const startInteractingQuest = { + 'Start Interacting!': { + description: 'Start interacting with the environment!', + objectives: { + hasTalkedToBruno: false, + hasWashedHands: false, + wasInRestroom: false, + }, + }, +}; diff --git a/src/scenes/bootstrap.js b/src/scenes/bootstrap.js index ffcca8f..b1311fb 100644 --- a/src/scenes/bootstrap.js +++ b/src/scenes/bootstrap.js @@ -6,12 +6,13 @@ import { k } from '../kplayCtx'; import { getGameState } from '../utils/gameState'; import { addPlayerControls } from './../player.controls'; import { resetPausingVariables } from '../utils/resetPausingVariables'; +import { addQuests } from '../core/kaplay/quests'; export async function bootstrap(bootMapCb, mapArgs) { const gameState = getGameState(); const player = makePlayer(gameState.player); - const [map, spawnpoint, gameObjects, interactions, sounds] = + const [map, spawnpoint, gameObjects, interactions, sounds, quests] = await bootMapCb(); if (mapArgs?.enter_tag === 'Player') { @@ -32,4 +33,5 @@ export async function bootstrap(bootMapCb, mapArgs) { addGameObjects(gameObjects, map, spawnpoint).forEach((obj) => map.add(obj)); attachInteractions(interactions, 'player'); addSceneSounds(sounds, k, map); + addQuests(player, quests); } diff --git a/src/utils/gameState.js b/src/utils/gameState.js index 4949419..b74f059 100644 --- a/src/utils/gameState.js +++ b/src/utils/gameState.js @@ -12,6 +12,7 @@ const initialState = () => ({ direction: 'down', isInDialog: false, collectedCoins: 0, + quests: {}, score: 0, scene: 'start', position: { x: 32, y: 384 }, @@ -56,6 +57,7 @@ export const saveGameState = (newState) => { }; export const getGameState = () => { + console.log(currentState); if (currentState) { return currentState; } else { diff --git a/src/utils/questHandler.js b/src/utils/questHandler.js new file mode 100644 index 0000000..de30b05 --- /dev/null +++ b/src/utils/questHandler.js @@ -0,0 +1,55 @@ +import { setGameState } from './gameState'; + +export const completeQuest = (player, questName) => { + if (playerHasQuest(player, questName)) { + const { objectives } = player.state.quests[questName]; + for (let objective in objectives) { + if (!objectives[objective]) { + console.log('an objective is false'); + return; + } + } + console.log('deletes quest object'); + + setGameState({ player: { ...player.state } }); + } + alert('Completed quest!'); +}; + +export const completeQuestObjective = (player, questName, objective) => { + if ( + !playerHasQuest(player, questName) || + !playerHasObjective(player, questName, objective) + ) { + return; + } + player.state.quests[questName].objectives[objective] = true; + player.state.quests = { ...player.state.quests }; + setGameState({ player: { ...player.state } }); + alert('Completed quest objective!'); +}; + +export const retrieveQuestObjectiveStatus = (player, questName, objective) => { + if ( + !playerHasQuest(player, questName) || + !playerHasObjective(player, questName, objective) + ) { + return; + } + return player.state.quests[questName].objectives[objective]; +}; + +export const recieveQuest = (player, quest) => { + if (!playerHasQuest(player, Object.keys(quest)[0])) { + player.state.quests = { ...player.state.quests, ...quest }; + setGameState({ player: { ...player.state } }); + } +}; + +const playerHasQuest = (player, questName) => { + return questName in player.state.quests; +}; + +const playerHasObjective = (player, questName, objective) => { + return objective in player.state.quests[questName].objectives; +}; From fcddaea212f6be8d46f0b4eaad5bdd96446c3c99 Mon Sep 17 00:00:00 2001 From: Jacob Wilson Date: Mon, 28 Oct 2024 20:40:45 -0500 Subject: [PATCH 2/3] Created quests for game --- src/core/kaplay/quests/index.js | 13 ------ src/factories/player.factory.js | 1 - .../map_start/bruno.interaction.js | 16 ++++++- .../map_start/enterMapCityLeft.interaction.js | 1 - .../map_start/restroom.interactions.js | 12 +++-- src/interactions/quests/constants.quests.js | 10 ++++ src/interactions/quests/map_start/index.js | 5 -- .../map_start/startInteracting.quests.js | 10 ---- src/scenes/bootstrap.js | 2 - src/utils/gameState.js | 4 -- src/utils/questHandler.js | 46 ++++++++++++++----- src/utils/resetPausingVariables.js | 2 +- 12 files changed, 69 insertions(+), 53 deletions(-) delete mode 100644 src/core/kaplay/quests/index.js delete mode 100644 src/interactions/quests/map_start/index.js delete mode 100644 src/interactions/quests/map_start/startInteracting.quests.js diff --git a/src/core/kaplay/quests/index.js b/src/core/kaplay/quests/index.js deleted file mode 100644 index ab8e36d..0000000 --- a/src/core/kaplay/quests/index.js +++ /dev/null @@ -1,13 +0,0 @@ -import { setGameState } from '../../../utils/gameState'; - -export const addQuests = (player, quests) => { - if (quests === undefined) { - return; - } - quests.forEach((quest) => { - if (!Object.keys(player.state.quests).includes(Object.keys(quest)[0])) { - player.state.quests = { ...player.state.quests, ...quest }; - setGameState({ player: { ...player.state } }); - } - }); -}; diff --git a/src/factories/player.factory.js b/src/factories/player.factory.js index 3dd135b..0919d5f 100644 --- a/src/factories/player.factory.js +++ b/src/factories/player.factory.js @@ -37,7 +37,6 @@ export function makePlayer(playerState = {}, customScale = scaleFactor) { const playerStateHandler = { set: function (target, key, value) { - console.log('hit'); const gameState = getGameState(); gameState.player[key] = value; setGameState(gameState); diff --git a/src/interactions/map_start/bruno.interaction.js b/src/interactions/map_start/bruno.interaction.js index 0e63eb5..a3d653c 100644 --- a/src/interactions/map_start/bruno.interaction.js +++ b/src/interactions/map_start/bruno.interaction.js @@ -1,23 +1,35 @@ import { interactionHandler } from '../handler.interactions'; import { displayDialogue } from '../../utils'; import { conversationBruno, bruno } from '../../constants'; -import { completeQuestObjective, recieveQuest } from '../../utils/questHandler'; +import { + completeQuestObjective, + completeQuest, + playerHasQuest, + recieveQuest, +} from '../../utils/questHandler'; import { map_start_quests } from '../quests/constants.quests'; export const interactionWithBruno = (player, k, map) => { interactionHandler(player, bruno.name, k, () => { - recieveQuest(player, map_start_quests['Start Interacting!']); displayDialogue({ k, player, characterName: bruno.name, text: conversationBruno, onDisplayEnd: () => { + if (!playerHasQuest(player, 'Start Interacting!')) { + recieveQuest( + player, + map_start_quests['Start Interacting!'] + ); + } completeQuestObjective( player, 'Start Interacting!', 'hasTalkedToBruno' ); + // Internally checks if all objectives are complete + completeQuest(player, 'Start Interacting!'); }, }); }); diff --git a/src/interactions/map_start/enterMapCityLeft.interaction.js b/src/interactions/map_start/enterMapCityLeft.interaction.js index 4e03c31..85b0098 100644 --- a/src/interactions/map_start/enterMapCityLeft.interaction.js +++ b/src/interactions/map_start/enterMapCityLeft.interaction.js @@ -31,7 +31,6 @@ export const enterMapCityLeftInteraction = (player, k, map) => { ) { k.go('city', 'spawn_office_left'); } else { - console.log(map_start_quests); recieveQuest(player, map_start_quests['Start Interacting!']); if (!hasTalkedToBruno) { displayDialogue({ diff --git a/src/interactions/map_start/restroom.interactions.js b/src/interactions/map_start/restroom.interactions.js index dcf3432..bb497f8 100644 --- a/src/interactions/map_start/restroom.interactions.js +++ b/src/interactions/map_start/restroom.interactions.js @@ -1,18 +1,23 @@ import { displayDialogue } from '../../utils'; import { + completeQuest, completeQuestObjective, retrieveQuestObjectiveStatus, } from '../../utils/questHandler'; export const restroomInteractions = (player, k, map) => { const questName = 'Start Interacting!'; + const hasTalkedToBruno = retrieveQuestObjectiveStatus( + player, + questName, + 'hasTalkedToBruno' + ); player.onCollide('restroom_toilet', () => { completeQuestObjective(player, questName, 'wasInRestroom'); + completeQuest(player, questName); const dialogue = ['You feel refreshed now.', 'Ready for the ride.']; - if ( - !retrieveQuestObjectiveStatus(player, questName, 'hasTalkedToBruno') - ) { + if (!hasTalkedToBruno) { dialogue.push('You should talk to Bruno first.'); } displayDialogue({ @@ -30,6 +35,7 @@ export const restroomInteractions = (player, k, map) => { text: ['You washed your hands. Good job!'], onDisplayEnd: () => { completeQuestObjective(player, questName, sinkObjective); + completeQuest(player, questName); }, }); }); diff --git a/src/interactions/quests/constants.quests.js b/src/interactions/quests/constants.quests.js index feb90e1..db129c2 100644 --- a/src/interactions/quests/constants.quests.js +++ b/src/interactions/quests/constants.quests.js @@ -1,5 +1,6 @@ import { makeQuest } from '../../factories/quest.factory'; +// Structured this way for now so we can get the correct quest and structures are correct for player state export const map_start_quests = { 'Start Interacting!': makeQuest( 'Start Interacting!', @@ -10,4 +11,13 @@ export const map_start_quests = { wasInRestroom: false, } ), + 'Start Learning!': makeQuest( + 'Start Learning!', + 'Go to the classroom & start learning a programming language!', + { + hasBeenInClassroom: false, + hasTalkedToTeacher: false, + hasLearnedASubject: false, + } + ), }; diff --git a/src/interactions/quests/map_start/index.js b/src/interactions/quests/map_start/index.js deleted file mode 100644 index 065c961..0000000 --- a/src/interactions/quests/map_start/index.js +++ /dev/null @@ -1,5 +0,0 @@ -import { startInteractingQuest } from './startInteracting.quests'; - -const quests = [startInteractingQuest]; - -export default quests; diff --git a/src/interactions/quests/map_start/startInteracting.quests.js b/src/interactions/quests/map_start/startInteracting.quests.js deleted file mode 100644 index 1ccbf86..0000000 --- a/src/interactions/quests/map_start/startInteracting.quests.js +++ /dev/null @@ -1,10 +0,0 @@ -export const startInteractingQuest = { - 'Start Interacting!': { - description: 'Start interacting with the environment!', - objectives: { - hasTalkedToBruno: false, - hasWashedHands: false, - wasInRestroom: false, - }, - }, -}; diff --git a/src/scenes/bootstrap.js b/src/scenes/bootstrap.js index b1311fb..8089b92 100644 --- a/src/scenes/bootstrap.js +++ b/src/scenes/bootstrap.js @@ -6,7 +6,6 @@ import { k } from '../kplayCtx'; import { getGameState } from '../utils/gameState'; import { addPlayerControls } from './../player.controls'; import { resetPausingVariables } from '../utils/resetPausingVariables'; -import { addQuests } from '../core/kaplay/quests'; export async function bootstrap(bootMapCb, mapArgs) { const gameState = getGameState(); @@ -33,5 +32,4 @@ export async function bootstrap(bootMapCb, mapArgs) { addGameObjects(gameObjects, map, spawnpoint).forEach((obj) => map.add(obj)); attachInteractions(interactions, 'player'); addSceneSounds(sounds, k, map); - addQuests(player, quests); } diff --git a/src/utils/gameState.js b/src/utils/gameState.js index b74f059..b59ad40 100644 --- a/src/utils/gameState.js +++ b/src/utils/gameState.js @@ -4,9 +4,6 @@ const LOCAL_STORAGE_GAME_STATE_KEY = 'gameState'; const initialState = () => ({ player: { - hasTalkedToBruno: false, - wasInRestroom: false, - hasHandsWashed: false, energy: 100, speed: speedByScaleFactor, direction: 'down', @@ -57,7 +54,6 @@ export const saveGameState = (newState) => { }; export const getGameState = () => { - console.log(currentState); if (currentState) { return currentState; } else { diff --git a/src/utils/questHandler.js b/src/utils/questHandler.js index de30b05..1c650e8 100644 --- a/src/utils/questHandler.js +++ b/src/utils/questHandler.js @@ -1,55 +1,79 @@ -import { setGameState } from './gameState'; - export const completeQuest = (player, questName) => { if (playerHasQuest(player, questName)) { + if (player.state.quests[questName].done) { + return; + } + const { objectives } = player.state.quests[questName]; + // New object to contain quest properties + const newObj = {}; + let hasFoundIncompleteObjective = false; + // If there is an objective that is not finished then alert user for (let objective in objectives) { if (!objectives[objective]) { - console.log('an objective is false'); - return; + alert(`You have not finished: "${objective}"`); + hasFoundIncompleteObjective = true; } } - console.log('deletes quest object'); - setGameState({ player: { ...player.state } }); + if (hasFoundIncompleteObjective) { + return; + } + + // Triggers set handler in Proxy state object + newObj[questName] = { ...player.state.quests[questName], done: true }; + player.state.quests = { ...player.state.quests, ...newObj }; + alert(`Completed "${questName}"`); } - alert('Completed quest!'); }; export const completeQuestObjective = (player, questName, objective) => { + // If player does not have quest or objective if ( !playerHasQuest(player, questName) || !playerHasObjective(player, questName, objective) ) { return; } + + if (player.state.quests[questName].objectives[objective]) { + return; + } + + // Sets objective to true status player.state.quests[questName].objectives[objective] = true; + // Triggers set handler in Proxy state object player.state.quests = { ...player.state.quests }; - setGameState({ player: { ...player.state } }); - alert('Completed quest objective!'); + alert(`Completed quest "${objective}"`); }; export const retrieveQuestObjectiveStatus = (player, questName, objective) => { + // If player does not have quest or objective if ( !playerHasQuest(player, questName) || !playerHasObjective(player, questName, objective) ) { return; } + + // Returns objective status return player.state.quests[questName].objectives[objective]; }; export const recieveQuest = (player, quest) => { if (!playerHasQuest(player, Object.keys(quest)[0])) { + // Triggers set handler in Proxy state object player.state.quests = { ...player.state.quests, ...quest }; - setGameState({ player: { ...player.state } }); } }; -const playerHasQuest = (player, questName) => { +export const playerHasQuest = (player, questName) => { return questName in player.state.quests; }; const playerHasObjective = (player, questName, objective) => { return objective in player.state.quests[questName].objectives; }; + +// If you want to add quests, you can recieve them through interactions. +// Use recieveQuest to get the quest via the player's state object. diff --git a/src/utils/resetPausingVariables.js b/src/utils/resetPausingVariables.js index a48af11..bff586c 100644 --- a/src/utils/resetPausingVariables.js +++ b/src/utils/resetPausingVariables.js @@ -1,3 +1,3 @@ export const resetPausingVariables = (player) => { - player.state = { ...player.state, isInDialog: false }; + player.state.isInDialog = false; }; From cb28199adc66611c99873b6c8b48d20d21f36376 Mon Sep 17 00:00:00 2001 From: Jacob Wilson Date: Mon, 28 Oct 2024 20:53:24 -0500 Subject: [PATCH 3/3] Fixed unused variable; Better alert messages --- src/interactions/map_campus_house_1/index.js | 2 -- src/scenes/bootstrap.js | 2 +- src/utils/questHandler.js | 5 +++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/interactions/map_campus_house_1/index.js b/src/interactions/map_campus_house_1/index.js index a5aaf58..5c9048a 100644 --- a/src/interactions/map_campus_house_1/index.js +++ b/src/interactions/map_campus_house_1/index.js @@ -22,9 +22,7 @@ const interactions = [ livingRoomCouchInteractions, bedroomTableInteractions, diningTableInteractions, - livingroomcouchInteractions, bedroomPlantInteractions, - restroomToiletInteractions, ]; export default interactions; diff --git a/src/scenes/bootstrap.js b/src/scenes/bootstrap.js index 8089b92..ffcca8f 100644 --- a/src/scenes/bootstrap.js +++ b/src/scenes/bootstrap.js @@ -11,7 +11,7 @@ export async function bootstrap(bootMapCb, mapArgs) { const gameState = getGameState(); const player = makePlayer(gameState.player); - const [map, spawnpoint, gameObjects, interactions, sounds, quests] = + const [map, spawnpoint, gameObjects, interactions, sounds] = await bootMapCb(); if (mapArgs?.enter_tag === 'Player') { diff --git a/src/utils/questHandler.js b/src/utils/questHandler.js index 1c650e8..5a85891 100644 --- a/src/utils/questHandler.js +++ b/src/utils/questHandler.js @@ -23,7 +23,7 @@ export const completeQuest = (player, questName) => { // Triggers set handler in Proxy state object newObj[questName] = { ...player.state.quests[questName], done: true }; player.state.quests = { ...player.state.quests, ...newObj }; - alert(`Completed "${questName}"`); + alert(`Completed the quest: "${questName}"`); } }; @@ -44,7 +44,7 @@ export const completeQuestObjective = (player, questName, objective) => { player.state.quests[questName].objectives[objective] = true; // Triggers set handler in Proxy state object player.state.quests = { ...player.state.quests }; - alert(`Completed quest "${objective}"`); + alert(`Completed quest objective: "${objective}"`); }; export const retrieveQuestObjectiveStatus = (player, questName, objective) => { @@ -64,6 +64,7 @@ export const recieveQuest = (player, quest) => { if (!playerHasQuest(player, Object.keys(quest)[0])) { // Triggers set handler in Proxy state object player.state.quests = { ...player.state.quests, ...quest }; + alert(`You have started the quest: "${Object.keys(quest)[0]}"`); } };