A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(tileset): sensor + material collision brush modifiers
yhh690f43862d17
diff --git a/desktop/src/i18n/messages/tile.ts b/desktop/src/i18n/messages/tile.tsindex 15b1cfd89..eb8178788 100644--- a/desktop/src/i18n/messages/tile.ts+++ b/desktop/src/i18n/messages/tile.ts@@ -88,8 +88,13 @@ export const tileMessages = defineMessages({'tile.shape.box': { en: 'Box', zh: '矩形' },'tile.shape.polygon': { en: 'Polygon', zh: '多边形' },'tile.shape.circle': { en: 'Circle', zh: '圆形' },+ 'tile.modifiers': { en: 'Modifiers', zh: '修饰' },'tile.oneWay': { en: 'One-way', zh: '单向' },'tile.oneWayTip': { en: 'One-way platform: painted collision is solid on top, pass-through from below', zh: '单向平台:所画碰撞上方实心、下方可穿过' },+ 'tile.sensor': { en: 'Sensor', zh: '触发器' },+ 'tile.sensorTip': { en: 'Sensor: painted collision is a non-solid trigger (fires events, no physical response)', zh: '触发器:所画碰撞为非实心触发(触发事件、无物理响应)' },+ 'tile.friction': { en: 'friction', zh: '摩擦' },+ 'tile.restitution': { en: 'bounce', zh: '弹性' },'tile.cell.circleTip': { en: 'Tile #{id} — click to fit a circle collider (click again to clear)', zh: '瓦片 #{id}——点击放入圆形碰撞(再点清除)' },// Slope / half-tile presets (polygon mode): pick one, then click tiles to stamp it.'tile.slope.presets': { en: 'Presets', zh: '预设' },diff --git a/desktop/src/panels/TilesetEditor.tsx b/desktop/src/panels/TilesetEditor.tsxindex b4e1c2dfd..ac51f31e5 100644--- a/desktop/src/panels/TilesetEditor.tsx+++ b/desktop/src/panels/TilesetEditor.tsx@@ -9,8 +9,9 @@* against. Subscribes to the reactive TilesetDocument; mutations go through* TilesetCommands (one undo step each).*- * Scope: grid geometry + box/polygon collision (freeform vertices or one-click slope /- * half-tile presets) + terrain peering + per-tile animation.+ * Scope: grid geometry + box/polygon/circle collision (freeform vertices, one-click slope+ * presets, fitted discs) with one-way / sensor / material brush modifiers + terrain peering+ * + per-tile animation.*/import {@@ -28,7 +29,7 @@ import { usePanelWindow } from '@/components/PanelWindow';import { DirtyDot } from '@/components/DirtyDot';import { GridField } from '@/components/GridField';import { TilesetDocument } from '@/tileset/TilesetDocument';-import { TilesetCommands } from '@/tileset/TilesetCommands';+import { TilesetCommands, type TileCollisionMods } from '@/tileset/TilesetCommands';import { ProjectStore } from '@/project/ProjectStore';import { colsFor, rowsFor, TERRAIN_COLORS } from '@/tools/tileMath';import { AnimPreview, tileThumbStyle, type TileAtlas } from '@/tools/tileThumb';@@ -140,9 +141,12 @@ export function TilesetEditor() {// In polygon mode, an active preset stamps that canned slope/half-tile on click;// null = freeform (click opens the vertex editor). See slopePresets.const [activePreset, setActivePreset] = useState<SlopePreset | null>(null);- // One-way brush: while on, painted/stamped collision is a solid-top jump-through- // platform (normal {0,1}). A modifier orthogonal to the shape.+ // Collision brush modifiers, orthogonal to the shape: while set, painted/stamped+ // collision carries a solid-top one-way normal, sensor flag, and/or a material override.const [oneWayOn, setOneWayOn] = useState(false);+ const [sensorOn, setSensorOn] = useState(false);+ const [frictionStr, setFrictionStr] = useState('');+ const [restitutionStr, setRestitutionStr] = useState('');const [activeSet, setActiveSet] = useState(0);const [hovered, setHovered] = useState<number | null>(null);// Live collision paint stroke: which tiles + the target on/off state, one undo step.@@ -180,11 +184,23 @@ export function TilesetEditor() {return c?.type === 'circle' ? { cx: c.cx, cy: c.cy, r: c.r } : null;};const hasOneWay = (id: number): boolean => !!asset.tiles[id]?.collision?.oneWay;- const oneWayMod = oneWayOn ? { nx: 0, ny: 1 } : undefined;+ const hasSensor = (id: number): boolean => !!asset.tiles[id]?.collision?.sensor;+ const numOrU = (s: string): number | undefined => {+ const n = Number(s);+ return s.trim() !== '' && Number.isFinite(n) ? n : undefined;+ };+ const brushMods = ((): TileCollisionMods => {+ const m: TileCollisionMods = {};+ if (oneWayOn) m.oneWay = { nx: 0, ny: 1 };+ if (sensorOn) m.sensor = true;+ const fr = numOrU(frictionStr); if (fr !== undefined) m.friction = fr;+ const re = numOrU(restitutionStr); if (re !== undefined) m.restitution = re;+ return m;+ })();const commitDrag = () => {const d = dragRef.current;- if (d) TilesetCommands.paintCollision([...d.ids], d.on, oneWayOn ? { nx: 0, ny: 1 } : undefined);+ if (d) TilesetCommands.paintCollision([...d.ids], d.on, brushMods);setDrag(null);};@@ -234,13 +250,13 @@ export function TilesetEditor() {cells.push(<divkey={id}- className={'ts-cell ts-pcell' + (hasPolygon(id) ? ' is-poly' : '') + (hasOneWay(id) ? ' is-oneway' : '')}+ className={'ts-cell ts-pcell' + (hasPolygon(id) ? ' is-poly' : '') + (hasOneWay(id) ? ' is-oneway' : '') + (hasSensor(id) ? ' is-sensor' : '')}style={{ left, top, width: w, height: h }}title={activePreset ? t('tile.slope.stampTip', { name: t(activePreset.labelKey) }) : t('tile.cell.polyTip', { id })}onPointerDown={(e) => {e.preventDefault();// A preset stamps directly; freeform opens the vertex editor.- if (activePreset) TilesetCommands.setTilePolygon(id, presetPointsPx(activePreset, tw, th), oneWayMod);+ if (activePreset) TilesetCommands.setTilePolygon(id, presetPointsPx(activePreset, tw, th), brushMods);else setPolyTile(id);}}>@@ -260,14 +276,14 @@ export function TilesetEditor() {cells.push(<divkey={id}- className={'ts-cell ts-pcell' + (circ ? ' is-poly' : '') + (hasOneWay(id) ? ' is-oneway' : '')}+ className={'ts-cell ts-pcell' + (circ ? ' is-poly' : '') + (hasOneWay(id) ? ' is-oneway' : '') + (hasSensor(id) ? ' is-sensor' : '')}style={{ left, top, width: w, height: h }}title={t('tile.cell.circleTip', { id })}onPointerDown={(e) => {e.preventDefault();// Toggle: click a circle tile again to clear it; else stamp a fitted disc.if (circ) TilesetCommands.setTileCircle(id, 0, 0, 0);- else TilesetCommands.setTileCircle(id, tw / 2, th / 2, Math.min(tw, th) / 2, oneWayMod);+ else TilesetCommands.setTileCircle(id, tw / 2, th / 2, Math.min(tw, th) / 2, brushMods);}}>{circ && (@@ -281,7 +297,7 @@ export function TilesetEditor() {cells.push(<divkey={id}- className={'ts-cell' + (isSolid(id) ? ' is-solid' : '') + (hasOneWay(id) ? ' is-oneway' : '')}+ className={'ts-cell' + (isSolid(id) ? ' is-solid' : '') + (hasOneWay(id) ? ' is-oneway' : '') + (hasSensor(id) ? ' is-sensor' : '')}style={{ left, top, width: w, height: h }}title={`#${id}`}onPointerDown={(e) => { e.preventDefault(); setDrag({ ids: new Set([id]), on: !isSolid(id) }); }}@@ -370,27 +386,16 @@ export function TilesetEditor() {]}/>{mode === 'collision' && (- <>- <Segmented- value={shape}- onChange={setShape}- ariaLabel={t('tile.shape.aria')}- options={[- { value: 'box', label: t('tile.shape.box') },- { value: 'polygon', label: t('tile.shape.polygon') },- { value: 'circle', label: t('tile.shape.circle') },- ]}- />- <button- type="button"- className={'ts-oneway' + (oneWayOn ? ' is-active' : '')}- title={t('tile.oneWayTip')}- aria-pressed={oneWayOn}- onClick={() => setOneWayOn((v) => !v)}- >- <ArrowUp size={13} /> {t('tile.oneWay')}- </button>- </>+ <Segmented+ value={shape}+ onChange={setShape}+ ariaLabel={t('tile.shape.aria')}+ options={[+ { value: 'box', label: t('tile.shape.box') },+ { value: 'polygon', label: t('tile.shape.polygon') },+ { value: 'circle', label: t('tile.shape.circle') },+ ]}+ />)}<span className="ts-sep" /><label className="ts-field">@@ -499,6 +504,44 @@ export function TilesetEditor() {</div>)}+ {mode === 'collision' && (+ <div className="ts-modbar">+ <span className="ts-slabel">{t('tile.modifiers')}</span>+ <button+ type="button"+ className={'ts-oneway' + (oneWayOn ? ' is-active' : '')}+ title={t('tile.oneWayTip')}+ aria-pressed={oneWayOn}+ onClick={() => setOneWayOn((v) => !v)}+ >+ <ArrowUp size={13} /> {t('tile.oneWay')}+ </button>+ <button+ type="button"+ className={'ts-oneway' + (sensorOn ? ' is-active' : '')}+ title={t('tile.sensorTip')}+ aria-pressed={sensorOn}+ onClick={() => setSensorOn((v) => !v)}+ >+ {t('tile.sensor')}+ </button>+ <label className="ts-modnum">+ <span>{t('tile.friction')}</span>+ <input+ type="text" inputMode="decimal" placeholder="0.3" value={frictionStr} spellCheck={false}+ onChange={(e) => setFrictionStr(e.target.value)}+ />+ </label>+ <label className="ts-modnum">+ <span>{t('tile.restitution')}</span>+ <input+ type="text" inputMode="decimal" placeholder="0" value={restitutionStr} spellCheck={false}+ onChange={(e) => setRestitutionStr(e.target.value)}+ />+ </label>+ </div>+ )}+{mode === 'collision' && shape === 'polygon' && (<div className="ts-slopes"><span className="ts-slabel">{t('tile.slope.presets')}</span>diff --git a/desktop/src/theme/tileset.css b/desktop/src/theme/tileset.cssindex 66f1b213f..3dc707e76 100644--- a/desktop/src/theme/tileset.css+++ b/desktop/src/theme/tileset.css@@ -182,6 +182,32 @@}/* Solid side of a one-way tile — a bright top edge (the default {0,1} solid-top normal). */.ts-cell.is-oneway { box-shadow: inset 0 2px 0 var(--warn); }++/* Collision brush-modifier sub-bar (one-way / sensor toggles + material inputs) */+.ts-modbar {+ display: flex;+ align-items: center;+ gap: 6px;+ flex-wrap: wrap;+ padding: 6px 10px;+ border-bottom: 1px solid var(--line);+ background: var(--srf-2);+ flex: 0 0 auto;+}+.ts-modnum { display: inline-flex; align-items: center; gap: 4px; color: var(--text-faint); font-size: var(--fs-2xs); }+.ts-modnum input {+ width: 44px;+ background: var(--srf-3);+ border: 1px solid var(--line);+ border-radius: var(--r-xs);+ color: var(--text);+ padding: 2px 5px;+ font: inherit;+ font-size: var(--fs-2xs);+}+.ts-modnum input:focus { outline: none; border-color: var(--star); }+/* Sensor tile — a dashed outline (non-solid trigger). */+.ts-cell.is-sensor { outline: 1px dashed var(--warn); outline-offset: -2px; }.ts-tname {width: 110px;background: var(--inset);diff --git a/desktop/src/tileset/TilesetCommands.ts b/desktop/src/tileset/TilesetCommands.tsindex bc0249b55..6a5386357 100644--- a/desktop/src/tileset/TilesetCommands.ts+++ b/desktop/src/tileset/TilesetCommands.ts@@ -15,6 +15,27 @@ import { t } from '@/i18n';type GridPatch = Partial<Pick<TilesetAsset, 'tileWidth' | 'tileHeight' | 'margin' | 'spacing' | 'columns'>>;+/** Cross-cutting collision modifiers applied to whatever shape is painted/stamped. */+export interface TileCollisionMods {+ oneWay?: { nx: number; ny: number };+ sensor?: boolean;+ density?: number;+ friction?: number;+ restitution?: number;+}++/** Attach the present modifiers to a bare shape (absent ones stay at engine defaults). */+function withMods<T extends object>(shape: T, mods?: TileCollisionMods): T & TileCollisionMods {+ const out = { ...shape } as T & TileCollisionMods;+ if (!mods) return out;+ if (mods.oneWay) out.oneWay = mods.oneWay;+ if (mods.sensor) out.sensor = true;+ if (mods.density !== undefined) out.density = mods.density;+ if (mods.friction !== undefined) out.friction = mods.friction;+ if (mods.restitution !== undefined) out.restitution = mods.restitution;+ return out;+}+/** Drop a tile entry that no longer carries any metadata, keeping the map sparse. */function pruneEmpty(asset: TilesetAsset, id: number): void {const t = asset.tiles[id];@@ -37,13 +58,13 @@ export const TilesetCommands = {},/** Set box collision on/off for a set of tiles as ONE undo step (a paint stroke).- * `oneWay` (a solid-side normal) tags the painted boxes as jump-through platforms. */- paintCollision(tileIds: number[], on: boolean, oneWay?: { nx: number; ny: number }): void {+ * `mods` (one-way / sensor / material) ride along on the painted boxes. */+ paintCollision(tileIds: number[], on: boolean, mods?: TileCollisionMods): void {if (tileIds.length === 0) return;TilesetDocument.edit(on ? 'Add Tile Collision' : 'Remove Tile Collision', (a) => {for (const id of tileIds) {if (id <= 0) continue;- if (on) a.tiles[id] = { ...(a.tiles[id] ?? {}), collision: { type: 'box', ...(oneWay ? { oneWay } : {}) } };+ if (on) a.tiles[id] = { ...(a.tiles[id] ?? {}), collision: withMods({ type: 'box' as const }, mods) };else if (a.tiles[id]?.collision) {delete a.tiles[id].collision;pruneEmpty(a, id);@@ -56,11 +77,11 @@ export const TilesetCommands = {* Set a tile's polygon collision outline (tile-local pixels) as ONE undo step. Fewer* than 3 points clears any polygon collision on the tile (parseTileset would drop it).*/- setTilePolygon(id: number, points: [number, number][], oneWay?: { nx: number; ny: number }): void {+ setTilePolygon(id: number, points: [number, number][], mods?: TileCollisionMods): void {if (id <= 0) return;TilesetDocument.edit('Edit Tile Collision Shape', (a) => {if (points.length >= 3) {- a.tiles[id] = { ...(a.tiles[id] ?? {}), collision: { type: 'polygon', points, ...(oneWay ? { oneWay } : {}) } };+ a.tiles[id] = { ...(a.tiles[id] ?? {}), collision: withMods({ type: 'polygon' as const, points }, mods) };} else if (a.tiles[id]?.collision?.type === 'polygon') {delete a.tiles[id].collision;pruneEmpty(a, id);@@ -69,11 +90,11 @@ export const TilesetCommands = {},/** Set a tile's circle collision (tile-local pixels) as ONE undo step; r ≤ 0 clears it. */- setTileCircle(id: number, cx: number, cy: number, r: number, oneWay?: { nx: number; ny: number }): void {+ setTileCircle(id: number, cx: number, cy: number, r: number, mods?: TileCollisionMods): void {if (id <= 0) return;TilesetDocument.edit('Edit Tile Collision Shape', (a) => {if (r > 0) {- a.tiles[id] = { ...(a.tiles[id] ?? {}), collision: { type: 'circle', cx, cy, r, ...(oneWay ? { oneWay } : {}) } };+ a.tiles[id] = { ...(a.tiles[id] ?? {}), collision: withMods({ type: 'circle' as const, cx, cy, r }, mods) };} else if (a.tiles[id]?.collision?.type === 'circle') {delete a.tiles[id].collision;pruneEmpty(a, id);diff --git a/desktop/tests/tileset-commands.test.ts b/desktop/tests/tileset-commands.test.tsindex 39c983b92..35670f0c5 100644--- a/desktop/tests/tileset-commands.test.ts+++ b/desktop/tests/tileset-commands.test.ts@@ -54,4 +54,26 @@ describe('TilesetCommands', () => {EditorHistory.undo();expect(TilesetDocument.asset!.tileWidth).toBe(16);});++ it('paintCollision carries the brush modifiers (one-way / sensor / material)', () => {+ TilesetCommands.paintCollision([5], true, { oneWay: { nx: 0, ny: 1 }, sensor: true, friction: 0.1 });+ expect(tiles()[5].collision).toEqual({ type: 'box', oneWay: { nx: 0, ny: 1 }, sensor: true, friction: 0.1 });+ });++ it('a plain box carries no modifier keys', () => {+ TilesetCommands.paintCollision([5], true, {});+ expect(tiles()[5].collision).toEqual({ type: 'box' });+ });++ it('setTileCircle stores a circle (with modifiers) and clears on r ≤ 0', () => {+ TilesetCommands.setTileCircle(5, 8, 8, 6, { oneWay: { nx: 0, ny: 1 } });+ expect(tiles()[5].collision).toEqual({ type: 'circle', cx: 8, cy: 8, r: 6, oneWay: { nx: 0, ny: 1 } });+ TilesetCommands.setTileCircle(5, 0, 0, 0);+ expect(tiles()[5]).toBeUndefined(); // pruned+ });++ it('setTilePolygon carries modifiers on the polygon shape', () => {+ TilesetCommands.setTilePolygon(5, [[0, 16], [16, 16], [16, 0]], { sensor: true });+ expect(tiles()[5].collision).toEqual({ type: 'polygon', points: [[0, 16], [16, 16], [16, 0]], sensor: true });+ });});