diff --git a/Slang/Main.cpp b/Slang/Main.cpp index d2fad70..f63ddbc 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -58,7 +58,7 @@ bool IsVar(const string& varName, const unordered_map& varia return false; } -vector VarValues(const vector& varNames, const unordered_map& variableValues) +vector VarValues(const vector& varNames, unordered_map& variableValues) { vector realValues; @@ -66,7 +66,8 @@ vector VarValues(const vector& varNames, const unordered_map { string varName = trim(varNames[varIndex]); - auto iA = variableValues.find(varName); + realValues.push_back(EvalExpression(varName, variableValues)); + /*auto iA = variableValues.find(varName); if (iA != variableValues.end()) { realValues.push_back(iA->second); @@ -78,7 +79,7 @@ vector VarValues(const vector& varNames, const unordered_map realValues.push_back(iB->second); else realValues.push_back(varName); - } + }*/ } return realValues; @@ -267,34 +268,14 @@ int varOperation(const vector& str, unordered_map& v { if (IsVar(split(str[0], '.')[0], variableValues)) { - if (str[1] == "=") - EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); - else if (str[1] == "+=") - EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); - else if (str[1] == "-=") - EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); - else if (str[1] == "*=") - EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); - else if (str[1] == "/=") - EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); - else - LogWarning("unrecognized operator \'" + str[1] + "\'"); + //InterpreterLog(unWrapVec(vector(str.begin() + 2, str.end()))); + variableValues[split(str[0], '.')[0]] = EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); return 0; } else if (IsVar(split(str[0], '.')[0], globalVariableValues)) { - if (str[1] == "=") - EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); - else if (str[1] == "+=") - EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); - else if (str[1] == "-=") - EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); - else if (str[1] == "*=") - EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); - else if (str[1] == "/=") - EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); - else - LogWarning("unrecognized operator \'" + str[1] + "\'"); + //InterpreterLog(unWrapVec(vector(str.begin() + 2, str.end()))); + globalVariableValues[split(str[0], '.')[0]] = EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); return 0; } } diff --git a/Slang/builtin.h b/Slang/builtin.h index c26ce36..5a22bff 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -30,6 +30,18 @@ public: boost::any nullType; +float clamp(float v, float min, float max) +{ + if (v < min) return min; + if (v > max) return max; + return v; +} + +float lerp(float a, float b, float f) +{ + return a + f * (b - a); +} + int LogWarning(const string& warningText) { cerr << "\x1B[33mWARNING: " << warningText << "\033[0m\t\t" << endl; @@ -203,6 +215,8 @@ boost::any CPPFunction(const string& name, const vector& args) return tan(AnyAsFloat(args[0])); else if (name == "CPP.Math.Round") return AnyAsInt(args[0]); + else if (name == "CPP.Math.Lerp") + return lerp(AnyAsFloat(args[0]), AnyAsFloat(args[1]), AnyAsFloat(args[2])); else if (name == "CPP.Graphics.Init") { InterpreterLog("Init graphics"); diff --git a/Slang/builtin.slg b/Slang/builtin.slg index 4814a1a..3f5284d 100644 --- a/Slang/builtin.slg +++ b/Slang/builtin.slg @@ -45,6 +45,13 @@ func Round(input) return out } +// Linearly interpolates between a and b by t +func Lerp(a, b, t) +{ + float out = CPP.Math.Lerp(a, b, t) + return out +} + // Clamps input between min and max func Clamp(input, min, max) { @@ -102,6 +109,5 @@ func NVec2(x, y) func GetKey(keyName) { bool b = CPP.Input.GetKey(keyName) - print b return b } diff --git a/Slang/graphics.h b/Slang/graphics.h index 5a1b1a9..c5506a4 100644 --- a/Slang/graphics.h +++ b/Slang/graphics.h @@ -19,8 +19,10 @@ #include #include #include +#include using namespace std; +using namespace boost; int WINDOW_WIDTH = 1280; int WINDOW_HEIGHT = 720; @@ -212,7 +214,7 @@ public: return y; } - int EditSubComponent(std::string componentName, std::string oper, boost::any otherVal) + Vec2 EditSubComponent(std::string componentName, std::string oper, boost::any otherVal) { if (componentName == "x") { @@ -240,6 +242,7 @@ public: else if (oper == "/=") y /= AnyAsFloat(otherVal); } + return *this; } float x, y; @@ -381,7 +384,7 @@ public: return position.y; } - int EditSubComponent(std::string componentName, std::string oper, boost::any otherVal) + Sprite EditSubComponent(std::string componentName, std::string oper, boost::any otherVal) { if (componentName == "position") { @@ -422,6 +425,7 @@ public: else if (oper == "/=") position.y /= AnyAsFloat(otherVal); } + return *this; } Vec2 position; @@ -706,14 +710,14 @@ int updateLoop() SDL_RenderClear(gRenderer); - ExecuteFunction("Update", vector {}); + ExecuteFunction("Update", vector {dt}); // Present the backbuffer SDL_RenderPresent(gRenderer); // Calculate frame time auto stopTime = std::chrono::high_resolution_clock::now(); - dt = std::chrono::duration(stopTime - startTime).count(); + dt = std::chrono::duration(stopTime - startTime).count() / 1000.0f; } return 0; diff --git a/Slang/main.h b/Slang/main.h index afcb5e1..c0af115 100644 --- a/Slang/main.h +++ b/Slang/main.h @@ -5,5 +5,6 @@ using namespace std; boost::any ExecuteFunction(const string& functionName, const vector& inputVarVals); +boost::any EvalExpression(const string& ex, unordered_map& variableValues); #endif diff --git a/Slang/script.slg b/Slang/script.slg index 402d18d..13724d0 100644 --- a/Slang/script.slg +++ b/Slang/script.slg @@ -1,9 +1,10 @@ -int SCREENW = 780 -int SCREENH = 500 +int SCREENW = 900 +int SCREENH = 600 int scoreOne = 0 int scoreTwo = 0 -Vec2 paddleScale = NVec2(16, 70) + +float paddleMoveSpeed = 200 func Main(input, in) { @@ -22,29 +23,47 @@ func Start() float yPosPaddle = yPosBall - paddleScale.y / 2 Vec2 lPaddlePosition = NVec2(15, yPosPaddle) + global Vec2 lPaddleTargetPosition = NVec2(15, yPosPaddle) float rOffset = SCREENW - 15 Vec2 rPaddlePosition = NVec2(rOffset, yPosPaddle) + global Vec2 rPaddleTargetPosition = NVec2(15, yPosPaddle) global Sprite ballSprite = CPP.Graphics.Sprite("./square.png", ballPosition, ballScale, 0) global Sprite lPaddle = CPP.Graphics.Sprite("./square.png", lPaddlePosition, paddleScale, 0) global Sprite rPaddle = CPP.Graphics.Sprite("./square.png", rPaddlePosition, paddleScale, 0) } -func Update() +func Update(deltaTime) { - Vec2 as = NVec2(16, 70) - print "updating" - + //print deltaTime if GetKey("W") == true { - print "W" - as.x += 4 - // lPaddle.position.y += 1 + float newX = lPaddle.position.x + // Subtract from Y to move up, because vertical coordinates are reversed + float newY = lPaddleTargetPosition.y - paddleMoveSpeed * deltaTime + newY = Clamp(newY, 0, SCREENH - 70) + lPaddleTargetPosition = NVec2(newX, newY) } + if GetKey("S") == true + { + float newX = lPaddle.position.x + // Add to Y to move down, because vertical coordinates are reversed + float newY = lPaddleTargetPosition.y + paddleMoveSpeed * deltaTime + newY = Clamp(newY, 0, SCREENH - 70) + lPaddleTargetPosition = NVec2(newX, newY) + } + // Lerps from old position to destination smoothly + float oldY = lPaddle.position.y + float stopSpeed = deltaTime * 15 + float newY = lPaddleTargetPosition.y + float lerpedY = Lerp(oldY, newY, stopSpeed) + print "0 < " + newY + " < " + SCREENH + lPaddle.position = NVec2(newX, lerpedY) - print as.x + " , " + as.y + + // Finally draws all of the sprites CPP.Graphics.Draw(ballSprite) CPP.Graphics.Draw(lPaddle) CPP.Graphics.Draw(rPaddle)