@name Hatch
@inputs Hatch:entity
@persist FakeHatch:entity
@persist Open Moving A WaitingToEnter:array AB

interval(100)
runOnLast(1)

if(first()) {
    FakeHatch = propSpawn(Hatch:model(), Hatch:pos(), 1)
    FakeHatch:setAlpha(0)
    
    holoCreate(1)
    holoMaterial(1, Hatch:getMaterial())
    holoModel(1, Hatch:model())
    holoPos(1, Hatch:pos())
    
    holoCreate(2)
    holoModel(2, "models/props_c17/oildrum001.mdl")
    holoAng(2, entity():toWorld(ang(90, 0, 0)))
    holoPos(2, Hatch:toWorld(vec(-40, -23, -5)))
    
    holoCreate(3)
    holoScale(3, vec())
    holoPos(3, Hatch:toWorld(vec(-40, -23, -5)))
    
    holoParent(1, 2)
    holoParent(2, 3)
    
    Hatch:setAlpha(0)
    
    entity():setAlpha(0)
}

if(!Open & !Moving) {
    findIncludeClass("player")
    findInSphere(Hatch:pos(), 500)
    find()
    foreach(ID, Ply:entity=findToArray()) {
        if(changed(Ply:keyUse()) & Ply:keyUse() & Ply:aimEntity() == Hatch) {
            moneyRequest(Ply, 3000, "Open Hatch?")
        }
    }
}

if(moneyClk()) {
    if(!Open) {Open = 1}
    WaitingToEnter:pushEntity(moneyClkPlayer())
}

if(Open) {
    if(A != 60) {A++}
} else {
    if(A != 0) {A--}
}

if(changed(A)) {Moving = 1 holoAng(3, entity():toWorld(ang(0, 0, -A)))}
else {Moving = 0}

if(changed(Moving) & !first()) {
    if(Moving) {
        FakeHatch:soundPlay("doors/doormove7.wav", 0, "doors/doormove7.wav")
    } else {
        soundStop("doors/doormove7.wav")
        FakeHatch:soundPlay("DoorStop", 4, "doors/drawbridge_stop1.wav")
    }
}

if(Moving) {
    FakeHatch:setPos(holoEntity(1):pos())
    FakeHatch:setAng(holoEntity(1):angles())
} elseif(Open) {
    AB = 1
    
    findIncludeClass("player")
    findInSphere(Hatch:pos(), 10)
    find()
    foreach(ID, Ply:entity=findToArray()) {
        Ply:teleport(Hatch:toWorld(vec(0, 0, -200)))
        AB = 0
        stoptimer("Close")
    }
    
    if(AB) {timer("Close", 3000)}
    
    if(clk("Close")) {
        Open = 0
    }
}

if(last()) {
    FakeHatch:propDelete()
    Hatch:setAlpha(255)
}
