@name Damage Detector E2
@inputs Clk Damage On 
@outputs 
@persist Health  
@trigger all
interval(100)

if (first()|dupefinished()) {
    Health = Damage > 0 ? 500 + Damage : 500
}

if (On) {
    if (changed(Clk) & Clk) {
        Health = clamp(Health-Damage,0,500)
    }
}

if (changed(Health == 0) & Health == 0) {
    Prop1 = propSpawn("models/sprops/cylinders/size_4/cylinder_9x18.mdl",1)
    Prop2 = propSpawn("models/sprops/cylinders/size_4/cylinder_9x18.mdl",1)
    Prop3 = propSpawn("models/sprops/cylinders/size_4/cylinder_9x18.mdl",1)
    Prop4 = propSpawn("models/sprops/cylinders/size_4/cylinder_9x18.mdl",1)
    
    Prop1:setPos(owner():aimEntity():pos())
    Prop2:setPos(owner():aimEntity():pos())
    Prop3:setPos(owner():aimEntity():pos())
    Prop4:setPos(owner():aimEntity():pos())
    
    Prop1:propFreeze(0)
    Prop2:propFreeze(0)
    Prop3:propFreeze(0)
    Prop4:propFreeze(0)
    
    owner():aimEntity():propDelete()
}

if (changed(owner():keyUse()) & owner():keyUse() & owner():aimEntity():model() == "models/sprops/cylinders/size_4/cylinder_9x18.mdl") {
    owner():aimEntity():propDelete()
}
