using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace SmartRoom
{
///
/// Zentrale Schnittstelle zu allen (virtuellen) Aktoren des Raums.
/// Später kann dieselbe Schnittstelle echte Aktoren über den Arduino ansteuern.
///
public class RoomDevices : MonoBehaviour
{
public readonly List windows = new List();
public readonly List blinds = new List();
public readonly List heaters = new List();
public readonly List vents = new List();
public LightActuator lights;
public ProjectorActuator projector;
public DoorActuator door;
public Light sun;
public RoomEnvironment env;
static readonly Color AmbSky = new Color(0.62f, 0.68f, 0.78f);
static readonly Color AmbEquator = new Color(0.52f, 0.52f, 0.54f);
static readonly Color AmbGround = new Color(0.30f, 0.29f, 0.28f);
// ---------------------------------------------------------------- Fenster
public void SetWindows(WindowState s) { foreach (var w in windows) w.target = s; }
public WindowState WindowTarget => windows.Count > 0 ? windows[0].target : WindowState.Closed;
public WindowState WindowCurrent => windows.Count > 0 ? windows[0].Current : WindowState.Closed;
public bool WindowsMoving => windows.Exists(w => w.IsMoving);
public float WindowAirflow => Average(windows, w => w.Airflow);
public string WindowLabel => WindowsMoving ? (WindowTarget == WindowState.Closed ? "schließt …" : "öffnet …") : WindowCurrent.Label();
// ---------------------------------------------------------------- Jalousien
public void SetBlinds(float v) { foreach (var b in blinds) b.target = Mathf.Clamp01(v); }
public float BlindTarget => blinds.Count > 0 ? blinds[0].target : 0f;
public float BlindPosition => Average(blinds, b => b.Current);
public float BlindClosure => Average(blinds, b => b.Closure);
// ---------------------------------------------------------------- Heizung
public void SetHeating(float v) { foreach (var h in heaters) h.target = Mathf.Clamp01(v); }
public float HeatingTarget => heaters.Count > 0 ? heaters[0].target : 0f;
public float HeatingLevel => Average(heaters, h => h.Current);
// ---------------------------------------------------------------- Lüftung
public void SetVentilation(float v) { foreach (var x in vents) x.target = Mathf.Clamp01(v); }
public float VentTarget => vents.Count > 0 ? vents[0].target : 0f;
public float VentLevel => Average(vents, x => x.Current);
// ---------------------------------------------------------------- Licht
public void SetLights(float v) { if (lights != null) lights.target = Mathf.Clamp01(v); }
public float LightTarget => lights != null ? lights.target : 0f;
public float LightLevel => lights != null ? lights.Current : 0f;
public bool BeamerOn => projector != null && projector.beamerOn;
public float DoorOpenness => door != null ? door.Openness : 0f;
static float Average(List list, System.Func f)
{
if (list.Count == 0) return 0f;
float s = 0f;
foreach (var x in list) s += f(x);
return s / list.Count;
}
/// Sonne + Umgebungslicht passend zu Sonneneinstrahlung, Jalousien und Kunstlicht.
void Update()
{
float solar = env != null ? env.solar : 300f;
float day = Mathf.Clamp01(solar / 800f);
if (sun != null)
{
sun.intensity = Mathf.Lerp(0.08f, 1.25f, day);
sun.color = Color.Lerp(new Color(1f, 0.8f, 0.65f), new Color(1f, 0.97f, 0.92f), day);
}
float k = 0.22f + 0.5f * LightLevel + 0.45f * day * (1f - 0.85f * BlindClosure);
RenderSettings.ambientMode = AmbientMode.Trilight;
RenderSettings.ambientSkyColor = AmbSky * k;
RenderSettings.ambientEquatorColor = AmbEquator * k;
RenderSettings.ambientGroundColor = AmbGround * k;
}
}
}