All files / src/store/features/engine engineState.ts

70.37% Statements 19/27
50% Branches 1/2
62.5% Functions 5/8
69.23% Lines 18/26

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 13240x   40x           40x                                                                                                                                     40x                 40x 1x 1x     40x 1x         40x         1x 1x 1x 1x 1x           1x                                   40x   40x  
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import type { EngineSchema } from "lib/project/loadEngineSchema";
import keyBy from "lodash/keyBy";
import { Condition } from "shared/lib/conditionsFilter";
import {
  CollisionExtraFlag,
  CollisionTileDef,
} from "shared/lib/resources/types";
import projectActions from "store/features/project/projectActions";
 
export type EngineFieldType =
  | "number"
  | "slider"
  | "checkbox"
  | "select"
  | "label"
  | "togglebuttons"
  | "mask"
  | "animationstate";
 
export type EngineFieldCType = "UBYTE" | "UWORD" | "BYTE" | "WORD" | "define";
 
type EngineFieldUnitsType =
  | "px"
  | "subpx"
  | "subpxVel"
  | "subpxAcc"
  | "subpxVelPrecise" // Extra precision - take top 8-bits as per frame movement
  | "subpxAccPrecise";
 
export type EngineFieldSchema = {
  key: string;
  sceneType?: string;
  label: string;
  description?: string;
  group: string;
  type: EngineFieldType;
  cType: EngineFieldCType;
  defaultValue: number | string | boolean | undefined;
  min?: number;
  max?: number;
  options?: [number, string][];
  file?: string;
  conditions?: Condition[];
  editUnits?: EngineFieldUnitsType;
  isHeading?: boolean;
  indent?: number;
  runtimeOnly?: boolean;
};
 
export type ExtraActorCollisionFlagDef = {
  key: string;
  label: string;
  description?: string;
  setFlag: CollisionExtraFlag;
  clearFlags?: CollisionExtraFlag[];
};
 
export type SceneTypeSchema = {
  key: string;
  label: string;
  files?: string[];
  collisionTiles?: CollisionTileDef[];
  extraActorCollisionFlags?: ExtraActorCollisionFlagDef[];
};
 
export interface EngineState {
  loaded: boolean;
  fields: EngineFieldSchema[];
  lookup: Record<string, EngineFieldSchema>;
  sceneTypes: SceneTypeSchema[];
  consts: Record<string, number>;
  defaultEngineFieldId: string;
}
 
export const initialState: EngineState = {
  loaded: false,
  fields: [],
  lookup: {},
  sceneTypes: [],
  consts: {},
  defaultEngineFieldId: "",
};
 
const getDefaultEngineFieldId = (fields: EngineFieldSchema[]) => {
  const field = fields.find((f) => f.cType !== "define");
  return field ? field.key : "";
};
 
const sortSceneTypes = (sceneTypes: SceneTypeSchema[]) => {
  return [...sceneTypes].sort((a, b) => {
    return a.label.localeCompare(b.label);
  });
};
 
const engineSlice = createSlice({
  name: "engine",
  initialState,
  reducers: {
    setEngineSchema: (state, action: PayloadAction<EngineSchema>) => {
      state.fields = action.payload.fields;
      state.lookup = keyBy(action.payload.fields, "key");
      state.sceneTypes = sortSceneTypes(action.payload.sceneTypes);
      state.consts = action.payload.consts;
      state.defaultEngineFieldId = getDefaultEngineFieldId(
        action.payload.fields,
      );
    },
  },
  extraReducers: (builder) =>
    builder
      .addCase(projectActions.loadProject.pending, (state, _action) => {
        state.loaded = false;
      })
      .addCase(projectActions.loadProject.fulfilled, (state, action) => {
        state.fields = action.payload.engineSchema.fields;
        state.lookup = keyBy(action.payload.engineSchema.fields, "key");
        state.sceneTypes = sortSceneTypes(
          action.payload.engineSchema.sceneTypes,
        );
        state.consts = action.payload.engineSchema.consts;
        state.defaultEngineFieldId = getDefaultEngineFieldId(
          action.payload.engineSchema.fields,
        );
        state.loaded = true;
      }),
});
 
export const { actions } = engineSlice;
 
export default engineSlice.reducer;