Skip to content

Commit

Permalink
improve ts
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel committed Jul 8, 2023
1 parent c81e21e commit 75c9c19
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 49 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
on: [push]
name: test
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npm run check
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
"license": "MIT",
"main": "./dist/mandarina.js",
"module": "./dist/mandarina.mjs",
"types": "./dist/mandarina.d.ts",
"types": "./dist/types.d.ts",
"type": "module",
"scripts": {
"dev": "nodemon --watch ./src --watch ./example --exec \"npm run build && npm run example\" --ext \"ts\"",
"build": "node scripts/build.js",
"build": "node scripts/build.js && npm run dts",
"watch": "nodemon --watch ./src --exec \"npm run build\" --ext \"ts\"",
"build:example": "esbuild example/main.ts --bundle --sourcemap --outfile=example/game.js",
"dev:example": "esbuild example/main.ts --bundle --outfile=example/game.js --servedir=./example/",
"watch:example": "esbuild example/main.ts --bundle --outfile=example/game.js --sourcemap --watch --servedir=./example/",
"fmt": "eslint --fix src/"
"fmt": "eslint --fix src/",
"dts": "tsc",
"check": "tsc --noEmit"
},
"files": [
"dist/",
Expand Down
2 changes: 0 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ function buildTypes() {
types,
}),
);

writeFile(`${distDir}/mandarina.d.ts`, dts);
}

buildTypes();
2 changes: 1 addition & 1 deletion src/actions/audio.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as KA from "kaboom";
import { createAction, getGameData } from "../game";
import { createAction, getGameData } from "game";

