Skip to content

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.

feat(tileset): author circle + one-way collision in the editor

yhha97011725cee
diff --git a/desktop/src/i18n/messages/tile.ts b/desktop/src/i18n/messages/tile.ts
index 8bf12575d..15b1cfd89 100644
--- a/desktop/src/i18n/messages/tile.ts
+++ b/desktop/src/i18n/messages/tile.ts
@@ -87,6 +87,10 @@ export const tileMessages = defineMessages({
'tile.shape.aria': { en: 'Collision shape', zh: '碰撞形状' },
'tile.shape.box': { en: 'Box', zh: '矩形' },
'tile.shape.polygon': { en: 'Polygon', zh: '多边形' },
+ 'tile.shape.circle': { en: 'Circle', 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.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: '预设' },
'tile.slope.freeform': { en: 'Custom…', zh: '自定义…' },
diff --git a/desktop/src/panels/TilesetEditor.tsx b/desktop/src/panels/TilesetEditor.tsx
index 2e6216157..b4e1c2dfd 100644
--- a/desktop/src/panels/TilesetEditor.tsx
+++ b/desktop/src/panels/TilesetEditor.tsx
@@ -17,7 +17,7 @@ import {
useEffect, useRef, useState, useSyncExternalStore,
type CSSProperties, type MouseEvent as ReactMouseEvent,
} from 'react';
-import { Save, Plus, Trash2, X } from 'lucide-react';
+import { Save, Plus, Trash2, X, ArrowUp } from 'lucide-react';
import {
TB_N, TB_E, TB_S, TB_W, TB_NE, TB_SE, TB_SW, TB_NW,
type TilesetAsset, type TilesetAnimFrame,
@@ -135,11 +135,14 @@ export function TilesetEditor() {
const [zoom, setZoom] = useState(2);
const [mode, setMode] = useState<'collision' | 'terrain' | 'animation'>('collision');
const [animTile, setAnimTile] = useState<number | null>(null);
- const [shape, setShape] = useState<'box' | 'polygon'>('box');
+ const [shape, setShape] = useState<'box' | 'polygon' | 'circle'>('box');
const [polyTile, setPolyTile] = useState<number | null>(null);
// 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.
+ const [oneWayOn, setOneWayOn] = useState(false);
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.
@@ -172,10 +175,16 @@ export function TilesetEditor() {
const c = asset.tiles[id]?.collision;
return c?.type === 'polygon' ? c.points : null;
};
+ const circleOf = (id: number): { cx: number; cy: number; r: number } | null => {
+ const c = asset.tiles[id]?.collision;
+ 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 commitDrag = () => {
const d = dragRef.current;
- if (d) TilesetCommands.paintCollision([...d.ids], d.on);
+ if (d) TilesetCommands.paintCollision([...d.ids], d.on, oneWayOn ? { nx: 0, ny: 1 } : undefined);
setDrag(null);
};
@@ -225,13 +234,13 @@ export function TilesetEditor() {
cells.push(
<div
key={id}
- className={'ts-cell ts-pcell' + (hasPolygon(id) ? ' is-poly' : '')}
+ className={'ts-cell ts-pcell' + (hasPolygon(id) ? ' is-poly' : '') + (hasOneWay(id) ? ' is-oneway' : '')}
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));
+ if (activePreset) TilesetCommands.setTilePolygon(id, presetPointsPx(activePreset, tw, th), oneWayMod);
else setPolyTile(id);
}}
>
@@ -246,11 +255,33 @@ export function TilesetEditor() {
)}
</div>,
);
+ } else if (mode === 'collision' && shape === 'circle') {
+ const circ = circleOf(id);
+ cells.push(
+ <div
+ key={id}
+ className={'ts-cell ts-pcell' + (circ ? ' is-poly' : '') + (hasOneWay(id) ? ' is-oneway' : '')}
+ 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);
+ }}
+ >
+ {circ && (
+ <svg className="ts-pe-svg" width={w} height={h} viewBox={`0 0 ${tw} ${th}`} preserveAspectRatio="none">
+ <circle className="ts-pe-poly" cx={circ.cx} cy={circ.cy} r={circ.r} style={{ vectorEffect: 'non-scaling-stroke' }} />
+ </svg>
+ )}
+ </div>,
+ );
} else if (mode === 'collision') {
cells.push(
<div
key={id}
- className={'ts-cell' + (isSolid(id) ? ' is-solid' : '')}
+ className={'ts-cell' + (isSolid(id) ? ' is-solid' : '') + (hasOneWay(id) ? ' is-oneway' : '')}
style={{ left, top, width: w, height: h }}
title={`#${id}`}
onPointerDown={(e) => { e.preventDefault(); setDrag({ ids: new Set([id]), on: !isSolid(id) }); }}
@@ -339,15 +370,27 @@ 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') },
- ]}
- />
+ <>
+ <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>
+ </>
)}
<span className="ts-sep" />
<label className="ts-field">
diff --git a/desktop/src/theme/tileset.css b/desktop/src/theme/tileset.css
index 26d101257..66f1b213f 100644
--- a/desktop/src/theme/tileset.css
+++ b/desktop/src/theme/tileset.css
@@ -159,6 +159,29 @@
.ts-slope:not(.is-active):hover { border-color: var(--line-strong); color: var(--text); }
.ts-slope.is-active { border-color: var(--star); color: var(--text); }
.ts-slope svg { display: block; }
+
+/* One-way brush toggle + the solid-side edge marker on one-way tiles */
+.ts-oneway {
+ display: inline-flex;
+ align-items: center;
+ gap: 4px;
+ background: var(--srf-3);
+ border: 1px solid var(--line);
+ border-radius: var(--r-sm);
+ color: var(--text-dim);
+ padding: 3px 8px;
+ cursor: pointer;
+ font: inherit;
+ transition: color var(--t-fast), border-color var(--t-fast);
+}
+.ts-oneway:not(.is-active):hover { border-color: var(--line-strong); color: var(--text); }
+.ts-oneway.is-active {
+ border-color: var(--warn);
+ color: var(--text);
+ background: color-mix(in srgb, var(--warn) 15%, var(--srf-3));
+}
+/* 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); }
.ts-tname {
width: 110px;
background: var(--inset);
diff --git a/desktop/src/tileset/TilesetCommands.ts b/desktop/src/tileset/TilesetCommands.ts
index 3e533dec4..bc0249b55 100644
--- a/desktop/src/tileset/TilesetCommands.ts
+++ b/desktop/src/tileset/TilesetCommands.ts
@@ -36,13 +36,14 @@ export const TilesetCommands = {
});
},
- /** Set box collision on/off for a set of tiles as ONE undo step (a paint stroke). */
- paintCollision(tileIds: number[], on: boolean): void {
+ /** 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 {
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' } };
+ if (on) a.tiles[id] = { ...(a.tiles[id] ?? {}), collision: { type: 'box', ...(oneWay ? { oneWay } : {}) } };
else if (a.tiles[id]?.collision) {
delete a.tiles[id].collision;
pruneEmpty(a, id);
@@ -55,11 +56,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][]): void {
+ setTilePolygon(id: number, points: [number, number][], oneWay?: { nx: number; ny: number }): 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 } };
+ a.tiles[id] = { ...(a.tiles[id] ?? {}), collision: { type: 'polygon', points, ...(oneWay ? { oneWay } : {}) } };
} else if (a.tiles[id]?.collision?.type === 'polygon') {
delete a.tiles[id].collision;
pruneEmpty(a, id);
@@ -67,6 +68,19 @@ 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 {
+ 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 } : {}) } };
+ } else if (a.tiles[id]?.collision?.type === 'circle') {
+ delete a.tiles[id].collision;
+ pruneEmpty(a, id);
+ }
+ });
+ },
+
/** Add a terrain (autotile) set; returns the new set's index. */
addTerrain(name: string, mode: TerrainMode): void {
TilesetDocument.edit('Add Terrain', (a) => {