@name C. Box
@persist T M P
@persist [E O H B]:entity
if(first())
{
    E = entity()
    O = owner()
    
    runOnTick(1)
    runOnLast(1)
    
    H = holoCreate(1,O:shootPos(),vec(0),O:eyeAngles())
    cameraCreate(1,O:shootPos(),O:eyeAngles())
    cameraParent(1,H)
    
    B = holoCreate(2,O:boxCenterW(),vec(1.75,1.75,2.1),O:angles())
    holoModel(2,"models/props_junk/cardboard_box001a.mdl")
    
    holoCreate(3)
    holoModel(3,"models/sprops/cylinders/size_2/cylinder_3x480.mdl")
    holoMaterial(3,"models/debug/debugwhite")
    holoColor(3,vec4(255,0,0,0))
}

if(changed(numPlayers()))
{
    holoVisible(3,players(),0)
    holoVisible(3,O,1)
}

Tggl = O:keyDuck()&O:keyZoom()
if(changed(Tggl)&Tggl)
{
    T = !T
    
    M = T ? O:isCrouch() : 0
}

if(T)
{
    if(changed(O:isCrouch()))
    {
        M = O:isCrouch()
    }
    
    holoPos(1,O:keyAttack2() ? O:shootPos()+O:eye()*50+vec(0,0,5) : O:shootPos()+vec(0,0,50)-O:eye()*150)
    holoAng(1,O:eyeAngles())
    
    holoPos(2,O:boxCenterW()+vec(0,0,(200-P*200)+clamp(O:vel():length(),-100,100)/4.5))
    holoPos(2,B:toWorld(vec(-5,-15,-5)))
    holoAng(2,O:angles():setYaw(O:eyeAngles():yaw()+90))
    
    holoPos(3,(O:shootPos()+O:aimPos())/2)
    holoAng(3,O:eyeAngles()+ang(90,0,0))
    holoScale(3,vec(0.25,0.25,O:aimPos():distance(O:shootPos())/480))
    holoAlpha(3,O:keyAttack2()*M*150)
}

if(M)
{
    P+=0.05
}else
{
    P-=0.05
}
P = clamp(P,0,1)
if(changed(P))
{
    holoAlpha(2,P*255)
    holoPos(2,O:boxCenterW()+vec(0,0,(200-P*200)+clamp(O:vel():length(),-100,100)/4.5))
    holoPos(2,B:toWorld(vec(-5,-15,-5)))
}

if(changed(M))
{
    print(4,"Box "+toString(M):replace("1","activated."):replace("0","deactivated."))
    
    cameraToggle(1,M)
    
    holoAlpha(3,M*150)
    
    B:soundPlay("equip",0.823,"player/jump.wav")
}

if(last())
{
    cameraToggle(1,0)
    cameraRemove(1)

    holoDeleteAll()
}
