2026-03-31 03:01:11 +02:00
|
|
|
import { cloneEditorSelection, type EditorSelection } from "../core/selection";
|
2026-03-31 03:30:11 +02:00
|
|
|
import type { ToolMode } from "../core/tool-mode";
|
2026-03-31 03:00:38 +02:00
|
|
|
import type { Vec3 } from "../core/vector";
|
|
|
|
|
import { createOpaqueId } from "../core/ids";
|
2026-03-31 03:01:11 +02:00
|
|
|
import { cloneEntityInstance, createPlayerStartEntity } from "../entities/entity-instances";
|
2026-03-31 03:00:38 +02:00
|
|
|
|
|
|
|
|
import type { EditorCommand } from "./command";
|
|
|
|
|
|
|
|
|
|
interface SetPlayerStartCommandOptions {
|
|
|
|
|
entityId?: string;
|
|
|
|
|
position: Vec3;
|
|
|
|
|
yawDegrees: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setSinglePlayerStartSelection(entityId: string): EditorSelection {
|
|
|
|
|
return {
|
|
|
|
|
kind: "entities",
|
|
|
|
|
ids: [entityId]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createSetPlayerStartCommand(options: SetPlayerStartCommandOptions): EditorCommand {
|
|
|
|
|
const nextEntity = createPlayerStartEntity({
|
|
|
|
|
id: options.entityId,
|
|
|
|
|
position: options.position,
|
|
|
|
|
yawDegrees: options.yawDegrees
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let previousEntity = null as typeof nextEntity | null;
|
|
|
|
|
let previousSelection: EditorSelection | null = null;
|
2026-03-31 03:30:11 +02:00
|
|
|
let previousToolMode: ToolMode | null = null;
|
2026-03-31 03:00:38 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: createOpaqueId("command"),
|
|
|
|
|
label: options.entityId === undefined ? "Place player start" : "Move player start",
|
|
|
|
|
execute(context) {
|
|
|
|
|
const currentDocument = context.getDocument();
|
|
|
|
|
const currentEntity = currentDocument.entities[nextEntity.id];
|
|
|
|
|
|
|
|
|
|
if (currentEntity !== undefined && currentEntity.kind !== "playerStart") {
|
|
|
|
|
throw new Error(`Entity ${nextEntity.id} is not a player start.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (previousSelection === null) {
|
2026-03-31 03:01:11 +02:00
|
|
|
previousSelection = cloneEditorSelection(context.getSelection());
|
2026-03-31 03:00:38 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:30:11 +02:00
|
|
|
if (previousToolMode === null) {
|
|
|
|
|
previousToolMode = context.getToolMode();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:00:38 +02:00
|
|
|
if (previousEntity === null && currentEntity !== undefined) {
|
2026-03-31 03:01:11 +02:00
|
|
|
previousEntity = cloneEntityInstance(currentEntity);
|
2026-03-31 03:00:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.setDocument({
|
|
|
|
|
...currentDocument,
|
|
|
|
|
entities: {
|
|
|
|
|
...currentDocument.entities,
|
|
|
|
|
[nextEntity.id]: nextEntity
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
context.setSelection(setSinglePlayerStartSelection(nextEntity.id));
|
|
|
|
|
context.setToolMode("select");
|
|
|
|
|
},
|
|
|
|
|
undo(context) {
|
|
|
|
|
const currentDocument = context.getDocument();
|
|
|
|
|
const nextEntities = {
|
|
|
|
|
...currentDocument.entities
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (previousEntity === null) {
|
|
|
|
|
delete nextEntities[nextEntity.id];
|
|
|
|
|
} else {
|
|
|
|
|
nextEntities[nextEntity.id] = previousEntity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.setDocument({
|
|
|
|
|
...currentDocument,
|
|
|
|
|
entities: nextEntities
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (previousSelection !== null) {
|
|
|
|
|
context.setSelection(previousSelection);
|
|
|
|
|
}
|
2026-03-31 03:30:11 +02:00
|
|
|
|
|
|
|
|
if (previousToolMode !== null) {
|
|
|
|
|
context.setToolMode(previousToolMode);
|
|
|
|
|
}
|
2026-03-31 03:00:38 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|