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

70.83% Statements 17/24
50% Branches 1/2
66.67% Functions 4/6
69.57% Lines 16/23

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 12424x   24x           24x                                                                                                                                     24x                 24x 1x 1x     24x         1x 1x 1x 1x 1x           1x                               24x   24x  
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import type { EngineSchema } from "lib/project/loadEngineSchema";
import keyBy from "lodash/keyBy";
import { BaseCondition } 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";
 
export 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?: BaseCondition[];
  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 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 = 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 = action.payload.engineSchema.sceneTypes;
        state.consts = action.payload.engineSchema.consts;
        state.defaultEngineFieldId = getDefaultEngineFieldId(
          action.payload.engineSchema.fields,
        );
        state.loaded = true;
      }),
});
 
export const { actions, reducer } = engineSlice;
 
export default reducer;