Skip to content

Commit 8009138

Browse files
committed
AnimateVars - Refactor to use Interpreter's Push( ) command
1 parent b9b7b9b commit 8009138

File tree

1 file changed

+17
-45
lines changed

1 file changed

+17
-45
lines changed

src/game_interpreter.cpp

+17-45
Original file line numberDiff line numberDiff line change
@@ -5371,15 +5371,8 @@ std::vector<double> interpolate(double start, double end, double duration, const
53715371
}
53725372

53735373
bool Game_Interpreter::CommandAnimateVariable(lcf::rpg::EventCommand const& com) {
5374-
// CommandInterpolateVariable("typeStart/typeEnd",[useVarTarget, target, useVarStart, start, useVarEnd, end, useVarDuration, duration])
5374+
// $InterpolateVariable("typeStart/typeEnd",[useVarTarget, target, useVarStart, start, useVarEnd, end, useVarDuration, duration])
53755375

5376-
auto* frame = GetFramePtr();
5377-
const auto& list = frame->commands;
5378-
auto& index = frame->current_command;
5379-
5380-
int i = frame->current_command + 1;
5381-
5382-
// Extract parameters: target, start, end, and duration for the animation
53835376
int32_t target = ValueOrVariable(com.parameters[0], com.parameters[1]);
53845377
int32_t start = ValueOrVariable(com.parameters[2], com.parameters[3]);
53855378
int32_t end = ValueOrVariable(com.parameters[4], com.parameters[5]);
@@ -5389,13 +5382,12 @@ bool Game_Interpreter::CommandAnimateVariable(lcf::rpg::EventCommand const& com)
53895382
lcf::rpg::EventCommand waitCom;
53905383
waitCom.code = int(Cmd::Wait);
53915384

5392-
lcf::rpg::EventCommand updateVarCom;
5393-
updateVarCom.code = int(Cmd::ControlVars);
5394-
std::vector<int32_t> updateVarParams = { 0, static_cast<int32_t>(target), 0, 0, 0, static_cast<int32_t>(end) };
5395-
updateVarCom.parameters = lcf::DBArray<int32_t>(updateVarParams.begin(), updateVarParams.end());
5385+
lcf::rpg::EventCommand animatedCom;
5386+
animatedCom.code = int(Cmd::ControlVars);
5387+
std::vector<int32_t> animatedVarParams = { 0, static_cast<int32_t>(target), 0, 0, 0, static_cast<int32_t>(end) };
5388+
animatedCom.parameters = lcf::DBArray<int32_t>(animatedVarParams.begin(), animatedVarParams.end());
53965389

5397-
lcf::rpg::EventCommand branchCom;
5398-
branchCom.code = int(Cmd::ShowChoiceOption);
5390+
std::vector<lcf::rpg::EventCommand> cmdList;
53995391

54005392
// Extract easing information
54015393
std::string easeStart = ToString(com.string);
@@ -5408,41 +5400,21 @@ bool Game_Interpreter::CommandAnimateVariable(lcf::rpg::EventCommand const& com)
54085400
easeStart = easeStart.substr(0, pos);
54095401
}
54105402

5411-
// Check if new commands don't exist in the timeline yet
5412-
if (!(i < frame->commands.size() && frame->commands.at(i).code == int(Cmd::ShowChoiceOption))) {
5413-
// Insert animation commands
5414-
Output::Debug("inserting animation commands");
5415-
std::vector<double> interpolatedValues = interpolate(start, end, duration, easeStart, easeEnd);
5416-
5417-
// Insert ShowChoiceOption command
5418-
// This helps me isolating all the "keyframes" commands inside a nested commands, it also helps to avoid creating a repeated list.
5419-
// It's problematic when "start", "end" and "duration" are variables.
5420-
frame->commands.insert(frame->commands.begin() + i, branchCom);
5421-
i++;
5422-
5423-
// Insert updateVarCom and waitCom commands for each interpolated value
5424-
for (int value : interpolatedValues) {
5425-
updateVarParams.back() = value;
5426-
updateVarCom.parameters = lcf::DBArray<int32_t>(updateVarParams.begin(), updateVarParams.end());
5427-
updateVarCom.indent = com.indent + 1;
5403+
// Insert animation commands
5404+
std::vector<double> interpolatedValues = interpolate(start, end, duration, easeStart, easeEnd);
54285405

5429-
frame->commands.insert(frame->commands.begin() + i, updateVarCom);
5430-
i++;
5431-
frame->commands.insert(frame->commands.begin() + i, waitCom);
5432-
i++;
5433-
}
5406+
// Insert animatedCom and waitCom commands for each interpolated value
5407+
for (int value : interpolatedValues) {
5408+
animatedVarParams.back() = value;
5409+
animatedCom.parameters = lcf::DBArray<int32_t>(animatedVarParams.begin(), animatedVarParams.end());
5410+
animatedCom.indent = com.indent + 1;
54345411

5435-
// Insert ShowChoiceEnd command
5436-
branchCom.code = int(Cmd::ShowChoiceEnd);
5437-
frame->commands.insert(frame->commands.begin() + i, branchCom);
5438-
i++;
5439-
}
5440-
else {
5441-
Output::Debug("Animated Commands Already Exists");
5412+
cmdList.push_back(animatedCom);
5413+
cmdList.push_back(waitCom);
54425414
}
54435415

54445416
// Update current_command index and return true to indicate success
5445-
frame->current_command = index + 2;
5446-
return false;
5417+
Push(cmdList, 0, false);
5418+
return true;
54475419
}
54485420

0 commit comments

Comments
 (0)