From 4546c8e2a209ad6639e75988016578cfd2b3c3cf Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Thu, 13 Jan 2022 22:19:11 -0500 Subject: [PATCH] Started working on *simple* axis aligned collisions --- Slang/builtin.h | 15 ++++++++++++--- Slang/script.slg | 6 ++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Slang/builtin.h b/Slang/builtin.h index 11fc2c5..d312c58 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -18,14 +18,14 @@ using namespace std; using namespace boost; -vector types = { "int", "float", "string", "bool", "void", "null", "Sprite", "Vec2"}; +vector types = { "int", "float", "string", "bool", "void", "null", "Sprite", "Vec2" }; unordered_map>> builtinFunctionValues; unordered_map builtinVarVals; class NullType { public: - string type = "NULL"; + string type = "NULL"; }; boost::any nullType; @@ -131,7 +131,14 @@ boost::any EditClassSubComponent(boost::any value, string oper, boost::any other bool AxisAlignedCollision(const Sprite& a, const Sprite& b) { + bool colX = false; + if ((a.position.x + (a.scale.x / 2) <= b.position.x + (b.scale.x / 2)) && (a.position.x - (a.scale.x / 2) >= b.position.x - (b.scale.x / 2))) + colX = true; + bool colY = false; + if ((a.position.y + (a.scale.y / 2) <= b.position.y + (b.scale.y / 2)) && (a.position.y - (a.scale.y / 2) >= b.position.y - (b.scale.y / 2))) + colY = true; + return colX && colY; } // Initial script processing, which loads variables and functions from builtin @@ -235,8 +242,10 @@ boost::any CPPFunction(const string& name, const vector& args) return s; } else if (name == "CPP.Graphics.Draw") - { any_cast(args[0]).Draw(); + else if (name == "CPP.Physics.AxisAlignedCollision") + { + return AxisAlignedCollision(any_cast(args[0]), any_cast(args[1])); } else if (name == "CPP.Input.GetKey") return KEYS[StringRaw(any_cast(args[0]))] == 1; diff --git a/Slang/script.slg b/Slang/script.slg index c28b3da..d8f19a6 100644 --- a/Slang/script.slg +++ b/Slang/script.slg @@ -139,4 +139,10 @@ func HandleBallBounce() } } } +} + +func Colliding(a, b) +{ + bool b = CPP.Physics.AxisAlignedCollision(a, b) + return b } \ No newline at end of file