New creation methods for object libs.

This commit is contained in:
jussi
2024-04-18 13:33:58 +03:00
parent 70b40f6782
commit bdd660be01
7 changed files with 197 additions and 37 deletions

View File

@@ -104,14 +104,14 @@ end
local function tileCollision( entity ) local function tileCollision( entity )
local vPos = entity.pos + entity.vel -- Future pos with current vel. local vPos = entity.pos + entity.vel -- Future pos with current vel.
local vRect = entity.colRect:clone() local vRect = Rect:tempR( entity.colRect )
local tinyGap = 0.001 -- Tiny gap between collisionRect and tile to prevent getting stuck on all seams. local tinyGap = 0.001 -- Tiny gap between collisionRect and tile to prevent getting stuck on all seams.
-- Move test rect to predicted position. -- Move test rect to predicted position.
vRect.x = vPos.x - vRect.width / 2 vRect.x = vPos.x - vRect.width / 2
-- Tile range where collision box is affecting. -- Tile range where collision box is affecting.
local tileRect = Rect:new( local tileRect = Rect:temp(
math.floor( vRect.x / TILE_SIZE ), math.floor( vRect.x / TILE_SIZE ),
math.floor( vRect.y / TILE_SIZE ), math.floor( vRect.y / TILE_SIZE ),
math.floor( ( vRect.x + vRect.width ) / TILE_SIZE ), math.floor( ( vRect.x + vRect.width ) / TILE_SIZE ),
@@ -120,7 +120,7 @@ local function tileCollision( entity )
for y = tileRect.y, tileRect.height do for y = tileRect.y, tileRect.height do
if 0 < entity.vel.x then if 0 < entity.vel.x then
if isTileWall( Vec2:new( tileRect.width, y ) ) then if isTileWall( Vec2:temp( tileRect.width, y ) ) then
-- Use new_x to push out of tile. -- Use new_x to push out of tile.
local new_x = tileRect.width * TILE_SIZE - ( entity.colRect.x + entity.colRect.width ) local new_x = tileRect.width * TILE_SIZE - ( entity.colRect.x + entity.colRect.width )
@@ -129,7 +129,7 @@ local function tileCollision( entity )
break break
end end
elseif entity.vel.x < 0 then elseif entity.vel.x < 0 then
if isTileWall( Vec2:new( tileRect.x, y ) ) then if isTileWall( Vec2:temp( tileRect.x, y ) ) then
local new_x = ( tileRect.x * TILE_SIZE + TILE_SIZE ) - entity.colRect.x local new_x = ( tileRect.x * TILE_SIZE + TILE_SIZE ) - entity.colRect.x
entity.vel.x = new_x + tinyGap entity.vel.x = new_x + tinyGap
@@ -151,7 +151,7 @@ local function tileCollision( entity )
for x = tileRect.x, tileRect.width do for x = tileRect.x, tileRect.width do
if 0 < entity.vel.y then if 0 < entity.vel.y then
if isTileWall( Vec2:new( x, tileRect.height ) ) then if isTileWall( Vec2:temp( x, tileRect.height ) ) then
local new_y = tileRect.height * TILE_SIZE - ( entity.colRect.y + entity.colRect.height ) local new_y = tileRect.height * TILE_SIZE - ( entity.colRect.y + entity.colRect.height )
-- math.max prevents bounce when hitting right on the corner. -- math.max prevents bounce when hitting right on the corner.
entity.vel.y = math.max( new_y - tinyGap, 0 ) entity.vel.y = math.max( new_y - tinyGap, 0 )
@@ -160,7 +160,7 @@ local function tileCollision( entity )
break break
end end
elseif entity.vel.y < 0 then elseif entity.vel.y < 0 then
if isTileWall( Vec2:new( x, tileRect.y ) ) then if isTileWall( Vec2:temp( x, tileRect.y ) ) then
local new_y = ( tileRect.y * TILE_SIZE + TILE_SIZE ) - entity.colRect.y local new_y = ( tileRect.y * TILE_SIZE + TILE_SIZE ) - entity.colRect.y
entity.vel.y = new_y + tinyGap entity.vel.y = new_y + tinyGap
@@ -207,7 +207,6 @@ local function playerMovement( delta )
end end
player.vel.x = Util.clamp( player.vel.x, -PLAYER_MAXSPEED, PLAYER_MAXSPEED ) player.vel.x = Util.clamp( player.vel.x, -PLAYER_MAXSPEED, PLAYER_MAXSPEED )
player.vel.y = player.vel.y + GRAVITY * delta player.vel.y = player.vel.y + GRAVITY * delta
-- Drop from platform. -- Drop from platform.
@@ -216,7 +215,7 @@ local function playerMovement( delta )
end end
tileCollision( player ) tileCollision( player )
player.pos = player.pos + player.vel player.pos:addEq( player.vel )
player.colRect.x = player.pos.x - player.colRect.width / 2 player.colRect.x = player.pos.x - player.colRect.width / 2
player.colRect.y = player.pos.y - player.colRect.height player.colRect.y = player.pos.y - player.colRect.height
end end
@@ -235,7 +234,7 @@ local function drawMap()
for x = 1, tilemap.size.x do for x = 1, tilemap.size.x do
for y = 1, tilemap.size.y do for y = 1, tilemap.size.y do
local tile = tilemap.tiles[x][y] local tile = tilemap.tiles[x][y]
local pos = Vec2:new( x - 1, y - 1 ) local pos = Vec2:temp( x - 1, y - 1 )
if 0 < tile then if 0 < tile then
RL.DrawTextureRec( tex, tilemap.tileRects[ tile ], { pos.x * TILE_SIZE, pos.y * TILE_SIZE }, RL.WHITE ) RL.DrawTextureRec( tex, tilemap.tileRects[ tile ], { pos.x * TILE_SIZE, pos.y * TILE_SIZE }, RL.WHITE )
@@ -272,7 +271,7 @@ local function drawPlayer()
-- Draw rect. -- Draw rect.
local src = player.frames[ player.curFrame ]:clone() local src = player.frames[ player.curFrame ]:clone()
local dst = Rect:new( local dst = Rect:temp(
player.pos.x - src.width / 2, player.pos.x - src.width / 2,
player.pos.y - src.height, player.pos.y - src.height,
src.width, src.width,

View File

@@ -65,6 +65,17 @@ function Color:newT( t )
return object return object
end end
function Color:newC( c )
local object = setmetatable( {}, metatable )
object.r = c.r
object.g = c.g
object.b = c.b
object.a = c.a
return object
end
function Color:set( r, g, b, a ) function Color:set( r, g, b, a )
self.r = r or 255 self.r = r or 255
self.g = g or 255 self.g = g or 255
@@ -76,6 +87,13 @@ function Color:setT( t )
self.r, self.g, self.b, self.a = table.unpack( t ) self.r, self.g, self.b, self.a = table.unpack( t )
end end
function Color:setC( c )
self.r = c.r
self.g = c.g
self.b = c.b
self.a = c.a
end
function Color:arr() function Color:arr()
return { self.r, self.g, self.b, self.a } return { self.r, self.g, self.b, self.a }
end end
@@ -145,6 +163,8 @@ function Color:lerp( color, amount )
) )
end end
-- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100 local TEMP_COUNT = 100
local tempPool = {} local tempPool = {}
local curTemp = 1 local curTemp = 1
@@ -153,7 +173,6 @@ for _ = 1, TEMP_COUNT do
table.insert( tempPool, Color:new( 255, 255, 255, 255 ) ) table.insert( tempPool, Color:new( 255, 255, 255, 255 ) )
end end
-- Uses pre generated objects to avoid "slow" table generation.
function Color:temp( r, g, b, a ) function Color:temp( r, g, b, a )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1 curTemp = curTemp + 1
@@ -170,7 +189,6 @@ function Color:temp( r, g, b, a )
return object return object
end end
-- Uses pre generated objects to avoid "slow" table generation.
function Color:tempT( t ) function Color:tempT( t )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1 curTemp = curTemp + 1
@@ -184,6 +202,22 @@ function Color:tempT( t )
return object return object
end end
function Color:tempC( c )
local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then
curTemp = 1
end
object.r = c.r
object.g = c.g
object.b = c.b
object.a = c.a
return object
end
function Color:getTempId() function Color:getTempId()
return curTemp return curTemp
end end

View File

@@ -57,6 +57,17 @@ function Quaternion:newT( t )
return object return object
end end
function Quaternion:newQ( q )
local object = setmetatable( {}, metatable )
object.x = q.x
object.y = q.y
object.z = q.z
object.w = q.w
return object
end
function Quaternion:set( x, y, z, w ) function Quaternion:set( x, y, z, w )
self.x = x or 0 self.x = x or 0
self.y = y or 0 self.y = y or 0
@@ -68,6 +79,13 @@ function Quaternion:setT( t )
self.x, self.y, self.z, self.w = table.unpack( t ) self.x, self.y, self.z, self.w = table.unpack( t )
end end
function Quaternion:setQ( q )
self.x = q.x
self.y = q.y
self.z = q.z
self.w = q.w
end
function Quaternion:arr() function Quaternion:arr()
return { self.x, self.y, self.z, self.w } return { self.x, self.y, self.z, self.w }
end end
@@ -153,6 +171,8 @@ function Quaternion:transform( mat )
return Quaternion:newT( RL.QuaternionTransform( self, mat ) ) return Quaternion:newT( RL.QuaternionTransform( self, mat ) )
end end
-- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100 local TEMP_COUNT = 100
local tempPool = {} local tempPool = {}
local curTemp = 1 local curTemp = 1
@@ -161,7 +181,6 @@ for _ = 1, TEMP_COUNT do
table.insert( tempPool, Quaternion:new( 0, 0, 0, 1 ) ) table.insert( tempPool, Quaternion:new( 0, 0, 0, 1 ) )
end end
-- Uses pre generated objects to avoid "slow" table generation.
function Quaternion:temp( x, y, z, w ) function Quaternion:temp( x, y, z, w )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1 curTemp = curTemp + 1
@@ -178,7 +197,6 @@ function Quaternion:temp( x, y, z, w )
return object return object
end end
-- Uses pre generated objects to avoid "slow" table generation.
function Quaternion:tempT( t ) function Quaternion:tempT( t )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1 curTemp = curTemp + 1
@@ -192,6 +210,22 @@ function Quaternion:tempT( t )
return object return object
end end
function Quaternion:tempQ( q )
local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then
curTemp = 1
end
object.x = q.x
object.y = q.y
object.z = q.z
object.w = q.w
return object
end
function Quaternion:getTempId() function Quaternion:getTempId()
return curTemp return curTemp
end end

View File

@@ -62,6 +62,17 @@ function Rectangle:newT( t )
return object return object
end end
function Rectangle:newR( r )
local object = setmetatable( {}, Rectangle.meta )
object.x = r.x
object.y = r.y
object.width = r.width
object.height = r.height
return object
end
function Rectangle:set( x, y, width, height ) function Rectangle:set( x, y, width, height )
self.x = x or 0 self.x = x or 0
self.y = y or 0 self.y = y or 0
@@ -73,6 +84,13 @@ function Rectangle:setT( t )
self.x, self.y, self.width, self.height = table.unpack( t ) self.x, self.y, self.width, self.height = table.unpack( t )
end end
function Rectangle:setR( r )
self.x = r.x
self.y = r.y
self.width = r.width
self.height = r.height
end
function Rectangle:arr() function Rectangle:arr()
return { self.x, self.y, self.width, self.height } return { self.x, self.y, self.width, self.height }
end end
@@ -153,6 +171,8 @@ function Rectangle:getCollisionRec( rec )
return Rectangle:new( RL.GetCollisionRec( self, rec ) ) return Rectangle:new( RL.GetCollisionRec( self, rec ) )
end end
-- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100 local TEMP_COUNT = 100
local tempPool = {} local tempPool = {}
local curTemp = 1 local curTemp = 1
@@ -161,7 +181,6 @@ for _ = 1, TEMP_COUNT do
table.insert( tempPool, Rectangle:new( 0, 0, 0, 0 ) ) table.insert( tempPool, Rectangle:new( 0, 0, 0, 0 ) )
end end
-- Uses pre generated objects to avoid "slow" table generation.
function Rectangle:temp( x, y, width, height ) function Rectangle:temp( x, y, width, height )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1 curTemp = curTemp + 1
@@ -178,7 +197,6 @@ function Rectangle:temp( x, y, width, height )
return object return object
end end
-- Uses pre generated objects to avoid "slow" table generation.
function Rectangle:tempT( t ) function Rectangle:tempT( t )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1 curTemp = curTemp + 1
@@ -192,6 +210,22 @@ function Rectangle:tempT( t )
return object return object
end end
function Rectangle:tempR( r )
local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then
curTemp = 1
end
object.x = r.x
object.y = r.y
object.width = r.width
object.height = r.height
return object
end
function Rectangle:getTempId() function Rectangle:getTempId()
return curTemp return curTemp
end end

View File

@@ -11,7 +11,6 @@ local metatable = {
end, end,
__add = function( v1, v2 ) __add = function( v1, v2 )
return Vector2:new( v1.x + v2.x, v1.y + v2.y ) return Vector2:new( v1.x + v2.x, v1.y + v2.y )
-- return Vector2:newOld( v1.x + v2.x, v1.y + v2.y )
end, end,
__sub = function( v1, v2 ) __sub = function( v1, v2 )
return Vector2:new( v1.x - v2.x, v1.y - v2.y ) return Vector2:new( v1.x - v2.x, v1.y - v2.y )
@@ -59,6 +58,16 @@ function Vector2:newT( t )
return object return object
end end
function Vector2:newV( v )
local object = setmetatable( {}, metatable )
object.x = v.x
object.y = v.y
return object
end
function Vector2:set( x, y ) function Vector2:set( x, y )
self.x = x or 0 self.x = x or 0
self.y = y or 0 self.y = y or 0
@@ -68,6 +77,11 @@ function Vector2:setT( t )
self.x, self.y = table.unpack( t ) self.x, self.y = table.unpack( t )
end end
function Vector2:setV( v )
self.x = v.x
self.y = v.y
end
function Vector2:arr() function Vector2:arr()
return { self.x, self.y } return { self.x, self.y }
end end
@@ -204,6 +218,8 @@ function Vector2:divEq( v2 )
self.y = self.y / v2.y self.y = self.y / v2.y
end end
-- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100 local TEMP_COUNT = 100
local tempPool = {} local tempPool = {}
local curTemp = 1 local curTemp = 1
@@ -212,7 +228,6 @@ for _ = 1, TEMP_COUNT do
table.insert( tempPool, Vector2:new( 0, 0 ) ) table.insert( tempPool, Vector2:new( 0, 0 ) )
end end
-- Uses pre generated objects to avoid "slow" table generation.
function Vector2:temp( x, y ) function Vector2:temp( x, y )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1 curTemp = curTemp + 1
@@ -227,7 +242,6 @@ function Vector2:temp( x, y )
return object return object
end end
-- Uses pre generated objects to avoid "slow" table generation.
function Vector2:tempT( t ) function Vector2:tempT( t )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1 curTemp = curTemp + 1
@@ -241,6 +255,20 @@ function Vector2:tempT( t )
return object return object
end end
function Vector2: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
return object
end
function Vector2:getTempId() function Vector2:getTempId()
return curTemp return curTemp
end end

View File

@@ -61,6 +61,16 @@ function Vector3:newT( t )
return object return object
end 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 ) function Vector3:set( x, y, z )
self.x = x or 0 self.x = x or 0
self.y = y or 0 self.y = y or 0
@@ -71,6 +81,12 @@ function Vector3:setT( t )
self.x, self.y, self.z = table.unpack( t ) self.x, self.y, self.z = table.unpack( t )
end end
function Vector3:setV( v )
self.x = v.x
self.y = v.y
self.z = v.z
end
function Vector3:arr() function Vector3:arr()
return { self.x, self.y, self.z } return { self.x, self.y, self.z }
end end
@@ -232,6 +248,8 @@ function Vector3:divEq( v2 )
self.z = self.z / v2.z self.z = self.z / v2.z
end end
-- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100 local TEMP_COUNT = 100
local tempPool = {} local tempPool = {}
local curTemp = 1 local curTemp = 1
@@ -240,7 +258,6 @@ for _ = 1, TEMP_COUNT do
table.insert( tempPool, Vector3:new( 0, 0, 0 ) ) table.insert( tempPool, Vector3:new( 0, 0, 0 ) )
end end
-- Uses pre generated objects to avoid "slow" table generation.
function Vector3:temp( x, y, z ) function Vector3:temp( x, y, z )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1 curTemp = curTemp + 1
@@ -256,7 +273,6 @@ function Vector3:temp( x, y, z )
return object return object
end end
-- Uses pre generated objects to avoid "slow" table generation.
function Vector3:tempT( t ) function Vector3:tempT( t )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1 curTemp = curTemp + 1
@@ -270,6 +286,21 @@ function Vector3:tempT( t )
return object return object
end 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() function Vector3:getTempId()
return curTemp return curTemp
end end

View File

@@ -57,7 +57,7 @@ local function setApplePos()
while search do while search do
search = false search = false
applePos = Vec2:new( math.random( 0, LEVEL_SIZE - 1 ), math.random( 0, LEVEL_SIZE - 1 ) ) applePos:set( math.random( 0, LEVEL_SIZE - 1 ), math.random( 0, LEVEL_SIZE - 1 ) )
for _, seg in ipairs( snake.segments ) do for _, seg in ipairs( snake.segments ) do
search = applePos == seg.pos search = applePos == seg.pos
@@ -99,16 +99,16 @@ local function moveSnake()
-- Move body. -- Move body.
for i, seg in ipairs( snake.segments ) do for i, seg in ipairs( snake.segments ) do
if i < #snake.segments then if i < #snake.segments then
seg.pos = snake.segments[ i+1 ].pos:clone() seg.pos:setV( snake.segments[ i+1 ].pos )
seg.heading = snake.segments[ i+1 ].heading:clone() seg.heading:setV( snake.segments[ i+1 ].heading )
else else
seg.pos = snake.headPos:clone() seg.pos:setV( snake.headPos )
seg.heading = snake.heading:clone() seg.heading:setV( snake.heading )
end end
end end
-- Move head. -- Move head.
snake.heading = Vec2:new( snake.control.x, snake.control.y ) snake.heading:set( snake.control.x, snake.control.y )
snake.headPos = Vec2:new( snake.headPos.x + snake.heading.x, snake.headPos.y + snake.heading.y ) snake.headPos:set( snake.headPos.x + snake.heading.x, snake.headPos.y + snake.heading.y )
-- Check appple eating. -- Check appple eating.
if snake.headPos == applePos then if snake.headPos == applePos then
@@ -133,13 +133,13 @@ function RL.update( delta )
if gameState == STATE.GAME then -- Run game. if gameState == STATE.GAME then -- Run game.
-- Controls. -- Controls.
if RL.IsKeyPressed( RL.KEY_RIGHT ) and 0 <= snake.heading.x then if RL.IsKeyPressed( RL.KEY_RIGHT ) and 0 <= snake.heading.x then
snake.control = Vec2:new( 1, 0 ) snake.control:set( 1, 0 )
elseif RL.IsKeyPressed( RL.KEY_LEFT ) and snake.heading.x <= 0 then elseif RL.IsKeyPressed( RL.KEY_LEFT ) and snake.heading.x <= 0 then
snake.control = Vec2:new( -1, 0 ) snake.control:set( -1, 0 )
elseif RL.IsKeyPressed( RL.KEY_DOWN ) and 0 <= snake.heading.y then elseif RL.IsKeyPressed( RL.KEY_DOWN ) and 0 <= snake.heading.y then
snake.control = Vec2:new( 0, 1 ) snake.control:set( 0, 1 )
elseif RL.IsKeyPressed( RL.KEY_UP ) and snake.heading.y <= 0 then elseif RL.IsKeyPressed( RL.KEY_UP ) and snake.heading.y <= 0 then
snake.control = Vec2:new( 0, -1 ) snake.control:set( 0, -1 )
end end
moveTimer = moveTimer - gameSpeed * delta moveTimer = moveTimer - gameSpeed * delta
@@ -167,16 +167,16 @@ end
--[[ Check if next segment is on left side. There are more mathematically elegant solution to this, but there is --[[ Check if next segment is on left side. There are more mathematically elegant solution to this, but there is
only four possibilities so we can just check them all. ]]-- only four possibilities so we can just check them all. ]]--
local function onLeft( this, nextSeg ) local function onLeft( this, nextSeg )
return ( this == Vec2:new( 0, -1 ) and nextSeg == Vec2:new( -1, 0 ) ) return ( this == Vec2:temp( 0, -1 ) and nextSeg == Vec2:temp( -1, 0 ) )
or ( this == Vec2:new( -1, 0 ) and nextSeg == Vec2:new( 0, 1 ) ) or ( this == Vec2:temp( -1, 0 ) and nextSeg == Vec2:temp( 0, 1 ) )
or ( this == Vec2:new( 0, 1 ) and nextSeg == Vec2:new( 1, 0 ) ) or ( this == Vec2:temp( 0, 1 ) and nextSeg == Vec2:temp( 1, 0 ) )
or ( this == Vec2:new( 1, 0 ) and nextSeg == Vec2:new( 0, -1 ) ) or ( this == Vec2:temp( 1, 0 ) and nextSeg == Vec2:temp( 0, -1 ) )
end end
local function drawSnake() local function drawSnake()
for i, seg in ipairs( snake.segments ) do for i, seg in ipairs( snake.segments ) do
local angle = seg.heading:atan2() local angle = seg.heading:atan2()
local source = Rect:new( 16, 0, 8, 8 ) local source = Rect:temp( 16, 0, 8, 8 )
if i == 1 then -- Tail segment. Yes tail is actually the 'first' segment. if i == 1 then -- Tail segment. Yes tail is actually the 'first' segment.
source.x = 8 source.x = 8