--

---@class THlib.reisen_player:THlib.player_class
reisen_player = Class(player_class)

reisen_player._load = function()
    if CheckRes('tex','reisen_player') then
        return
    end
    LoadTexture('reisen_player', 'THlib\\player\\reisen\\reisen.png')
    LoadImageGroup('reisen_player', 'reisen_player', 0, 0, 32, 48, 8, 3, 1, 1)
    --Starts at 128 x 148, increments by 24x and has 3y size (opt)
    LoadAnimation('reisen_options','reisen_player',128, 148, 16, 8, 4, 1, 8, 0, 0, 0)

    --Pen. bullet starts at 6 x 193, 39 x 30
    LoadImage('pen_bullet_r','reisen_player',6,193,39,30,40,40,true)
    SetImageState('pen_bullet_r','',Color(128,255,255,255))

    --main bullet starts at 0 x 148, 32 x 9
    LoadImage('main_bullet_r','reisen_player',0,148,32,9,32,12,true)
    SetImageState('main_bullet_r','mul+add',Color(128,255,255,255))
    LoadImage('main_bullet_r_d','reisen_player',0,148,32,9,32,12,true)
    SetImageState('main_bullet_r_d','mul+add',Color(128,255,255,255))

    LoadImage('main_bullet_r_f','reisen_player',0,148,32,9,32,12,true)

    --big spread starts at 0 x 166, 64 x 18
    LoadImage('big_spread_r','reisen_player',0,166,64,18,32,23,true)
    SetImageState('big_spread_r','',Color(64,255,255,255))
    LoadImage('big_spread_r_d','reisen_player',0,166,64,18,32,23,true)
    SetImageState('big_spread_r_d','mul+add',Color(128,255,255,255))

    LoadImageFromFile('reisen_wave','THlib\\player\\reisen\\reisen_wave.png',true,128,128,false)
    SetImageState('reisen_wave','mul+add',color.White)
    LoadPS('reisen_wave_ps','THlib\\player\\reisen\\reisen_wave.psi','reisen_wave',128,128,false)


    LoadImageFromFile('flashbomb_rei','THlib\\player\\reisen\\flashbomb.png',true,64,64,false)
    LoadSound('flashbomb','THlib\\player\\reisen\\bom_flash_01.wav')
end

function reisen_player:init()
    reisen_player._load()
    player_class.init(self)
    self.name = 'reisen'
    self.imgs = {}
    self.A = 1
    self.B = 1
    for i = 1, 24 do
        self.imgs[i] = 'reisen_player' .. i
    end
    self.hspeed = 4
    self.offset = { 600, 600, 600, 600 }
    self.opts = {}
    for i = 1, 4 do
        self.opts[i] = New(reisen_option,i)
    end

end

function reisen_player:frame()
    player_class.frame(self)
end

function reisen_player:shoot()
    if self.timer % 4 == 0 then
        local temp1 = New(reisen_spreadshot,self.x - 10,self.y,90,true)
        local temp2 = New(reisen_spreadshot,self.x + 10,self.y,90,true)
        temp1.img,temp2.img = 'main_bullet_r','main_bullet_r'
        if self.slow == 0 then
            for i=1, 4 do
                New(reisen_spreadshot,self.opts[i].x,self.opts[i].y,ran:Float(90-25,90+25))
            end
        else
            for i=1, 4 do
                New(reisen_piercing,self.opts[i].x,self.opts[i].y)
            end
        end
    end
end

function Interpolate_Decelerate(a,b,x)
    return (a + (b - a) * (1 - (1 - x)^2))
end

function reisen_player:spell()
    New(tasker,function()
        player.hspeed = 2
        player.lspeed = 1
        New(player_spell_mask,255, 0, 0, 30, 300, 30)
        New(player_shield, 30, 360, 30, Interpolate_Decelerate)
        task.Wait(60)
        New(reisen_eye_bomb)
        task.Wait(300)
        player.hspeed = 4
        player.lspeed = 2
    end)
    self.nextspell = 300
end

function reisen_player:flash()
    New(tasker, function()
        New(player_shield, 30, 30, 30, Interpolate_Decelerate)
        for i=1, 16 do
            local temp = 90 + (360/5) * i
            New(reisen_flashbul,player.x + 100 * cos(temp),player.y + 100 * sin(temp), Interpolate_Decelerate,60)
            New(reisen_flashbul,player.x + 50 * cos(temp + 180/5),player.y + 50 * sin(temp + 180/5), Interpolate_Decelerate,60)
        end
        player.collect_line = -224
        task.Wait(1)
        player.collect_line = 96
        task.Wait(59)
        PlaySound('flashbomb',0.5,0)
    end)
end

local lerp = math.lerp

reisen_option = Class(object)
function reisen_option:init(id)
    self.id = id
    local off_foc = {{x = -30, y = -5}, {x = -15, y = -20}, {x = 15, y = -20}, {x = 30, y = -5}}
    self._x,self._y = {},{}
    self._x[0],self._y[0] = 32 * cos(id * 90), 32 * sin(id * 90)
    self._x[1],self._y[1] = off_foc[id].x, off_foc[id].y
    self.x,self.y = player.x,player.y
    self.img = 'reisen_options'

    self.layer = LAYER_PLAYER + 1

    self.bound = false
    self.alphapad = 255
