create-spud 0.1.5 → 0.1.6
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/example/index.html +1 -1
- package/example/package.json +1 -2
- package/example/readme.md +0 -5
- package/example/src/audio.ts +2 -7
- package/example/src/canvas.ts +42 -0
- package/example/src/gameLoop.ts +42 -0
- package/example/src/gameplay.ts +10 -15
- package/example/src/main.ts +16 -0
- package/example/src/state.ts +3 -0
- package/example/vite.config.ts +0 -10
- package/index.ts +7 -2
- package/package.json +1 -1
- package/readme.md +1 -2
- package/example/_gitignore +0 -24
- package/example/demo.html +0 -22
- package/example/src/demo.ts +0 -50
- package/example/src/helpers.ts +0 -24
- package/example/src/index.ts +0 -64
package/example/index.html
CHANGED
package/example/package.json
CHANGED
package/example/readme.md
CHANGED
package/example/src/audio.ts
CHANGED
|
@@ -19,8 +19,8 @@ export const sfx = audio.createSounds({
|
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
/*
|
|
22
|
-
createMusic is for longer tracks that can
|
|
23
|
-
|
|
22
|
+
createMusic is for longer tracks that can stream and don't need
|
|
23
|
+
frame-perfect timing.
|
|
24
24
|
*/
|
|
25
25
|
export const menuMusic = audio.createMusic({
|
|
26
26
|
url: menuMusicUrl,
|
|
@@ -28,9 +28,4 @@ export const menuMusic = audio.createMusic({
|
|
|
28
28
|
volume: 0.4,
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
/*
|
|
32
|
-
in spud demo mode (spud.isDemoMode === true), audio methods are no-ops.
|
|
33
|
-
that means you can always call sfx("...").play() or music.play() without
|
|
34
|
-
adding conditional checks; the demo stays silent automatically.
|
|
35
|
-
*/
|
|
36
31
|
menuMusic.play();
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export function createBounds() {
|
|
2
|
+
return { x: 0, y: 0, width: 0, height: 0, centerX: 0, centerY: 0 };
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
type Bounds = ReturnType<typeof createBounds>;
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
by default, canvas rendering is blurry on high-dpi screens.
|
|
9
|
+
this fixes it by scaling the pixel buffer by the devicePixelRatio
|
|
10
|
+
and then setting a matching transform so that the drawing coords
|
|
11
|
+
can stay in CSS pixels.
|
|
12
|
+
|
|
13
|
+
`bounds` is mutated in place and kept in sync with the canvas via a
|
|
14
|
+
ResizeObserver, so it's cheap to read bounds.width/height from your
|
|
15
|
+
update and draw functions.
|
|
16
|
+
|
|
17
|
+
see also: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Optimizing_canvas
|
|
18
|
+
*/
|
|
19
|
+
export function scaleAndObserveCanvasSize(ctx: CanvasRenderingContext2D, bounds: Bounds) {
|
|
20
|
+
const { canvas } = ctx;
|
|
21
|
+
|
|
22
|
+
function updateCanvasSize() {
|
|
23
|
+
const { x, y, width, height } = canvas.getBoundingClientRect();
|
|
24
|
+
const dpr = window.devicePixelRatio;
|
|
25
|
+
|
|
26
|
+
canvas.width = width * dpr;
|
|
27
|
+
canvas.height = height * dpr;
|
|
28
|
+
canvas.style.width = `${width}px`;
|
|
29
|
+
canvas.style.height = `${height}px`;
|
|
30
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
31
|
+
|
|
32
|
+
bounds.x = x;
|
|
33
|
+
bounds.y = y;
|
|
34
|
+
bounds.width = width;
|
|
35
|
+
bounds.height = height;
|
|
36
|
+
bounds.centerX = width / 2;
|
|
37
|
+
bounds.centerY = height / 2;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
updateCanvasSize();
|
|
41
|
+
new ResizeObserver(updateCanvasSize).observe(canvas);
|
|
42
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
type GameLoopConfig<State> = {
|
|
2
|
+
ctx: CanvasRenderingContext2D;
|
|
3
|
+
state: State;
|
|
4
|
+
update: (state: State, dt: number) => void;
|
|
5
|
+
render: (state: State, ctx: CanvasRenderingContext2D) => void;
|
|
6
|
+
fixedDeltaTime?: number;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function gameLoop<State>({
|
|
10
|
+
ctx,
|
|
11
|
+
state,
|
|
12
|
+
update,
|
|
13
|
+
render,
|
|
14
|
+
fixedDeltaTime = 8 / 1000,
|
|
15
|
+
}: GameLoopConfig<State>) {
|
|
16
|
+
let lastFrameTime: number | null = null;
|
|
17
|
+
let accumulator = 0;
|
|
18
|
+
|
|
19
|
+
function tick(now: number) {
|
|
20
|
+
if (lastFrameTime === null) {
|
|
21
|
+
lastFrameTime = now;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const elapsed = now - lastFrameTime;
|
|
25
|
+
const dt = Math.min(elapsed / 1000, 0.1);
|
|
26
|
+
lastFrameTime = now;
|
|
27
|
+
|
|
28
|
+
{
|
|
29
|
+
accumulator += dt;
|
|
30
|
+
while (accumulator >= fixedDeltaTime) {
|
|
31
|
+
update(state, fixedDeltaTime);
|
|
32
|
+
accumulator -= fixedDeltaTime;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
render(state, ctx);
|
|
37
|
+
requestAnimationFrame(tick);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
render(state, ctx);
|
|
41
|
+
requestAnimationFrame(tick);
|
|
42
|
+
}
|
package/example/src/gameplay.ts
CHANGED
|
@@ -1,30 +1,25 @@
|
|
|
1
1
|
import { Button, gamepads } from "@spud.gg/api";
|
|
2
|
-
import { State } from "./state";
|
|
3
|
-
import { CanvasFrameRect } from "./helpers";
|
|
2
|
+
import type { State } from "./state";
|
|
4
3
|
/* moon_sprite_import */
|
|
5
4
|
/* audio_import */
|
|
6
5
|
|
|
7
6
|
/*
|
|
8
7
|
the update loop runs in a fixed time-step and should be used for any
|
|
9
|
-
state updates. `
|
|
8
|
+
state updates. `dt` is the fixed simulation step in seconds (see gameLoop.ts).
|
|
9
|
+
read state.bounds here if you need to clamp or wrap positions to the play area.
|
|
10
10
|
*/
|
|
11
|
-
export function update(state: State,
|
|
12
|
-
state.elapsedSeconds +=
|
|
11
|
+
export function update(state: State, dt: number) {
|
|
12
|
+
state.elapsedSeconds += dt;
|
|
13
13
|
if (gamepads.anyPlayer.buttonJustPressed(Button.RightTrigger)) {
|
|
14
14
|
/* audio_shoot_sfx */
|
|
15
15
|
}
|
|
16
|
+
|
|
17
|
+
// handle any post-update cleanup below:
|
|
18
|
+
gamepads.clearInputs();
|
|
16
19
|
}
|
|
17
20
|
|
|
18
|
-
export function render({
|
|
19
|
-
state
|
|
20
|
-
ctx,
|
|
21
|
-
rect,
|
|
22
|
-
}: {
|
|
23
|
-
state: State;
|
|
24
|
-
ctx: CanvasRenderingContext2D;
|
|
25
|
-
rect: CanvasFrameRect;
|
|
26
|
-
}) {
|
|
27
|
-
const { width, height, centerX, centerY } = rect;
|
|
21
|
+
export function render(state: State, ctx: CanvasRenderingContext2D) {
|
|
22
|
+
const { width, height, centerX, centerY } = state.bounds;
|
|
28
23
|
|
|
29
24
|
/* pixel_art_image_smoothing */
|
|
30
25
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/*
|
|
2
|
+
main.ts is the main entry point for your game.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { scaleAndObserveCanvasSize } from "./canvas";
|
|
6
|
+
import { gameLoop } from "./gameLoop";
|
|
7
|
+
import { render, update } from "./gameplay";
|
|
8
|
+
import { state } from "./state";
|
|
9
|
+
|
|
10
|
+
const canvas = document.createElement("canvas");
|
|
11
|
+
document.body.appendChild(canvas);
|
|
12
|
+
|
|
13
|
+
const ctx = canvas.getContext("2d", { alpha: false })!;
|
|
14
|
+
scaleAndObserveCanvasSize(ctx, state.bounds);
|
|
15
|
+
|
|
16
|
+
gameLoop({ ctx, state, update, render });
|
package/example/src/state.ts
CHANGED
package/example/vite.config.ts
CHANGED
|
@@ -1,22 +1,12 @@
|
|
|
1
1
|
import { defineConfig } from "vite";
|
|
2
|
-
import { dirname, resolve } from "node:path";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
2
|
import { ViteImageOptimizer } from "vite-plugin-image-optimizer";
|
|
5
3
|
|
|
6
|
-
const dir = dirname(fileURLToPath(import.meta.url));
|
|
7
|
-
|
|
8
4
|
export default defineConfig({
|
|
9
5
|
build: {
|
|
10
6
|
sourcemap: true,
|
|
11
7
|
modulePreload: {
|
|
12
8
|
polyfill: false,
|
|
13
9
|
},
|
|
14
|
-
rolldownOptions: {
|
|
15
|
-
input: {
|
|
16
|
-
main: resolve(dir, "index.html"),
|
|
17
|
-
demo: resolve(dir, "demo.html"),
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
10
|
},
|
|
21
11
|
plugins: [ViteImageOptimizer()],
|
|
22
12
|
resolve: {
|
package/index.ts
CHANGED
|
@@ -40,7 +40,7 @@ const { rawGameName, features, shouldContinue } = await group(
|
|
|
40
40
|
}),
|
|
41
41
|
shouldContinue: ({ results }) =>
|
|
42
42
|
confirm({
|
|
43
|
-
message: `
|
|
43
|
+
message: `Ready to write the files to "./${kebabcase((results.rawGameName ?? "").trim())}". Continue?`,
|
|
44
44
|
}),
|
|
45
45
|
},
|
|
46
46
|
{
|
|
@@ -66,7 +66,11 @@ await tasks([
|
|
|
66
66
|
title: `Scaffolding project in ./${slug}/`,
|
|
67
67
|
async task() {
|
|
68
68
|
await mkdir(targetDir, { recursive: true });
|
|
69
|
-
await copyDir(templateDir, targetDir, {
|
|
69
|
+
await copyDir(templateDir, targetDir, {
|
|
70
|
+
gameName,
|
|
71
|
+
slug,
|
|
72
|
+
features,
|
|
73
|
+
});
|
|
70
74
|
return "Initialized project from spud starter template";
|
|
71
75
|
},
|
|
72
76
|
},
|
|
@@ -189,6 +193,7 @@ async function copyDir(
|
|
|
189
193
|
? 'ctx.font = "48px Atari";'
|
|
190
194
|
: 'ctx.font = "48px sans-serif";',
|
|
191
195
|
);
|
|
196
|
+
|
|
192
197
|
await Bun.write(destPath, replaced);
|
|
193
198
|
}
|
|
194
199
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -21,8 +21,6 @@ bun create spud
|
|
|
21
21
|
- Canvas setup with [DPI-aware scaling](https://web.dev/articles/canvas-hidipi)
|
|
22
22
|
- Fixed‑timestep update loop + variable-rate render loop ready to edit
|
|
23
23
|
- Static asset import handling through [Vite](https://vite.dev/guide/assets#importing-asset-as-url) and [image optimization](https://github.com/FatehAK/vite-plugin-image-optimizer)
|
|
24
|
-
- `index.html` and `demo.html` entry points with scripts ready to run
|
|
25
|
-
- _(Demo mode is a non-interactive gameplay example, providing your potential players a preview into your actual game. Spud favors realistic self-playing demos over pre-recorded trailers.)_
|
|
26
24
|
- Gamepad integration with the [spud API (npm)](https://www.npmjs.com/package/@spud.gg/api)
|
|
27
25
|
- Optional:
|
|
28
26
|
- Audio examples (low-latency [Web Audio API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Using_Web_Audio_API) sfx and [streaming background music](https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement)) with [autoplay best practices](https://developer.chrome.com/blog/web-audio-autoplay) for web games.
|
|
@@ -52,4 +50,5 @@ curl -fsSL https://bun.sh/install | bash
|
|
|
52
50
|
<!--
|
|
53
51
|
TODO
|
|
54
52
|
- add hmr hints for Vite to cleanup / reinit the raf loops and keep game state reloadable.
|
|
53
|
+
- handle resize
|
|
55
54
|
-->
|
package/example/_gitignore
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# Logs
|
|
2
|
-
logs
|
|
3
|
-
*.log
|
|
4
|
-
npm-debug.log*
|
|
5
|
-
yarn-debug.log*
|
|
6
|
-
yarn-error.log*
|
|
7
|
-
pnpm-debug.log*
|
|
8
|
-
lerna-debug.log*
|
|
9
|
-
|
|
10
|
-
node_modules
|
|
11
|
-
dist
|
|
12
|
-
dist-ssr
|
|
13
|
-
*.local
|
|
14
|
-
|
|
15
|
-
# Editor directories and files
|
|
16
|
-
.vscode/*
|
|
17
|
-
!.vscode/extensions.json
|
|
18
|
-
.idea
|
|
19
|
-
.DS_Store
|
|
20
|
-
*.suo
|
|
21
|
-
*.ntvs*
|
|
22
|
-
*.njsproj
|
|
23
|
-
*.sln
|
|
24
|
-
*.sw?
|
package/example/demo.html
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
<!doctype html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="utf-8" />
|
|
5
|
-
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
-
<title>game_name</title>
|
|
8
|
-
<style>
|
|
9
|
-
/* fonts_face_css */
|
|
10
|
-
canvas {
|
|
11
|
-
position: fixed;
|
|
12
|
-
inset: 0;
|
|
13
|
-
height: 100vh;
|
|
14
|
-
width: 100vw;
|
|
15
|
-
/* pixel_art_canvas_css */
|
|
16
|
-
}
|
|
17
|
-
</style>
|
|
18
|
-
</head>
|
|
19
|
-
<body>
|
|
20
|
-
<script type="module" src="/src/demo.ts"></script>
|
|
21
|
-
</body>
|
|
22
|
-
</html>
|
package/example/src/demo.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
demo.ts is the entry point for the demo.html file, which displays a
|
|
3
|
-
non-interactive demo of your game. it should be a minimal preview
|
|
4
|
-
version of your game that starts automatically and does not play any
|
|
5
|
-
sounds, use local storage, or handle inputs.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { render, update } from "./gameplay";
|
|
9
|
-
import { state } from "./state";
|
|
10
|
-
import { handleCanvasDpi } from "./helpers";
|
|
11
|
-
|
|
12
|
-
const canvas = document.createElement("canvas");
|
|
13
|
-
document.body.appendChild(canvas);
|
|
14
|
-
|
|
15
|
-
const ctx = canvas.getContext("2d", { alpha: false })!;
|
|
16
|
-
|
|
17
|
-
let lastFrameTime: number | null = null;
|
|
18
|
-
let physicsAccumulatorMs = 0;
|
|
19
|
-
|
|
20
|
-
function gameLoop(now: number) {
|
|
21
|
-
// initialize lastFrameTime on the first tick
|
|
22
|
-
if (lastFrameTime === null) {
|
|
23
|
-
lastFrameTime = now;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// cap extreme frame deltas so one frame doesn't do a huge catch-up after tab restore
|
|
27
|
-
const deltaTime = Math.min(now - lastFrameTime, 100);
|
|
28
|
-
lastFrameTime = now;
|
|
29
|
-
|
|
30
|
-
const rect = handleCanvasDpi(ctx);
|
|
31
|
-
|
|
32
|
-
{
|
|
33
|
-
// process physics updates at a fixed 120hz time step
|
|
34
|
-
physicsAccumulatorMs += deltaTime;
|
|
35
|
-
const physicsTicksPerSecond = 120;
|
|
36
|
-
const fixedDeltaMs = 1000 / physicsTicksPerSecond;
|
|
37
|
-
while (physicsAccumulatorMs >= fixedDeltaMs) {
|
|
38
|
-
physicsAccumulatorMs -= fixedDeltaMs;
|
|
39
|
-
update(state, fixedDeltaMs);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
render({ state, ctx, rect });
|
|
44
|
-
requestAnimationFrame(gameLoop); // queue up the next tick
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// render immediately, then start the game loop
|
|
48
|
-
const rect = handleCanvasDpi(ctx);
|
|
49
|
-
render({ state, ctx, rect });
|
|
50
|
-
requestAnimationFrame(gameLoop);
|
package/example/src/helpers.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
by default, canvas rendering is blurry on high-dpi screens.
|
|
3
|
-
this fixes it by scaling the pixel buffer by the devicePixelRatio
|
|
4
|
-
and then setting a matching transform so that the drawing coords
|
|
5
|
-
can stay in CSS pixels.
|
|
6
|
-
|
|
7
|
-
see also: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Optimizing_canvas
|
|
8
|
-
*/
|
|
9
|
-
export function handleCanvasDpi(ctx: CanvasRenderingContext2D) {
|
|
10
|
-
const { width, height } = ctx.canvas.getBoundingClientRect();
|
|
11
|
-
const targetWidth = width * window.devicePixelRatio;
|
|
12
|
-
const targetHeight = height * window.devicePixelRatio;
|
|
13
|
-
const rect = { width, height, centerX: width / 2, centerY: height / 2 };
|
|
14
|
-
|
|
15
|
-
// optimization: since setting canvas.width and .height reset the canvas state, skip if the size didn't change
|
|
16
|
-
if (ctx.canvas.width === targetWidth && ctx.canvas.height === targetHeight) return rect;
|
|
17
|
-
|
|
18
|
-
ctx.canvas.width = targetWidth;
|
|
19
|
-
ctx.canvas.height = targetHeight;
|
|
20
|
-
ctx.setTransform(window.devicePixelRatio, 0, 0, window.devicePixelRatio, 0, 0);
|
|
21
|
-
return rect;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export type CanvasFrameRect = ReturnType<typeof handleCanvasDpi>;
|
package/example/src/index.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
index.ts is the main entry point for your game. it's responsible for
|
|
3
|
-
setting up the canvas and the two main game loops: a 120hz fixed time
|
|
4
|
-
step physics/update loop, and a variable framerate draw cycle.
|
|
5
|
-
see also:
|
|
6
|
-
- https://gafferongames.com/post/fix_your_timestep/
|
|
7
|
-
- https://www.youtube.com/watch?v=yGhfUcPjXuE
|
|
8
|
-
- https://gist.github.com/HipHopHuman/3e9b4a94b30ac9387d9a99ef2d29eb1a
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { spud, gamepads } from "@spud.gg/api";
|
|
12
|
-
import { render, update } from "./gameplay";
|
|
13
|
-
import { state } from "./state";
|
|
14
|
-
import { handleCanvasDpi } from "./helpers";
|
|
15
|
-
|
|
16
|
-
const canvas = document.createElement("canvas");
|
|
17
|
-
document.body.appendChild(canvas);
|
|
18
|
-
|
|
19
|
-
const ctx = canvas.getContext("2d", { alpha: false })!;
|
|
20
|
-
|
|
21
|
-
let lastFrameTime: number | null = null;
|
|
22
|
-
let physicsAccumulatorMs = 0;
|
|
23
|
-
let animationFrame = -1;
|
|
24
|
-
|
|
25
|
-
function gameLoop(now: number) {
|
|
26
|
-
// initialize lastFrameTime on the first tick
|
|
27
|
-
if (lastFrameTime === null) {
|
|
28
|
-
lastFrameTime = now;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// cap extreme frame deltas after tab restore so one frame doesn't do a huge catch-up
|
|
32
|
-
const deltaTime = Math.min(now - lastFrameTime, 100);
|
|
33
|
-
lastFrameTime = now;
|
|
34
|
-
|
|
35
|
-
const rect = handleCanvasDpi(ctx);
|
|
36
|
-
|
|
37
|
-
{
|
|
38
|
-
// process physics updates at a fixed 120hz time step
|
|
39
|
-
physicsAccumulatorMs = spud.isPaused ? 0 : physicsAccumulatorMs + deltaTime;
|
|
40
|
-
const physicsTicksPerSecond = 120;
|
|
41
|
-
const fixedDeltaMs = 1000 / physicsTicksPerSecond;
|
|
42
|
-
while (physicsAccumulatorMs >= fixedDeltaMs) {
|
|
43
|
-
physicsAccumulatorMs -= fixedDeltaMs;
|
|
44
|
-
update(state, fixedDeltaMs);
|
|
45
|
-
gamepads.clearInputs();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
render({ state, ctx, rect });
|
|
50
|
-
animationFrame = requestAnimationFrame(gameLoop); // queue up the next tick
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// render immediately, then start the game loop
|
|
54
|
-
const rect = handleCanvasDpi(ctx);
|
|
55
|
-
render({ state, ctx, rect });
|
|
56
|
-
animationFrame = requestAnimationFrame(gameLoop);
|
|
57
|
-
|
|
58
|
-
// enable hot reloading in dev mode, but ensure the canvas is properly cleaned up
|
|
59
|
-
if (import.meta.hot) {
|
|
60
|
-
import.meta.hot.accept(() => {
|
|
61
|
-
cancelAnimationFrame(animationFrame);
|
|
62
|
-
canvas.remove();
|
|
63
|
-
});
|
|
64
|
-
}
|