Make work with astra

This commit is contained in:
Andras Schmelczer 2026-09-14 08:01:49 +01:00
commit c8ee5045e2
11 changed files with 6338 additions and 4613 deletions

View file

@ -82,6 +82,8 @@ public class CarClient : NetworkBehaviour {
[ClientRpc(channel = 0)]
public void RpcDecodeBits (ulong newStateBits)
{
if (stateBuffer == null)
stateBuffer = new StateBuffer(delay);
CompressedStateFrame compressedStateFrame = new CompressedStateFrame(newStateBits);
StateFrame newStateFrame = compressedStateFrame.GetDecodedStateFrame(transform);
stateBuffer.AddFrame(ref newStateFrame);

View file

@ -1,11 +1,206 @@
fileFormatVersion: 2
guid: a77c00a63a6d7274dafec539e3ac85b8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
// 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);
}
}

View file

@ -46,8 +46,9 @@ public class TrafficController : NetworkBehaviour {
private float timePassedSpawn = 0;
private int current = 0;
void Start () {
if (!isServer) return;
public override void OnStartServer () {
base.OnStartServer();
url = url.TrimEnd('/');
trafficLights = new TrafficLights();

View file

@ -1,40 +1,44 @@
using System.Collections;
using System.Collections.Generic;
using System;
using System.Collections;
using UnityEngine;
/* Itt lehet állítani, hogy melyik build legyen szerver és melyik kliens.
* WebGL szerver nem lehet.
*/
public class canvasChooser : MonoBehaviour
{
public GameObject serverStartCanvas;
public GameObject clientStartCanvas;
public GameObject mainCamera;
public class canvasChooser : MonoBehaviour {
private IEnumerator Start()
{
string[] args = Environment.GetCommandLineArgs();
bool server = Array.IndexOf(args, "--server") >= 0;
serverStartCanvas.SetActive(server);
clientStartCanvas.SetActive(!server);
mainCamera.SetActive(!server);
public GameObject serverStartCanvas;
public GameObject clientStartCanvas;
public GameObject mainCamera;
// Use this for initialization
void Start () {
#if UNITY_STANDALONE
serverStartCanvas.SetActive(false);
clientStartCanvas.SetActive(true);
mainCamera.SetActive(true);
/*serverStartCanvas.SetActive(true);
clientStartCanvas.SetActive(false);
mainCamera.SetActive(false);*/
#endif
#if UNITY_WEBGL
serverStartCanvas.SetActive(false);
clientStartCanvas.SetActive(true);
mainCamera.SetActive(true);
#endif
#if UNITY_EDITOR
serverStartCanvas.SetActive(false);
clientStartCanvas.SetActive(true);
mainCamera.SetActive(true);
#endif
}
// Let the scene finish starting before UNet activates its scene objects.
yield return null;
if (server)
{
serverStartMenu menu = serverStartCanvas.GetComponent<serverStartMenu>();
string trafficUrl = Argument(args, "--traffic-url");
string eventUrl = Argument(args, "--event-url");
if (trafficUrl != null) menu.trafficAddress.text = trafficUrl.TrimEnd('/');
if (eventUrl != null) menu.eventAddress.text = eventUrl.TrimEnd('/');
menu.createServer();
Debug.Log("Simulation server started on port " + menu.network.networkPort);
}
else
{
string address = Argument(args, "--connect");
if (address != null)
clientStartCanvas.GetComponent<clientMenu>().joinServer(address);
}
}
private static string Argument(string[] args, string name)
{
int index = Array.IndexOf(args, name);
return index >= 0 && index + 1 < args.Length ? args[index + 1] : null;
}
}

View file

@ -1,40 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/* Itt lehet állítani, hogy melyik build legyen szerver és melyik kliens.
* WebGL szerver nem lehet.
*/
public class canvasChooser : MonoBehaviour {
public GameObject serverStartCanvas;
public GameObject clientStartCanvas;
public GameObject mainCamera;
// Use this for initialization
void Start () {
#if UNITY_STANDALONE
serverStartCanvas.SetActive(false);
clientStartCanvas.SetActive(true);
mainCamera.SetActive(true);
/*serverStartCanvas.SetActive(true);
clientStartCanvas.SetActive(false);
mainCamera.SetActive(false);*/
#endif
#if UNITY_WEBGL
serverStartCanvas.SetActive(false);
clientStartCanvas.SetActive(true);
mainCamera.SetActive(true);
#endif
#if UNITY_EDITOR
serverStartCanvas.SetActive(false);
clientStartCanvas.SetActive(true);
mainCamera.SetActive(true);
#endif
}
}
fileFormatVersion: 2
guid: a5533452e90b11345b3c3510cc82a51f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -30,6 +30,8 @@ public class clientGameMenu : NetworkBehaviour {
[ClientRpc(channel = 0)]
public void RpcSetNewTweet (Tweet newTweet)
{
if (tweetAnimator == null)
tweetAnimator = new TweetAnimator(0.5f, 7.5f, tweetContainer);
tweetAnimator.SetNewTweet(newTweet);
}

View file

@ -34,8 +34,6 @@ public class serverRunningMenu : NetworkBehaviour
if (timePassed < 1) timePassed += Time.deltaTime;
else
{
Debug.Log(NetworkServer.connections);
Debug.Log(NetworkServer.connections.Count);
timePassed = 0;

View file

@ -1,33 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
[NetworkSettings(channel = 0, sendInterval = 0)]
public class serverStartMenu : NetworkBehaviour
{
public TrafficController traffic;
public tweetPoller tweet;
public InputField trafficAddress;
public InputField eventAddress;
public GameObject next;
public NetworkManager network;
public void createServer ()
{
if(trafficAddress.text != "")
traffic.url = trafficAddress.text;
if (eventAddress.text != "")
tweet.url = eventAddress.text;
network.StartServer();
gameObject.SetActive(false);
next.SetActive(true);
}
}
fileFormatVersion: 2
guid: 02335c5d9c8e07347918bd15ceb1adc2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,17 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class destroyCar : NetworkBehaviour {
public carCount text;
void OnTriggerEnter (Collider other)
{
if(isServer) {
text.IncrementValue();
Destroy(other.gameObject);
}
}
}
fileFormatVersion: 2
guid: 88b7ddbf951e255478383ee9f8ecc0b2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -19,12 +19,13 @@ public class networkManagerCustom : NetworkManager
offlineCanvas.SetActive(true);
offlineCanvas.GetComponent<clientMenu>().errorField.text = "Error: " + conn.lastError.ToString();
base.OnServerDisconnect(conn);
base.OnClientDisconnect(conn);
}
public override void OnClientConnect (NetworkConnection conn)
{
Debug.Log("Simulation client connected to " + networkAddress);
//onlineCanvas.SetActive(true);
offlineCanvas.GetComponent<clientMenu>().errorField.text = "No errors so far.";

View file

@ -1,11 +1,94 @@
fileFormatVersion: 2
guid: ed316f5bd09830c46bd502b7cb2bed3f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 200
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
// Recovered from the bundled Windows server assembly; UNet-generated code omitted.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class trafficLightColor : NetworkBehaviour
{
private Renderer render;
public float delay = 1.5f;
private Color[] newColors;
private float timeOfUpdate;
private bool dirty;
public float blinkInterval = 1f;
private float blinkTime;
private bool blinkingIsYellow;
private bool areLightsOff;
private Dictionary<string, Color> colorLUT = new Dictionary<string, Color>
{
{
"red",
Color.red
},
{
"yellow",
Color.yellow
},
{
"green",
Color.green
},
{
"off",
Color.white
}
};
private void Start()
{
render = GetComponent<Renderer>();
}
private void Update()
{
if (dirty && Time.time - timeOfUpdate > delay)
{
render.materials[5].color = newColors[0];
render.materials[7].color = newColors[1];
render.materials[4].color = newColors[2];
render.materials[3].color = newColors[3];
areLightsOff = render.materials[5].color == Color.white || render.materials[7].color == Color.white || render.materials[4].color == Color.white || render.materials[3].color == Color.white;
dirty = false;
}
if (areLightsOff)
{
render.materials[5].color = ((!blinkingIsYellow) ? Color.white : Color.yellow);
render.materials[7].color = ((!blinkingIsYellow) ? Color.white : Color.yellow);
render.materials[4].color = ((!blinkingIsYellow) ? Color.white : Color.yellow);
render.materials[3].color = ((!blinkingIsYellow) ? Color.white : Color.yellow);
}
blinkTime += Time.deltaTime;
if (blinkTime > blinkInterval)
{
blinkTime = 0f;
blinkingIsYellow = !blinkingIsYellow;
}
}
[ClientRpc]
public void RpcUpdateColors(string north, string east, string south, string west)
{
if (!dirty)
{
newColors = new Color[4]
{
colorLUT[north],
colorLUT[east],
colorLUT[south],
colorLUT[west]
};
timeOfUpdate = Time.time;
dirty = true;
}
}
}