-
Notifications
You must be signed in to change notification settings - Fork 57
/
copy-behaviors.ts
52 lines (45 loc) · 1.36 KB
/
copy-behaviors.ts
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
import { AnyAction } from "redux";
import { getBehavior } from "oni-save-parser";
import { OniSaveState, defaultOniSaveState, BehaviorCopyData } from "../state";
import { isCopyBehaviorsAction } from "../actions/copy-behaviors";
import {
gameObjectsByIdSelector,
gameObjectTypesByIdSelector
} from "../selectors/game-objects";
export default function copyBehaviorsReducer(
state: OniSaveState = defaultOniSaveState,
action: AnyAction
): OniSaveState {
if (!isCopyBehaviorsAction(action)) {
return state;
}
const { gameObjectId, behaviors } = action.payload;
const gameObjects = gameObjectsByIdSelector.local(state);
if (!gameObjects || !gameObjects[gameObjectId]) {
return state;
}
const gameObject = gameObjects[gameObjectId];
const typesById = gameObjectTypesByIdSelector.local(state);
if (!typesById || !typesById[gameObjectId]) {
return state;
}
const gameObjectType = typesById[gameObjectId];
const copyBehaviors: Record<string, BehaviorCopyData> = {};
for (const behaviorName of behaviors) {
const behavior = getBehavior(gameObject, behaviorName);
if (!behavior) {
continue;
}
copyBehaviors[behaviorName] = {
templateData: behavior.templateData,
extraData: behavior.extraData
};
}
return {
...state,
copyPasteData: {
gameObjectType,
behaviors: copyBehaviors
}
};
}