@mertdogar/phaser-procedural-walls 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mert Dogar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # @mertdogar/phaser-procedural-walls
2
+
3
+ A Phaser 4 scene plugin that draws top-down floorplan walls from a few lines of data. You describe wall centerlines and window positions; the plugin draws thick wall bodies, a front face, see-through windows with sills, and optional Arcade Physics colliders, and it depth-sorts each wall so characters walk in front of and behind walls correctly.
4
+
5
+ ![Full floorplan rendered by the demo](docs/images/overview.jpg)
6
+
7
+ ## Features
8
+
9
+ - Axis-aligned wall segments with automatic corner and T-junction filling
10
+ - Per-wall style presets: flat colors or tiled textures for the wall top and its face
11
+ - Windows cut into the face with translucent glass and an opaque sill
12
+ - One depth-sorted Container per wall, so `sprite.setDepth(sprite.y)` is all a character needs
13
+ - Optional Arcade static bodies: a thin plane under horizontal walls, a full rectangle for vertical ones
14
+ - Pure geometry module with unit tests, no Phaser needed to test it
15
+
16
+ ## Install
17
+
18
+ ```bash
19
+ pnpm add @mertdogar/phaser-procedural-walls phaser
20
+ ```
21
+
22
+ Register the scene plugin in your game config:
23
+
24
+ ```ts
25
+ import Phaser from "phaser";
26
+ import { WallMapPlugin } from "@mertdogar/phaser-procedural-walls";
27
+
28
+ new Phaser.Game({
29
+ physics: { default: "arcade" },
30
+ plugins: { scene: [{ key: "WallMapPlugin", plugin: WallMapPlugin, mapping: "wallMapPlugin" }] },
31
+ scene: MyScene,
32
+ });
33
+ ```
34
+
35
+ ## Minimal example
36
+
37
+ ```ts
38
+ const wallMap = this.add.wallMap({
39
+ presets: {
40
+ brick: { fill: 0xb59a8c, edge: 0x4a3830, lipHeight: 88, lipFill: 0x8f7a70, windowFill: 0x3b7d86 },
41
+ },
42
+ walls: [
43
+ { x1: 0, y1: 0, x2: 400, y2: 0, thickness: 22, preset: "brick", windows: [{ offset: 60, width: 60 }] },
44
+ { x1: 0, y1: 0, x2: 0, y2: 300, thickness: 22, preset: "brick" },
45
+ { x1: 400, y1: 0, x2: 400, y2: 300, thickness: 22, preset: "brick" },
46
+ { x1: 0, y1: 300, x2: 400, y2: 300, thickness: 22, preset: "brick", windows: [{ offset: 170, width: 60 }] },
47
+ ],
48
+ collide: true,
49
+ });
50
+
51
+ const player = this.physics.add.sprite(200, 150, "player").setOrigin(0.5, 1);
52
+ this.physics.add.collider(player, wallMap.bodies!);
53
+
54
+ // in update()
55
+ player.setDepth(player.y);
56
+ ```
57
+
58
+ <p>
59
+ <img src="docs/images/player-in-front.jpg" width="48%" alt="Player standing in front of a wall face">
60
+ <img src="docs/images/player-behind-window.jpg" width="48%" alt="Player behind the wall, visible through the window">
61
+ </p>
62
+
63
+ ## Documentation
64
+
65
+ - [Quick start](docs/quickstart.md). Build a walkable room with a window from an empty folder.
66
+ - [API reference](docs/api.md). Every config field, preset option, and method.
67
+ - [How it works](docs/how-it-works.md). Corner filling, depth sorting, the collision plane, and window holes.
68
+
69
+ ## Develop
70
+
71
+ ```bash
72
+ pnpm install
73
+ pnpm dev # demo: full floorplan, arrow keys move the player
74
+ pnpm test # geometry unit tests
75
+ pnpm build # library build to dist/
76
+ ```
77
+
78
+ ## License
79
+
80
+ MIT
@@ -0,0 +1,17 @@
1
+ import Phaser from "phaser";
2
+ import type { WallMapConfig } from "./types";
3
+ export declare class WallMap {
4
+ readonly scene: Phaser.Scene;
5
+ containers: Phaser.GameObjects.Container[];
6
+ bodies: Phaser.Physics.Arcade.StaticGroup | null;
7
+ private config;
8
+ constructor(scene: Phaser.Scene, config: WallMapConfig);
9
+ setWalls(walls: WallMapConfig["walls"]): this;
10
+ redraw(): this;
11
+ destroy(): void;
12
+ private clear;
13
+ private build;
14
+ private drawWall;
15
+ private fillRect;
16
+ private addBody;
17
+ }
@@ -0,0 +1,13 @@
1
+ import Phaser from "phaser";
2
+ import { WallMap } from "./WallMap";
3
+ import type { WallMapConfig } from "./types";
4
+ declare module "phaser" {
5
+ namespace GameObjects {
6
+ interface GameObjectFactory {
7
+ wallMap(config: WallMapConfig): WallMap;
8
+ }
9
+ }
10
+ }
11
+ export declare class WallMapPlugin extends Phaser.Plugins.ScenePlugin {
12
+ constructor(scene: Phaser.Scene, pluginManager: Phaser.Plugins.PluginManager, pluginKey: string);
13
+ }
@@ -0,0 +1,4 @@
1
+ import type { Rect, ResolvedWall, WallPreset, WallSpec } from "./types";
2
+ export declare function endExtension(px: number, py: number, self: WallSpec, all: WallSpec[]): number;
3
+ export declare function cutRects(rect: Rect, holes: Rect[], horizontal: boolean): Rect[];
4
+ export declare function resolveWalls(walls: WallSpec[], presets: Record<string, WallPreset>): ResolvedWall[];
@@ -0,0 +1,4 @@
1
+ export { WallMapPlugin } from "./WallMapPlugin";
2
+ export { WallMap } from "./WallMap";
3
+ export { resolveWalls } from "./geometry";
4
+ export type * from "./types";
package/dist/index.js ADDED
@@ -0,0 +1,131 @@
1
+ var F = Object.defineProperty;
2
+ var z = (i, n, s) => n in i ? F(i, n, { enumerable: !0, configurable: !0, writable: !0, value: s }) : i[n] = s;
3
+ var g = (i, n, s) => z(i, typeof n != "symbol" ? n + "" : n, s);
4
+ import I from "phaser";
5
+ const M = { lipHeight: 0, windowInset: 0.6, sillHeight: 8 }, S = 8;
6
+ function $(i) {
7
+ var t;
8
+ if (i.x1 !== i.x2 && i.y1 !== i.y2)
9
+ throw new Error(`Wall is not axis-aligned: (${i.x1},${i.y1})-(${i.x2},${i.y2})`);
10
+ if (!(i.x1 > i.x2 || i.y1 > i.y2)) return i;
11
+ const s = Math.abs(i.x2 - i.x1) + Math.abs(i.y2 - i.y1);
12
+ return {
13
+ ...i,
14
+ x1: i.x2,
15
+ y1: i.y2,
16
+ x2: i.x1,
17
+ y2: i.y1,
18
+ windows: (t = i.windows) == null ? void 0 : t.map((e) => ({ ...e, offset: s - e.offset - e.width }))
19
+ };
20
+ }
21
+ function T(i, n, s) {
22
+ const t = i >= Math.min(s.x1, s.x2) && i <= Math.max(s.x1, s.x2), e = n >= Math.min(s.y1, s.y2) && n <= Math.max(s.y1, s.y2);
23
+ return t && e;
24
+ }
25
+ function P(i, n, s, t) {
26
+ let e = 0;
27
+ for (const h of t)
28
+ h !== s && T(i, n, h) && (e = Math.max(e, h.thickness / 2));
29
+ return e;
30
+ }
31
+ function R(i, n, s) {
32
+ if (n.length === 0) return [i];
33
+ const t = [], e = i.x + i.w, h = i.y + i.h;
34
+ if (s) {
35
+ const l = Math.min(...n.map((d) => d.y)), a = Math.max(...n.map((d) => d.y + d.h));
36
+ l > i.y && t.push({ x: i.x, y: i.y, w: i.w, h: l - i.y }), a < h && t.push({ x: i.x, y: a, w: i.w, h: h - a });
37
+ let c = i.x;
38
+ for (const d of [...n].sort((f, o) => f.x - o.x))
39
+ d.x > c && t.push({ x: c, y: l, w: d.x - c, h: a - l }), c = d.x + d.w;
40
+ return c < e && t.push({ x: c, y: l, w: e - c, h: a - l }), t;
41
+ }
42
+ const y = Math.min(...n.map((l) => l.x)), r = Math.max(...n.map((l) => l.x + l.w));
43
+ y > i.x && t.push({ x: i.x, y: i.y, w: y - i.x, h: i.h }), r < e && t.push({ x: r, y: i.y, w: e - r, h: i.h });
44
+ let x = i.y;
45
+ for (const l of [...n].sort((a, c) => a.y - c.y))
46
+ l.y > x && t.push({ x: y, y: x, w: r - y, h: l.y - x }), x = l.y + l.h;
47
+ return x < h && t.push({ x: y, y: x, w: r - y, h: h - x }), t;
48
+ }
49
+ function v(i, n) {
50
+ const s = i.map($);
51
+ return s.map((t) => {
52
+ const e = n[t.preset];
53
+ if (!e) throw new Error(`Unknown wall preset "${t.preset}"`);
54
+ const h = e.lipHeight ?? M.lipHeight, y = e.windowInset ?? M.windowInset, r = e.sillHeight ?? M.sillHeight, x = t.thickness, l = x / 2, a = P(t.x1, t.y1, t, s), c = P(t.x2, t.y2, t, s), d = t.y1 === t.y2, f = d ? { x: t.x1 - a, y: t.y1 - l, w: t.x2 - t.x1 + a + c, h: x } : { x: t.x1 - l, y: t.y1 - a, w: x, h: t.y2 - t.y1 + a + c }, o = h <= 0 ? null : { x: f.x, y: f.y + f.h, w: f.w, h }, m = (t.windows ?? []).map((u) => {
55
+ if (!d) return { x: t.x1 - l * y, y: t.y1 + u.offset, w: x * y, h: u.width };
56
+ const p = t.x1 + u.offset;
57
+ return o ? { x: p, y: Math.round(o.y + o.h * (1 - y) / 2), w: u.width, h: Math.round(o.h * y) } : { x: p, y: t.y1 - l * y, w: u.width, h: x * y };
58
+ }), w = d && o !== null, W = w && r > 0 ? m.map((u) => {
59
+ const p = Math.min(r, u.h);
60
+ return { x: u.x, y: u.y + u.h - p, w: u.w, h: p };
61
+ }) : [], H = R(f, w ? [] : m, d), E = o ? R(o, w ? m : [], !0) : [], b = f.y + f.h + (o ? o.h : 0), k = d ? { x: f.x, y: b - S, w: f.w, h: S } : { x: f.x, y: f.y, w: f.w, h: b - f.y };
62
+ return { spec: t, horizontal: d, body: f, lip: o, bodyPieces: H, lipPieces: E, windows: m, sills: W, collider: k, depth: b };
63
+ });
64
+ }
65
+ class A {
66
+ constructor(n, s) {
67
+ g(this, "scene");
68
+ g(this, "containers", []);
69
+ g(this, "bodies", null);
70
+ g(this, "config");
71
+ this.scene = n, this.config = s, this.build();
72
+ }
73
+ setWalls(n) {
74
+ return this.config = { ...this.config, walls: n }, this.clear(), this.build(), this;
75
+ }
76
+ redraw() {
77
+ return this.clear(), this.build(), this;
78
+ }
79
+ destroy() {
80
+ this.clear();
81
+ }
82
+ clear() {
83
+ var n;
84
+ for (const s of this.containers) s.destroy();
85
+ this.containers = [], (n = this.bodies) == null || n.destroy(!0), this.bodies = null;
86
+ }
87
+ build() {
88
+ const { presets: n, walls: s, collide: t } = this.config, e = v(s, n);
89
+ t && (this.bodies = this.scene.physics.add.staticGroup());
90
+ for (const h of e)
91
+ this.containers.push(this.drawWall(h, n[h.spec.preset])), this.bodies && this.addBody(h);
92
+ }
93
+ drawWall(n, s) {
94
+ const { body: t, lip: e, bodyPieces: h, lipPieces: y, windows: r, sills: x } = n, l = this.scene.add.container(0, 0), a = this.scene.add.graphics(), c = this.scene.add.graphics(), d = s.edgeWidth ?? 2;
95
+ for (const o of h) this.fillRect(l, a, o, s.fill, s.texture);
96
+ for (const o of y) this.fillRect(l, a, o, s.lipFill ?? s.fill, s.lipTexture);
97
+ const f = s.windowFill ?? 4030344;
98
+ for (const o of r)
99
+ a.fillStyle(f, s.windowAlpha ?? 0.5), a.fillRect(o.x, o.y, o.w, o.h);
100
+ l.add(a);
101
+ for (const o of x) this.fillRect(l, c, o, s.fill, s.texture);
102
+ for (const o of x) c.lineStyle(1, s.edge).lineBetween(o.x, o.y, o.x + o.w, o.y);
103
+ if (s.windowFrame !== void 0)
104
+ for (const o of r) c.lineStyle(1, s.windowFrame).strokeRect(o.x, o.y, o.w, o.h);
105
+ return c.lineStyle(d, s.edge), c.strokeRect(t.x, t.y, t.w, t.h), e && c.lineStyle(d, s.edge).strokeRect(e.x, e.y, e.w, e.h), l.add(c), l.setDepth(n.depth), l;
106
+ }
107
+ fillRect(n, s, t, e, h) {
108
+ if (!h) {
109
+ s.fillStyle(e).fillRect(t.x, t.y, t.w, t.h);
110
+ return;
111
+ }
112
+ const y = this.scene.add.tileSprite(t.x + t.w / 2, t.y + t.h / 2, t.w, t.h, h);
113
+ y.setTilePosition(t.x, t.y), n.add(y);
114
+ }
115
+ addBody(n) {
116
+ const { collider: s } = n, t = this.scene.add.zone(s.x + s.w / 2, s.y + s.h / 2, s.w, s.h);
117
+ this.scene.physics.add.existing(t, !0), this.bodies.add(t);
118
+ }
119
+ }
120
+ class D extends I.Plugins.ScenePlugin {
121
+ constructor(n, s, t) {
122
+ super(n, s, t), typeof n.add.wallMap != "function" && s.registerGameObject("wallMap", function(e) {
123
+ return new A(this.scene, e);
124
+ });
125
+ }
126
+ }
127
+ export {
128
+ A as WallMap,
129
+ D as WallMapPlugin,
130
+ v as resolveWalls
131
+ };
@@ -0,0 +1,50 @@
1
+ export interface WallPreset {
2
+ fill: number;
3
+ edge: number;
4
+ edgeWidth?: number;
5
+ lipHeight?: number;
6
+ lipFill?: number;
7
+ windowFill?: number;
8
+ windowFrame?: number;
9
+ windowInset?: number;
10
+ windowAlpha?: number;
11
+ sillHeight?: number;
12
+ texture?: string;
13
+ lipTexture?: string;
14
+ }
15
+ export interface WindowSpec {
16
+ offset: number;
17
+ width: number;
18
+ }
19
+ export interface WallSpec {
20
+ x1: number;
21
+ y1: number;
22
+ x2: number;
23
+ y2: number;
24
+ thickness: number;
25
+ preset: string;
26
+ windows?: WindowSpec[];
27
+ }
28
+ export interface WallMapConfig {
29
+ presets: Record<string, WallPreset>;
30
+ walls: WallSpec[];
31
+ collide?: boolean;
32
+ }
33
+ export interface Rect {
34
+ x: number;
35
+ y: number;
36
+ w: number;
37
+ h: number;
38
+ }
39
+ export interface ResolvedWall {
40
+ spec: WallSpec;
41
+ horizontal: boolean;
42
+ body: Rect;
43
+ lip: Rect | null;
44
+ bodyPieces: Rect[];
45
+ lipPieces: Rect[];
46
+ windows: Rect[];
47
+ sills: Rect[];
48
+ collider: Rect;
49
+ depth: number;
50
+ }
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@mertdogar/phaser-procedural-walls",
3
+ "version": "0.1.0",
4
+ "description": "Phaser 4 scene plugin that renders procedural top-down walls with faces, see-through windows, depth sorting and Arcade colliders from wall centerline data.",
5
+ "keywords": [
6
+ "phaser",
7
+ "phaser4",
8
+ "phaser-plugin",
9
+ "procedural",
10
+ "walls",
11
+ "floorplan",
12
+ "top-down",
13
+ "tilemap",
14
+ "game"
15
+ ],
16
+ "author": "Mert Dogar <mertdogar@gmail.com>",
17
+ "license": "MIT",
18
+ "homepage": "https://github.com/mertdogar/phaser-procedural-walls#readme",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/mertdogar/phaser-procedural-walls.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/mertdogar/phaser-procedural-walls/issues"
25
+ },
26
+ "type": "module",
27
+ "main": "./dist/index.js",
28
+ "module": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.js"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "sideEffects": false,
38
+ "files": [
39
+ "dist",
40
+ "LICENSE"
41
+ ],
42
+ "engines": {
43
+ "node": ">=20"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
+ "scripts": {
49
+ "dev": "vite",
50
+ "build": "vite build && tsc -p tsconfig.lib.json",
51
+ "test": "vitest run",
52
+ "typecheck": "tsc --noEmit",
53
+ "lint": "oxlint src demo tests",
54
+ "prepublishOnly": "pnpm typecheck && pnpm lint && pnpm test && pnpm build"
55
+ },
56
+ "peerDependencies": {
57
+ "phaser": "^4.0.0"
58
+ },
59
+ "devDependencies": {
60
+ "oxlint": "^1.0.0",
61
+ "phaser": "^4.2.1",
62
+ "typescript": "^5.6.0",
63
+ "vite": "^6.0.0",
64
+ "vitest": "^3.0.0"
65
+ }
66
+ }