All files / src/lib/project/migration/versions 410to420.ts

100% Statements 65/65
93.1% Branches 27/29
100% Functions 11/11
100% Lines 58/58

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 18511x 11x             11x   11x     6x 5x 5x 80x 80x 80x   80x   80x         5x         1x     11x           11x     9x 5x   5x       5x         5x         4x     11x           11x     7x         4x   4x 4x         3x     11x 11x 7x   4x 1x           3x       7x 14x     7x       14x 13x             7x 7x   7x                 11x                 11x     5x     2x 2x 2x               11x     5x       2x 2x 2x 2x 2x         3x     11x                
import { TILE_SIZE } from "consts";
import {
  ScriptEventMigrationFn,
  ProjectResourcesMigration,
  createScriptEventsMigrator,
  ProjectResourcesMigrationFn,
  pipeMigrationFns,
} from "lib/project/migration/helpers";
import { ensureNumber } from "shared/types";
 
export const migrateFrom410r1To420r1Event: ScriptEventMigrationFn = (
  scriptEvent,
) => {
  if (scriptEvent.args && scriptEvent.command === "EVENT_SWITCH") {
    const args: Record<string, unknown> = { ...scriptEvent.args };
    for (let i = 0; i < 16; i++) {
      const key = `value${i}`;
      const defaultValue = i + 1;
      const storedValue = args[key];
      const value =
        typeof storedValue === "number" ? storedValue : defaultValue;
      // Convert to constvalue
      args[key] = {
        type: "number",
        value,
      };
    }
    return {
      ...scriptEvent,
      args,
    };
  }
  return scriptEvent;
};
 
export const migrate410r1To420r1: ProjectResourcesMigration = {
  from: { version: "4.1.0", release: "1" },
  to: { version: "4.2.0", release: "1" },
  migrationFn: createScriptEventsMigrator(migrateFrom410r1To420r1Event),
};
 
export const migrateFrom420r1To420r2Event: ScriptEventMigrationFn = (
  scriptEvent,
) => {
  if (scriptEvent.args && scriptEvent.command === "EVENT_WAIT") {
    const args: Record<string, unknown> = { ...scriptEvent.args };
    // Convert to constvalue
    args["frames"] = {
      type: "number",
      value: typeof args["frames"] === "number" ? args["frames"] : 30,
    };
    args["time"] = {
      type: "number",
      value: typeof args["time"] === "number" ? args["time"] : 0.5,
    };
 
    return {
      ...scriptEvent,
      args,
    };
  }
  return scriptEvent;
};
 
export const migrate420r1To420r2: ProjectResourcesMigration = {
  from: { version: "4.2.0", release: "1" },
  to: { version: "4.2.0", release: "2" },
  migrationFn: createScriptEventsMigrator(migrateFrom420r1To420r2Event),
};
 
export const migrateFrom420r2To420r3Event: ScriptEventMigrationFn = (
  scriptEvent,
) => {
  if (
    scriptEvent.args &&
    (scriptEvent.command === "EVENT_ACTOR_MOVE_TO" ||
      scriptEvent.command === "EVENT_ACTOR_MOVE_RELATIVE")
  ) {
    const args: Record<string, unknown> = { ...scriptEvent.args };
    // If useCollisions was set default to all collisions
    args["collideWith"] = args["useCollisions"] ? ["walls", "actors"] : [];
    return {
      ...scriptEvent,
      args,
    };
  }
  return scriptEvent;
};
 
export const migrateFrom420r2To420r3EngineFields: ProjectResourcesMigrationFn =
  (resources) => {
    const engineFieldValues = resources.engineFieldValues.engineFieldValues.map(
      (fieldValue) => {
        if (fieldValue.id === "shooter_scroll_speed") {
          return {
            ...fieldValue,
            value:
              typeof fieldValue.value === "number" ? fieldValue.value * 2 : 0,
          };
        }
        return fieldValue;
      },
    );
 
    const hasFieldValue = (id: string) => {
      return engineFieldValues.some((fieldValue) => fieldValue.id === id);
    };
 
    const setDefaultFieldValue = (
      id: string,
      value: string | number | undefined,
    ) => {
      if (!hasFieldValue(id)) {
        engineFieldValues.push({
          id: id,
          value,
        });
      }
    };
 
    setDefaultFieldValue("FEAT_PLATFORM_COYOTE_TIME", 0);
    setDefaultFieldValue("FEAT_PLATFORM_DROP_THROUGH", 0);
 
    return {
      ...resources,
      engineFieldValues: {
        ...resources.engineFieldValues,
        engineFieldValues,
      },
    };
  };
 
export const migrate420r2To420r3: ProjectResourcesMigration = {
  from: { version: "4.2.0", release: "2" },
  to: { version: "4.2.0", release: "3" },
  migrationFn: pipeMigrationFns([
    createScriptEventsMigrator(migrateFrom420r2To420r3Event),
    migrateFrom420r2To420r3EngineFields,
  ]),
};
 
export const migrateFrom420r3To420r4Sprites: ProjectResourcesMigrationFn = (
  resources,
) => {
  return {
    ...resources,
    sprites: resources.sprites.map((sprite) => {
      const currentBoundsY = sprite.boundsY || 0;
      const currentBoundsHeight = sprite.boundsHeight || 16;
      return {
        ...sprite,
        boundsY: TILE_SIZE - currentBoundsY - currentBoundsHeight,
      };
    }),
  };
};
 
export const migrateFrom420r3To420r4Event: ScriptEventMigrationFn = (
  scriptEvent,
) => {
  if (
    scriptEvent.args &&
    scriptEvent.command === "EVENT_ACTOR_SET_COLLISION_BOX"
  ) {
    const args: Record<string, unknown> = { ...scriptEvent.args };
    const currentBoundsY = ensureNumber(args["y"], 0);
    const currentBoundsHeight = ensureNumber(args["height"], 16);
    args["y"] = TILE_SIZE - currentBoundsY - currentBoundsHeight;
    return {
      ...scriptEvent,
      args,
    };
  }
  return scriptEvent;
};
 
export const migrate420r3To420r4: ProjectResourcesMigration = {
  from: { version: "4.2.0", release: "3" },
  to: { version: "4.2.0", release: "4" },
  migrationFn: pipeMigrationFns([
    migrateFrom420r3To420r4Sprites,
    createScriptEventsMigrator(migrateFrom420r3To420r4Event),
  ]),
};