R=R or {}
require("./sys/sys")(R)
require("./sys/math")(R)
require("./3d/geom")(R)
require("./3d/actor")(R)
require("./3d/model")(R)
require("./3d/scene")(R)

local GLOBAL=require("./global")

local SCENE=nil
local MODEL_GMAN=nil
local ACTOR_GMAN=nil
local MAT_GMAN=nil

local m=math
local s=m.sin
local c=m.cos
local r=m.random
local tau=m.pi*2
local t=0


VSCENE2=nil
MODEL_SHARDS2={}
ACTOR_SHARDS2={}

function setupScreenBreak2()
    VSCENE2=R.Scene:new()

    local modelsVor2=require("./data/model/vor2")
    for i=1,#modelsVor2 do
        local shard=modelsVor2[i]

        MODEL_SHARDS2[i]=R.Model:new()
        shard.origin={
            shard.origin[1]/shard.divide_by,
            shard.origin[2]/shard.divide_by,
            shard.origin[3]/shard.divide_by,
        }
        
        MODEL_SHARDS2[i]:load(shard)

        -- set UV values (i hate lua and fking arrays that start from 1)
        for j=1,#MODEL_SHARDS2[i].tris do
            local verts=MODEL_SHARDS2[i].tris[j].vx
            local sox=shard.origin[1]*shard.divide_by
            local soy=shard.origin[2]*shard.divide_by
            MODEL_SHARDS2[i].tris[j].ex.sox = sox
            MODEL_SHARDS2[i].tris[j].ex.soy = soy
            MODEL_SHARDS2[i].tris[j].ex.u1 = shard.vertices[1+(verts[1]-1) * 3] - sox + 120
            MODEL_SHARDS2[i].tris[j].ex.v1 = shard.vertices[1+(verts[1]-1) * 3 + 1] - soy + 68
            MODEL_SHARDS2[i].tris[j].ex.u2 = shard.vertices[1+(verts[2]-1) * 3] - sox + 120
            MODEL_SHARDS2[i].tris[j].ex.v2 = shard.vertices[1+(verts[2]-1) * 3 + 1] - soy + 68
            MODEL_SHARDS2[i].tris[j].ex.u3 = shard.vertices[1+(verts[3]-1) * 3] - sox + 120
            MODEL_SHARDS2[i].tris[j].ex.v3 = shard.vertices[1+(verts[3]-1) * 3 + 1] - soy + 68
        end
         
        ACTOR_SHARDS2[i]=R.Actor:new(MODEL_SHARDS2[i])
        ACTOR_SHARDS2[i]:setDrawFn(DrawVBankTexture,R.Model.Z_SORT|R.Model.CALC_NORMAL)

        VSCENE2:registerActor(ACTOR_SHARDS2[i])
    end

end

--[[
function DrawVBankTexture(tr)
	if tr.ex.u1 then
		ttri(
			tr.v[1].x,tr.v[1].y,
			tr.v[2].x,tr.v[2].y,
			tr.v[3].x,tr.v[3].y,
			tr.ex.u1,tr.ex.v1,
			tr.ex.u2,tr.ex.v2,
			tr.ex.u3,tr.ex.v3,
			2, -1,
            tr.v[1].z,
            tr.v[2].z,
            tr.v[3].z
		)
	end
end
--[[]]


    return {
        duration=5000,
        bdr=function(y,t,unitT)
        end,
        musicTic=function(isFirstRun,t,unitT)
            if isFirstRun then
                vbank(1)
                memcpy(0x8000,0,120*136)
                cls()
                setupScreenBreak2()
                --firstIn=false
                --vbank(0)
                --memcpy(0,0x8000,120*136)
            else
                vbank(0)
                memcpy(0,0x8000,120*136)
            end
            vbank(1)
            cls(4)
            for i=1,#ACTOR_SHARDS2 do
                local actor=ACTOR_SHARDS2[i]
                t = R.smoothStep((unitT)%1,0,1)
            local sox = actor.model.tris[1].ex.sox
            local soy = actor.model.tris[1].ex.soy
            t = 1/(((sox)^2+(soy)^2)^.5) *t*10
    
                local mat=R.M44:new(R.M44.ID)
                mat:mulM44(R.M44:newRotZ((unitT/1) * (i/10)%1))
                mat:mulM44(R.M44:newTrans(0,0,0.65-t/2))
                actor:setMat(mat)
    
            end
    
            --VSCENE2:getCam():setPos(R.V4:new({0,0,-1,1}))
            --VSCENE2:getCam():setAim(R.V4:new({0,0,0,1}))
    
            VSCENE2:draw()
            vbank(0)
            cls()
        end,
    }

-- <PALETTE>
-- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
-- </PALETTE>