mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-02-20 15:41:01 +00:00
work on it
This commit is contained in:
parent
5bda7e56e0
commit
6066e227a2
@ -1,19 +1,63 @@
|
||||
using System;
|
||||
using QSB.Player;
|
||||
using QSB.WorldSync;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace QSB.EchoesOfTheEye.LightSensorSync;
|
||||
|
||||
[RequireComponent(typeof(LightSensor))]
|
||||
/// <summary>
|
||||
/// stores a bit of extra data needed for player light sensor sync
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(SingleLightSensor))]
|
||||
public class QSBPlayerLightSensor : MonoBehaviour
|
||||
{
|
||||
private LightSensor _lightSensor;
|
||||
private void Awake() => _lightSensor = GetComponent<LightSensor>();
|
||||
private SingleLightSensor _lightSensor;
|
||||
|
||||
internal bool _locallyIlluminated;
|
||||
|
||||
public Action OnDetectLocalLight;
|
||||
public Action OnDetectLocalDarkness;
|
||||
|
||||
internal readonly List<uint> _illuminatedBy = new();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_lightSensor = GetComponent<SingleLightSensor>();
|
||||
|
||||
RequestInitialStatesMessage.SendInitialState += SendInitialState;
|
||||
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
RequestInitialStatesMessage.SendInitialState -= SendInitialState;
|
||||
QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
|
||||
}
|
||||
|
||||
private void SendInitialState(uint to)
|
||||
{
|
||||
// todo send the messages
|
||||
}
|
||||
|
||||
private void OnPlayerLeave(PlayerInfo player) => SetIlluminated(player.PlayerId, false);
|
||||
|
||||
public void SetIlluminated(uint playerId, bool locallyIlluminated)
|
||||
{
|
||||
var illuminated = _illuminatedBy.Count > 0;
|
||||
if (locallyIlluminated)
|
||||
{
|
||||
_illuminatedBy.SafeAdd(playerId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_illuminatedBy.QuickRemove(playerId);
|
||||
}
|
||||
|
||||
if (!illuminated && _illuminatedBy.Count > 0)
|
||||
{
|
||||
_lightSensor._illuminated = true;
|
||||
_lightSensor.OnDetectLight.Invoke();
|
||||
}
|
||||
else if (illuminated && _illuminatedBy.Count == 0)
|
||||
{
|
||||
_lightSensor._illuminated = false;
|
||||
_lightSensor.OnDetectDarkness.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,5 +23,9 @@ public class RequestInitialStatesMessage : QSBMessage
|
||||
DebugLog.DebugWrite($"sent initial states to {to}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// called on the host.
|
||||
/// use this to send initial states to whoever is asking for it.
|
||||
/// </summary>
|
||||
public static event Action<uint> SendInitialState;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user