@name metamount2
@inputs Camera:wirelink Vehicle:entity
@outputs 
@persist Base:entity DestHeight Ang:angle [Pos]:vector Gravity BreakFactor Acceleration Speed MaxSpeed
@trigger 
@model models/maxofs2d/cube_tool.mdl

interval(60)
if (first()) {
    DestHeight = 50
    MaxSpeed = 30
    Acceleration = 5
    BreakFactor = 0.8
    
    Camera["Distance", number] = 100
    Camera["Parent", entity] = entity()
    
    Base = entity()
    
    Pos = entity():pos()
    
    holoCreate(1)
    holoModel(1, "models/sprops/misc/origin.mdl")
    
    rangerFilter(Base)
    rangerPersist(1)
}

Camera["Activated", number] = Vehicle:driver():isValid()



local RD = rangerOffset(DestHeight * 2, Pos, vec(0, 0, -1))

#local AimDir = owner():eye():setZ(0):normalized()
local AimDir = Vehicle:toLocalAxis( Vehicle:driver():eye() ):setZ(0):normalized()
local SideDir = AimDir:cross(vec(0, 0, 1))
local Strafe = 0
local Forward = 0

if (owner():keyPressed("up")) {
    Forward = 1
}
elseif (owner():keyPressed("down")) {
    Forward = -1
}

if (owner():keyPressed("right")) {
    Strafe = 1
}
elseif (owner():keyPressed("left")) {
    Strafe = -1
}

if (owner():keyPressed("up") || owner():keyPressed("down") || owner():keyPressed("right") || owner():keyPressed("left")) {
    Speed = min(Speed + Acceleration, MaxSpeed)
}
else {
    Speed *= BreakFactor
}

#[if (RD:distance() < DestHeight) {
    Gravity = 0
}
else { 
    Gravity -= 1
}]#



if (RD:distance() <= DestHeight) {
    Gravity = 0
    
    local Diff = RD:distance() - DestHeight
    Pos += Diff * vec(0, 0, -1)
}
else { 
    Gravity -= 1
}


local MoveDir = (AimDir * Forward + SideDir * Strafe):normalized()

Pos += MoveDir * Speed + vec(0, 0, Gravity)

if (MoveDir:length() > 0) {
    Ang = slerp(quat(MoveDir:toAngle()), quat(Ang), 0.5):toAngle()
}

entity():setPos(mix(Pos, entity():pos(), 0.5))
entity():setAng(Ang)
if (!entity():isPlayerHolding()) { entity():propFreeze(1) }
#holoPos(1, mix(Pos, holoEntity(1):pos(), 0.5))
#holoAng(1, Ang)
