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): one-click slope/half-tile collision presets + inline outline preview

yhh5cd9c308696b
diff --git a/desktop/src/i18n/messages/tile.ts b/desktop/src/i18n/messages/tile.ts
index c9c078da9..8bf12575d 100644
--- a/desktop/src/i18n/messages/tile.ts
+++ b/desktop/src/i18n/messages/tile.ts
@@ -87,6 +87,16 @@ export const tileMessages = defineMessages({
'tile.shape.aria': { en: 'Collision shape', zh: '碰撞形状' },
'tile.shape.box': { en: 'Box', zh: '矩形' },
'tile.shape.polygon': { en: 'Polygon', zh: '多边形' },
+ // 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: '自定义…' },
+ 'tile.slope.stampTip': { en: 'Click a tile to stamp “{name}”', zh: '点瓦片盖上“{name}”' },
+ 'tile.slope.rampR': { en: 'Ramp ◢ (up →)', zh: '斜坡 ◢(向右升)' },
+ 'tile.slope.rampL': { en: 'Ramp ◣ (up ←)', zh: '斜坡 ◣(向左升)' },
+ 'tile.slope.halfBottom': { en: 'Half (bottom)', zh: '半砖(下)' },
+ 'tile.slope.halfTop': { en: 'Half (top)', zh: '半砖(上)' },
+ 'tile.slope.halfLeft': { en: 'Half (left)', zh: '半砖(左)' },
+ 'tile.slope.halfRight': { en: 'Half (right)', zh: '半砖(右)' },
'tile.solidCount': { en: '{count} solid', zh: '{count} 个实心' },
'tile.save': { en: 'Save', zh: '保存' },
'tile.done': { en: 'Done', zh: '完成' },
diff --git a/desktop/src/panels/TilesetEditor.tsx b/desktop/src/panels/TilesetEditor.tsx
index 6f181f663..0da76cdc6 100644
--- a/desktop/src/panels/TilesetEditor.tsx
+++ b/desktop/src/panels/TilesetEditor.tsx
@@ -9,8 +9,8 @@
* against. Subscribes to the reactive TilesetDocument; mutations go through
* TilesetCommands (one undo step each).
*
- * Scope: grid geometry + box collision + terrain peering. Per-tile polygon shapes and
- * animation slot onto the same asset later (the schema already carries them).
+ * Scope: grid geometry + box/polygon collision (freeform vertices or one-click slope /
+ * half-tile presets) + terrain peering + per-tile animation.
*/
import {
@@ -31,6 +31,7 @@ import { TilesetCommands } from '@/tileset/TilesetCommands';
import { ProjectStore } from '@/project/ProjectStore';
import { colsFor, rowsFor, TERRAIN_COLORS } from '@/tools/tileMath';
import { AnimPreview, tileThumbStyle, type TileAtlas } from '@/tools/tileThumb';
+import { SLOPE_PRESETS, presetPointsPx, type SlopePreset } from '@/tileset/slopePresets';
import { t } from '@/i18n';
// Peering zones in the cell's 3×3 grid; center (membership) is handled separately.
@@ -134,6 +135,9 @@ export function TilesetEditor() {
const [animTile, setAnimTile] = useState<number | null>(null);
const [shape, setShape] = useState<'box' | 'polygon'>('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);
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.
@@ -162,6 +166,10 @@ export function TilesetEditor() {
const isSolid = (id: number): boolean =>
drag?.ids.has(id) ? drag.on : asset.tiles[id]?.collision?.type === 'box';
const hasPolygon = (id: number): boolean => asset.tiles[id]?.collision?.type === 'polygon';
+ const polyPointsOf = (id: number): [number, number][] | null => {
+ const c = asset.tiles[id]?.collision;
+ return c?.type === 'polygon' ? c.points : null;
+ };
const commitDrag = () => {
const d = dragRef.current;
@@ -211,14 +219,30 @@ export function TilesetEditor() {
const w = tw * zoom;
const h = th * zoom;
if (mode === 'collision' && shape === 'polygon') {
+ const pts = polyPointsOf(id);
cells.push(
<div
key={id}
className={'ts-cell ts-pcell' + (hasPolygon(id) ? ' is-poly' : '')}
style={{ left, top, width: w, height: h }}
- title={t('tile.cell.polyTip', { id })}
- onPointerDown={(e) => { e.preventDefault(); setPolyTile(id); }}
- />,
+ 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));
+ else setPolyTile(id);
+ }}
+ >
+ {pts && (
+ <svg className="ts-pe-svg" width={w} height={h} viewBox={`0 0 ${tw} ${th}`} preserveAspectRatio="none">
+ <polygon
+ className="ts-pe-poly"
+ points={pts.map((p) => `${p[0]},${p[1]}`).join(' ')}
+ style={{ vectorEffect: 'non-scaling-stroke' }}
+ />
+ </svg>
+ )}
+ </div>,
);
} else if (mode === 'collision') {
cells.push(
@@ -430,6 +454,34 @@ export function TilesetEditor() {
</div>
)}
+ {mode === 'collision' && shape === 'polygon' && (
+ <div className="ts-slopes">
+ <span className="ts-slabel">{t('tile.slope.presets')}</span>
+ {SLOPE_PRESETS.map((p) => (
+ <button
+ key={p.id}
+ type="button"
+ className={'ts-slope' + (activePreset?.id === p.id ? ' is-active' : '')}
+ title={t(p.labelKey)}
+ aria-label={t(p.labelKey)}
+ onClick={() => setActivePreset((cur) => (cur?.id === p.id ? null : p))}
+ >
+ <svg width={20} height={20} viewBox="0 0 10 10" preserveAspectRatio="none" aria-hidden="true">
+ <polygon className="ts-pe-poly" points={p.points.map(([x, y]) => `${x * 10},${y * 10}`).join(' ')} style={{ vectorEffect: 'non-scaling-stroke' }} />
+ </svg>
+ </button>
+ ))}
+ <button
+ type="button"
+ className={'ts-slope ts-slope-free' + (activePreset === null ? ' is-active' : '')}
+ title={t('tile.slope.freeform')}
+ onClick={() => setActivePreset(null)}
+ >
+ {t('tile.slope.freeform')}
+ </button>
+ </div>
+ )}
+
<div className="ts-canvas" onPointerUp={mode === 'collision' ? commitDrag : undefined}
onPointerLeave={mode === 'collision' ? commitDrag : undefined}>
{!texUrl ? (
diff --git a/desktop/src/theme/tileset.css b/desktop/src/theme/tileset.css
index e5247056c..26d101257 100644
--- a/desktop/src/theme/tileset.css
+++ b/desktop/src/theme/tileset.css
@@ -129,6 +129,36 @@
.ts-tmode { color: var(--text-faint); font-size: var(--fs-2xs); }
.ts-add { padding: 3px 6px; }
.ts-tedit { display: inline-flex; align-items: center; gap: 6px; margin-left: auto; }
+
+/* Collision polygon presets (slopes / half-tiles) — a sub-bar mirroring the terrain bar */
+.ts-slopes {
+ 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-slabel { color: var(--text-faint); font-size: var(--fs-2xs); margin-right: 2px; }
+.ts-slope {
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ background: var(--srf-3);
+ border: 1px solid var(--line);
+ border-radius: var(--r-sm);
+ color: var(--text-dim);
+ padding: 3px;
+ cursor: pointer;
+ font: inherit;
+ transition: color var(--t-fast), border-color var(--t-fast);
+}
+.ts-slope-free { padding: 3px 8px; }
+.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; }
.ts-tname {
width: 110px;
background: var(--inset);
diff --git a/desktop/src/tileset/slopePresets.ts b/desktop/src/tileset/slopePresets.ts
new file mode 100644
index 000000000..1ae25a12b
--- /dev/null
+++ b/desktop/src/tileset/slopePresets.ts
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file slopePresets.ts
+ * @brief Canned per-tile collision polygons — one click stamps a slope / half-tile
+ * instead of hand-placing vertices. Points are NORMALIZED [0,1] in the atlas's
+ * own space (origin top-left, y-down), so they scale to any tile size; the
+ * tileset editor multiplies by (tileWidth, tileHeight) before committing the
+ * tile-local-pixel polygon the .estileset stores (see TilesetCommands.setTilePolygon).
+ */
+
+import type { MsgKey } from '@/i18n';
+
+export interface SlopePreset {
+ id: string;
+ /** i18n key for the button label / tooltip. */
+ labelKey: MsgKey;
+ /** Polygon vertices, normalized [0,1], y-down (matching the atlas). */
+ points: [number, number][];
+}
+
+// The starter set: two 45° floor ramps + four half-tiles. Covers the overwhelming
+// majority of platformer tile collision that a plain box can't express; freeform
+// polygon (the modal editor) handles the rest.
+export const SLOPE_PRESETS: SlopePreset[] = [
+ // Floor ramps — solid BELOW the diagonal surface.
+ { id: 'rampR', labelKey: 'tile.slope.rampR', points: [[0, 1], [1, 1], [1, 0]] }, // rises left→right
+ { id: 'rampL', labelKey: 'tile.slope.rampL', points: [[0, 0], [0, 1], [1, 1]] }, // rises right→left
+ // Half tiles.
+ { id: 'halfBottom', labelKey: 'tile.slope.halfBottom', points: [[0, 0.5], [1, 0.5], [1, 1], [0, 1]] },
+ { id: 'halfTop', labelKey: 'tile.slope.halfTop', points: [[0, 0], [1, 0], [1, 0.5], [0, 0.5]] },
+ { id: 'halfLeft', labelKey: 'tile.slope.halfLeft', points: [[0, 0], [0.5, 0], [0.5, 1], [0, 1]] },
+ { id: 'halfRight', labelKey: 'tile.slope.halfRight', points: [[0.5, 0], [1, 0], [1, 1], [0.5, 1]] },
+];
+
+/** A preset's vertices in tile-local pixels for the given tile size (what .estileset stores). */
+export function presetPointsPx(preset: SlopePreset, tileW: number, tileH: number): [number, number][] {
+ return preset.points.map(([x, y]) => [Math.round(x * tileW), Math.round(y * tileH)] as [number, number]);
+}
diff --git a/desktop/tests/slope-presets.test.ts b/desktop/tests/slope-presets.test.ts
new file mode 100644
index 000000000..d619aaa77
--- /dev/null
+++ b/desktop/tests/slope-presets.test.ts
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import { describe, it, expect } from 'vitest';
+import { SLOPE_PRESETS, presetPointsPx } from '@/tileset/slopePresets';
+
+describe('slope presets', () => {
+ it('every preset is a valid polygon (≥3 normalized [0,1] points)', () => {
+ for (const p of SLOPE_PRESETS) {
+ expect(p.points.length).toBeGreaterThanOrEqual(3);
+ for (const [x, y] of p.points) {
+ expect(x).toBeGreaterThanOrEqual(0);
+ expect(x).toBeLessThanOrEqual(1);
+ expect(y).toBeGreaterThanOrEqual(0);
+ expect(y).toBeLessThanOrEqual(1);
+ }
+ }
+ });
+
+ it('ids and label keys are unique', () => {
+ expect(new Set(SLOPE_PRESETS.map((p) => p.id)).size).toBe(SLOPE_PRESETS.length);
+ expect(new Set(SLOPE_PRESETS.map((p) => p.labelKey)).size).toBe(SLOPE_PRESETS.length);
+ });
+
+ it('presetPointsPx scales normalized points to tile-local pixels', () => {
+ const ramp = SLOPE_PRESETS.find((p) => p.id === 'rampR')!;
+ expect(presetPointsPx(ramp, 32, 16)).toEqual([[0, 16], [32, 16], [32, 0]]);
+ });
+
+ it('presetPointsPx rounds to integer pixels for odd tile sizes', () => {
+ const half = SLOPE_PRESETS.find((p) => p.id === 'halfBottom')!; // y: 0.5 → round(0.5*15)=8
+ const px = presetPointsPx(half, 15, 15);
+ for (const [x, y] of px) {
+ expect(Number.isInteger(x)).toBe(true);
+ expect(Number.isInteger(y)).toBe(true);
+ }
+ });
+});