Skip to content

Commit 0f98189

Browse files
committed
AnimateVars - Major Refactor, everything works!
Update game_interpreter.cpp
1 parent 44889bf commit 0f98189

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
@@ -5114,129 +5114,65 @@ int Game_Interpreter::ManiacBitmask(int value, int mask) const {
51145114

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

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

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

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

5154-
return p + q + r + s;
5133+
//return p.y;
51555134
}
51565135

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

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

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

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

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

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

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

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

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

53845297
lcf::rpg::EventCommand animatedCom;
53855298
animatedCom.code = int(Cmd::ControlVars);
53865299
std::vector<int32_t> animatedVarParams = { 0, static_cast<int32_t>(target), 0, 0, 0, static_cast<int32_t>(end) };
5387-
animatedCom.parameters = lcf::DBArray<int32_t>(animatedVarParams.begin(), animatedVarParams.end());
53885300

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

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

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

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

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

5405-
// Insert animatedCom and waitCom commands for each interpolated value
5406-
for (int value : interpolatedValues) {
5407-
animatedVarParams.back() = value;
5336+
animatedVarParams.back() = interpolatedValue;
54085337
animatedCom.parameters = lcf::DBArray<int32_t>(animatedVarParams.begin(), animatedVarParams.end());
5409-
animatedCom.indent = com.indent + 1;
54105338

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

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

0 commit comments

Comments
 (0)