Refactor set-player-start-command.ts to use upsert command
This commit is contained in:
@@ -1,10 +1,8 @@
|
|||||||
import { cloneEditorSelection, type EditorSelection } from "../core/selection";
|
|
||||||
import type { ToolMode } from "../core/tool-mode";
|
|
||||||
import type { Vec3 } from "../core/vector";
|
import type { Vec3 } from "../core/vector";
|
||||||
import { createOpaqueId } from "../core/ids";
|
import { createPlayerStartEntity } from "../entities/entity-instances";
|
||||||
import { cloneEntityInstance, createPlayerStartEntity } from "../entities/entity-instances";
|
|
||||||
|
|
||||||
import type { EditorCommand } from "./command";
|
import type { EditorCommand } from "./command";
|
||||||
|
import { createUpsertEntityCommand } from "./upsert-entity-command";
|
||||||
|
|
||||||
interface SetPlayerStartCommandOptions {
|
interface SetPlayerStartCommandOptions {
|
||||||
entityId?: string;
|
entityId?: string;
|
||||||
@@ -12,81 +10,13 @@ interface SetPlayerStartCommandOptions {
|
|||||||
yawDegrees: number;
|
yawDegrees: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setSinglePlayerStartSelection(entityId: string): EditorSelection {
|
|
||||||
return {
|
|
||||||
kind: "entities",
|
|
||||||
ids: [entityId]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createSetPlayerStartCommand(options: SetPlayerStartCommandOptions): EditorCommand {
|
export function createSetPlayerStartCommand(options: SetPlayerStartCommandOptions): EditorCommand {
|
||||||
const nextEntity = createPlayerStartEntity({
|
return createUpsertEntityCommand({
|
||||||
|
entity: createPlayerStartEntity({
|
||||||
id: options.entityId,
|
id: options.entityId,
|
||||||
position: options.position,
|
position: options.position,
|
||||||
yawDegrees: options.yawDegrees
|
yawDegrees: options.yawDegrees
|
||||||
|
}),
|
||||||
|
label: options.entityId === undefined ? "Place player start" : "Move player start"
|
||||||
});
|
});
|
||||||
|
|
||||||
let previousEntity = null as typeof nextEntity | null;
|
|
||||||
let previousSelection: EditorSelection | null = null;
|
|
||||||
let previousToolMode: ToolMode | null = null;
|
|
||||||
|
|
||||||
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) {
|
|
||||||
previousSelection = cloneEditorSelection(context.getSelection());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (previousToolMode === null) {
|
|
||||||
previousToolMode = context.getToolMode();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (previousEntity === null && currentEntity !== undefined) {
|
|
||||||
previousEntity = cloneEntityInstance(currentEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (previousToolMode !== null) {
|
|
||||||
context.setToolMode(previousToolMode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user