class KActor_Ball extends KActor
placeable;

var() float MaxNetUpdateInterval;
var float NextNetUpdateTime;

var KRigidBodyState KState, KRepState;
var bool bNewKState;
var int StateCount, LastStateCount;

replication
{
    unreliable if(Role == ROLE_Authority)
        KRepState, StateCount;
}

function postnetbeginplay()
{
settimer(30,false);
}

function Tick(float Delta)
{
    PackState();
}

function timer()
{
destroy();
}

//Pack current state to be replicated
function PackState()
{
    local bool bChanged;

    if(!KIsAwake())
        return;

    KGetRigidBodyState(KState);

    bChanged = Level.TimeSeconds > NextNetUpdateTime;
    bChanged = bChanged || VSize(KRBVecToVector(KState.Position) - KRBVecToVector(KRepState.Position)) > 5;
    bChanged = bChanged || VSize(KRBVecToVector(KState.LinVel) - KRBVecToVector(KRepState.LinVel)) > 1;
    bChanged = bChanged || VSize(KRBVecToVector(KState.AngVel) - KRBVecToVector(KRepState.AngVel)) > 1;

    if(bChanged)
    {
        NextNetUpdateTime = Level.TimeSeconds + MaxNetUpdateInterval;
        KRepState = KState;
        StateCount++;
    }
    else
        return;
}

//New state recieved.
simulated event PostNetReceive()
{
    if(StateCount == LastStateCount)
        return;
}

//Apply new state.
simulated event bool KUpdateState(out KRigidBodyState newState)
{
    //This should never get called on the server - but just in case!
    if(Role == ROLE_Authority || StateCount == LastStateCount)
        return false;

    //Apply received data as new position of actor.
    newState = KRepState;
    StateCount = LastStateCount;

    return true;
}

event KApplyForce(out vector Force, out vector Torque)
{
  Force = Vector(Rotation) * -0.00001;
  Torque = Force;
}

defaultproperties
{
     StaticMesh=StaticMesh'RcBall.ball1'
     bNoDelete=False
     bAlwaysRelevant=True
     RemoteRole=ROLE_SimulatedProxy
     DrawScale=2
     bCollideWorld=True
     bNetNotify=True
     Begin Object Class=KarmaParamsRBFull Name=KarmaParams0
         KMass=2.000000
         KLinearDamping=0.000000
         KAngularDamping=0.000000
         KBuoyancy=2.500000
         KStartEnabled=True
         KActorGravScale=0.750000
         bClientOnly=False
         bKDoubleTickRate=True
         bKAllowRotate=True
         KFriction=1.000000
         KRestitution=1.000000
         KImpactThreshold=1.000000
     End Object
     KParams=KarmaParamsRBFull'R.KActor_Ball.KarmaParams0'

}
