Skip to content

Commit 1428fdb

Browse files
committed
AnimateVars - Major Refactor, everything works!
Update game_interpreter.cpp
1 parent 8f19da6 commit 1428fdb

File tree

1 file changed

+107
-180
lines changed

1 file changed

+107
-180
lines changed

src/game_interpreter.cpp

Lines changed: 107 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -5107,129 +5107,65 @@ int Game_Interpreter::ManiacBitmask(int value, int mask) const {
51075107

51085108
return value;
51095109
}
5110-
std::vector<double> parseBezier(const std::string& bezierParams) {
5111-
std::vector<double> params;
5112-
std::string temp;
5113-
size_t startPos = bezierParams.find("(") + 1;
5114-
size_t endPos = bezierParams.find(")");
5115-
std::string valuesString = bezierParams.substr(startPos, endPos - startPos);
51165110

5117-
size_t commaPos = valuesString.find(",");
5118-
while (commaPos != std::string::npos) {
5119-
temp = valuesString.substr(0, commaPos);
5120-
params.push_back(std::stod(temp));
5121-
valuesString = valuesString.substr(commaPos + 1);
5122-
commaPos = valuesString.find(",");
5123-
}
5124-
// Push the last value into the vector
5125-
params.push_back(std::stod(valuesString));
5126-
5127-
return params;
5128-
}
5129-
5130-
//FIXME: cubicBezier is completely Broken
5131-
// references to how it should work:
5111+
// references for cubic bezier:
51325112
// https://matthewlein.com/tools/ceaser
51335113
// https://cubic-bezier.com/
5114+
double cubicBezier(float t, const double& p0,const double& p1, const double& p2, const double& p3) {
51345115

5135-
double cubicBezier(double t, double p0, double p1, double p2, double p3) {
5136-
double u = 1 - t;
5137-
double tt = t * t;
5138-
double uu = u * u;
5139-
double uuu = uu * u;
5140-
double ttt = tt * t;
5116+
float u = 1 - t;
5117+
float tt = t * t;
5118+
float uu = u * u;
5119+
float uuu = uu * u;
5120+
float ttt = tt * t;
51415121

5142-
double p = uuu * p0; // (1-t)^3
5143-
double q = 3 * uu * t * p1; // 3t(1-t)^2
5144-
double r = 3 * u * tt * p2; // 3(1-t)t^2
5145-
double s = ttt * p3; // t^3
5122+
//Point2d p = {0,0};
5123+
//p.x = uuu * 0 + 3 * uu * t * p0 + 3 * u * tt * p2 + ttt * 1;
5124+
return uuu * 0 + 3 * uu * t * p1 + 3 * u * tt * p3 + ttt * 1;
51465125

5147-
return p + q + r + s;
5126+
//return p.y;
51485127
}
51495128

5150-
double getEasedT(const std::string& easingType, double t, double b, double c, double d) {
5151-
if (easingType == "linear") {
5152-
return c * t / d + b;
5153-
}
5154-
else if (easingType == "quadIn") {
5155-
t /= d;
5156-
return c * t * t + b;
5157-
}
5158-
else if (easingType == "quadOut") {
5159-
t /= d;
5160-
return -c * t * (t - 2) + b;
5161-
}
5162-
else if (easingType == "quadInOut") {
5163-
t /= d / 2;
5164-
if (t < 1) {
5165-
return c / 2 * t * t + b;
5166-
}
5167-
else {
5168-
t -= 1;
5169-
return -c / 2 * (t * (t - 2) - 1) + b;
5170-
}
5171-
}
5172-
else if (easingType == "cubicIn") {
5173-
t /= d;
5174-
return c * t * t * t + b;
5175-
}
5176-
else if (easingType == "cubicOut") {
5177-
t = (t / d) - 1;
5178-
return c * (t * t * t + 1) + b;
5179-
}
5180-
else if (easingType == "cubicInOut") {
5181-
t /= d / 2;
5182-
if (t < 1) {
5183-
return c / 2 * t * t * t + b;
5184-
}
5185-
else {
5186-
t -= 2;
5187-
return c / 2 * (t * t * t + 2) + b;
5188-
}
5189-
}
5190-
else if (easingType == "sinIn") {
5191-
return -c * cos(t / d * (M_PI / 2)) + c + b;
5192-
}
5193-
else if (easingType == "sinOut") {
5194-
return c * sin(t / d * (M_PI / 2)) + b;
5195-
}
5196-
else if (easingType == "sinInOut") {
5197-
return -c / 2 * (cos(M_PI * t / d) - 1) + b;
5198-
}
5199-
else if (easingType == "expoIn") {
5200-
return c * pow(2, 10 * (t / d - 1)) + b;
5201-
}
5202-
else if (easingType == "expoOut") {
5203-
return c * (-pow(2, -10 * t / d) + 1) + b;
5204-
}
5205-
else if (easingType == "expoInOut") {
5206-
t /= d / 2;
5207-
if (t < 1) {
5208-
return c / 2 * pow(2, 10 * (t - 1)) + b;
5209-
}
5210-
else {
5211-
t -= 1;
5212-
return c / 2 * (-pow(2, -10 * t) + 2) + b;
5213-
}
5214-
}
5215-
else if (easingType == "circIn") {
5216-
t /= d;
5217-
return -c * (sqrt(1 - t * t) - 1) + b;
5218-
}
5219-
else if (easingType == "circOut") {
5220-
t = (t / d) - 1;
5221-
return c * sqrt(1 - t * t) + b;
5222-
}
5223-
else if (easingType == "circInOut") {
5224-
t /= d / 2;
5225-
if (t < 1) {
5226-
return -c / 2 * (sqrt(1 - t * t) - 1) + b;
5227-
}
5228-
else {
5229-
t -= 2;
5230-
return c / 2 * (sqrt(1 - t * t) + 1) + b;
5231-
}
5232-
}
5129+
double getEasedTime(const std::string& easingType, double t, double b, double c, double d) {
5130+
if (easingType == "linear") return cubicBezier(t, 0.250, 0.250, 0.750, 0.750);
5131+
5132+
else if (easingType == "ease") return cubicBezier(t, 0.250, 0.100, 0.250, 1.000);
5133+
else if (easingType == "easeIn") return cubicBezier(t, 0.420, 0.000, 1.000, 1.000);
5134+
else if (easingType == "easeOut") return cubicBezier(t, 0.000, 0.000, 0.580, 1.000);
5135+
else if (easingType == "easeInOut") return cubicBezier(t, 0.420, 0.000, 0.580, 1.000);
5136+
5137+
else if (easingType == "quadIn") return cubicBezier(t, 0.550, 0.085, 0.680, 0.530);
5138+
else if (easingType == "quadOut") return cubicBezier(t, 0.250, 0.460, 0.450, 0.940);
5139+
else if (easingType == "quadInOut") return cubicBezier(t, 0.455, 0.030, 0.515, 0.955);
5140+
5141+
else if (easingType == "cubicIn") return cubicBezier(t, 0.550, 0.055, 0.675, 0.190);
5142+
else if (easingType == "cubicOut") return cubicBezier(t, 0.215, 0.610, 0.355, 1.000);
5143+
else if (easingType == "cubicInOut") return cubicBezier(t, 0.645, 0.045, 0.355, 1.000);
5144+
5145+
else if (easingType == "quartIn") return cubicBezier(t, 0.895, 0.030, 0.685, 0.220);
5146+
else if (easingType == "quartOut") return cubicBezier(t, 0.165, 0.840, 0.440, 1.000);
5147+
else if (easingType == "quartInOut") return cubicBezier(t, 0.770, 0.000, 0.175, 1.000);
5148+
5149+
else if (easingType == "quintIn") return cubicBezier(t, 0.755, 0.050, 0.855, 0.060);
5150+
else if (easingType == "quintOut") return cubicBezier(t, 0.230, 1.000, 0.320, 1.000);
5151+
else if (easingType == "quintInOut") return cubicBezier(t, 0.860, 0.000, 0.070, 1.000);
5152+
5153+
else if (easingType == "sineIn") return cubicBezier(t, 0.470, 0.000, 0.745, 0.715);
5154+
else if (easingType == "sineOut") return cubicBezier(t, 0.390, 0.575, 0.565, 1.000);
5155+
else if (easingType == "sineInOut") return cubicBezier(t, 0.445, 0.050, 0.550, 0.950);
5156+
5157+
else if (easingType == "ExpoIn") return cubicBezier(t, 0.950, 0.050, 0.795, 0.035);
5158+
else if (easingType == "expoOut") return cubicBezier(t, 0.190, 1.000, 0.220, 1.000);
5159+
else if (easingType == "expoInOut") return cubicBezier(t, 1.000, 0.000, 0.000, 1.000);
5160+
5161+
else if (easingType == "circIn") return cubicBezier(t, 0.600, 0.040, 0.980, 0.335);
5162+
else if (easingType == "circOut") return cubicBezier(t, 0.075, 0.820, 0.165, 1.000);
5163+
else if (easingType == "circInOut") return cubicBezier(t, 0.785, 0.135, 0.150, 0.860);
5164+
5165+
else if (easingType == "backIn") return cubicBezier(t, 0.600, -0.280, 0.735, 0.045);
5166+
else if (easingType == "backOut") return cubicBezier(t, 0.175, 0.885, 0.320, 1.275);
5167+
else if (easingType == "backInOut") return cubicBezier(t, 0.680, -0.550, 0.265, 1.550);
5168+
52335169
else if (easingType == "elasticIn") {
52345170
if (t == 0) {
52355171
return b;
@@ -5279,8 +5215,9 @@ double getEasedT(const std::string& easingType, double t, double b, double c, do
52795215
double postFix = a * pow(2, -10 * (t -= 1)); // this is a fix, again, with post-increment operators
52805216
return postFix * sin((t * d - s) * (2 * M_PI) / p) * 0.5 + c + b;
52815217
}
5218+
52825219
else if (easingType == "bounceIn") {
5283-
return c - getEasedT("bounceOut", d - t, 0, c, d) + b;
5220+
return c - getEasedTime("bounceOut", d - t, 0, c, d) + b;
52845221
}
52855222
else if (easingType == "bounceOut") {
52865223
if ((t /= d) < (1 / 2.75)) {
@@ -5301,65 +5238,30 @@ double getEasedT(const std::string& easingType, double t, double b, double c, do
53015238
}
53025239
else if (easingType == "bounceInOut") {
53035240
if (t < d / 2) {
5304-
return getEasedT("bounceIn", t * 2, 0, c, d) * 0.5 + b;
5241+
return getEasedTime("bounceIn", t * 2, 0, c, d) * 0.5 + b;
53055242
}
53065243
else {
5307-
return getEasedT("bounceOut", t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b;
5308-
}
5309-
}
5310-
if (easingType.substr(0, 6) == "bezier") {
5311-
std::vector < double > bezierParams = parseBezier(easingType.substr(7));
5312-
if (bezierParams.size() == 4) {
5313-
return cubicBezier(t / d, bezierParams[0], bezierParams[1], bezierParams[2], bezierParams[3]);
5244+
return getEasedTime("bounceOut", t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b;
53145245
}
53155246
}
53165247

5317-
return c * t / d + b; // Default to linear easing if the easing type is not recognized
5318-
}
5319-
5320-
std::vector<double> interpolate(double start, double end, double duration, const std::string& easingTypeAtStart, const std::string& easingTypeAtEnd) {
5321-
std::vector<double> interpolatedValues;
5322-
interpolatedValues.push_back(start);
5248+
if (easingType.substr(0, 6) == "bezier") {
5249+
std::vector<double> bezierParams;
53235250

5324-
// Calculate the number of steps based on the duration
5325-
int numSteps = static_cast<int>(duration); // Convert duration to an integer
5326-
double stepSize = 1.0 / numSteps;
5251+
size_t startPos = easingType.find("(") + 1;
5252+
size_t endPos = easingType.find(")");
5253+
std::string valuesString = easingType.substr(startPos, endPos - startPos);
53275254

5328-
// Calculate the halfway point
5329-
double halfway = start + (end - start) * 0.5;
5255+
std::istringstream iss(valuesString);
5256+
double value;
53305257

5331-
if (easingTypeAtEnd == "null") {
5332-
// Use easingTypeAtStart for the entire animation
5333-
for (int step = 1; step <= numSteps; ++step) {
5334-
double t = step * stepSize;
5335-
double easedT = getEasedT(easingTypeAtStart, t, 0, 1, 1); // Call getEasedT with appropriate parameters
5336-
double interpolatedValue = start + easedT * (end - start);
5337-
interpolatedValues.push_back(interpolatedValue);
5338-
}
5339-
}
5340-
else {
5341-
// Generate the first half of the interpolation
5342-
for (int step = 1; step <= numSteps / 2; ++step) {
5343-
double t = step * stepSize;
5344-
double normalizedT = t / 0.5; // Normalize the time for the first half
5345-
double easedT = getEasedT(easingTypeAtStart, normalizedT, 0, 1, 1); // Call getEasedT with appropriate parameters
5346-
double interpolatedValue = start + easedT * (halfway - start);
5347-
interpolatedValues.push_back(interpolatedValue);
5348-
}
5258+
while (iss >> value) bezierParams.push_back(value), iss.ignore();
53495259

5350-
// Generate the second half of the interpolation
5351-
for (int step = numSteps / 2 + 1; step <= numSteps; ++step) {
5352-
double t = step * stepSize;
5353-
double normalizedT = (t - 0.5) / 0.5; // Normalize the time for the second half
5354-
double easedT = getEasedT(easingTypeAtEnd, normalizedT, 0, 1, 1); // Call getEasedT with appropriate parameters
5355-
double interpolatedValue = halfway + easedT * (end - halfway);
5356-
interpolatedValues.push_back(interpolatedValue);
5357-
}
5260+
if (bezierParams.size() == 4)
5261+
return cubicBezier(t, bezierParams[0], bezierParams[1], bezierParams[2], bezierParams[3]);
53585262
}
53595263

5360-
interpolatedValues.push_back(end);
5361-
5362-
return interpolatedValues;
5264+
return c * t / d + b; // Default to linear easing if the easing type is not recognized
53635265
}
53645266

53655267
bool Game_Interpreter::CommandAnimateVariable(lcf::rpg::EventCommand const& com) {
@@ -5370,42 +5272,67 @@ bool Game_Interpreter::CommandAnimateVariable(lcf::rpg::EventCommand const& com)
53705272
int32_t end = ValueOrVariable(com.parameters[4], com.parameters[5]);
53715273
int32_t duration = ValueOrVariable(com.parameters[6], com.parameters[7]);
53725274

5275+
// Extract easing information
5276+
std::string easingTypeAtStart = ToString(com.string);
5277+
std::string easingTypeAtEnd = "null";
5278+
5279+
std::size_t pos = easingTypeAtStart.find('/');
5280+
5281+
if (pos != std::string::npos) {
5282+
easingTypeAtEnd = easingTypeAtStart.substr(pos + 1);
5283+
easingTypeAtStart = easingTypeAtStart.substr(0, pos);
5284+
}
5285+
53735286
// Prepare animation-related commands
53745287
lcf::rpg::EventCommand waitCom;
53755288
waitCom.code = int(Cmd::Wait);
53765289

53775290
lcf::rpg::EventCommand animatedCom;
53785291
animatedCom.code = int(Cmd::ControlVars);
53795292
std::vector<int32_t> animatedVarParams = { 0, static_cast<int32_t>(target), 0, 0, 0, static_cast<int32_t>(end) };
5380-
animatedCom.parameters = lcf::DBArray<int32_t>(animatedVarParams.begin(), animatedVarParams.end());
53815293

53825294
std::vector<lcf::rpg::EventCommand> cmdList;
53835295

5384-
// Extract easing information
5385-
std::string easeStart = ToString(com.string);
5386-
std::string easeEnd = "null";
5296+
int numSteps = static_cast<int>(duration);
5297+
double stepSize = 1.0 / numSteps;
53875298

5388-
std::size_t pos = easeStart.find('/');
5299+
for (int step = 1; step <= numSteps; ++step) {
5300+
double normalizedTime;
5301+
double currentTime = step * stepSize;
5302+
double halfway;
53895303

5390-
if (pos != std::string::npos) {
5391-
easeEnd = easeStart.substr(pos + 1);
5392-
easeStart = easeStart.substr(0, pos);
5393-
}
5304+
std::string easingType;
5305+
5306+
if (easingTypeAtEnd == "null") { // use a single interpolation.
5307+
normalizedTime = currentTime;
5308+
easingType = easingTypeAtStart;
5309+
halfway = (step <= numSteps / 2) ? end : start;
5310+
}
5311+
else {
5312+
if (step <= numSteps / 2) { // use 2 interpolations: start and end.
5313+
normalizedTime = currentTime / 0.5;
5314+
easingType = easingTypeAtStart;
5315+
}
5316+
else {
5317+
normalizedTime = (currentTime - 0.5) / 0.5;
5318+
easingType = easingTypeAtEnd;
5319+
}
5320+
halfway = start + 0.5 * (end - start);
5321+
}
5322+
5323+
double easedTime = getEasedTime(easingType, normalizedTime, 0, 1, 1);
53945324

5395-
// Insert animation commands
5396-
std::vector<double> interpolatedValues = interpolate(start, end, duration, easeStart, easeEnd);
5325+
double startValue = (step <= numSteps / 2) ? start : halfway;
5326+
double endValue = (step <= numSteps / 2) ? halfway : end;
5327+
double interpolatedValue = startValue + easedTime * (endValue - startValue);
53975328

5398-
// Insert animatedCom and waitCom commands for each interpolated value
5399-
for (int value : interpolatedValues) {
5400-
animatedVarParams.back() = value;
5329+
animatedVarParams.back() = interpolatedValue;
54015330
animatedCom.parameters = lcf::DBArray<int32_t>(animatedVarParams.begin(), animatedVarParams.end());
5402-
animatedCom.indent = com.indent + 1;
54035331

54045332
cmdList.push_back(animatedCom);
54055333
cmdList.push_back(waitCom);
54065334
}
54075335

5408-
// Update current_command index and return true to indicate success
54095336
Push(cmdList, 0, false);
54105337
return true;
54115338
}

0 commit comments

Comments
 (0)