summaryrefslogtreecommitdiff
path: root/examples/resources/lib
diff options
context:
space:
mode:
authorjussi2024-05-22 00:11:40 +0300
committerjussi2024-05-22 00:11:40 +0300
commitcb1bc03681dff958da6ff851cfe32dfdd83f6f8d (patch)
tree59ba256ce41cec8ac7e0d91b68baf222a9ff596a /examples/resources/lib
parentc95c797da61b4b2829d542f6d7c164a88951cad4 (diff)
downloadreilua-enhanced-cb1bc03681dff958da6ff851cfe32dfdd83f6f8d.tar.gz
reilua-enhanced-cb1bc03681dff958da6ff851cfe32dfdd83f6f8d.tar.bz2
reilua-enhanced-cb1bc03681dff958da6ff851cfe32dfdd83f6f8d.zip
Object lib serialization.
Diffstat (limited to 'examples/resources/lib')
-rw-r--r--examples/resources/lib/boundingBox.lua136
-rw-r--r--examples/resources/lib/color.lua4
-rw-r--r--examples/resources/lib/matrix.lua26
-rw-r--r--examples/resources/lib/quaternion.lua4
-rw-r--r--examples/resources/lib/rectangle.lua12
-rw-r--r--examples/resources/lib/rune.lua4
-rw-r--r--examples/resources/lib/utillib.lua2
-rw-r--r--examples/resources/lib/vector2.lua9
-rw-r--r--examples/resources/lib/vector3.lua8
9 files changed, 197 insertions, 8 deletions
diff --git a/examples/resources/lib/boundingBox.lua b/examples/resources/lib/boundingBox.lua
new file mode 100644
index 0000000..da79875
--- /dev/null
+++ b/examples/resources/lib/boundingBox.lua
@@ -0,0 +1,136 @@
+-- For luaJit compatibility.
+if table.unpack == nil then
+ table.unpack = unpack
+end
+
+local Vector3 = require( "vector3" )
+
+local BoundingBox = {}
+local metatable = {
+ __index = BoundingBox,
+ __tostring = function( b )
+ return "{{"..tostring( b.min.x )..", "..tostring( b.min.y )..", "..tostring( b.min.z ).."}, {"
+ ..tostring( b.max.x )..", "..tostring( b.max.y )..", "..tostring( b.max.z ).."}}"
+ end,
+}
+
+--- Expects format { ... }, { ... }
+function BoundingBox:new( min, max )
+ local object = setmetatable( {}, metatable )
+
+ object.min = Vector3:newT( min )
+ object.max = Vector3:newT( max )
+
+ return object
+end
+
+--- Expects format { { ... }, { ... } }
+function BoundingBox:newT( t )
+ local object = setmetatable( {}, metatable )
+
+ object.min = Vector3:newT( t[1] )
+ object.max = Vector3:newT( t[2] )
+
+ return object
+end
+
+--- Expects format { Vector3, Vector3 }
+function BoundingBox:newV( min, max )
+ local object = setmetatable( {}, metatable )
+
+ object.min = min:clone()
+ object.max = max:clone()
+
+ return object
+end
+
+--- Expects format BoundingBox
+function BoundingBox:newB( b )
+ local object = setmetatable( {}, metatable )
+
+ object.min = b.min:clone()
+ object.max = b.max:clone()
+
+ return object
+end
+
+function BoundingBox:serialize()
+ return table.concat( { "BoundingBox:new({", self.min.x, ",", self.min.y, ",", self.min.z, "},{", self.max.x, ",", self.max.y, ",", self.max.z, "})" } )
+end
+
+--- Expects format { ... }, { ... }
+function BoundingBox:set( min, max )
+ self.min:setT( min )
+ self.max:setT( max )
+end
+
+--- Expects format { { ... }, { ... } }
+function BoundingBox:setT( t )
+ self.min:setT( t[1] )
+ self.max:setT( t[2] )
+end
+
+--- Expects format { Vector3, Vector3 }
+function BoundingBox:setV( min, max )
+ self.min:setV( min )
+ self.max:setV( max )
+end
+
+--- Expects format BoundingBox
+function BoundingBox:setB( b )
+ self.min:setV( b.min )
+ self.max:setV( b.max )
+end
+
+function BoundingBox:arr()
+ return { { self.min.x, self.min.y, self.min.z }, { self.max.x, self.max.y, self.max.z } }
+end
+
+function BoundingBox:unpack()
+ return self.min, self.max
+end
+
+function BoundingBox:clone()
+ return BoundingBox:newB( self )
+end
+
+function BoundingBox:scale( scalar )
+ return BoundingBox:newV( self.min, self.max:scale( scalar ) )
+end
+
+function BoundingBox:getPoints()
+ return {
+ self.min:clone(), -- Down back left.
+ Vector3:new( self.max.x, self.min.y, self.min.z ), -- Down back right.
+ Vector3:new( self.min.x, self.min.y, self.max.z ), -- Down front left.
+ Vector3:new( self.max.x, self.min.y, self.max.z ), -- Down front right.
+ Vector3:new( self.min.x, self.max.y, self.min.z ), -- Up back left.
+ Vector3:new( self.max.x, self.max.y, self.min.z ), -- Up back right.
+ Vector3:new( self.min.x, self.max.y, self.max.z ), -- Up front left.
+ self.max:clone(), -- Up front right.
+ }
+end
+
+function BoundingBox:checkCollisionBox( b )
+ return RL.CheckCollisionBoxes( self:maxToPos(), b:maxToPos() )
+end
+
+function BoundingBox:checkCollisionSphere( center, radius )
+ return RL.CheckCollisionBoxSphere( self:maxToPos(), center, radius )
+end
+
+function BoundingBox:getRayCollision( ray )
+ return RL.GetRayCollisionBox( ray, self:maxToPos() )
+end
+
+-- Max to position from size.
+function BoundingBox:maxToPos()
+ return BoundingBox:new( self.min, self.min + self.max )
+end
+
+-- Max to size from position.
+function BoundingBox:maxToSize()
+ return BoundingBox:new( self.min, self.max - self.min )
+end
+
+return BoundingBox
diff --git a/examples/resources/lib/color.lua b/examples/resources/lib/color.lua
index 9fc31f0..99c44e6 100644
--- a/examples/resources/lib/color.lua
+++ b/examples/resources/lib/color.lua
@@ -91,6 +91,10 @@ function Color:setC( c )
self.a = c.a
end
+function Color:serialize()
+ return "Color:new("..self.r..","..self.g..","..self.b..","..self.a..")"
+end
+
function Color:arr()
return { self.r, self.g, self.b, self.a }
end
diff --git a/examples/resources/lib/matrix.lua b/examples/resources/lib/matrix.lua
index 1dc128f..e94cb10 100644
--- a/examples/resources/lib/matrix.lua
+++ b/examples/resources/lib/matrix.lua
@@ -20,7 +20,7 @@ local function copyMatrix( orig )
end
local Matrix = {}
-Matrix.meta = {
+local metatable = {
__index = Matrix,
__tostring = function( m )
return "{\n"
@@ -45,7 +45,7 @@ Matrix.meta = {
}
function Matrix:new( m )
- local object = setmetatable( {}, Matrix.meta )
+ local object = setmetatable( {}, metatable )
object.m = copyMatrix( m )
@@ -56,6 +56,28 @@ function Matrix:set( m )
self.m = copyMatrix( m )
end
+function Matrix:serialize()
+ local str = { "Matrix:new({" }
+
+ for i, row in ipairs( self.m ) do
+ table.insert( str, "{" )
+
+ for c, v in ipairs( row ) do
+ table.insert( str, v )
+ if c < 4 then
+ table.insert( str, "," )
+ end
+ end
+ table.insert( str, "}" )
+ if i < 4 then
+ table.insert( str, "," )
+ end
+ end
+ table.insert( str, "})" )
+
+ return table.concat( str )
+end
+
function Matrix:clone()
return Matrix:new( self.m )
end
diff --git a/examples/resources/lib/quaternion.lua b/examples/resources/lib/quaternion.lua
index db700d8..b91f474 100644
--- a/examples/resources/lib/quaternion.lua
+++ b/examples/resources/lib/quaternion.lua
@@ -86,6 +86,10 @@ function Quaternion:setQ( q )
self.w = q.w
end
+function Quaternion:serialize()
+ return "Quaternion:new("..self.x..","..self.y..","..self.z..","..self.w..")"
+end
+
function Quaternion:arr()
return { self.x, self.y, self.z, self.w }
end
diff --git a/examples/resources/lib/rectangle.lua b/examples/resources/lib/rectangle.lua
index 7413f53..5fc6c63 100644
--- a/examples/resources/lib/rectangle.lua
+++ b/examples/resources/lib/rectangle.lua
@@ -6,7 +6,7 @@ end
local Vector2 = require( "vector2" )
local Rectangle = {}
-Rectangle.meta = {
+local metatable = {
__index = Rectangle,
__tostring = function( r )
return "{"..tostring( r.x )..", "..tostring( r.y )..", "..tostring( r.width )..", "..tostring( r.height ).."}"
@@ -44,7 +44,7 @@ Rectangle.meta = {
}
function Rectangle:new( x, y, width, height )
- local object = setmetatable( {}, Rectangle.meta )
+ local object = setmetatable( {}, metatable )
object.x = x or 0
object.y = y or object.x
@@ -55,7 +55,7 @@ function Rectangle:new( x, y, width, height )
end
function Rectangle:newT( t )
- local object = setmetatable( {}, Rectangle.meta )
+ local object = setmetatable( {}, metatable )
object.x, object.y, object.width, object.height = table.unpack( t )
@@ -63,7 +63,7 @@ function Rectangle:newT( t )
end
function Rectangle:newR( r )
- local object = setmetatable( {}, Rectangle.meta )
+ local object = setmetatable( {}, metatable )
object.x = r.x
object.y = r.y
@@ -91,6 +91,10 @@ function Rectangle:setR( r )
self.height = r.height
end
+function Rectangle:serialize()
+ return "Rectangle:new("..self.x..","..self.y..","..self.width..","..self.height..")"
+end
+
function Rectangle:arr()
return { self.x, self.y, self.width, self.height }
end
diff --git a/examples/resources/lib/rune.lua b/examples/resources/lib/rune.lua
index 12bf72d..5e04d8b 100644
--- a/examples/resources/lib/rune.lua
+++ b/examples/resources/lib/rune.lua
@@ -32,6 +32,10 @@ function Rune:set( string )
self.string = string or ""
end
+function Rune:serialize()
+ return "Rune:new("..self.string..")"
+end
+
function Rune:clone()
return Rune:new( self.string )
end
diff --git a/examples/resources/lib/utillib.lua b/examples/resources/lib/utillib.lua
index 4054006..c66cf26 100644
--- a/examples/resources/lib/utillib.lua
+++ b/examples/resources/lib/utillib.lua
@@ -132,7 +132,7 @@ function utillib.toBoolean( v )
return false
end
-function utillib.bool2Number( bool )
+function utillib.boolToNumber( bool )
return bool and 1 or 0
end
diff --git a/examples/resources/lib/vector2.lua b/examples/resources/lib/vector2.lua
index 68adc2b..8616a51 100644
--- a/examples/resources/lib/vector2.lua
+++ b/examples/resources/lib/vector2.lua
@@ -67,7 +67,6 @@ function Vector2:newV( v )
return object
end
-
function Vector2:set( x, y )
self.x = x or 0
self.y = y or self.x
@@ -82,6 +81,10 @@ function Vector2:setV( v )
self.y = v.y
end
+function Vector2:serialize()
+ return "Vector2:new("..self.x..","..self.y..")"
+end
+
function Vector2:arr()
return { self.x, self.y }
end
@@ -106,6 +109,10 @@ function Vector2:max( v2 )
return Vector2:new( math.max( self.x, v2.x ), math.max( self.y, v2.y ) )
end
+function Vector2:round()
+ return Vector2:new( RL.Round( self.x ), RL.Round( self.y ) )
+end
+
function Vector2:floor()
return Vector2:new( math.floor( self.x ), math.floor( self.y ) )
end
diff --git a/examples/resources/lib/vector3.lua b/examples/resources/lib/vector3.lua
index 86f5270..216962e 100644
--- a/examples/resources/lib/vector3.lua
+++ b/examples/resources/lib/vector3.lua
@@ -87,6 +87,10 @@ function Vector3:setV( v )
self.z = v.z
end
+function Vector3:serialize()
+ return "Vector3:new("..self.x..","..self.y..","..self.z..")"
+end
+
function Vector3:arr()
return { self.x, self.y, self.z }
end
@@ -123,6 +127,10 @@ function Vector3:max( v2 )
return Vector3:newT( RL.Vector3Max( self, v2 ) )
end
+function Vector3:round()
+ return Vector3:new( RL.Round( self.x ), RL.Round( self.y ), RL.Round( self.z ) )
+end
+
function Vector3:floor()
return Vector3:new( math.floor( self.x ), math.floor( self.y ), math.floor( self.z ) )
end