Skip to content

Commit

Permalink
Add value support to Camera Move To
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaltby committed Apr 9, 2024
1 parent 2c9e844 commit ed03afb
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 31 deletions.
63 changes: 63 additions & 0 deletions src/lib/compiler/scriptBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
precompileScriptValue,
sortFetchOperations,
multiplyScriptValueConst,
addScriptValueConst,
} from "shared/lib/scriptValue/helpers";

export type ScriptOutput = string[];
Expand Down Expand Up @@ -3547,6 +3548,68 @@ extern void __mute_mask_${symbol};
this._stackPop(2);
};

cameraMoveToScriptValues = (
valueX: ScriptValue,
valueY: ScriptValue,
speed = 0,
units: DistanceUnitType = "tiles"
) => {
const cameraMoveArgsRef = this._declareLocal("camera_move_args", 2, true);
const xOffset = 80;
const yOffset = 72;

const stackPtr = this.stackPtr;
this._addComment("Camera Move To");

const [rpnOpsX, fetchOpsX] = precompileScriptValue(
optimiseScriptValue(
addScriptValueConst(
multiplyScriptValueConst(valueX, units === "tiles" ? 8 : 1),
xOffset
)
),
"x"
);
const [rpnOpsY, fetchOpsY] = precompileScriptValue(
optimiseScriptValue(
addScriptValueConst(
multiplyScriptValueConst(valueY, units === "tiles" ? 8 : 1),
yOffset
)
),
"y"
);

const localsLookup = this._performFetchOperations([
...fetchOpsX,
...fetchOpsY,
]);

const rpn = this._rpn();

this._addComment(`-- Calculate coordinate values`);

// X Value
this._performValueRPN(rpn, rpnOpsX, localsLookup);
rpn.refSet(this._localRef(cameraMoveArgsRef, 0));

// Y Value
this._performValueRPN(rpn, rpnOpsY, localsLookup);
rpn.refSet(this._localRef(cameraMoveArgsRef, 1));

rpn.stop();

this._addComment(`-- Move Camera`);
if (speed === 0) {
this._cameraSetPos(".ARG1");
} else {
this._cameraMoveTo(".ARG1", speed, ".CAMERA_UNLOCK");
}

this._assertStackNeutral(stackPtr);
this._addNL();
};

cameraLock = (speed = 0, axis: ScriptBuilderAxis[]) => {
const actorRef = this._declareLocal("actor", 4);
this._addComment("Camera Lock");
Expand Down
39 changes: 8 additions & 31 deletions src/lib/events/eventCameraMoveTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,32 @@ const fields = [
key: "x",
label: l10n("FIELD_X"),
description: l10n("FIELD_X_DESC"),
type: "union",
types: ["number", "variable", "property"],
defaultType: "number",
type: "value",
min: 0,
max: 2047,
width: "50%",
unitsField: "units",
unitsDefault: "tiles",
unitsAllowed: ["tiles", "pixels"],
defaultValue: {
number: 0,
variable: "LAST_VARIABLE",
property: "$self$:xpos",
type: "number",
value: 0,
},
},
{
key: "y",
label: l10n("FIELD_Y"),
description: l10n("FIELD_Y_DESC"),
type: "union",
types: ["number", "variable", "property"],
defaultType: "number",
type: "value",
min: 0,
max: 2047,
width: "50%",
unitsField: "units",
unitsDefault: "tiles",
unitsAllowed: ["tiles", "pixels"],
defaultValue: {
number: 0,
variable: "LAST_VARIABLE",
property: "$self$:ypos",
type: "number",
value: 0,
},
},
],
Expand All @@ -67,25 +61,8 @@ const fields = [
];

const compile = (input, helpers) => {
const {
cameraMoveTo,
cameraMoveToVariables,
variableFromUnion,
temporaryEntityVariable,
} = helpers;
if (input.x.type === "number" && input.y.type === "number") {
cameraMoveTo(
input.x.value,
input.y.value,
Number(input.speed),
input.units
);
} else {
// If any value is not a number transfer values into variables and use variable implementation
const xVar = variableFromUnion(input.x, temporaryEntityVariable(0));
const yVar = variableFromUnion(input.y, temporaryEntityVariable(1));
cameraMoveToVariables(xVar, yVar, Number(input.speed), input.units);
}
const { cameraMoveToScriptValues } = helpers;
cameraMoveToScriptValues(input.x, input.y, Number(input.speed), input.units);
};

module.exports = {
Expand Down
14 changes: 14 additions & 0 deletions src/shared/lib/scriptValue/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,17 @@ export const multiplyScriptValueConst = (
},
};
};

export const addScriptValueConst = (
value: ScriptValue,
num: number
): ScriptValue => {
return {
type: "add",
valueA: value,
valueB: {
type: "number",
value: num,
},
};
};

0 comments on commit ed03afb

Please sign in to comment.