export function playAudio(
channel: string,
Expand Down
5 changes: 3 additions & 2 deletions src/actions/background.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type * as KA from "kaboom";
import { createAction, getGameData } from "../game";
import type { VisualAction } from "types";
import { createAction, getGameData } from "game";

export function showBackground(background: string | KA.Color) {
const { k } = getGameData();
Expand Down Expand Up @@ -48,7 +49,7 @@ export function showBackground(background: string | KA.Color) {
back() {
bg.destroy();
},
fadeIn() {
fadeIn(this: VisualAction) {
this.fade = true;
return this;
},
Expand Down
2 changes: 1 addition & 1 deletion src/actions/chapters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createAction, getGameData } from "../game";
import { createAction, getGameData } from "game";

export function jump(name: string) {
return createAction({
Expand Down
8 changes: 4 additions & 4 deletions src/actions/character.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as KA from "kaboom";
import type { VisualAlign } from "../types";
import type { VisualAction, VisualAlign } from "../types";
import type { VisuaLEffectsOpt } from "../components/visual";
import { createAction, getGameData } from "../game";
import { getSpriteDimensions } from "../util";
Expand All @@ -18,7 +18,7 @@ export function showCharacter(
type: "visual",
autoskip: true,
fade: false,
start() {
start(this: VisualAction) {
const textbox = m._textbox;
const comps: KA.Comp[] = [];

Expand Down Expand Up @@ -54,7 +54,7 @@ export function showCharacter(
if (this.side) {
const moveTweenComp = () => {
return {
add() {
add(this: KA.GameObj<any>) {
k.tween(
this.pos.x,
alignments[align].x,
Expand Down Expand Up @@ -134,7 +134,7 @@ export function hideCharacter(characterId: string) {
return;
},
back() {
this.showCharacter(characterId, "default", "center");
showCharacter(characterId, "default", "center");
},
});
}
7 changes: 6 additions & 1 deletion src/actions/textbox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Choice } from "../types";
import type { Action } from "../types";
import { createAction, getGameData } from "../game";
import { addChoices } from "../objects/choices";

Expand Down Expand Up @@ -84,6 +84,11 @@ export function say(...args: string[]) {
}

// TODO: Implement choices
export type Choice = {
text: string;
actions: () => Action[];
};

export function choice(choices: Choice[]) {
return createAction({
id: "choice",
Expand Down
4 changes: 2 additions & 2 deletions src/components/visual.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as KA from "kaboom";
import type { UnionToIntersection } from "../types";
import { getGameData } from "../game";
import type { UnionToIntersection } from "types";
import { getGameData } from "game";
import { onAddObj } from "../util";

export type VisualEffect = "fade" | "appearFrom";
Expand Down
7 changes: 2 additions & 5 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import type {
LoadImageOpt,
SpriteData,
BaseAction,
NormalAction,
VisualAction,
AudioAction,
} from "./types";
import { addTextbox } from "./objects/textbox";
} from "types";
import { addTextbox } from "objects/textbox";

// Constants
const LAYERS = [
Expand Down
16 changes: 8 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type * as KA from "kaboom";
import type { MandarinaOpt, MandarinaPlugin } from "./types";
import type { MandarinaOpt, MandarinaPlugin } from "types";
import kaboom from "kaboom";
import { startNovel, addChapter, addCharacter, data, loadImage } from "./game";
import { jump } from "./actions/chapters";
import { LayerPlugin, layerPlugin } from "./plugins/layer";
import { showCharacter, hideCharacter } from "./actions/character";
import { showBackground } from "./actions/background";
import { playAudio } from "./actions/audio";
import { say, showTextbox, hideTextbox, choice } from "./actions/textbox";
import { startNovel, addChapter, addCharacter, data, loadImage } from "game";
import { jump } from "actions/chapters";
import { LayerPlugin, layerPlugin } from "plugins/layer";
import { showCharacter, hideCharacter } from "actions/character";
import { showBackground } from "actions/background";
import { playAudio } from "actions/audio";
import { say, showTextbox, hideTextbox, choice } from "actions/textbox";

export function mandarinaPlugin(
opt: MandarinaOpt,
Expand Down
4 changes: 2 additions & 2 deletions src/objects/choices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Choice } from "../types";
import { getGameData } from "../game";
import type { Choice } from "types";
import { getGameData } from "game";

export function makeChoice(choice: Choice) {
const { k } = getGameData();
Expand Down
4 changes: 2 additions & 2 deletions src/objects/textbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function textboxComp(): TextboxComp {
curChar: 0,

add() {
textbox = this.text;
namebox = this.name;
textbox = this.text!;
namebox = this.name!;
},
write(this: KA.GameObj, text) {
// TODO: Pronouns replacement support
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/layer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EmptyComp, KaboomCtx } from "kaboom";
import type { EmptyComp, KaboomCtx, GameObj } from "kaboom";

export interface LayerPlugin {
layers: (layersArr: string[], def?: string) => void;
Expand All @@ -24,7 +24,7 @@ export function layerPlugin(k: KaboomCtx): LayerPlugin {
layer(name: string) {
return {
id: "layer",
add() {
add(this: GameObj<any>) {
if (userLayers.indexOf(name) == -1) {
throw new Error(`no layer "${name}"`);
}
Expand Down
22 changes: 12 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import type * as KA from "kaboom";
import type { LayerPlugin } from "./plugins/layer";
export type * from "./components/visual";
export type { LayerPlugin };
import type { LayerPlugin } from "plugins/layer";
import type { Choice } from "actions/textbox";
export type {
VisualComp,
VisuaLEffectsOpt,
VisualCompOpt,
VisualEffect,
OptByEffects,
FadeEffectOpt,
AppearFromEffectOpt,
} from "./components/visual";
export type { LayerPlugin, Choice };

// #region Main function
declare function mandarina(opt?: MandarinaOpt & KA.KaboomOpt): MandarinaPlugin;
Expand Down Expand Up @@ -250,13 +259,6 @@ export type CharacterDataOpt = {
};
// #endregion

// #region Choices
export type Choice = {
text: string;
actions: () => Action[];
};
// #endregion

// #region Textbox
export type Textbox = KA.GameObj<
KA.PosComp | KA.AnchorComp | KA.OpacityComp | TextboxComp
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as KA from "kaboom";
import { getGameData } from "./game";
import { VisualAlign } from "./types";
import { getGameData } from "game";
import { VisualAlign } from "types";

export function getAlignment(
align: VisualAlign,
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"emitDeclarationOnly": true,
"skipLibCheck": true,
"strict": true,
"baseUrl": "./"
}
"baseUrl": "./src/"
},
"include": ["./src/*.ts"]
}

0 comments on commit 75c9c19

Please sign in to comment.