Skip to content

Commit

Permalink
fix many aciton related bugs, added actionStack
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel committed Sep 12, 2023
1 parent f87a353 commit 3f5ab8a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 51 deletions.
2 changes: 1 addition & 1 deletion doc.json

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions src/actions/chapters.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { createAction, getGameData } from "game";
import { createAction, insertChapter } from "game";

export function jump(name: string) {
return createAction({
id: "jump",
type: "normal",
autoskip: true,
start() {
getGameData().currentAction = -1;
getGameData().currentChapter = name;
insertChapter(name);
},
skip() {
return;
},
back() {
getGameData().currentAction = -1;
getGameData().currentChapter = name;
},
back() {},
});
}
2 changes: 1 addition & 1 deletion src/actions/textbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function choice(
type: "normal",
canSkip: false,
async start() {
addChoices(choices, setter);
await addChoices(choices, setter);
},
skip() {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/components/choice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function choiceComponent(
actions: actions,
add(this: KA.GameObj) {
this.onClick(() => {
insertActions(this.actions());
insertActions(this.actions(), 1);
this.parent?.destroy();
setter?.(value);
});
Expand Down
58 changes: 28 additions & 30 deletions src/game.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type * as KA from "kaboom";
import type {
Action,
ActionType,
CharacterDataOpt,
GameData,
LoadImageOpt,
Expand All @@ -20,9 +19,9 @@ const LAYERS = [
];

export const data: Partial<GameData> = {
chapters: new Map<string, BaseAction[]>(),
actionStack: [],
chapters: new Map<string, Action[]>(),
characters: new Map(),
currentChapter: "start",
currentAction: 0,
processingAction: false,
playingAudios: new Map(),
Expand Down Expand Up @@ -67,6 +66,8 @@ export function setVar<T>(name: string, value: T): (value: T) => void {
const data = getGameData();
data.variables[name] = value;

console.log(`Variable ${name} set to ${value}`);

return (value) => {
data.variables[name] = value;
};
Expand All @@ -79,6 +80,8 @@ export function getVar(name: string): any {

// Chapters
export function addChapter(name: string, actions: () => Action<any>[]) {
console.log(`Chapter ${name} added with ${actions().length} actions.`);

getGameData().chapters.set(name, actions());
}

Expand All @@ -101,38 +104,27 @@ export function addCharacter(

export function createAction<T extends Action<unknown>>(opt: T): Action<T> {
const action = { ...opt };

return action as unknown as Action<T>;
}

function getCurrentAction() {
const data = getGameData();

const chapter = data.chapters.get(data.currentChapter);
if (!chapter)
throw new Error(`Chapter "${data.currentChapter}" not found.`);

return chapter[data.currentAction];
const { actionStack, currentAction } = getGameData();
return actionStack[currentAction];
}

function getPreviousAction() {
const data = getGameData();
const chapter = data.chapters.get(data.currentChapter);

if (!chapter)
throw new Error(`Chapter "${data.currentChapter}" not found.`);

return chapter[data.currentAction - 1];
const { actionStack, currentAction } = getGameData();
return actionStack[currentAction - 1];
}

export function insertActions(actions: Action[]) {
const data = getGameData();
const chapter = data.chapters.get(data.currentChapter);

if (!chapter)
throw new Error(`Chapter "${data.currentChapter}" not found.`);
export function insertActions(actions: Action[], mod: number = 0) {
const { actionStack, currentAction } = getGameData();
actionStack.splice(currentAction + 1, 0, ...actions);
}

chapter.splice(data.currentAction, 0, ...actions);
export function insertChapter(name: string) {
const { chapters, actionStack, currentAction } = getGameData();
actionStack.splice(currentAction + 1, 0, ...(chapters.get(name) ?? []));
}

async function nextAction() {
Expand All @@ -142,7 +134,9 @@ async function nextAction() {

data.processingAction = true;

console.log(`Started to process action ${data.currentAction}`, action);
await action.start();
console.log(`Finished to process action ${data.currentAction}`, action);

// If it not stopped by another process, continue
if (data.processingAction) {
Expand All @@ -168,13 +162,18 @@ function previousAction() {
function skipAction() {
if (getCurrentAction().canSkip === false) return;

console.log(`Skipped action ${getGameData().currentAction}`);
getCurrentAction().skip?.();
getGameData().processingAction = false;
getGameData().currentAction++;
}

export function startNovel() {
const { k, m, opt, isProcessingAction } = getGameData();
const input = opt.inputs ?? {
pc: {
next: "space",
screenshoot: "f2",
},
};

// some variables
m.setVar("pronoun", 2);
Expand All @@ -183,15 +182,14 @@ export function startNovel() {
k.onLoad(() => {
k.layers(LAYERS, "textbox");
m._textbox = addTextbox(opt.textbox);
insertChapter("start");
nextAction();

// User input
// TODO: Add support for customize keys
k.onUpdate(() => {
k.debug.log(isProcessingAction());
if (
k.isKeyPressed("space") ||
k.isKeyPressed("right") ||
k.isKeyPressed(input.pc?.next) ||
k.isKeyPressed("enter") ||
k.isMousePressed()
) {
Expand Down
11 changes: 5 additions & 6 deletions src/objects/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function makeChoice(
actions: (v: string) => Action[],
opt?: ChoiceOpt,
setter?: (value: any) => void,
): Choice {
) {
const { k } = getGameData();

const options = {
Expand Down Expand Up @@ -118,14 +118,13 @@ export async function addChoices(
choices[choiceName].value,
choices[choiceName].actions,
opt.choice,
setter,
(v: any) => {
if (setter) setter(v);
resolve(choicesBox);
},
);

choicesBox.add(ch);

ch.onClick(() => {
resolve(choicesBox);
});
});

k.add(choicesBox);
Expand Down
14 changes: 9 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export type TextOptions = {
export type GameData = {
k: KA.KaboomCtx & KaboomPlugins;
m: MandarinaPlugin;
actionStack: Action[];
opt: MandarinaOpt;
chapters: Map<string, BaseAction[]>;
chapters: Map<string, Action[]>;
characters: Map<string, CharacterData>;
currentChapter: string;
currentAction: number;
processingAction: boolean;
playingAudios: Map<string, KA.AudioPlay[]>;
Expand All @@ -48,12 +48,12 @@ export type GameData = {
isProcessingAction(): boolean;
};

type Inputs = "pc" | "gamepad" | "touch";
type Inputs = "pc" | "gamepad";
type GameActions = "next" | "screenshoot";

export type GameInputs = {
[key in Inputs]: {
[key in GameActions]: KA.Key;
[key in Inputs]?: {
[key in GameActions]?: KA.Key;
};
};

Expand Down Expand Up @@ -201,6 +201,8 @@ export type MandarinaOpt = {
writeCommaWait?: number;
/** Visual Novel language (used for set pronouns languages). */
language?: "english" | "spanish";
/** Input settings. */
inputs?: GameInputs;
};

/** `loadImage()` options. */
Expand All @@ -227,6 +229,8 @@ export interface BaseAction {
autoskip?: boolean;
/** If action is skippeable. (default to true) */
canSkip?: boolean;
/** If action was auto inserted */
inserted?: boolean;
/** Runs when action starts */
start(): void | Promise<void>;
/** Runs when action is backed. */
Expand Down

0 comments on commit 3f5ab8a

Please sign in to comment.