Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created quest add on to player state #190

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/factories/player.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -35,7 +35,7 @@ export function makePlayer(playerProps = {}, customScale = scaleFactor) {
);
}

const playerState = {
const playerStateHandler = {
set: function (target, key, value) {
const gameState = getGameState();
gameState.player[key] = value;
Expand All @@ -48,10 +48,10 @@ export function makePlayer(playerProps = {}, customScale = scaleFactor) {
},
};
const state = new Proxy(
{
...playerProps,
},
playerState
// target
playerState,
// handler
playerStateHandler
);

const player = k.make([
Expand Down
6 changes: 6 additions & 0 deletions src/factories/quest.factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const makeQuest = (name, description, objectives) => {
const newQuest = {};
const questDetails = { description, objectives, done: false };
newQuest[name] = questDetails;
return newQuest;
};
2 changes: 0 additions & 2 deletions src/interactions/map_campus_house_1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const interactions = [
livingRoomCouchInteractions,
bedroomTableInteractions,
diningTableInteractions,
livingroomcouchInteractions,
bedroomPlantInteractions,
restroomToiletInteractions,
];

export default interactions;
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
21 changes: 20 additions & 1 deletion src/interactions/map_start/bruno.interaction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { interactionHandler } from '../handler.interactions';
import { displayDialogue } from '../../utils';
import { conversationBruno, bruno } from '../../constants';
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, () => {
Expand All @@ -10,7 +17,19 @@ export const interactionWithBruno = (player, k, map) => {
characterName: bruno.name,
text: conversationBruno,
onDisplayEnd: () => {
player.state.hasTalkedToBruno = true;
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!');
},
});
});
Expand Down
35 changes: 29 additions & 6 deletions src/interactions/map_start/enterMapCityLeft.interaction.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
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) {
recieveQuest(player, map_start_quests['Start Interacting!']);
if (!hasTalkedToBruno) {
displayDialogue({
k,
player,
Expand All @@ -21,7 +44,7 @@ export const enterMapCityLeftInteraction = (player, k, map) => {

return;
} else {
if (!player.state.wasInRestroom) {
if (!wasInRestroom) {
displayDialogue({
k,
player,
Expand All @@ -34,7 +57,7 @@ export const enterMapCityLeftInteraction = (player, k, map) => {
return;
}

if (!player.state.hasHandsWashed) {
if (!hasWashedHands) {
displayDialogue({
k,
player,
Expand Down
35 changes: 29 additions & 6 deletions src/interactions/map_start/enterMapCityRight.interactions.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -22,7 +45,7 @@ export const enterMapCityRightInteraction = (player, k, map) => {

return;
} else {
if (!player.state.wasInRestroom) {
if (!wasInRestroom) {
displayDialogue({
k,
player,
Expand All @@ -35,7 +58,7 @@ export const enterMapCityRightInteraction = (player, k, map) => {
return;
}

if (!player.state.hasHandsWashed) {
if (!hasWashedHands) {
displayDialogue({
k,
player,
Expand Down
21 changes: 17 additions & 4 deletions src/interactions/map_start/restroom.interactions.js
Original file line number Diff line number Diff line change
@@ -1,12 +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', () => {
player.state.wasInRestroom = true;
player.state.hasHandsWashed = false;
completeQuestObjective(player, questName, 'wasInRestroom');
completeQuest(player, questName);
const dialogue = ['You feel refreshed now.', 'Ready for the ride.'];

if (!player.state.hasTalkedToBruno) {
if (!hasTalkedToBruno) {
dialogue.push('You should talk to Bruno first.');
}
displayDialogue({
Expand All @@ -17,12 +28,14 @@ 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);
completeQuest(player, questName);
},
});
});
Expand Down
23 changes: 23 additions & 0 deletions src/interactions/quests/constants.quests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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!',
'Start interacting with the environment!',
{
hasTalkedToBruno: false,
hasWashedHands: false,
wasInRestroom: false,
}
),
'Start Learning!': makeQuest(
'Start Learning!',
'Go to the classroom & start learning a programming language!',
{
hasBeenInClassroom: false,
hasTalkedToTeacher: false,
hasLearnedASubject: false,
}
),
};
5 changes: 1 addition & 4 deletions src/utils/gameState.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ const LOCAL_STORAGE_GAME_STATE_KEY = 'gameState';

const initialState = () => ({
player: {
hasTalkedToBruno: false,
wasInRestroom: false,
hasHandsWashed: false,
hasOpenedChest: false,
energy: 100,
coinsCollected: 0,
coinsSpent: 0,
speed: speedByScaleFactor,
direction: 'down',
isInDialog: false,
collectedCoins: 0,
quests: {},
score: 0,
scene: 'start',
position: { x: 32, y: 384 },
Expand Down
80 changes: 80 additions & 0 deletions src/utils/questHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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]) {
alert(`You have not finished: "${objective}"`);
hasFoundIncompleteObjective = true;
}
}

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 the quest: "${questName}"`);
}
};

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 };
alert(`Completed quest objective: "${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 };
alert(`You have started the quest: "${Object.keys(quest)[0]}"`);
}
};

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.