local setmetatable=setmetatable local getmetatable=getmetatable local type=type local clamp=clamp local acos=math.acos local sin=math.sin local cos=math.cos local sqrt=math.sqrt local error=error local min=math.min local max=math.max local abs=math.abs local pow=math.pow local Time=UnityEngine.Time local ToAngle=57.29578 local ToRad=0.01745329 local Epsilon=0.00001 local Infinite=1/0 local Sqrt2=0.7071067811865475244008443621048490 local PI=3.14159265358979323846264338327950 local destroy = ReusableTable.DestroyVector2 local function clamp(v,min,max) return v>max and max or (vEpsilon then v[1],v[2]=v[1]/m,v[2]/m else v:Set(0,0) end return v end function LuaVector2.Normalize(v) local v=LuaVector2.Clone(v) LuaVector2.Normalized(v) return v end function LuaVector2.ClampMagnitude(vector,maxLength) if vector.sqrMagnitude > (maxLength^2) then return vector.normalized * maxLength end return LuaVector2.Clone(vector) end function LuaVector2.ClampMagnitudeQuick(target,vector,maxLength) if vector.sqrMagnitude > (maxLength^2) then local m = maxLength / vector.magnitude target:Set(vector[1]*m,vector[2]*m) else target:Set(vector[1],vector[2]) end return target end function LuaVector2.Dot(a,b) return a[1]*b[1] + a[2]*b[2] end function LuaVector2.Distance(a,b) local x = a[1]-b[1] local y = a[2]-b[2] return math.sqrt(x ^ 2 + y ^ 2) end function LuaVector2.Lerp(a,b,t) t = clamp(t,0,1) return LuaVector2.New(a[1]+(b[1]-a[1])*t ,a[2]+(b[2]-a[2])*t ) end function LuaVector2.Better_Lerp(a,b,c,t) t = clamp(t,0,1) c[1],c[2] = a[1]+(b[1]-a[1])*t,a[2]+(b[2]-a[2])*t return c end function I:LerpTo(b,t) t = clamp(t,0,1) self[1],self[2]= self[1]+(b[1]-self[1])*t,self[2]+(b[2]-self[2])*t end function LuaVector2.LerpUnclamped(a,b,t) return LuaVector2.New(a[1]+(b[1]-a[1])*t ,a[2]+(b[2]-a[2])*t ) end function LuaVector2.Better_LerpUnclamped(a,b,c,t) c[1],c[2] = a[1]+(b[1]-a[1])*t,a[2]+(b[2]-a[2])*t return c end function I:LerpUnclampedTo(b,t) self[1],self[2]= self[1]+(b[1]-self[1])*t,self[2]+(b[2]-self[2])*t end function LuaVector2.Min(a,b) return LuaVector2.New(min(a[1],b[1]),min(a[2],b[2])) end function LuaVector2.Better_Min(a,b,c) c[1],c[2] = min(a[1],b[1]),min(a[2],b[2]) return c end function LuaVector2.Max(a,b) return LuaVector2.New(max(a[1],b[1]),max(a[2],b[2])) end function LuaVector2.Better_Max(a,b,c) c[1],c[2] = max(a[1],b[1]),max(a[2],b[2]) return c end function LuaVector2.MoveTowards(a,b,adv) local v = b - a local m = sqrt(v[1]^2+v[2]^2) if m>adv and m~=0 then v[1],v[2] = v[1]/m,v[2]/m v[1],v[2] = v[1]*adv,v[2]*adv v[1],v[2] = v[1]+a[1],v[2]+a[2] return v end return LuaVector2.Clone(b) end local towardHelper = LuaVector2.New(0,0,0) function LuaVector2.SelfMoveTowards(self,b,adv) LuaVector2.Better_Sub(b,self,towardHelper) local m = sqrt(towardHelper[1]^2+towardHelper[2]^2) if m>adv and m~=0 then towardHelper[1],towardHelper[2] = towardHelper[1]/m,towardHelper[2]/m towardHelper[1],towardHelper[2] = towardHelper[1]*adv,towardHelper[2]*adv self[1],self[2] = towardHelper[1]+self[1],towardHelper[2]+self[2] return self end self[1],self[2] = b[1],b[2] return self end function LuaVector2.Better_MoveTowards(a,b,t,adv) LuaVector2.Better_Sub(b,a,t) local m = sqrt(t[1]^2+t[2]^2) if m>adv and m~=0 then t[1],t[2] = t[1]/m,t[2]/m t[1],t[2] = t[1]*adv,t[2]*adv t[1],t[2] = t[1]+a[1],t[2]+a[2] return t end t[1],t[2] = b[1],b[2] return t end local cSmoothDamp = VectorHelper.Vector2SmoothDamp function LuaVector2._SmoothDamp(current,target,currentVelocity,smoothTime,maxSpeed,deltaTime) if(maxSpeed) then if(deltaTime) then return cSmoothDamp(current,target,currentVelocity,smoothTime,maxSpeed,deltaTime,0,0,0,0) else return cSmoothDamp(current,target,currentVelocity,smoothTime,maxSpeed,0,0,0,0) end end return cSmoothDamp(current,target,currentVelocity,smoothTime,0,0,0,0) end local _SmoothDamp = LuaVector2._SmoothDamp function LuaVector2.SmoothDamp(current,target,currentVelocity,smoothTime,maxSpeed,deltaTime) --may be fast -- local x,y -- if(maxSpeed) then -- if(deltaTime) then -- x,y = cSmoothDamp(current,target,currentVelocity[1],currentVelocity[2],smoothTime,maxSpeed,deltaTime,0,0) -- else -- x,y = cSmoothDamp(current,target,currentVelocity[1],currentVelocity[2],smoothTime,maxSpeed,0,0) -- end -- return LuaVector2(x,y) -- end -- x,y = cSmoothDamp(current,target,currentVelocity[1],currentVelocity[2],smoothTime,0,0) -- return LuaVector2(x,y) return LuaVector2(_SmoothDamp(current,target,currentVelocity,smoothTime,maxSpeed,deltaTime)) end function LuaVector2.SelfSmoothDamp(self,target,currentVelocity,smoothTime,maxSpeed,deltaTime) currentVelocity[1],currentVelocity[2],self[1],self[2] = _SmoothDamp(self,target,currentVelocity,smoothTime,maxSpeed,deltaTime) return self end function LuaVector2.Better_SmoothDamp(current,target,t,currentVelocity,smoothTime,maxSpeed,deltaTime) currentVelocity[1],currentVelocity[2],t[1],t[2] = _SmoothDamp(current,target,currentVelocity,smoothTime,maxSpeed,deltaTime) return t end function LuaVector2.Magnitude( v ) return sqrt(v[1]^2+v[2]^2) end function LuaVector2:Set( x,y ) self[1],self[2]=x,y end function LuaVector2:ToString( ) return LuaVector2.__tostring(self) end local destroyer = ReusableTable.DestroyVector2 function I:Destroy() destroyer(self) end setmetatable(LuaVector2,LuaVector2)