require "lua/entity.lua" require "lua/utilities.lua" --print("plant init") myTime = 0 thePlayer = {} nextPlayerUpdateTime = 0 playerUpdateTimeOffsetNear = 0.1 playerUpdateTimeOffsetFar = 1 playerUpdateTimeOffset = playerUpdateTimeOffsetNear nextWobbleTime = 0 wobbleTimeOffset = 4 isWobbling = 0 distanceFromPlayer = math.huge lastDistanceFromPlayer = math.huge local px,py,pz = position() local rx,ry,rz = rotation() local sx,sy,sz = scale() local usx = (sx + sy) * 0.5 local crx,cry,crz = rotation() wobbleDistance = 2.5 * usx playerDistanceThreshold = 20 * usx function wobbleByAxis() --print("wobbleByAxis 2") if isWobbling == 0 then isWobbling = 1 wobbleState = 0 --print (wobbleState) tppx,tppy,tppz = thePlayer.position() --local pvx,pvy,pvz = tppx-px, tppy-py, tppz-pz local tpdx,tpdy,tpdz = thePlayer.direction() rax,ray,raz = getCross( { tpdx*-1 , tpdy*-1 , tpdz*-1 } , { 0 , 0 , 1 } ) --print ( rax , ray , raz) rotateByAxis( rax , ray , raz , 7 , 0.4 , 0.5 , 0.5 ) trigger.send(1,"FlowerPedalRustle") trigger.send(1,"SunFlowerRustle") nextWobbleTime = myTime + wobbleTimeOffset nextWobbleStateTime = myTime + 0.4 wobbleState = 1 elseif wobbleState == 1 then --print (wobbleState) if nextWobbleStateTime <= myTime then wobbleState = 2 end elseif wobbleState == 2 then --print (wobbleState) --rotateSmooth(rx,ry,rz, 0.3) rotateByAxis( rax , ray , raz , -7 , 0.6 , 0.5 , 0.5 ) nextWobbleStateTime = myTime + 0.6 wobbleState = 3 elseif wobbleState == 3 then --print (wobbleState) if nextWobbleStateTime <= myTime then wobbleState = 4 end elseif wobbleState == 4 then --print (wobbleState) --rotateSmooth(rx,ry,rz, 0.3) rotateByAxis( rax , ray , raz , 3 , 0.7 , 0.5 , 0.5 ) nextWobbleStateTime = myTime + 0.7 wobbleState = 5 elseif wobbleState == 5 then --print (wobbleState) if nextWobbleStateTime <= myTime then wobbleState = 6 end elseif wobbleState == 6 then --print (wobbleState) --rotateSmooth(rx,ry,rz, 0.3) rotateByAxis( rax , ray , raz , -3 , 0.9 , 0.5 , 0.5 ) nextWobbleStateTime = myTime + 0.9 wobbleState = 7 elseif wobbleState == 7 then --print (wobbleState) if nextWobbleStateTime <= myTime then isWobbling = 0 --rotation(rx,ry,rz) --print("done wobbling") end end end while tick() do myTime = myTime + dt --[[ if not thePlayer or not thePlayer.position then --print("updating player object") thePlayer = player_obj() end ]] if nextPlayerUpdateTime <= myTime then lastDistanceFromPlayer = distanceFromPlayer or math.huge --distanceFromPlayer = getDistance2D( {position()} , {thePlayer.position()} ) thePlayer , distanceFromPlayer = getNearestPlayer( {position()} ) --print ( getNearestPlayer( position() ) ) --print(lastDistanceFromPlayer , distanceFromPlayer) nextPlayerUpdateTime = myTime + playerUpdateTimeOffset end --print ( distanceFromPlayer , wobbleDistance , lastDistanceFromPlayer , isWobbling ) if distanceFromPlayer then if ( distanceFromPlayer <= wobbleDistance and ( lastDistanceFromPlayer > distanceFromPlayer + 0.001 ) ) or isWobbling == 1 then lastDistanceFromPlayer = distanceFromPlayer wobbleByAxis() elseif distanceFromPlayer < playerDistanceThreshold then --print("player near") playerUpdateTimeOffset = playerUpdateTimeOffsetNear --rotation(rx,ry,rz) elseif distanceFromPlayer >= playerDistanceThreshold then --print("player far") playerUpdateTimeOffset = playerUpdateTimeOffsetFar end elseif isWobbling == 1 then wobbleByAxis() end end