//Algollen Nutshell script
//Made by Garrean, Sprites by DylanDude

freeslot("MT_GIRLALGOLLEN", "S_GIRLALGOLLEN", "S_GIRLALGOLLEN_ROLL1", "S_GIRLALGOLLEN_ROLL2",
"S_GIRLALGOLLEN_ROLL3", "S_GIRLALGOLLEN_ROLL4", "S_GIRLALGOLLEN_ROLL5", "S_GIRLALGOLLEN_ROLL6",
"S_GIRLALGOLLEN_ROLL7", "S_GIRLALGOLLEN_ROLL8", "SPR_ALGO", "SFX_ALGO")

sfxinfo[sfx_algo] = {
        singular = false,
        priority = 64,
        flags = SF_NOMULTIPLESOUND
}

//Female Algollen

states[S_GIRLALGOLLEN] = {
	sprite = SPR_ALGO,
    frame = A,
	tics = -1,
	nextstate = S_GIRLALGOLLEN
}

states[S_GIRLALGOLLEN_ROLL1] = {
	sprite = SPR_ALGO,
    frame = A,
	tics = 5,
	nextstate = S_GIRLALGOLLEN_ROLL2
}

states[S_GIRLALGOLLEN_ROLL2] = {
	sprite = SPR_ALGO,
    frame = B,
	tics = 5,
	nextstate = S_GIRLALGOLLEN_ROLL3
}

states[S_GIRLALGOLLEN_ROLL3] = {
	sprite = SPR_ALGO,
    frame = C,
	tics = 5,
	nextstate = S_GIRLALGOLLEN_ROLL4
}

states[S_GIRLALGOLLEN_ROLL4] = {
	sprite = SPR_ALGO,
    frame = D,
	tics = 5,
	nextstate = S_GIRLALGOLLEN_ROLL5
}

states[S_GIRLALGOLLEN_ROLL5] = {
	sprite = SPR_ALGO,
    frame = E,
	tics = 5,
	nextstate = S_GIRLALGOLLEN_ROLL6
}

states[S_GIRLALGOLLEN_ROLL6] = {
	sprite = SPR_ALGO,
    frame = F,
	tics = 5,
	nextstate = S_GIRLALGOLLEN_ROLL7
}

states[S_GIRLALGOLLEN_ROLL7] = {
	sprite = SPR_ALGO,
    frame = G,
	tics = 5,
	nextstate = S_GIRLALGOLLEN_ROLL8
}

states[S_GIRLALGOLLEN_ROLL8] = {
	sprite = SPR_ALGO,
    frame = H,
	tics = 5,
	nextstate = S_GIRLALGOLLEN_ROLL1
}

//Consider S23A

mobjinfo[MT_GIRLALGOLLEN] = {
	//$Name Algollen (Female)
	//$Sprite ALGOA0
	//$Category Forest Items
	doomednum = 3020,
    spawnstate = S_GIRLALGOLLEN,
    seestate = S_GIRLALGOLLEN_ROLL1,
	activesound = sfx_s3k8b,
    radius = 10 * FRACUNIT,
    height = 10 * FRACUNIT,
	spawnhealth = 200,
    flags = MF_SPECIAL | MF_BOUNCE | MF_SHOOTABLE
}

-- Allow players to kick the shell
local function GIRLALGOLLENTouch(special, toucher)
    if (toucher.momx or toucher.momy) then
        special.momx = toucher.momx
        special.momy = toucher.momy
        special.momz = P_AproxDistance(toucher.momx, toucher.momy) / 4

        if (toucher.momz > 0) then
            special.momz = $ + (toucher.momz / 8)
        end
   
        special.state = special.info.seestate
    end
    
    return true
end

addHook("TouchSpecial", GIRLALGOLLENTouch, MT_GIRLALGOLLEN)

-- Make the shell bounce when it hits the ground
local function GIRLALGOLLENBounce(mobj)
    if mobj.momz == 0 then
    
        -- NEW ADDITION: Make the shell stop rolling visually when it stops rolling physically
        if mobj.momx == 0 and mobj.momy == 0 then
			S_StopSoundByID(mobj, sfx_s3k8b)
            mobj.state = mobj.info.spawnstate
        end
        if abs(mobj.momz) > FU*7 then
			mobj.momz = -9*mobj.lastmomz/10
			S_StartSound(mo, mo.info.activesound)
		end
		
        mobj.momz = $ + (P_AproxDistance(mobj.momx, mobj.momy) >> 2)
    end
end

addHook("MobjThinker", GIRLALGOLLENBounce, MT_GIRLALGOLLEN)