-- LUA Script - precede every function and global member with lowercase name of script + '_main' local U = require "scriptbank\\utillib" local P = require "scriptbank\\physlib" local hingeChain = {} local linkSpacing = { w = 0.0, h = -1.2, l = 0.0 } local endSpacing = { w = 0.0, h = -1.0, l = 0.0 } local topConstraint = 0 local lastLink = nil local dropChain = false local breakable = true function hinge_chain_alternating_init_name( e, name ) hingeChain[ e ] = { name = name, linkUsed = false } if name == "ChainStart" then topConstraint = P.AddEntitySingleHinge( e, "TopCentreW" ) end end local function buildChain( chainHead ) for k, v in pairs( hingeChain ) do if not v.linkUsed and v.name == "ChainLink" then P.AddEntityDoubleHinge( chainHead, k, "BottomCentreW", "TopCentreL", linkSpacing, 1 ) hingeChain[ k ].linkUsed = true lastLink = k buildChain( k ) break end end end local function addEndEntity( e ) P.AddEntityDoubleHinge( lastLink, e, "BottomCentreW", "TopCentreW", endSpacing, 1 ) P.SetObjectDamping( lastLink, 0.5, 0.5 ) end local timer1, timer2 = math.huge, math.huge local chainMade = false function hinge_chain_alternating_main( e ) local hc = hingeChain[ e ] if chainMade then if hc ~= nil then if lastLink ~= nil and hc.name == "ChainEnd" then addEndEntity( e ) lastLink = nil return end end if dropChain and hc.name == "ChainStart" then if timer2 == math.huge then timer2 = g_Time + 10000 end if g_Time > timer2 then RemoveConstraint( topConstraint ) hingeChain[ e ] = nil end end if breakable and hc.name == "ChainLink" then P.RemoveEntityConstraintsIfDead( e ) end return end if hc ~= nil and hc.name == "ChainStart" then if g_Time > timer1 then hc.linkUsed = true buildChain( e ) chainMade = true elseif timer1 == math.huge then -- allow time for all entities to be initialised timer1 = g_Time + 100 -- 1/10 second should be enough end end end