Skip to content

Commit b9ea8eb

Browse files
committed
feat(ex): TypeScriptToLua
This shows how to make use of https://typescripttolua.github.io/ in an Usagi game to write your code with TypeScript, which then transpiles to Lua.
1 parent ba570c7 commit b9ea8eb

6 files changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# ignore outputted Lua
2+
*.lua
3+
node_modules
4+
package-lock.json
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# TypeScriptToLua Usagi Example
2+
3+
An example showing how to use TypeScript with Usagi Engine by transpiling it to
4+
Lua. Includes updating, drawing, shape primitive, text, and importing exported
5+
TS functions.
6+
7+
Using https://typescripttolua.github.io/
8+
9+
Requires Node.js to be installed.
10+
11+
Install the package and its CLI:
12+
13+
```
14+
npm install -D typescript typescript-to-lua
15+
```
16+
17+
Compile your TypeScript to Lua:
18+
19+
```
20+
npx tstl
21+
```
22+
23+
## Using with Usagi's Dev Mode
24+
25+
Start the transpiler in watch mode:
26+
27+
```
28+
npm run dev:tstl
29+
```
30+
31+
Start Usagi's dev mode:
32+
33+
```
34+
npm run dev:usagi
35+
```
36+
37+
Export your game:
38+
39+
```
40+
npm run export
41+
```
42+
43+
## Things Worth Knowing
44+
45+
Look at `./main.ts` to see how to structure your code. Usagi relies on globals,
46+
so https://typescripttolua.github.io/docs/assigning-global-variables is followed
47+
by declaring the `_config`, `_update`, and `_draw` functions. It'd be nice if
48+
there were typedefs for Usagi, but for now, it has to be done manually. This
49+
would be a good community contribution. There are a few typedefs to get started.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function greet(this: void, name: string): string {
2+
return `Hello, ${name}`;
3+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/************** Bare minimum type declarations for this example *************/
2+
3+
declare var _config: (this: void) => Record<string, string | number | boolean>;
4+
declare var _update: (this: void, dt: number) => void;
5+
declare var _draw: (this: void, dt: number) => void;
6+
7+
/** @noSelf **/
8+
declare interface InputInterface {
9+
RIGHT: number;
10+
LEFT: number;
11+
held: (btn: number) => boolean;
12+
}
13+
/** @noSelf **/
14+
declare interface GfxInterface {
15+
rect_fill: (
16+
x: number,
17+
y: number,
18+
w: number,
19+
h: number,
20+
color: number,
21+
) => void;
22+
text: (
23+
text: string,
24+
x: number,
25+
y: number,
26+
color: number,
27+
) => void;
28+
COLOR_WHITE: number;
29+
COLOR_BLACK: number;
30+
clear: (color: number) => void;
31+
}
32+
declare const input: InputInterface;
33+
declare const gfx: GfxInterface;
34+
35+
import greet from "./greet";
36+
37+
let state = {
38+
x: 10,
39+
y: 40,
40+
};
41+
_config = () => {
42+
return { game_width: 240, game_height: 240, name: "TS to Lua" };
43+
};
44+
45+
_update = (dt: number) => {
46+
// px/sec
47+
let spd = 100;
48+
if (input.held(input.LEFT)) {
49+
state.x -= spd * dt;
50+
}
51+
52+
if (input.held(input.RIGHT)) {
53+
state.x += spd * dt;
54+
}
55+
};
56+
57+
_draw = (dt: number) => {
58+
gfx.clear(gfx.COLOR_BLACK);
59+
gfx.rect_fill(state.x, state.y, 16, 16, gfx.COLOR_WHITE);
60+
61+
gfx.text(greet("Alucard"), 4, 4, gfx.COLOR_WHITE);
62+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"build": "tstl",
5+
"dev:tstl": "tstl --watch",
6+
"dev:usagi": "usagi dev",
7+
"export": "npm run build && usagi export"
8+
},
9+
"devDependencies": {
10+
"typescript": "^6.0.2",
11+
"typescript-to-lua": "^1.37.0"
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/TypeScriptToLua/TypeScriptToLua/master/tsconfig-schema.json",
3+
"compilerOptions": {
4+
"target": "ESNext",
5+
"lib": ["ESNext"],
6+
"types": [],
7+
"strict": true
8+
},
9+
"tstl": {
10+
"luaTarget": "5.5"
11+
}
12+
}

0 commit comments

Comments
 (0)