UnityTrafficSimulation/UnityProject/Assets/Scripts/CarLL.cs

206 lines
4.8 KiB
C#

// Recovered from the bundled Windows server assembly; UNet-generated code omitted.
using System;
using UnityEngine;
using UnityEngine.Networking;
[NetworkSettings(channel = 0, sendInterval = 0f)]
public class CarLL : NetworkBehaviour
{
public bool IsBraking;
public bool ShouldExplode;
public bool ShouldDie;
[Header("Networking options")]
public float minUpdateInterval = 0.04f;
public CarClient client;
public float updateIntervalSlow = 0.15f;
public float updateIntervalFast = 0.05f;
public float delay = 2f;
private float timeAlive;
private float timeOfLastUpdate;
[Header("Properites in SI")]
public float maxAcceleration = 7f;
public float maxBraking = -35f;
public float maxVelocity = 12f;
public float maxTurningVelocity = 9f;
public float burningTimeOut = 5f;
private float velocity;
[Header("Turn radii")]
public float rightTurnRadius = 6.5f;
public float leftTurnRadius = -11f;
[Header("Misc")]
public float rearWheelsOffset = -1f;
private float serverSideExplosionTime;
private bool shouldExplodeSent;
private bool shouldDieSent;
private float currentAngle;
private bool turningDirection;
private bool isTurning;
public void StartTurning(bool isLeftTurn)
{
currentAngle = 0f;
turningDirection = isLeftTurn;
isTurning = true;
sendUpdate();
}
public bool IsTurningDone()
{
return !isTurning;
}
private void Start()
{
}
public void FixedUpdate()
{
if (!base.isServer)
{
return;
}
timeAlive += Time.deltaTime;
if (ShouldExplode)
{
if (shouldExplodeSent)
{
if (timeAlive - timeOfLastUpdate > updateIntervalSlow)
{
sendUpdate();
}
if (timeAlive - serverSideExplosionTime > burningTimeOut && !shouldDieSent)
{
ShouldDie = true;
forceSendUpdate();
shouldDieSent = true;
base.transform.position = Vector3.zero;
}
if (timeAlive - serverSideExplosionTime > burningTimeOut + delay * 2f + 10f)
{
UnityEngine.Object.Destroy(base.gameObject);
}
return;
}
forceSendUpdate();
shouldExplodeSent = true;
serverSideExplosionTime = timeAlive;
}
if (isTurning && velocity > maxTurningVelocity)
{
IsBraking = true;
}
velocity += ((!IsBraking) ? maxAcceleration : maxBraking) * Time.deltaTime;
if (velocity > maxVelocity)
{
velocity = maxVelocity;
}
else if (velocity < 0f)
{
velocity = 0f;
}
float num = velocity * Time.deltaTime;
if (isTurning)
{
float num2 = ((!turningDirection) ? rightTurnRadius : leftTurnRadius);
Vector3 point = base.transform.position + base.transform.forward * rearWheelsOffset + base.transform.right * num2;
float num3 = num * 180f / (num2 * (float)Math.PI);
if (Mathf.Abs(currentAngle + num3) >= 90f)
{
num3 = (float)((!turningDirection) ? 90 : (-90)) - currentAngle;
isTurning = false;
}
else
{
currentAngle += num3;
}
base.transform.RotateAround(point, base.transform.up, num3);
if (timeAlive - timeOfLastUpdate > updateIntervalFast)
{
sendUpdate();
}
}
else
{
base.transform.position += base.transform.forward * num;
if (timeAlive - timeOfLastUpdate > updateIntervalSlow)
{
sendUpdate();
}
}
}
private void sendUpdate()
{
if (timeAlive - timeOfLastUpdate > minUpdateInterval)
{
forceSendUpdate();
}
}
private void forceSendUpdate()
{
client.RpcDecodeBits(stateToBits());
timeOfLastUpdate = timeAlive;
}
private ulong stateToBits()
{
ulong stateBits = 0uL;
boolToNthBit(IsBraking, 0, ref stateBits);
fixedFromNthBit(base.transform.position.x, 1, 12, 16f, ref stateBits);
fixedFromNthBit(base.transform.position.z, 13, 12, 16f, ref stateBits);
fixedFromNthBit(base.transform.rotation.eulerAngles.y, 25, 7, 0.35277778f, ref stateBits);
boolToNthBit(ShouldExplode, 32, ref stateBits);
boolToNthBit(ShouldDie, 33, ref stateBits);
boolToNthBit(isTurning, 34, ref stateBits);
fixedFromNthBit(timeAlive, 35, 28, 1000f, ref stateBits);
return stateBits;
}
private void boolToNthBit(bool value, int n, ref ulong stateBits)
{
stateBits |= (ulong)((long)((!value) ? 0 : 1) << n);
}
private void intFromNthBit(int value, int n, int length, ref ulong stateBits)
{
ulong num = (ulong)((1L << length) - 1);
stateBits |= ((ulong)value & num) << n;
}
private void fixedFromNthBit(float value, int n, int length, float multiplier, ref ulong stateBits)
{
int value2 = (int)Mathf.Round(value * multiplier);
intFromNthBit(value2, n, length, ref stateBits);
}
private void OnDrawGizmos()
{
Gizmos.DrawSphere(base.transform.position + base.transform.forward * rearWheelsOffset, 0.15f);
}
}