All files / src/lib/compiler applyPrefabs.ts

100% Statements 26/26
100% Branches 4/4
100% Functions 6/6
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 732x     2x   2x     8x 8x   8x   12x     8x 8x 6x   2x     2x 2x 1x   1x               2x               6x 6x 4x   2x     2x 2x 1x   1x               2x                    
import { keyBy } from "lodash";
import { ScriptEvent } from "shared/lib/entities/entitiesTypes";
import { ProjectResources } from "shared/lib/resources/types";
import { mapActorScript, mapTriggerScript } from "shared/lib/scripts/walk";
 
export const applyPrefabs = (
  projectData: ProjectResources
): ProjectResources => {
  const actorPrefabsLookup = keyBy(projectData.actorPrefabs, "id");
  const triggerPrefabsLookup = keyBy(projectData.triggerPrefabs, "id");
 
  return {
    ...projectData,
    scenes: projectData.scenes.map((scene) => ({
      ...scene,
      actors: scene.actors.map((actor) => {
        const prefab = actorPrefabsLookup[actor.prefabId];
        if (!prefab) {
          return actor;
        }
        const applyScriptEventOverrides = (
          scriptEvent: ScriptEvent
        ): ScriptEvent => {
          const override = actor.prefabScriptOverrides[scriptEvent.id];
          if (!override) {
            return scriptEvent;
          }
          return {
            ...scriptEvent,
            args: {
              ...scriptEvent.args,
              ...override.args,
            },
          };
        };
        return {
          ...actor,
          ...mapActorScript(prefab, applyScriptEventOverrides),
          _resourceType: actor._resourceType,
          id: actor.id,
        };
      }),
      triggers: scene.triggers.map((trigger) => {
        const prefab = triggerPrefabsLookup[trigger.prefabId];
        if (!prefab) {
          return trigger;
        }
        const applyScriptEventOverrides = (
          scriptEvent: ScriptEvent
        ): ScriptEvent => {
          const override = trigger.prefabScriptOverrides[scriptEvent.id];
          if (!override) {
            return scriptEvent;
          }
          return {
            ...scriptEvent,
            args: {
              ...scriptEvent.args,
              ...override.args,
            },
          };
        };
        return {
          ...trigger,
          ...mapTriggerScript(prefab, applyScriptEventOverrides),
          _resourceType: trigger._resourceType,
          id: trigger.id,
        };
      }),
    })),
  };
};