From bdd660be01f3742befe15dff26929a77eeefe61d Mon Sep 17 00:00:00 2001 From: jussi Date: Thu, 18 Apr 2024 13:33:58 +0300 Subject: New creation methods for object libs. --- examples/resources/lib/vector3.lua | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'examples/resources/lib/vector3.lua') diff --git a/examples/resources/lib/vector3.lua b/examples/resources/lib/vector3.lua index cdc4576..13f621c 100644 --- a/examples/resources/lib/vector3.lua +++ b/examples/resources/lib/vector3.lua @@ -61,6 +61,16 @@ function Vector3:newT( t ) return object end +function Vector3:newV( v ) + local object = setmetatable( {}, metatable ) + + object.x = v.x + object.y = v.y + object.z = v.z + + return object +end + function Vector3:set( x, y, z ) self.x = x or 0 self.y = y or 0 @@ -71,6 +81,12 @@ function Vector3:setT( t ) self.x, self.y, self.z = table.unpack( t ) end +function Vector3:setV( v ) + self.x = v.x + self.y = v.y + self.z = v.z +end + function Vector3:arr() return { self.x, self.y, self.z } end @@ -232,6 +248,8 @@ function Vector3:divEq( v2 ) self.z = self.z / v2.z end +-- Temp pre generated objects to avoid "slow" table generation. + local TEMP_COUNT = 100 local tempPool = {} local curTemp = 1 @@ -240,7 +258,6 @@ for _ = 1, TEMP_COUNT do table.insert( tempPool, Vector3:new( 0, 0, 0 ) ) end --- Uses pre generated objects to avoid "slow" table generation. function Vector3:temp( x, y, z ) local object = tempPool[ curTemp ] curTemp = curTemp + 1 @@ -256,7 +273,6 @@ function Vector3:temp( x, y, z ) return object end --- Uses pre generated objects to avoid "slow" table generation. function Vector3:tempT( t ) local object = tempPool[ curTemp ] curTemp = curTemp + 1 @@ -270,6 +286,21 @@ function Vector3:tempT( t ) return object end +function Vector3:tempV( v ) + local object = tempPool[ curTemp ] + curTemp = curTemp + 1 + + if TEMP_COUNT < curTemp then + curTemp = 1 + end + + object.x = v.x + object.y = v.y + object.z = v.z + + return object +end + function Vector3:getTempId() return curTemp end -- cgit v1.2.3