Add audio asset handling and import command

This commit is contained in:
2026-04-02 19:33:32 +02:00
parent a694679a40
commit 0cb7f93987
3 changed files with 301 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
import { compareEntityInstances, getEntityKindLabel, getEntityInstances, type EntityInstance } from "./entity-instances";
import type { ProjectAssetRecord } from "../assets/project-assets";
function getSortedEntitiesByKind(entities: Record<string, EntityInstance>, kind: EntityInstance["kind"]): EntityInstance[] {
return Object.values(entities)
@@ -6,27 +7,61 @@ function getSortedEntitiesByKind(entities: Record<string, EntityInstance>, kind:
.sort(compareEntityInstances);
}
export function getEntityDisplayLabel(entity: EntityInstance, entities: Record<string, EntityInstance>): string {
function getSoundEmitterLabelSuffix(entity: EntityInstance & { kind: "soundEmitter" }, assets?: Record<string, ProjectAssetRecord>): string {
if (entity.audioAssetId === null) {
return "No Audio Asset";
}
const asset = assets?.[entity.audioAssetId];
if (asset === undefined) {
return `Missing Audio Asset (${entity.audioAssetId})`;
}
if (asset.kind !== "audio") {
return `Invalid Audio Asset (${asset.sourceName})`;
}
return asset.sourceName;
}
export function getEntityDisplayLabel(
entity: EntityInstance,
entities: Record<string, EntityInstance>,
assets?: Record<string, ProjectAssetRecord>
): string {
const typedEntities = getSortedEntitiesByKind(entities, entity.kind);
const entityIndex = typedEntities.findIndex((candidate) => candidate.id === entity.id);
const baseLabel = getEntityKindLabel(entity.kind);
const numberedLabel = entityIndex <= 0 ? baseLabel : `${baseLabel} ${entityIndex + 1}`;
return entityIndex <= 0 ? baseLabel : `${baseLabel} ${entityIndex + 1}`;
if (entity.kind !== "soundEmitter" || assets === undefined) {
return numberedLabel;
}
return `${numberedLabel} · ${getSoundEmitterLabelSuffix(entity, assets)}`;
}
export function getEntityDisplayLabelById(entityId: string, entities: Record<string, EntityInstance>): string {
export function getEntityDisplayLabelById(
entityId: string,
entities: Record<string, EntityInstance>,
assets?: Record<string, ProjectAssetRecord>
): string {
const entity = entities[entityId];
if (entity === undefined) {
return "Entity";
}
return getEntityDisplayLabel(entity, entities);
return getEntityDisplayLabel(entity, entities, assets);
}
export function getSortedEntityDisplayLabels(entities: Record<string, EntityInstance>): Array<{ entity: EntityInstance; label: string }> {
export function getSortedEntityDisplayLabels(
entities: Record<string, EntityInstance>,
assets?: Record<string, ProjectAssetRecord>
): Array<{ entity: EntityInstance; label: string }> {
return getEntityInstances(entities).map((entity) => ({
entity,
label: getEntityDisplayLabel(entity, entities)
label: getEntityDisplayLabel(entity, entities, assets)
}));
}