create-spud 0.1.9 → 0.1.11

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.
@@ -1,7 +1,8 @@
1
1
  type GameLoopConfig<State> = {
2
2
  ctx: CanvasRenderingContext2D;
3
3
  state: State;
4
- update: (state: State, dt: number) => void;
4
+ update?: (state: State, dt: number) => void;
5
+ fixedUpdate?: (state: State, dt: number) => void;
5
6
  render: (state: State, ctx: CanvasRenderingContext2D) => void;
6
7
  fixedDeltaTime?: number;
7
8
  };
@@ -10,6 +11,7 @@ export function gameLoop<State>({
10
11
  ctx,
11
12
  state,
12
13
  update,
14
+ fixedUpdate,
13
15
  render,
14
16
  fixedDeltaTime = 8 / 1000,
15
17
  }: GameLoopConfig<State>) {
@@ -25,14 +27,16 @@ export function gameLoop<State>({
25
27
  const dt = Math.min(elapsed / 1000, 0.1);
26
28
  lastFrameTime = now;
27
29
 
28
- {
30
+ if (fixedUpdate) {
29
31
  accumulator += dt;
30
32
  while (accumulator >= fixedDeltaTime) {
31
- update(state, fixedDeltaTime);
33
+ fixedUpdate(state, fixedDeltaTime);
32
34
  accumulator -= fixedDeltaTime;
33
35
  }
34
36
  }
35
37
 
38
+ update?.(state, dt);
39
+
36
40
  render(state, ctx);
37
41
  requestAnimationFrame(tick);
38
42
  }
@@ -8,7 +8,7 @@ function justFired() {
8
8
  return pressedTrigger || keyboard.justPressed("Enter");
9
9
  }
10
10
 
11
- export function update(state: State, dt: number) {
11
+ export function fixedUpdate(state: State, dt: number) {
12
12
  // handle inputs and update state here
13
13
  state.elapsedSeconds += dt;
14
14
 
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { scaleAndObserveCanvasSize } from "./canvas";
6
6
  import { gameLoop } from "./gameLoop";
7
- import { render, update } from "./gameplay";
7
+ import { render, fixedUpdate } from "./gameplay";
8
8
  import { state } from "./state";
9
9
 
10
10
  const canvas = document.createElement("canvas");
@@ -13,4 +13,4 @@ document.body.appendChild(canvas);
13
13
  const ctx = canvas.getContext("2d", { alpha: false })!;
14
14
  scaleAndObserveCanvasSize(ctx, state.bounds);
15
15
 
16
- gameLoop({ ctx, state, update, render });
16
+ gameLoop({ ctx, state, fixedUpdate, render });
package/index.ts CHANGED
@@ -185,7 +185,7 @@ async function copyDir(
185
185
  playbackRate: 0.5 + Math.random(),
186
186
  });
187
187
  `
188
- : " /* handle inputs and update state */\n"",
188
+ : " /* handle inputs and update state */\n",
189
189
  )
190
190
  .replaceAll(
191
191
  "/* fonts_ctx_font */",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-spud",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "bin": {
5
5
  "create-spud": "./index.ts"
6
6
  },