Skip to content

Commit

Permalink
Add value support to Actor Set Position
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaltby committed Apr 9, 2024
1 parent 9e5d21f commit f5f7ec8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
48 changes: 48 additions & 0 deletions src/lib/compiler/scriptBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2557,6 +2557,54 @@ extern void __mute_mask_${symbol};
this._addNL();
};

actorSetPositionToToScriptValues = (
actorId: string,
valueX: ScriptValue,
valueY: ScriptValue,
units: DistanceUnitType = "tiles"
) => {
const actorRef = this._declareLocal("actor", 4);
const stackPtr = this.stackPtr;
this._addComment("Actor Set Position");

const [rpnOpsX, fetchOpsX] = precompileScriptValue(
optimiseScriptValue(
multiplyScriptValueConst(valueX, (units === "tiles" ? 8 : 1) * 16)
),
"x"
);
const [rpnOpsY, fetchOpsY] = precompileScriptValue(
optimiseScriptValue(
multiplyScriptValueConst(valueY, (units === "tiles" ? 8 : 1) * 16)
),
"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(actorRef, 1));

// Y Value
this._performValueRPN(rpn, rpnOpsY, localsLookup);
rpn.refSet(this._localRef(actorRef, 2));

rpn.stop();
this._addComment(`-- Position Actor`);
this.actorSetById(actorId);
this._actorSetPosition(actorRef);
this._assertStackNeutral(stackPtr);
this._addNL();
};

actorSetPositionRelative = (
x = 0,
y = 0,
Expand Down
43 changes: 13 additions & 30 deletions src/lib/events/eventActorSetPosition.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,63 +28,46 @@ 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: 255,
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: 255,
width: "50%",
unitsField: "units",
unitsDefault: "tiles",
unitsAllowed: ["tiles", "pixels"],
defaultValue: {
number: 0,
variable: "LAST_VARIABLE",
property: "$self$:ypos",
type: "number",
value: 0,
},
},
],
},
];

const compile = (input, helpers) => {
const {
actorSetActive,
actorSetPosition,
actorSetPositionToVariables,
variableFromUnion,
temporaryEntityVariable,
} = helpers;
if (input.x.type === "number" && input.y.type === "number") {
// If all inputs are numbers use fixed implementation
actorSetActive(input.actorId);
actorSetPosition(input.x.value, input.y.value, 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));
actorSetActive(input.actorId);
actorSetPositionToVariables(xVar, yVar, input.units);
}
const { actorSetPositionToToScriptValues } = helpers;
actorSetPositionToToScriptValues(
input.actorId,
input.x,
input.y,
input.units
);
};

module.exports = {
Expand Down

0 comments on commit f5f7ec8

Please sign in to comment.