print("elementaltotem_master init") require "lua/entity.lua" require "lua/utilities.lua" require "lua/level_hub/ruinsstate.lua" ruinsState = RuinsState.get() if ruinsState >= 21 then ruinsStage = 6 elseif ruinsState >= 17 then ruinsStage = 5 elseif ruinsState >= 14 then ruinsStage = 4 elseif ruinsState >= 11 then ruinsStage = 3 elseif ruinsState >= 7 then ruinsStage = 2 else ruinsStage = 1 end currentTotemStage = global_variable_get_int("Ruins_totemStage",1) myTime = 0 totemStages = { {"magic","water","tech" }, {"magic","fire","tech","water"}, {"water","fire","earth","magic","tech"}, {"magic","tech","death","fire","earth","water"}, {"water","life","fire","death","magic","tech","earth"}, {"death","air","life","earth","water","tech","magic","fire"}, {"air","air","air"}, } totemStageRewards = { -- 1 function(pos) spawnThrownLoot( "diamond" , pos , 100 , 0 , 5 ) end, -- 2 function(pos) spawnThrownLoot( "diamond" , pos , 100 , 0 , 5 ) spawnThrownLoot( "goldsack02" , pos , 500 , 0 , 1 ) spawnThrownLoot( "coin_a" , pos , 1 , 0 , 20 ) end, -- 3 function(pos) spawnThrownLoot( "diamond" , pos , 100 , 0 , 5 ) spawnThrownLoot( "goldsack01" , pos , 1000 , 0 , 1 ) spawnThrownLoot( "coin_a" , pos , 1 , 0 , 25 ) end, -- 4 function(pos) spawnThrownLoot( "diamond" , pos , 100 , 0 , 5 ) spawnThrownLoot( "goldsack01" , pos , 1000 , 0 , 1 ) spawnThrownLoot( "goldsack02" , pos , 500 , 0 , 1 ) spawnThrownLoot( "coin_a" , pos , 1 , 0 , 30 ) end, -- 5 function(pos) spawnThrownLoot( "diamond" , pos , 100 , 0 , 5 ) spawnThrownLoot( "goldsack01" , pos , 1000 , 0 , 2 ) spawnThrownLoot( "goldsack02" , pos , 500 , 0 , 1 ) spawnThrownLoot( "coin_a" , pos , 1 , 0 , 40 ) end, -- 6 function(pos) spawnThrownLoot( "diamond" , pos , 100 , 0 , 5 ) spawnThrownLoot( "goldsack01" , pos , 1000 , 0 , 2 ) spawnThrownLoot( "goldsack02" , pos , 500 , 0 , 3 ) spawnThrownLoot( "coin_a" , pos , 1 , 0 , 50 ) end, } elements = { air = "ElementalTotem_Chunk_air", death = "ElementalTotem_Chunk_death", earth = "ElementalTotem_Chunk_earth", fire = "ElementalTotem_Chunk_fire", life = "ElementalTotem_Chunk_life", magic = "ElementalTotem_Chunk_magic", tech = "ElementalTotem_Chunk_mech", water = "ElementalTotem_Chunk_water", -- heart, } baseObject = object("ElementalTotem_Base") crownObject = object("ElementalTotem_Crown") boxObject = object("ElementalTotem_MysteryBox") rockObject = object("ElementalTotem_HoleFiller") crownSpace = ft2m(3.7) baseSpace = ft2m(-4.5) chunkSpace = ft2m(6.5) chuckScale = 1.5 rockScale = 1.2 baseScale = chuckScale + 0.5 distanceToEmerge = ft2m(25) retractBuffer = ft2m(10) riseDistance = 10 sinkDistance = 15 dustTime = math.huge timePerChunk = 0.35 totemChunks = {} rockObject.scale( rockScale , rockScale , rockScale ) soundObj = object("emt_lvl_hub_obj_totem_Sounds") function playSound(s) print("playSound",s) if soundObj and soundObj.alive and soundObj.alive() then soundObj.trigger.send(1,s) end end sounds = { startUp = function() playSound("emt_lvl_hub_obj_totem_statr_up") end, moveLoop = function() playSound("emt_lvl_hub_obj_totem_loop") end, endUp = function() playSound("emt_lvl_hub_obj_end_up") end, endDown = function() playSound("emt_lvl_hub_obj_totem_into_ground") end, chunkDestroyed = function() playSound("emt_lvl_hub_obj_totem_hit_block") end, chunkDrop = function() playSound("emt_lvl_hub_obj_totem_drop") end, boxBreak = function() playSound("emt_lvl_hub_obj_totem_crystal_breal") end, spin = function() trigger.send(1,"sndTotemSpin") end, } function buildTotem(stage) print("buildTotem",stage) local px,py,pz = position() local rx,ry,rz = rotation() rockObject.position( -100 , -100 , -100 ) --Mystery Box boxObject.position( px,py,pz ) boxObject.rotation( rx,ry,0 ) boxObject.trigger.send(4) --Create Base originalBasePos = { baseObject.position( px,py,pz + baseSpace ) } originalBaseRot = { baseObject.rotation( rx,ry,rz ) } baseObject.scale( baseScale , baseScale , baseScale ) --Create Chunks for i = 1,#totemStages[stage] do print(totemStages[stage][i]) thisChunk = spawn(elements[ totemStages[stage][i] ]) thisChunk.position( px, py, pz ) thisChunk.rotation( rx, ry, rz ) thisChunk.scale( chuckScale , chuckScale , chuckScale ) totemChunks[i] = thisChunk end updateChunksCollision() updateChunksPosition() --Create Crown crownObject.scale( chuckScale , chuckScale , chuckScale ) updateCrownPosition() updateBoxPosition() totemBuilt = true end function removeDeadChunks() local newTotemChunks = {} for k,v in pairs(totemChunks) do if v.alive and v.alive() then table.insert( newTotemChunks , v ) end end totemChunks = newTotemChunks end function chunkFall() local px0,py0,pz0 = baseObject.position() local px1,py1,pz1 = totemChunks[1].position() local destPz = pz0 - baseSpace local offsetPz = pz1 - destPz local offsetInChunks = offsetPz/chunkSpace local fallTranslateLength = offsetInChunks * 0.5 print("chunkFall",pz0,pz1,destPz) print(baseSpace,chunkSpace) print(offsetPz,offsetInChunks,fallTranslateLength) totemChunks[1].translateSmooth( px0, py0 , destPz , fallTranslateLength , 1 , 0 ) chunksFalling = true chunksFallEndTime = myTime + fallTranslateLength updateChunksCollision() local px,py,pz = position() end function updateCrownPosition() local px,py,pz = 0,0,0 px,py,pz = totemChunks[#totemChunks].position() crownObject.position( px,py,pz + chunkSpace ) crownObject.rotation( totemChunks[#totemChunks].rotation() ) end function updateBoxPosition() px,py,pz = crownObject.position() boxObject.position( px,py,pz + crownSpace ) boxObject.rotateSmoothRelative( _ , _ , 0.3 , 0 ) end function updateChunksCollision() print("updateChunksCollision") for k,v in pairs(totemChunks) do if k == 1 then --v.collision.enable(true) v.trigger.send(3) else --v.collision.enable(false) v.trigger.send(4) end end end function updateChunksPosition() --print("updateChunksPosition",totemChunks) for k,v in pairs(totemChunks) do if not chunksFalling and k == 1 then --print(1,k) px0,py0,pz0 = baseObject.position() v.position( px0, py0 , pz0 - baseSpace + (chunkSpace * k-2) ) v.rotation( baseObject.rotation() ) elseif k > 1 then --print(2,k) px0,py0,pz0 = totemChunks[k-1].position() v.position( px0, py0 , pz0 + chunkSpace ) v.rotation( baseObject.rotation() ) end end end function totemSave() currentTotemStage = currentTotemStage + 1 global_variable_set_int("Ruins_totemStage",currentTotemStage) SaveGameProgress() end testTotem = false function rise(distance) --print("rise",distance,totemInMotion,Game_InCinematic(),chunksFalling) if not totemInMotion and not Game_InCinematic() and not chunksFalling and distance <= riseDistance then print("rising") baseObject.translateSmooth( originalBasePos[1] , originalBasePos[2] , originalBasePos[3] , #totemChunks * timePerChunk , 0.1 , 0.1 ) baseObject.rotateSmoothRelative( _ , _ , 45 * #totemChunks , #totemChunks * timePerChunk , 0.1 , 0.1 ) currentBehavior = sink object("fx_totem_UpDown_dust").trigger.send(1) sounds.startUp() sounds.moveLoop() rising = true totemInMotion = true dustTime = myTime + (#totemChunks * timePerChunk) end end function sink(distance) --print("sink",distance,totemInMotion,chunksFalling,Game_InCinematic()) if not totemInMotion and not chunksFalling and (Game_InCinematic() or (distance > sinkDistance)) then print("sinking") baseObject.translateSmooth( originalBasePos[1] , originalBasePos[2] , originalBasePos[3] - (#totemChunks * chunkSpace) , #totemChunks * timePerChunk , 0.1 , 0.1 ) baseObject.rotateSmoothRelative( _ , _ , (45 * #totemChunks) * -1 , #totemChunks * timePerChunk , 0.1 , 0.1 ) currentBehavior = rise object("fx_totem_UpDown_dust").trigger.send(1) sounds.startUp() sounds.moveLoop() sinking = true totemInMotion = true dustTime = myTime + (#totemChunks * timePerChunk) end end currentBehavior = sink function riseTick() currentBehavior( getDistance( {GetPlayerCentroidObj().position()} , {position()} ) ) end while tick() do myTime = myTime + dt if not totemBuilt then if (ruinsStage >= currentTotemStage and currentTotemStage < 7) or testTotem then rockPosition = rockPosition or {rockObject.position()} if pcall( buildTotem , currentTotemStage ) then --buildTotem success boxObject.setAlpha(1) crownObject.setAlpha(1) else --buildTotem fail print("!!! building totem failed !!!") --delete() for k,v in pairs(totemChunks) do v.delete() end totemChunks = {} boxObject.setAlpha(0) crownObject.setAlpha(0) if rockPosition then rockObject.position( unpack(rockPosition) ) end end else delete() end end if totemBuilt and dt > 0 then riseTick() end if dustTime <= myTime then print("dust off") object("fx_totem_UpDown_dust").trigger.send(2) totemInMotion = false dustTime = math.huge if rising then sounds.endUp() elseif sinking then sounds.endDown() end rising = false sinking = false end for k,v in pairs(elements) do if trigger.received(1,k .. "Destroyed") then print("elementaltotem_master: got " .. k .. "Destroyed") sounds.chunkDestroyed() waitingToUpdateChunks = true waitingToUpdateChunksTime = myTime + 0.1 print("set waitingToUpdateChunks", waitingToUpdateChunks , waitingToUpdateChunksTime ) end end if totemBuilt and dt > 0 then if waitingToUpdateChunks and waitingToUpdateChunksTime <= myTime then print("updating chunks") waitingToUpdateChunks = false removeDeadChunks() print("remaining chunks:",#totemChunks) if #totemChunks == 0 then local px0,py0,pz0 = baseObject.position() local px1,py1,pz1 = crownObject.position() local destPz = pz0 - baseSpace local offsetPz = pz1 - destPz local offsetInChunks = offsetPz/chunkSpace local fallTranslateLength = offsetInChunks * 0.5 crownObject.translateSmooth( px0, py0 , destPz , fallTranslateLength , 1 , 0 ) totemCompleteCinematic = true totemCompleteStage = 0 else chunkFall() end end if chunksFalling then if chunksFallEndTime <= myTime then print("fx_element_down_dust") object("fx_element_down_dust").trigger.send() sounds.chunkDrop() chunksFalling = false end --[[ else updateChunksPosition() ]] end if not waitingToUpdateChunks then updateChunksPosition() if #totemChunks >= 1 then updateCrownPosition() end if #totemChunks >= 1 then updateBoxPosition() end end end if waitToUpdateChunksCollision and waitToUpdateChunksCollisionTime <= myTime then waitToUpdateChunksCollision = false updateChunksCollision() end if totemCompleteCinematic and dt > 0 then --print("totemCompleteCinematic",totemCompleteCinematic,totemCompleteStage,cineTimer,myTime) if totemCompleteStage == 0 then print("start") sounds.spin() cineTimer = myTime + 1.5 cineStart = myTime totemCompleteStage = 1 elseif totemCompleteStage == 1 then --print("spin",15 * ((myTime-cineStart) + 0.5) ) boxObject.rotateSmoothRelative( _ , _ , 15 * ((myTime-cineStart) + 0.5) , 0 ) if cineTimer <= myTime then print("up") boxObject.translateSmoothRelative( _, _, ft2m(4) , 1 , 0 , 0.5 ) boxObject.rotateSmooth( rotation() , 0.1 , 1 , 1 ) cineTimer = myTime + 1 totemCompleteStage = 2 end elseif totemCompleteStage == 2 then if cineTimer <= myTime then print("down") local px,py,pz = crownObject.position() boxObject.translateSmooth( px, py, pz + crownSpace , 0.3 , 0.5 , 0 ) cineTimer = myTime + 0.3 totemCompleteStage = 3 end elseif totemCompleteStage == 3 then if cineTimer <= myTime then print("BOOM!") local px,py,pz = position() object("ElementalTotem_Fireworks").position( px,py,pz ) object("ElementalTotem_Fireworks").trigger.send(1) sounds.boxBreak() for i=1,7 do local newFirework = spawn("ElementalTotem_Fireworks",px,py,pz ) local rz = math.random(0,120) newFirework.rotation ( nil , nil , rz ) print( "newFirework" , newFirework , i, rz ) newFirework.trigger.send(1) end boxObject.delete() rockObject.position( unpack(rockPosition) ) crownObject.setAlphaSmooth(1,0,0.3) baseObject.setAlphaSmooth(1,0,0.3) totemStageRewards[currentTotemStage]( {px, py, pz+2} ) totemSave() cineTimer = myTime + 0.3 totemCompleteStage = 4 end elseif totemCompleteStage == 4 then if cineTimer <= myTime then print("cleanup") delete() end end end end if boxObject and boxObject.alive and boxObject.alive() then boxObject.delete() end if rockPosition then rockObject.position( unpack(rockPosition) ) end crownObject.delete() baseObject.delete() print("Totem is finished, see you next time!")