Skip to content

Commit

Permalink
chore: splitting concerns between a "core" and "game"
Browse files Browse the repository at this point in the history
When starting the split of the game state, I realized there's a lot of "not touching for long time" code spread everywhere, while next to something we may change a dozen of times a week. With that I also cleaned the imports.
  • Loading branch information
debone committed Feb 14, 2025
1 parent 7a1e37a commit 812a8cd
Show file tree
Hide file tree
Showing 39 changed files with 143 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from "@game/common/assert";
import { EaseMap } from "@game/lib/easing";
import { Signal } from "@game/state/lib/types";
import { makeArray } from "@game/common/arrays";
import { makeArray } from "@game/core/common/arrays";
import { assert } from "@game/core/common/assert";
import { EaseMap } from "@game/core/common/easing";
import { Signal } from "@game/core/signals/types";

// IDEAS/TODO
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, it, expect } from "vitest";
import { signal } from "@game/core/signals/signals";
import { describe, expect, it, vi } from "vitest";
import { AnimationPlan, linear } from "./animation-timed";
import { signal } from "@game/state/lib/signals";
import { vi } from "vitest";

describe("interpolate", () => {
it.each([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { signal } from "@game/state/lib/signals";
import { signal } from "@game/core/signals/signals";

import {
ParallelProps,
Expand Down Expand Up @@ -281,5 +281,6 @@ export {
SequenceEngine,
Step,
Transition,
Wait,
Wait
};

5 changes: 3 additions & 2 deletions src/ui/animation/types.ts → src/core/animation/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Signal } from "@game/state/lib/types";
import { EaseMap } from "@game/core/common/easing";
import { Signal } from "@game/core/signals/types";

import { createParticleEffect } from "./animation";
import { EaseMap } from "@game/lib/easing";

// Core animation signal with transition tracking
export type TransitionSignal<T> = Signal<T> & {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 10 additions & 6 deletions src/ui/lib/jsx-runtime.ts → src/core/jsx/jsx-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import {
animationIntrinsicElements,
setupAnimationElement,
} from "@game/core/animation/animation-timed";

import { setupGameObject } from "@game/core/jsx/phaser-jsx";

import {
EventId,
setupStateMachineElement,
StateId,
stateMachineIntrinsicElements,
} from "../../state/lib/state-machine";
} from "@game/core/state-machine/state-machine";
import {
stateObserverIntrinsicElements,
setupStateObserverElement,
} from "../../state/lib/state-observer";
import { setupAnimationElement } from "../animation/animation-timed";
import { animationIntrinsicElements } from "../animation/animation-timed";
import { setupGameObject } from "./phaser-jsx";
stateObserverIntrinsicElements,
} from "@game/core/state-machine/state-observer";

/*
JSX runtime
Expand Down
8 changes: 4 additions & 4 deletions src/ui/lib/phaser-jsx.ts → src/core/jsx/phaser-jsx.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { SignalImpl } from "@game/state/lib/signals";
import { Signal } from "@game/state/lib/types";
import { SignalImpl } from "@game/core/signals/signals";
import { Signal } from "@game/core/signals/types";

import {
cleanupSymbol,
ContainerElement,
ImageElement,
NineSliceElement,
PhaserGameObjectProps,
RectangleElement,
SignalCleanup,
SpriteElement,
TextElement,
} from "./types";

import { cleanupSymbol, SignalCleanup } from "@game/core/signals/types";

export const JsxElementsRegistry = {
elements: new Map<
Phaser.GameObjects.GameObject,
Expand Down
8 changes: 1 addition & 7 deletions src/ui/lib/types.ts → src/core/jsx/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SignalValue, Signal } from "@game/state/lib/types";
import { SignalValue, Signal } from "@game/core/signals/types";

declare global {
interface Window {
Expand Down Expand Up @@ -151,9 +151,3 @@ export interface PhaserGameObjectProps<
bind?: Record<string, Signal<any>>;
ref?: (gameObject: T) => void;
}

export const cleanupSymbol = Symbol("cleanup");

export interface SignalCleanup {
[cleanupSymbol]: Array<() => void>;
}
3 changes: 1 addition & 2 deletions src/state/lib/signals.ts → src/core/signals/signals.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {
Cleanup,
Effect,
EqualityFn,
MutableSignal,
Signal,
SignalValue,
Subscriber,
} from "./types";
} from "@game/core/signals/types";

/**
* Signal implementation
Expand Down
6 changes: 6 additions & 0 deletions src/state/lib/types.ts → src/core/signals/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ export interface MutableSignal<T> extends Signal<T> {
*/
mutate(fn: (value: T) => boolean): void;
}

export const cleanupSymbol = Symbol("cleanup");

export interface SignalCleanup {
[cleanupSymbol]: Array<() => void>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from "vitest";
import { signal } from "./signals";
import { FiniteStateMachine } from "./state-machine";

import { signal } from "@game/core/signals/signals";
import { FiniteStateMachine } from "@game/core/state-machine/state-machine";

describe("FiniteStateMachine", () => {
it("should transition between states with valid event", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from "@game/common/assert";
import { signal } from "./signals";
import { Signal } from "./types";
import { makeArray } from "@game/common/arrays";
import { makeArray } from "@game/core/common/arrays";
import { assert } from "@game/core/common/assert";
import { signal } from "@game/core/signals/signals";
import { Signal } from "@game/core/signals/types";

declare global {
namespace JSX {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { makeArray } from "@game/common/arrays";
import { assert } from "@game/common/assert";
import { makeArray } from "@game/core/common/arrays";
import { assert } from "@game/core/common/assert";

import { EventId, FiniteStateMachine, StateId } from "./state-machine";

declare global {
Expand Down Expand Up @@ -51,12 +52,12 @@ declare global {
}
}

interface StateObserverElement<S extends StateId, E extends EventId> {
export interface StateObserverElement<S extends StateId, E extends EventId> {
fsm: FiniteStateMachine<S, E>;
children?: StateObserverAction<S, E>[];
}

interface StateObserverContext<S extends StateId, E extends EventId> {
export interface StateObserverContext<S extends StateId, E extends EventId> {
fsm: FiniteStateMachine<S, E>;
type: "enter" | "exit" | "event" | "transition";
previous: S;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SignalValue } from "@game/state/lib/types";
import { TransformablePhaserGameObject } from "../lib/types";
import { TransformablePhaserGameObject } from "@game/core/jsx/types";
import { SignalValue } from "@game/core/signals/types";

export const ALIGN_ITEMS = {
CENTER: "center",
Expand Down
3 changes: 2 additions & 1 deletion src/ui/components/Flex.tsx → src/core/ui/Flex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* Based off https://github.com/jjcapellan/phaser3-flex
*/

import { isSignal } from "@game/state/lib/signals";
import { isSignal } from "@game/core/signals/signals";

import {
ALIGN_ITEMS,
DIRECTION,
Expand Down
File renamed without changes.
File renamed without changes.
18 changes: 0 additions & 18 deletions src/ui/components/FlexWrapped.tsx → src/core/ui/FlexWrapped.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,6 @@ import {
import { FlexColumn } from "./FlexColumn";
import { FlexRow } from "./FlexRow";

/**
* Alignment values for the flex column
*
* This is meant not to be exported! We don't want to be confused with AlignItems
*/
const ALIGNMENT = {
JUSTIFY: "justify",
STRETCH: "stretch",
TOP: "top",
RIGHT: "right",
BOTTOM: "bottom",
LEFT: "left",
CENTER: "center",
SPACE_BETWEEN: "space-between",
SPACE_AROUND: "space-around",
SPACE_EVENLY: "space-evenly",
} as const;

export class FlexWrapped extends AbstractFlex {
private linesContainer: AbstractFlex;
private currentLine: AbstractFlex;
Expand Down
9 changes: 6 additions & 3 deletions src/ui/components/Stack.tsx → src/core/ui/Stack.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getSignalValue } from "@game/state/lib/signals";
import { Signal } from "@game/state/lib/types";
import { PhaserGameObjectProps } from "@game/ui/lib/types";
import { getSignalValue } from "@game/core/signals/signals";
import { Signal } from "@game/core/signals/types";
import { PhaserGameObjectProps } from "@game/core/jsx/types";

type Alignment = "start" | "center" | "end";
type Direction = "horizontal" | "vertical";
Expand All @@ -25,6 +25,9 @@ interface StackProps
debug?: boolean;
}

/**
* @deprecated Use Flex instead
*/
export function Stack({
direction = "vertical",
spacing = 0,
Expand Down
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Game, Types } from "phaser";

import { GAME_HEIGHT, GAME_WIDTH } from "./consts";
import { GAME_HEIGHT, GAME_WIDTH } from "@game/consts";

import PhaserGamebus from "./lib/gamebus";
import { GameStateManager } from "./state/game-state";
import PhaserGamebus from "@game/lib/gamebus";
import { GameStateManager } from "@game/state/game-state";

import { SCENE_CLASSES } from "./scenes/scenes";
import { SCENE_CLASSES } from "@game/scenes/scenes";

if (import.meta.env.VITE_DEBUG) {
}
Expand Down
1 change: 1 addition & 0 deletions src/scenes/boot/boot-scene.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Scene } from "phaser";

import { SCENES } from "../scenes";
import { DebugPanel, DebugParameters } from "../game/debug-panel";

Expand Down
3 changes: 2 additions & 1 deletion src/scenes/boot/preloader-scene.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RESOURCES } from "@game/assets";
import { GAME_WIDTH } from "@game/consts";

import { AbstractScene } from "..";
import { RESOURCES } from "../../assets";
import { SCENES } from "../scenes";

export const RESOURCES_INDEX = Object.keys(RESOURCES).reduce(
Expand Down
56 changes: 56 additions & 0 deletions src/scenes/example-scene.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import PhaserGamebus from "@game/lib/gamebus";

import { AbstractScene } from "./index";
import { SCENES } from "./scenes";

/**
* Example scene
*
* Copy this file, place it where it is relevant, update dependencies and have fun :)
* Don't forget to add the scene to the scenes.ts file
*/

export class GameScene extends AbstractScene {
declare bus: Phaser.Events.EventEmitter;
declare gamebus: PhaserGamebus;

camera: Phaser.Cameras.Scene2D.Camera;

constructor() {
super(SCENES.GAME);
}

key_one!: Phaser.Input.Keyboard.Key;
key_two!: Phaser.Input.Keyboard.Key;

create() {
this.bus = this.gamebus.getBus();

this.camera = this.cameras.main;

this.key_one = this.input.keyboard!.addKey(
Phaser.Input.Keyboard.KeyCodes.ONE
);
this.key_two = this.input.keyboard!.addKey(
Phaser.Input.Keyboard.KeyCodes.TWO
);

this.registerSystems();

this.key_one.on("down", () => {
console.log("key_one");
});

this.key_two.on("down", () => {
console.log("key_two");
});
}

registerSystems() {
console.log("registering systems");
}

update(_time: number, _delta: number) {}

shutdown() {}
}
2 changes: 1 addition & 1 deletion src/scenes/game/debug-panel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Pane } from "tweakpane";
import { JsxElementsRegistry } from "../../ui/lib/phaser-jsx";
import { JsxElementsRegistry } from "@game/core/jsx/phaser-jsx";

export const DebugParameters: any = {
fps: 0,
Expand Down
11 changes: 6 additions & 5 deletions src/scenes/game/game-scene.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { computed, effect, signal } from "@game/core/signals/signals";
import { Signal } from "@game/core/signals/types";
import { Stack } from "@game/core/ui/Stack";
import PhaserGamebus from "@game/lib/gamebus";
import { GameStatus } from "@game/state/game-state";
import { computed, effect, signal } from "@game/state/lib/signals";
import { Signal } from "@game/state/lib/types";
import SoundSystem from "@game/systems/SoundSystem";

import { AbstractScene } from "..";
import PhaserGamebus from "../../lib/gamebus";
import { Stack } from "../../ui/components/Stack";
import { SCENES } from "../scenes";
import SoundSystem from "../../systems/SoundSystem.ts";

let i = 0;

Expand Down
7 changes: 4 additions & 3 deletions src/scenes/hud/hud-scene.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PhaserGamebus from "../../lib/gamebus";
import { Flex } from "../../ui/components/Flex";
import { FlexRow } from "../../ui/components/FlexRow";
import { Flex } from "@game/core/ui/Flex";
import { FlexRow } from "@game/core/ui/FlexRow";
import PhaserGamebus from "@game/lib/gamebus";

import { AbstractScene } from "../index";
import { SCENES } from "../scenes";
import { LeftPanel } from "./left-panel";
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/hud/left-panel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Flex } from "../../ui/components/Flex";
import { Flex } from "@game/core/ui/Flex";

export const LeftPanel = ({
width,
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/hud/right-panel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Flex } from "../../ui/components/Flex";
import { Flex } from "@game/core/ui/Flex";

export const RightPanel = ({
width,
Expand Down
Loading

0 comments on commit 812a8cd

Please sign in to comment.