@name FailCake TimeCar
@inputs Pod:wirelink Cam:wirelink Driver:entity
@persist E:entity [Ps Front Back LEF RIG Center]:ranger W A S D SHIFT GEARS:array
@persist Vel LasVel:vector Active Gear MaxGear Spd Pressing Mode LasMove MaxSpeed CanMove Ang Breakvel Pos:vector
@persist Angs:angle Pitch Br BREAK Dead StorePos:vector Charge MaxCrg Beep BUM M1 Alp
@persist StoreAng:angle Normalize StoreSpeed Reset
@trigger all


if(first()){
runOnTick(1)
propSpawnEffect(0)

E = entity()    
Alpha = 255
holoCreate(1)
holoModel(1,"models/props_vehicles/car004a_physics.mdl")    
holoScale(1,vec(1,1,1)/10)
holoPos(1,E:toWorld(vec(0,0,2)))

holoCreate(2)
holoModel(2,"models/dav0r/thruster.mdl")    
holoScale(2,vec(1,1,1)/10)
holoAng(2,ang(-90,0,0))
holoParent(2,holoEntity(1))
holoPos(2,E:toWorld(vec(-10.2,0,2)))

holoCreate(3)
holoModel(3,"models/Effects/muzzleflash/minigunmuzzle.mdl")    
holoScale(3,vec(1,1,1)/10)
holoAng(3,ang(-180,0,0))
holoParent(3,holoEntity(1))
holoAlpha(3,0)
holoPos(3,E:toWorld(vec(-10.9,0,2)))

## FADE EFFECT AFTER BOOM
holoCreate(4)
holoModel(4,"cube")    
holoScale(4,vec(0.1,3,3))
holoColor(4,vec(1,1,1))
holoParent(4,holoEntity(1))
holoAlpha(4,0)
holoPos(4,E:toWorld(vec(-13,0,5)))

## FIRE LOL

holoCreate(5)
holoModel(5,"models/Effects/muzzleflash/minigunmuzzle.mdl")    
holoScale(5,vec(1,1,1)/10)
holoAng(5,ang(-90,0,0))
holoParent(5,holoEntity(1))
holoAlpha(5,0)
holoPos(5,E:toWorld(vec(4,0,3)))

holoCreate(6)
holoModel(6,"models/Effects/muzzleflash/minigunmuzzle.mdl")    
holoScale(6,vec(1,1,1)/12)
holoAng(6,ang(-90,40,0))
holoParent(6,holoEntity(1))
holoAlpha(6,0)
holoPos(6,E:toWorld(vec(4,1,3)))

holoCreate(7)
holoModel(7,"models/Effects/muzzleflash/minigunmuzzle.mdl")    
holoScale(7,vec(1,1,1)/13)
holoAng(7,ang(-90,-40,0))
holoParent(7,holoEntity(1))
holoAlpha(7,0)
holoPos(7,E:toWorld(vec(4,-2,3)))

holoCreate(23)
holoModel(23,"cube")
holoPos(23,E:toWorld(vec(0,0,0)))
holoScale(23,vec(0.1,0.1,0.1))

Gear = 0
MaxSpeed = 5
Br = 0.01
MaxCrg = 3

## The Gears
GEARS:pushNumber(1) # Gear1
GEARS:pushNumber(2.5) # Gear 2
GEARS:pushNumber(3.8) # Gear 3
GEARS:pushNumber(4.5) # Gear 4
}

Active = Pod:number("Active")


## Car Rangers COLISIONS - Done
rangerFilter(holoEntity(1))
rangerFilter(holoEntity(2))
rangerFilter(holoEntity(3))

Center = rangerOffset(5,holoEntity(1):toWorld(vec(0,0,0)),-holoEntity(1):up())

Ps = rangerOffset(5,holoEntity(1):toWorld(vec(5,0,0)),-holoEntity(1):up())
Ps2 = rangerOffset(5,holoEntity(1):toWorld(vec(-5,0,0)),-holoEntity(1):up())
LEF = rangerOffset(4,holoEntity(1):pos(),-holoEntity(1):right())
RIG = rangerOffset(4,holoEntity(1):pos(),holoEntity(1):right())
Front = rangerOffset(9,holoEntity(1):pos(),holoEntity(1):forward())
Back = rangerOffset(9,holoEntity(1):pos(),-holoEntity(1):forward())


StoreAng = holoEntity(1):angles()


Angs = holoEntity(23):angles()
Pitch = Angs:pitch()

holoPos(23,Center:position())
holoAng(23,Center:entity():angles())

holoAng(1,holoEntity(1):angles() + ang(Pitch,Ang,0))




if(changed(Gear) & Gear & Mode != 0){
if(Gear == 1){
holoEntity(1):soundPlay(1,0,"vehicles/v8/first.wav")    
}elseif(Gear == 2){
holoEntity(1):soundPlay(1,0,"vehicles/v8/second.wav") 
}elseif(Gear == 3){
holoEntity(1):soundPlay(1,0,"vehicles/v8/third.wav") 
}elseif(Gear == 4){
holoEntity(1):soundPlay(1,0,"vehicles/v8/fourth_cruise_loop2.wav") 
}       
}

if(changed(Spd) & Spd == 0 & Mode == 0 & Active != 0){
holoEntity(1):soundPlay(1,0,"vehicles/v8/v8_idle_loop1.wav")    
}

