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

62.5% Statements 10/16
100% Branches 0/0
40% Functions 2/5
62.5% Lines 10/16

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 7322x 22x   22x                                                                   22x             22x         1x 1x             1x                       22x   22x  
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import keyBy from "lodash/keyBy";
import { CollisionTileDef } from "shared/lib/resources/types";
import projectActions from "store/features/project/projectActions";
 
export type EngineFieldType = "number" | "slider" | "checkbox" | "select";
 
export type EngineFieldCType = "UBYTE" | "UWORD" | "BYTE" | "WORD" | "define";
 
export type EngineFieldSchema = {
  key: string;
  sceneType?: string;
  label: string;
  group: string;
  type: EngineFieldType;
  cType: EngineFieldCType;
  defaultValue: number | string | boolean | undefined;
  min?: number;
  max?: number;
  options?: [number, string][];
  file?: string;
};
 
export type SceneTypeSchema = {
  key: string;
  label: string;
  files?: string[];
  collisionTiles?: CollisionTileDef[];
};
 
export interface EngineState {
  loaded: boolean;
  fields: EngineFieldSchema[];
  lookup: Record<string, EngineFieldSchema>;
  sceneTypes: SceneTypeSchema[];
}
 
export const initialState: EngineState = {
  loaded: false,
  fields: [],
  lookup: {},
  sceneTypes: [],
};
 
const engineSlice = createSlice({
  name: "engine",
  initialState,
  reducers: {
    setEngineFields: (state, action: PayloadAction<EngineFieldSchema[]>) => {
      state.fields = action.payload;
      state.lookup = keyBy(action.payload, "key");
    },
    setScenetypes: (state, action: PayloadAction<SceneTypeSchema[]>) => {
      state.sceneTypes = action.payload;
    },
  },
  extraReducers: (builder) =>
    builder
      .addCase(projectActions.loadProject.pending, (state, _action) => {
        state.loaded = false;
      })
      .addCase(projectActions.loadProject.fulfilled, (state, action) => {
        state.fields = action.payload.engineFields;
        state.lookup = keyBy(action.payload.engineFields, "key");
        state.sceneTypes = action.payload.sceneTypes;
        state.loaded = true;
      }),
});
 
export const { actions, reducer } = engineSlice;
 
export default reducer;