create-airjam 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/index.ts
4
+ import { program } from "commander";
5
+ import fs from "fs-extra";
6
+ import kleur from "kleur";
7
+ import path from "path";
8
+ import { fileURLToPath } from "url";
9
+ import prompts from "prompts";
10
+ var __filename = fileURLToPath(import.meta.url);
11
+ var __dirname = path.dirname(__filename);
12
+ async function main() {
13
+ program.name("create-airjam").description("Scaffold a new Air Jam game project").argument("[project-name]", "Name of the project directory").option("-t, --template <template>", "Template to use", "pong").parse();
14
+ const args = program.args;
15
+ const options = program.opts();
16
+ let projectName = args[0];
17
+ if (!projectName) {
18
+ const response = await prompts({
19
+ type: "text",
20
+ name: "projectName",
21
+ message: "Project name:",
22
+ initial: "my-airjam-game"
23
+ });
24
+ projectName = response.projectName;
25
+ if (!projectName) {
26
+ console.log(kleur.red("Project name is required"));
27
+ process.exit(1);
28
+ }
29
+ }
30
+ const targetDir = path.resolve(process.cwd(), projectName);
31
+ const templateDir = path.resolve(__dirname, "..", "templates", options.template);
32
+ if (!fs.existsSync(templateDir)) {
33
+ console.log(kleur.red(`Template "${options.template}" not found`));
34
+ process.exit(1);
35
+ }
36
+ if (fs.existsSync(targetDir)) {
37
+ const response = await prompts({
38
+ type: "confirm",
39
+ name: "overwrite",
40
+ message: `Directory "${projectName}" already exists. Overwrite?`,
41
+ initial: false
42
+ });
43
+ if (!response.overwrite) {
44
+ console.log(kleur.yellow("Aborted"));
45
+ process.exit(0);
46
+ }
47
+ await fs.remove(targetDir);
48
+ }
49
+ console.log(kleur.cyan(`
50
+ Creating project in ${targetDir}...
51
+ `));
52
+ await fs.copy(templateDir, targetDir);
53
+ const pkgPath = path.join(targetDir, "package.json");
54
+ if (fs.existsSync(pkgPath)) {
55
+ const pkg = await fs.readJson(pkgPath);
56
+ pkg.name = projectName;
57
+ await fs.writeJson(pkgPath, pkg, { spaces: 2 });
58
+ }
59
+ console.log(kleur.green("\u2713 Project created successfully!\n"));
60
+ console.log("Next steps:\n");
61
+ console.log(kleur.cyan(` cd ${projectName}`));
62
+ console.log(kleur.cyan(" npm install"));
63
+ console.log(kleur.cyan(" npm run dev"));
64
+ console.log("");
65
+ console.log(
66
+ kleur.dim("Then open http://localhost:5173 and scan the QR code with your phone!")
67
+ );
68
+ }
69
+ main().catch((err) => {
70
+ console.error(kleur.red("Error:"), err);
71
+ process.exit(1);
72
+ });
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "create-airjam",
3
+ "version": "0.1.0",
4
+ "description": "CLI to scaffold Air Jam game projects",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/vucinatim/air-jam.git",
10
+ "directory": "packages/create-airjam"
11
+ },
12
+ "homepage": "https://github.com/vucinatim/air-jam#readme",
13
+ "keywords": [
14
+ "air-jam",
15
+ "cli",
16
+ "scaffold",
17
+ "template",
18
+ "game",
19
+ "multiplayer",
20
+ "create-airjam",
21
+ "boilerplate",
22
+ "starter"
23
+ ],
24
+ "bin": {
25
+ "create-airjam": "./dist/index.js"
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "templates"
30
+ ],
31
+ "scripts": {
32
+ "build": "tsup",
33
+ "dev": "tsup --watch"
34
+ },
35
+ "dependencies": {
36
+ "commander": "^14.0.0",
37
+ "fs-extra": "^11.3.0",
38
+ "kleur": "^4.1.5",
39
+ "prompts": "^2.4.2"
40
+ },
41
+ "devDependencies": {
42
+ "@types/fs-extra": "^11.0.4",
43
+ "@types/prompts": "^2.4.9",
44
+ "tsup": "^8.5.1",
45
+ "typescript": "~5.9.3"
46
+ }
47
+ }
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>My Air Jam Game</title>
8
+ </head>
9
+ <body>
10
+ <div id="root"></div>
11
+ <script type="module" src="/src/main.tsx"></script>
12
+ </body>
13
+ </html>
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "my-airjam-game",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "vite",
8
+ "build": "tsc && vite build",
9
+ "preview": "vite preview"
10
+ },
11
+ "dependencies": {
12
+ "@air-jam/sdk": "^0.1.0",
13
+ "react": "^19.0.0",
14
+ "react-dom": "^19.0.0",
15
+ "react-router-dom": "^7.0.0",
16
+ "zod": "^4.2.1"
17
+ },
18
+ "devDependencies": {
19
+ "@tailwindcss/vite": "^4.0.0",
20
+ "@types/react": "^19.0.0",
21
+ "@types/react-dom": "^19.0.0",
22
+ "@vitejs/plugin-react": "^4.3.0",
23
+ "tailwindcss": "^4.0.0",
24
+ "typescript": "~5.6.0",
25
+ "vite": "^6.0.0"
26
+ }
27
+ }
@@ -0,0 +1,24 @@
1
+ import { AirJamProvider } from "@air-jam/sdk";
2
+ import { Route, Routes } from "react-router-dom";
3
+ import { ControllerView } from "./ControllerView";
4
+ import { HostView } from "./HostView";
5
+ import { gameInputSchema } from "./types";
6
+
7
+ export function App() {
8
+ return (
9
+ <AirJamProvider
10
+ controllerPath="/joypad"
11
+ input={{
12
+ schema: gameInputSchema,
13
+ latch: {
14
+ booleanFields: ["action"],
15
+ },
16
+ }}
17
+ >
18
+ <Routes>
19
+ <Route path="/" element={<HostView />} />
20
+ <Route path="/joypad" element={<ControllerView />} />
21
+ </Routes>
22
+ </AirJamProvider>
23
+ );
24
+ }
@@ -0,0 +1,64 @@
1
+ import { ControllerShell, useAirJamController } from "@air-jam/sdk";
2
+ import { useRef, useEffect } from "react";
3
+
4
+ export function ControllerView() {
5
+ const controller = useAirJamController();
6
+ const directionRef = useRef(0);
7
+
8
+ // Send input loop
9
+ useEffect(() => {
10
+ if (controller.connectionStatus !== "connected") return;
11
+
12
+ let animationId: number;
13
+ const loop = () => {
14
+ controller.sendInput({
15
+ direction: directionRef.current,
16
+ action: false,
17
+ });
18
+ animationId = requestAnimationFrame(loop);
19
+ };
20
+ loop();
21
+ return () => cancelAnimationFrame(animationId);
22
+ }, [controller.connectionStatus, controller]);
23
+
24
+ return (
25
+ <ControllerShell
26
+ connectionStatus={controller.connectionStatus}
27
+ roomId={controller.roomId}
28
+ >
29
+ <div className="flex h-full w-full flex-col items-center justify-center gap-8 bg-gray-900 p-4">
30
+ <h2 className="text-2xl font-bold text-white">Your Controller</h2>
31
+
32
+ <div className="flex flex-col gap-4">
33
+ {/* Up button */}
34
+ <button
35
+ type="button"
36
+ className="rounded-lg bg-blue-600 px-12 py-8 text-2xl font-bold text-white active:bg-blue-700"
37
+ onTouchStart={() => (directionRef.current = -1)}
38
+ onTouchEnd={() => (directionRef.current = 0)}
39
+ onMouseDown={() => (directionRef.current = -1)}
40
+ onMouseUp={() => (directionRef.current = 0)}
41
+ >
42
+ ▲ UP
43
+ </button>
44
+
45
+ {/* Down button */}
46
+ <button
47
+ type="button"
48
+ className="rounded-lg bg-blue-600 px-12 py-8 text-2xl font-bold text-white active:bg-blue-700"
49
+ onTouchStart={() => (directionRef.current = 1)}
50
+ onTouchEnd={() => (directionRef.current = 0)}
51
+ onMouseDown={() => (directionRef.current = 1)}
52
+ onMouseUp={() => (directionRef.current = 0)}
53
+ >
54
+ ▼ DOWN
55
+ </button>
56
+ </div>
57
+
58
+ <p className="text-sm text-gray-400">
59
+ Hold to move your paddle
60
+ </p>
61
+ </div>
62
+ </ControllerShell>
63
+ );
64
+ }
@@ -0,0 +1,148 @@
1
+ import { useAirJamHost, useGetInput } from "@air-jam/sdk";
2
+ import { useCallback, useEffect, useRef, useState } from "react";
3
+ import { type GameInput, gameInputSchema } from "./types";
4
+
5
+ const PADDLE_HEIGHT = 100;
6
+ const PADDLE_WIDTH = 15;
7
+ const BALL_SIZE = 15;
8
+ const PADDLE_SPEED = 8;
9
+ const BALL_SPEED = 5;
10
+
11
+ export function HostView() {
12
+ const host = useAirJamHost<typeof gameInputSchema>();
13
+ const getInput = useGetInput<typeof gameInputSchema>();
14
+ const canvasRef = useRef<HTMLCanvasElement>(null);
15
+ const [scores, setScores] = useState({ player1: 0, player2: 0 });
16
+
17
+ // Game state refs (to avoid re-renders in game loop)
18
+ const gameState = useRef({
19
+ paddle1Y: 250,
20
+ paddle2Y: 250,
21
+ ballX: 400,
22
+ ballY: 300,
23
+ ballVX: BALL_SPEED,
24
+ ballVY: BALL_SPEED,
25
+ });
26
+
27
+ const resetBall = useCallback(() => {
28
+ const state = gameState.current;
29
+ state.ballX = 400;
30
+ state.ballY = 300;
31
+ state.ballVX = BALL_SPEED * (Math.random() > 0.5 ? 1 : -1);
32
+ state.ballVY = BALL_SPEED * (Math.random() > 0.5 ? 1 : -1);
33
+ }, []);
34
+
35
+ // Game loop
36
+ useEffect(() => {
37
+ const canvas = canvasRef.current;
38
+ if (!canvas) return;
39
+ const ctx = canvas.getContext("2d");
40
+ if (!ctx) return;
41
+
42
+ let animationId: number;
43
+
44
+ const gameLoop = () => {
45
+ const state = gameState.current;
46
+ const controllers = host.controllers;
47
+
48
+ // Get input from first two controllers
49
+ const controllerIds = Object.keys(controllers);
50
+ const player1Input = controllerIds[0] ? getInput(controllerIds[0]) : null;
51
+ const player2Input = controllerIds[1] ? getInput(controllerIds[1]) : null;
52
+
53
+ // Move paddles based on input
54
+ if (player1Input) {
55
+ state.paddle1Y += (player1Input as GameInput).direction * PADDLE_SPEED;
56
+ state.paddle1Y = Math.max(0, Math.min(600 - PADDLE_HEIGHT, state.paddle1Y));
57
+ }
58
+ if (player2Input) {
59
+ state.paddle2Y += (player2Input as GameInput).direction * PADDLE_SPEED;
60
+ state.paddle2Y = Math.max(0, Math.min(600 - PADDLE_HEIGHT, state.paddle2Y));
61
+ }
62
+
63
+ // Move ball
64
+ state.ballX += state.ballVX;
65
+ state.ballY += state.ballVY;
66
+
67
+ // Ball collision with top/bottom walls
68
+ if (state.ballY <= 0 || state.ballY >= 600 - BALL_SIZE) {
69
+ state.ballVY *= -1;
70
+ }
71
+
72
+ // Ball collision with paddles
73
+ // Left paddle
74
+ if (
75
+ state.ballX <= 30 + PADDLE_WIDTH &&
76
+ state.ballX >= 30 &&
77
+ state.ballY + BALL_SIZE >= state.paddle1Y &&
78
+ state.ballY <= state.paddle1Y + PADDLE_HEIGHT
79
+ ) {
80
+ state.ballVX = Math.abs(state.ballVX);
81
+ }
82
+ // Right paddle
83
+ if (
84
+ state.ballX >= 800 - 30 - PADDLE_WIDTH - BALL_SIZE &&
85
+ state.ballX <= 800 - 30 &&
86
+ state.ballY + BALL_SIZE >= state.paddle2Y &&
87
+ state.ballY <= state.paddle2Y + PADDLE_HEIGHT
88
+ ) {
89
+ state.ballVX = -Math.abs(state.ballVX);
90
+ }
91
+
92
+ // Scoring
93
+ if (state.ballX <= 0) {
94
+ setScores((s) => ({ ...s, player2: s.player2 + 1 }));
95
+ resetBall();
96
+ }
97
+ if (state.ballX >= 800 - BALL_SIZE) {
98
+ setScores((s) => ({ ...s, player1: s.player1 + 1 }));
99
+ resetBall();
100
+ }
101
+
102
+ // Draw
103
+ ctx.fillStyle = "#000";
104
+ ctx.fillRect(0, 0, 800, 600);
105
+
106
+ ctx.fillStyle = "#fff";
107
+ // Paddles
108
+ ctx.fillRect(30, state.paddle1Y, PADDLE_WIDTH, PADDLE_HEIGHT);
109
+ ctx.fillRect(800 - 30 - PADDLE_WIDTH, state.paddle2Y, PADDLE_WIDTH, PADDLE_HEIGHT);
110
+ // Ball
111
+ ctx.fillRect(state.ballX, state.ballY, BALL_SIZE, BALL_SIZE);
112
+ // Center line
113
+ ctx.setLineDash([5, 15]);
114
+ ctx.beginPath();
115
+ ctx.moveTo(400, 0);
116
+ ctx.lineTo(400, 600);
117
+ ctx.strokeStyle = "#333";
118
+ ctx.stroke();
119
+
120
+ animationId = requestAnimationFrame(gameLoop);
121
+ };
122
+
123
+ gameLoop();
124
+ return () => cancelAnimationFrame(animationId);
125
+ }, [host.controllers, getInput, resetBall]);
126
+
127
+ return (
128
+ <div className="flex min-h-screen flex-col items-center justify-center bg-gray-900 p-4">
129
+ <h1 className="mb-4 text-3xl font-bold text-white">Pong</h1>
130
+ <div className="mb-4 text-2xl text-white">
131
+ {scores.player1} - {scores.player2}
132
+ </div>
133
+ <canvas
134
+ ref={canvasRef}
135
+ width={800}
136
+ height={600}
137
+ className="rounded-lg border-2 border-white"
138
+ />
139
+ <div className="mt-4 text-gray-400">
140
+ {Object.keys(host.controllers).length === 0 ? (
141
+ <p>Waiting for players to connect... Scan the QR code!</p>
142
+ ) : (
143
+ <p>{Object.keys(host.controllers).length} player(s) connected</p>
144
+ )}
145
+ </div>
146
+ </div>
147
+ );
148
+ }
@@ -0,0 +1 @@
1
+ @import "tailwindcss";
@@ -0,0 +1,13 @@
1
+ import { StrictMode } from "react";
2
+ import { createRoot } from "react-dom/client";
3
+ import { BrowserRouter } from "react-router-dom";
4
+ import { App } from "./App";
5
+ import "./index.css";
6
+
7
+ createRoot(document.getElementById("root")!).render(
8
+ <StrictMode>
9
+ <BrowserRouter>
10
+ <App />
11
+ </BrowserRouter>
12
+ </StrictMode>
13
+ );
@@ -0,0 +1,12 @@
1
+ import { z } from "zod";
2
+
3
+ // Define the input schema for your game
4
+ // This determines what data the controller sends to the host
5
+ export const gameInputSchema = z.object({
6
+ // Paddle movement: -1 (up), 0 (still), 1 (down)
7
+ direction: z.number().min(-1).max(1),
8
+ // Action button (e.g., start game, pause)
9
+ action: z.boolean(),
10
+ });
11
+
12
+ export type GameInput = z.infer<typeof gameInputSchema>;
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "lib": ["ES2022", "DOM", "DOM.Iterable"],
5
+ "module": "ESNext",
6
+ "moduleResolution": "Bundler",
7
+ "jsx": "react-jsx",
8
+ "strict": true,
9
+ "noEmit": true,
10
+ "skipLibCheck": true,
11
+ "esModuleInterop": true,
12
+ "resolveJsonModule": true,
13
+ "isolatedModules": true,
14
+ "baseUrl": ".",
15
+ "paths": {
16
+ "@/*": ["./src/*"]
17
+ }
18
+ },
19
+ "include": ["src"]
20
+ }
@@ -0,0 +1,30 @@
1
+ import tailwindcss from "@tailwindcss/vite";
2
+ import react from "@vitejs/plugin-react";
3
+ import path from "node:path";
4
+ import { defineConfig } from "vite";
5
+
6
+ export default defineConfig({
7
+ plugins: [react(), tailwindcss()],
8
+ resolve: {
9
+ alias: {
10
+ "@": path.resolve(__dirname, "./src"),
11
+ },
12
+ },
13
+ server: {
14
+ host: true,
15
+ port: 5173,
16
+ headers: {
17
+ // Required for embedding in Air Jam platform iframe
18
+ "Content-Security-Policy": "frame-ancestors *;",
19
+ },
20
+ cors: true,
21
+ },
22
+ preview: {
23
+ host: true,
24
+ port: 5173,
25
+ headers: {
26
+ "Content-Security-Policy": "frame-ancestors *;",
27
+ },
28
+ cors: true,
29
+ },
30
+ });