Skip to content

Commit 4c7930c

Browse files
committed
AnimateVars - Major Refactor, everything works!
Update game_interpreter.cpp
1 parent 8009138 commit 4c7930c

File tree

1 file changed

+107
-180
lines changed

1 file changed

+107
-180
lines changed

src/game_interpreter.cpp

+107-180
Original file line numberDiff line numberDiff line change
@@ -5115,129 +5115,65 @@ int Game_Interpreter::ManiacBitmask(int value, int mask) const {
51155115

51165116
return value;
51175117
}
5118-
std::vector<double> parseBezier(const std::string& bezierParams) {
5119-
std::vector<double> params;
5120-
std::string temp;
5121-
size_t startPos = bezierParams.find("(") + 1;
5122-
size_t endPos = bezierParams.find(")");
5123-
std::string valuesString = bezierParams.substr(startPos, endPos - startPos);
51245118

5125-
size_t commaPos = valuesString.find(",");
5126-
while (commaPos != std::string::npos) {
5127-
temp = valuesString.substr(0, commaPos);
5128-
params.push_back(std::stod(temp));
5129-
valuesString = valuesString.substr(commaPos + 1);
5130-
commaPos = valuesString.find(",");
5131-
}
5132-
// Push the last value into the vector
5133-
params.push_back(std::stod(valuesString));
5134-
5135-
return params;
5136-
}
5137-
5138-
//FIXME: cubicBezier is completely Broken
5139-
// references to how it should work:
5119+
// references for cubic bezier:
51405120
// https://matthewlein.com/tools/ceaser
51415121
// https://cubic-bezier.com/
5122+
double cubicBezier(float t, const double& p0,const double& p1, const double& p2, const double& p3) {
51425123

5143-
double cubicBezier(double t, double p0, double p1, double p2, double p3) {
5144-
double u = 1 - t;
5145-
double tt = t * t;
5146-
double uu = u * u;
5147-
double uuu = uu * u;
5148-
double ttt = tt * t;
5124+
float u = 1 - t;
5125+
float tt = t * t;
5126+
float uu = u * u;
5127+
float uuu = uu * u;
5128+
float ttt = tt * t;
51495129

5150-
double p = uuu * p0; // (1-t)^3
5151-
double q = 3 * uu * t * p1; // 3t(1-t)^2
5152-
double r = 3 * u * tt * p2; // 3(1-t)t^2
5153-
double s = ttt * p3; // t^3
5130+
//Point2d p = {0,0};
5131+
//p.x = uuu * 0 + 3 * uu * t * p0 + 3 * u * tt * p2 + ttt * 1;
5132+
return uuu * 0 + 3 * uu * t * p1 + 3 * u * tt * p3 + ttt * 1;
51545133

5155-
return p + q + r + s;
5134+
//return p.y;
51565135
}
51575136

5158-
double getEasedT(const std::string& easingType, double t, double b, double c, double d) {
5159-
if (easingType == "linear") {
5160-
return c * t / d + b;
5161-
}
5162-
else if (easingType == "quadIn") {
5163-
t /= d;
5164-
return c * t * t + b;
5165-
}
5166-
else if (easingType == "quadOut") {
5167-
t /= d;
5168-
return -c * t * (t - 2) + b;
5169-
}
5170-
else if (easingType == "quadInOut") {
5171-
t /= d / 2;
5172-
if (t < 1) {
5173-
return c / 2 * t * t + b;
5174-
}
5175-
else {
5176-
t -= 1;
5177-
return -c / 2 * (t * (t - 2) - 1) + b;
5178-
}
5179-
}
5180-
else if (easingType == "cubicIn") {
5181-
t /= d;
5182-
return c * t * t * t + b;
5183-
}
5184-
else if (easingType == "cubicOut") {
5185-
t = (t / d) - 1;
5186-
return c * (t * t * t + 1) + b;
5187-
}
5188-
else if (easingType == "cubicInOut") {
5189-
t /= d / 2;
5190-
if (t < 1) {
5191-
return c / 2 * t * t * t + b;
5192-
}
5193-
else {
5194-
t -= 2;
5195-
return c / 2 * (t * t * t + 2) + b;
5196-
}
5197-
}
5198-
else if (easingType == "sinIn") {
5199-
return -c * cos(t / d * (M_PI / 2)) + c + b;
5200-
}
5201-
else if (easingType == "sinOut") {
5202-
return c * sin(t / d * (M_PI / 2)) + b;
5203-
}
5204-
else if (easingType == "sinInOut") {
5205-
return -c / 2 * (cos(M_PI * t / d) - 1) + b;
5206-
}
5207-
else if (easingType == "expoIn") {
5208-
return c * pow(2, 10 * (t / d - 1)) + b;
5209-
}
5210-
else if (easingType == "expoOut") {
5211-
return c * (-pow(2, -10 * t / d) + 1) + b;
5212-
}
5213-
else if (easingType == "expoInOut") {
5214-
t /= d / 2;
5215-
if (t < 1) {
5216-
return c / 2 * pow(2, 10 * (t - 1)) + b;
5217-
}
5218-
else {
5219-
t -= 1;
5220-
return c / 2 * (-pow(2, -10 * t) + 2) + b;
5221-
}
5222-
}
5223-
else if (easingType == "circIn") {
5224-
t /= d;
5225-
return -c * (sqrt(1 - t * t) - 1) + b;
5226-
}
5227-
else if (easingType == "circOut") {
5228-
t = (t / d) - 1;
5229-
return c * sqrt(1 - t * t) + b;
5230-
}
5231-
else if (easingType == "circInOut") {
5232-
t /= d / 2;
5233-
if (t < 1) {
5234-
return -c / 2 * (sqrt(1 - t * t) - 1) + b;
5235-
}
5236-
else {
5237-
t -= 2;
5238-
return c / 2 * (sqrt(1 - t * t) + 1) + b;
5239-
}
5240-
}
5137+
double getEasedTime(const std::string& easingType, double t, double b, double c, double d) {
5138+
if (easingType == "linear") return cubicBezier(t, 0.250, 0.250, 0.750, 0.750);
5139+
5140+
else if (easingType == "ease") return cubicBezier(t, 0.250, 0.100, 0.250, 1.000);
5141+
else if (easingType == "easeIn") return cubicBezier(t, 0.420, 0.000, 1.000, 1.000);
5142+
else if (easingType == "easeOut") return cubicBezier(t, 0.000, 0.000, 0.580, 1.000);
5143+
else if (easingType == "easeInOut") return cubicBezier(t, 0.420, 0.000, 0.580, 1.000);
5144+
5145+
else if (easingType == "quadIn") return cubicBezier(t, 0.550, 0.085, 0.680, 0.530);
5146+
else if (easingType == "quadOut") return cubicBezier(t, 0.250, 0.460, 0.450, 0.940);
5147+
else if (easingType == "quadInOut") return cubicBezier(t, 0.455, 0.030, 0.515, 0.955);
5148+
5149+
else if (easingType == "cubicIn") return cubicBezier(t, 0.550, 0.055, 0.675, 0.190);
5150+
else if (easingType == "cubicOut") return cubicBezier(t, 0.215, 0.610, 0.355, 1.000);
5151+
else if (easingType == "cubicInOut") return cubicBezier(t, 0.645, 0.045, 0.355, 1.000);
5152+
5153+
else if (easingType == "quartIn") return cubicBezier(t, 0.895, 0.030, 0.685, 0.220);
5154+
else if (easingType == "quartOut") return cubicBezier(t, 0.165, 0.840, 0.440, 1.000);
5155+
else if (easingType == "quartInOut") return cubicBezier(t, 0.770, 0.000, 0.175, 1.000);
5156+
5157+
else if (easingType == "quintIn") return cubicBezier(t, 0.755, 0.050, 0.855, 0.060);
5158+
else if (easingType == "quintOut") return cubicBezier(t, 0.230, 1.000, 0.320, 1.000);
5159+
else if (easingType == "quintInOut") return cubicBezier(t, 0.860, 0.000, 0.070, 1.000);
5160+
5161+
else if (easingType == "sineIn") return cubicBezier(t, 0.470, 0.000, 0.745, 0.715);
5162+
else if (easingType == "sineOut") return cubicBezier(t, 0.390, 0.575, 0.565, 1.000);
5163+
else if (easingType == "sineInOut") return cubicBezier(t, 0.445, 0.050, 0.550, 0.950);
5164+
5165+
else if (easingType == "ExpoIn") return cubicBezier(t, 0.950, 0.050, 0.795, 0.035);
5166+
else if (easingType == "expoOut") return cubicBezier(t, 0.190, 1.000, 0.220, 1.000);
5167+
else if (easingType == "expoInOut") return cubicBezier(t, 1.000, 0.000, 0.000, 1.000);
5168+
5169+
else if (easingType == "circIn") return cubicBezier(t, 0.600, 0.040, 0.980, 0.335);
5170+
else if (easingType == "circOut") return cubicBezier(t, 0.075, 0.820, 0.165, 1.000);
5171+
else if (easingType == "circInOut") return cubicBezier(t, 0.785, 0.135, 0.150, 0.860);
5172+
5173+
else if (easingType == "backIn") return cubicBezier(t, 0.600, -0.280, 0.735, 0.045);
5174+
else if (easingType == "backOut") return cubicBezier(t, 0.175, 0.885, 0.320, 1.275);
5175+
else if (easingType == "backInOut") return cubicBezier(t, 0.680, -0.550, 0.265, 1.550);
5176+
52415177
else if (easingType == "elasticIn") {
52425178
if (t == 0) {
52435179
return b;
@@ -5287,8 +5223,9 @@ double getEasedT(const std::string& easingType, double t, double b, double c, do
52875223
double postFix = a * pow(2, -10 * (t -= 1)); // this is a fix, again, with post-increment operators
52885224
return postFix * sin((t * d - s) * (2 * M_PI) / p) * 0.5 + c + b;
52895225
}
5226+
52905227
else if (easingType == "bounceIn") {
5291-
return c - getEasedT("bounceOut", d - t, 0, c, d) + b;
5228+
return c - getEasedTime("bounceOut", d - t, 0, c, d) + b;
52925229
}
52935230
else if (easingType == "bounceOut") {
52945231
if ((t /= d) < (1 / 2.75)) {
@@ -5309,65 +5246,30 @@ double getEasedT(const std::string& easingType, double t, double b, double c, do
53095246
}
53105247
else if (easingType == "bounceInOut") {
53115248
if (t < d / 2) {
5312-
return getEasedT("bounceIn", t * 2, 0, c, d) * 0.5 + b;
5249+
return getEasedTime("bounceIn", t * 2, 0, c, d) * 0.5 + b;
53135250
}
53145251
else {
5315-
return getEasedT("bounceOut", t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b;
5316-
}
5317-
}
5318-
if (easingType.substr(0, 6) == "bezier") {
5319-
std::vector < double > bezierParams = parseBezier(easingType.substr(7));
5320-
if (bezierParams.size() == 4) {
5321-
return cubicBezier(t / d, bezierParams[0], bezierParams[1], bezierParams[2], bezierParams[3]);
5252+
return getEasedTime("bounceOut", t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b;
53225253
}
53235254
}
53245255

5325-
return c * t / d + b; // Default to linear easing if the easing type is not recognized
5326-
}
5327-
5328-
std::vector<double> interpolate(double start, double end, double duration, const std::string& easingTypeAtStart, const std::string& easingTypeAtEnd) {
5329-
std::vector<double> interpolatedValues;
5330-
interpolatedValues.push_back(start);
5256+
if (easingType.substr(0, 6) == "bezier") {
5257+
std::vector<double> bezierParams;
53315258

5332-
// Calculate the number of steps based on the duration
5333-
int numSteps = static_cast<int>(duration); // Convert duration to an integer
5334-
double stepSize = 1.0 / numSteps;
5259+
size_t startPos = easingType.find("(") + 1;
5260+
size_t endPos = easingType.find(")");
5261+
std::string valuesString = easingType.substr(startPos, endPos - startPos);
53355262

5336-
// Calculate the halfway point
5337-
double halfway = start + (end - start) * 0.5;
5263+
std::istringstream iss(valuesString);
5264+
double value;
53385265

5339-
if (easingTypeAtEnd == "null") {
5340-
// Use easingTypeAtStart for the entire animation
5341-
for (int step = 1; step <= numSteps; ++step) {
5342-
double t = step * stepSize;
5343-
double easedT = getEasedT(easingTypeAtStart, t, 0, 1, 1); // Call getEasedT with appropriate parameters
5344-
double interpolatedValue = start + easedT * (end - start);
5345-
interpolatedValues.push_back(interpolatedValue);
5346-
}
5347-
}
5348-
else {
5349-
// Generate the first half of the interpolation
5350-
for (int step = 1; step <= numSteps / 2; ++step) {
5351-
double t = step * stepSize;
5352-
double normalizedT = t / 0.5; // Normalize the time for the first half
5353-
double easedT = getEasedT(easingTypeAtStart, normalizedT, 0, 1, 1); // Call getEasedT with appropriate parameters
5354-
double interpolatedValue = start + easedT * (halfway - start);
5355-
interpolatedValues.push_back(interpolatedValue);
5356-
}
5266+
while (iss >> value) bezierParams.push_back(value), iss.ignore();
53575267

5358-
// Generate the second half of the interpolation
5359-
for (int step = numSteps / 2 + 1; step <= numSteps; ++step) {
5360-
double t = step * stepSize;
5361-
double normalizedT = (t - 0.5) / 0.5; // Normalize the time for the second half
5362-
double easedT = getEasedT(easingTypeAtEnd, normalizedT, 0, 1, 1); // Call getEasedT with appropriate parameters
5363-
double interpolatedValue = halfway + easedT * (end - halfway);
5364-
interpolatedValues.push_back(interpolatedValue);
5365-
}
5268+
if (bezierParams.size() == 4)
5269+
return cubicBezier(t, bezierParams[0], bezierParams[1], bezierParams[2], bezierParams[3]);
53665270
}
53675271

5368-
interpolatedValues.push_back(end);
5369-
5370-
return interpolatedValues;
5272+
return c * t / d + b; // Default to linear easing if the easing type is not recognized
53715273
}
53725274

53735275
bool Game_Interpreter::CommandAnimateVariable(lcf::rpg::EventCommand const& com) {
@@ -5378,42 +5280,67 @@ bool Game_Interpreter::CommandAnimateVariable(lcf::rpg::EventCommand const& com)
53785280
int32_t end = ValueOrVariable(com.parameters[4], com.parameters[5]);
53795281
int32_t duration = ValueOrVariable(com.parameters[6], com.parameters[7]);
53805282

5283+
// Extract easing information
5284+
std::string easingTypeAtStart = ToString(com.string);
5285+
std::string easingTypeAtEnd = "null";
5286+
5287+
std::size_t pos = easingTypeAtStart.find('/');
5288+
5289+
if (pos != std::string::npos) {
5290+
easingTypeAtEnd = easingTypeAtStart.substr(pos + 1);
5291+
easingTypeAtStart = easingTypeAtStart.substr(0, pos);
5292+
}
5293+
53815294
// Prepare animation-related commands
53825295
lcf::rpg::EventCommand waitCom;
53835296
waitCom.code = int(Cmd::Wait);
53845297

53855298
lcf::rpg::EventCommand animatedCom;
53865299
animatedCom.code = int(Cmd::ControlVars);
53875300
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());
53895301

53905302
std::vector<lcf::rpg::EventCommand> cmdList;
53915303

5392-
// Extract easing information
5393-
std::string easeStart = ToString(com.string);
5394-
std::string easeEnd = "null";
5304+
int numSteps = static_cast<int>(duration);
5305+
double stepSize = 1.0 / numSteps;
53955306

5396-
std::size_t pos = easeStart.find('/');
5307+
for (int step = 1; step <= numSteps; ++step) {
5308+
double normalizedTime;
5309+
double currentTime = step * stepSize;
5310+
double halfway;
53975311

5398-
if (pos != std::string::npos) {
5399-
easeEnd = easeStart.substr(pos + 1);
5400-
easeStart = easeStart.substr(0, pos);
5401-
}
5312+
std::string easingType;
5313+
5314+
if (easingTypeAtEnd == "null") { // use a single interpolation.
5315+
normalizedTime = currentTime;
5316+
easingType = easingTypeAtStart;
5317+
halfway = (step <= numSteps / 2) ? end : start;
5318+
}
5319+
else {
5320+
if (step <= numSteps / 2) { // use 2 interpolations: start and end.
5321+
normalizedTime = currentTime / 0.5;
5322+
easingType = easingTypeAtStart;
5323+
}
5324+
else {
5325+
normalizedTime = (currentTime - 0.5) / 0.5;
5326+
easingType = easingTypeAtEnd;
5327+
}
5328+
halfway = start + 0.5 * (end - start);
5329+
}
5330+
5331+
double easedTime = getEasedTime(easingType, normalizedTime, 0, 1, 1);
54025332

5403-
// Insert animation commands
5404-
std::vector<double> interpolatedValues = interpolate(start, end, duration, easeStart, easeEnd);
5333+
double startValue = (step <= numSteps / 2) ? start : halfway;
5334+
double endValue = (step <= numSteps / 2) ? halfway : end;
5335+
double interpolatedValue = startValue + easedTime * (endValue - startValue);
54055336

5406-
// Insert animatedCom and waitCom commands for each interpolated value
5407-
for (int value : interpolatedValues) {
5408-
animatedVarParams.back() = value;
5337+
animatedVarParams.back() = interpolatedValue;
54095338
animatedCom.parameters = lcf::DBArray<int32_t>(animatedVarParams.begin(), animatedVarParams.end());
5410-
animatedCom.indent = com.indent + 1;
54115339

54125340
cmdList.push_back(animatedCom);
54135341
cmdList.push_back(waitCom);
54145342
}
54155343

5416-
// Update current_command index and return true to indicate success
54175344
Push(cmdList, 0, false);
54185345
return true;
54195346
}

0 commit comments

Comments
 (0)