end
function reisen_option:frame()
    self._x[0],self._y[0] = 63 * cos(self.id * 90 + player.timer), 8 * sin(self.id * 90 + player.timer)
    if player.slow == 0 and sin(self.id * 90 + player.timer) >= 0 then
        self.layer = LAYER_PLAYER - 1
    else
        self.layer = LAYER_PLAYER + 1
    end
    self.x = lerp(self.x,player.x + self._x[player.slow],0.1 + 0.3 * player.slow)
    self.y = lerp(self.y,player.y + self._y[player.slow],0.1 + 0.3 * player.slow)

    if player.x < -50 and player.y > 0 then
        self.alphapad = math.lerp(self.alphapad,64,0.2)
    else
        self.alphapad = math.lerp(self.alphapad,255,0.2)
    end
    SetFontState('chain_count','',Color(self.alphapad,255,255,255))
end

reisen_piercing = Class(object)
function reisen_piercing:init(x,y)
    self.img = 'pen_bullet_r'
    self.layer = LAYER_PLAYER_BULLET
    self.group = GROUP_PLAYER_BULLET
    self.x = x
    self.y = y
    self.offx = x - player.x
    self.rot = 90
    self.killflag = true
    self.dmg = 0.2
    self.mute = true
end
function reisen_piercing:frame()
    self.x = self.offx + player.x
    self.y = self.y + 10
end
function reisen_piercing:colli(obj)
    obj.hp = obj.hp - self.dmg
end

reisen_spreadshot = Class(object)
function reisen_spreadshot:init(x,y,angle,is_fixed)
    self.x,self.y = x,y
    SetV2(self,10,angle,true,false)

    self.id = is_fixed
    self._x = x - player.x
    self.dmg = 2
    self.img = 'big_spread_r'
    self.group = GROUP_PLAYER_BULLET
    self.layer = LAYER_PLAYER_BULLET
end
function reisen_spreadshot:frame()
    if self.id then
        self.x = self._x + player.x
    end
    task.Do(self)
end
function reisen_spreadshot:kill()
    New(reisen_spreadshot_die,self.x,self.y,self.rot,self.img)
end

reisen_spreadshot_die = Class(object)
function reisen_spreadshot_die:init(x,y,angle,img)
    self.img = img .. "_d"
    self.x = x
    self.y = y
    self.rot = angle
end
function reisen_spreadshot_die:frame()
    self.hscale = (10-self.timer)/10
    if self.timer >= 10 then Del(self) end
end

reisen_eye_bomb = Class(object)
function reisen_eye_bomb:init()
    self.img = 'reisen_wave_ps'
    local pool = GetParticlePool(self)
    --pool:setEmissionFreq(1)
    pool:setSizeEnd(1.2)
    pool:setLifeMax(0.2)
    pool:setLifeMin(0.2)
    pool:setEmissionFreq(25)
    pool:setLifetime(1000)
    self.pool = pool
    self.layer = LAYER_PLAYER_BULLET+50
    self.group = GROUP_PLAYER_BULLET
    self.killflag = true
    self.dmg = 2
    self.a = 200
    self.b = 200
end
function reisen_eye_bomb:frame()
    self.x = player.x
    self.y = player.y

    for i, o in ObjList(GROUP_ENEMY_BULLET) do
        if Dist(self, o) < 200 then
            Kill(o)
        end
    end

    if self.timer >= 300 then
        self.pool:setEmissionFreq(0)
        self.colli = false
    end
    if self.timer >= 360 then
        Kill(self)
    end
end

reisen_flashbul = Class(object)
function reisen_flashbul:init(x,y,func,time)
    self.func = func
    self.x_, self.y_ = player.x, player.y
    self._x, self._y = x, y
    self.time = time
    self.img = 'main_bullet_r_f'
    self.navi = true
end
function reisen_flashbul:frame()
    self.x = self.func(self.x_,self._x,self.timer/self.time)
    self.y = self.func(self.y_,self._y,self.timer/self.time)
    if self.timer >= self.time then
        Kill(self)
    end
end
function reisen_flashbul:kill()
    New(flashbomb_circle,self.x,self.y,30)
end

flashbomb_circle = Class(object)
function flashbomb_circle:init(x,y,time)
    self.x, self.y = x, y
    self.time = time
    self.img = 'flashbomb_rei'
end
function flashbomb_circle:frame()
    for i, o in ObjList(GROUP_ENEMY_BULLET) do
        if Dist(self, o) < 32 then
            Kill(o)
        end
    end
    if self.timer <= 10 then
        local temp = Interpolate_Decelerate(0,1,math.min(self.timer/10,1))
        self.hscale, self.vscale = temp,temp
    end
    if self.timer >= self.time - 10 then
        local temp = Interpolate_Decelerate(1,0,math.min((self.timer - (self.time - 10))/10,1))
        self.hscale, self.vscale = temp,temp
    end
    if self.timer == self.time then Kill(self) end
end