if(changed(Mode) & Mode == 0){
if(Spd >= 2.7){
holoEntity(1):soundPlay(1,0,"vehicles/v8/v8_throttle_off_fast_loop1.wav")     
}else{
holoEntity(1):soundPlay(1,0,"vehicles/v8/v8_throttle_off_slow_loop2.wav") 
}    
}

if(changed(BREAK) & BREAK ){
if(Spd > 2.6){
holoEntity(1):soundPlay(2,0,"vehicles/v8/skid_highfriction.wav") 
}elseif(Spd > 0.5){
holoEntity(1):soundPlay(2,0,"vehicles/v8/skid_normalfriction.wav") 
}    
}

## On and off SOunds
if(changed(Active) & Active){
holoEntity(1):soundPlay(1,0,"vehicles/v8/v8_start_loop1.wav")    
}

if(changed(Active) & Active == 0){
holoEntity(1):soundPlay(1,0,"vehicles/v8/v8_stop1.wav")    
}

if(Active){
    
## CONTROLS
W = Pod:number("W")
S = Pod:number("S")
D = Pod:number("D")
A = Pod:number("A")
BREAK = Pod:number("Space")
SHIFT = Pod:number("Shift")
Reset = Pod:number("R")
M2 = Pod:number("Mouse2")
M1 = Pod:number("Mouse1")
## CONTROLS

## CAMERA CONTROLS ##
Angles = Driver:driver():eyeAngles()
Cam:setVector("Position",holoEntity(1):toWorld(vec(0,0,0)))
Cam:setAngle("Angle",holoEntity(1):angles())
## CAMERA CONTROLS ##

## HOLO SPEED <-

if(tickClk()){
Vel = holoEntity(1):pos():distance( LasVel )
LasVel = holoEntity(1):pos()
}

## MOVEMENT ##
if(Dead == 0){
if(Spd > 0.3 | Spd < -0.3){
if(A){
Ang= 0.8
}elseif(D){
Ang= -0.8
}elseif(A == 0 & D == 0){
Ang = 0
}
}else{
Ang = 0
}
}

if(changed(Reset) & Reset){
reset()    
}

if(M1){
holoEntity(1):soundPlay(34,0,"/synth/square.wav") 
soundVolume(34,0.2)   
}else{
soundStop(34)
}

if(BREAK){
if(Spd > 2.6){
Breakvel = 0.06  
}else{
Breakvel = 0.08
}
}else{
Breakvel = 0.02
}


if(Dead == 0){
if(Mode == 1){
if(Spd < MaxSpeed){
Spd+=Br
}
}elseif(Mode == -1){
if(Spd > -MaxSpeed){
Spd-=Br 
}
}elseif(Mode == 0){

if(LasMove == 1){
if(Spd > 0){
Spd-=Breakvel
}
}elseif(LasMove == -1){
if(Spd < 0){
Spd+=Breakvel
}
}
}
}

## GEARS ##

if(Vel >= GEARS[Gear,number] & Gear < GEARS:count()){
Gear+=1
}elseif(Vel < GEARS[Gear,number] & Gear > 0 & Mode == 0){
Gear-=1
} 


## W AND S
Pos = Center:position()   

if(Dead == 0){
if(W){
Mode = 1  

if(Spd > 0){
LasMove = 1
}

}elseif(S){
Mode = -1
if(Spd < 0){
LasMove = -1
}
 
}else{
Mode = 0 
}
}else{
Spd = 0
}


if(clk("RealPos")){
holoPos(1,StorePos)    
}

if(Dead == 0){
holoPos(1,Pos + holoEntity(1):forward()*Spd + vec(0,0,2.1)) 
}

if(changed(SHIFT) & SHIFT){
holoEntity(1):soundPlay(78,0,"weapons/physcannon/superphys_hold_loop.wav")    
} 


if(SHIFT & BUM == 0){
if(Charge < MaxCrg){
Charge += 0.01  
}
}elseif(SHIFT == 0 | Vel > 0.1){
if(Charge > 0){
Charge -= 0.02  
}else{
Beep = 0
}
}

if(Charge >= MaxCrg){  
    
if(Beep == 0){
Beep = 1
holoEntity(1):soundPlay(55,0,"buttons/blip1.wav")       
}    

if(changed(M2) & M2){
BUM = 1
Charge = 0    
} 
   
} 

soundPitch(78,Charge*20) 

if(changed(BUM) & BUM == 1){
soundStop(78)
soundStop(55)
holoAlpha(3,255)
holoEntity(1):soundPlay(58,0,"thrusters/rocket04.wav") 
StoreSpeed = Spd
Spd+=10
}

if(BUM == 1){
holoAng(3,holoEntity(1):toWorld(ang(-180,0,curtime()*1000)))

if(Alp < 20){
Alp+=0.1
}else{
BUM = 0
Alp = 0
Beep = 0
holoAlpha(3,0)
soundStop(58,1)

if(Spd > MaxSpeed){
Normalize = 1
}

}

}

if(Normalize){
if(Spd > StoreSpeed){
Spd-=0.3   
}else{
Normalize = 0  
Spd = StoreSpeed
}    

} 

}elseif(Active == 0){
LasVel = holoEntity(1):pos()

soundPitch(78,Charge*20) 
BUM = 0
Alp = 0
Normalize = 0

if(Charge > 0){
Charge -= 0.02 
}else{
soundStop(78)
Beep = 0
Charge = 0
}
Ang = 0
Spd = 0
Vel = 0
}



