namespace TimeWarpers {
using UnityEngine;
using uFrame.Kernel;
using UniRx;
using System;
public enum CarType {
Tesla,
PoliceCar
}
public partial class CarTypeSystem {
// Setup all the CarType behaviour. No need to think about Networking here.
// This logic happens for Local & Remote (owner and non-owner)
protected override void OnCarTypeSwitcherCreated( CarTypeSwitcher carTypeSwitcher ) {
// Listen for the latest new value
carTypeSwitcher.CarTypeLatestUniqueObservable
.Subscribe(carType => {
carTypeSwitcher.TeslaLogo.SetActive(carType == CarType.Tesla);
carTypeSwitcher.PoliceLights.SetActive(carType == CarType.PoliceCar);
}).DisposeWith(carTypeSwitcher);
}
// Local: Logic for the owner of the net object
protected override void OnLocalCarTypeSwitcherCreated( LocalCarTypeSwitcher group ) {
// The Local player (object owner) can use keyboard input
RxInput.OnKeyDown(KeyCode.Alpha1)
.Subscribe(_ => group.CarTypeSwitcher.CarType = CarType.Tesla)
.DisposeWith(group);
RxInput.OnKeyDown(KeyCode.Alpha2)
.Subscribe(_ => group.CarTypeSwitcher.CarType = CarType.PoliceCar)
.DisposeWith(group);
// Send local changes to the server
// Only send if the value changes
// Don't send more often than every 50ms
group.CarTypeSwitcher.CarTypeLatestUniqueObservable
.Sample( TimeSpan.FromMilliseconds(50) )
.Subscribe(carType =>
group.NetEntity.tno.SetCustomType("CarType", carType)
).DisposeWith(group);
}
// Remote: Logic for the players that don't own the object
protected override void OnRemoteCarTypeSwitcherCreated( RemoteCarTypeSwitcher group ) {
// Listen for changes to the server variable and apply them locally
group.NetEntity.GetLatestCustomType("CarType", CarType.Tesla)
.Subscribe(carType =>
group.CarTypeSwitcher.CarType = carType
).DisposeWith(group);
}
}
}