131 lines
5.2 KiB
C#
131 lines
5.2 KiB
C#
using UnityEngine;
|
||
|
||
namespace SmartRoom
|
||
{
|
||
/// <summary>
|
||
/// Einstiegspunkt: baut den Raum, verdrahtet Simulation, Steuerlogik, Arduino-Anbindung, Kamera und HUD.
|
||
/// Liegt in der Szene "Assets/Scenes/SmartRoom.unity" – wird aber auch in jeder anderen Szene
|
||
/// automatisch erzeugt (siehe <see cref="SmartRoomBootstrap"/>).
|
||
/// </summary>
|
||
[DefaultExecutionOrder(-100)]
|
||
public class SmartRoomApp : MonoBehaviour
|
||
{
|
||
[Tooltip("Classic = ursprünglicher Zwilling mit dunklem Dashboard, Ambient = Optik des Web-Frontends (Ambient²)")]
|
||
public RoomStyle style = RoomStyle.Classic;
|
||
|
||
public RoomDevices Devices { get; private set; }
|
||
|
||
void Awake()
|
||
{
|
||
Devices = RoomBuilder.Build(transform, style);
|
||
|
||
var env = GetOrAdd<RoomEnvironment>();
|
||
env.devices = Devices;
|
||
Devices.env = env;
|
||
|
||
var bridge = GetOrAdd<ArduinoBridge>();
|
||
bridge.env = env;
|
||
bridge.devices = Devices;
|
||
|
||
var weather = GetOrAdd<WeatherService>();
|
||
weather.env = env;
|
||
|
||
var controller = GetOrAdd<ClimateController>();
|
||
controller.env = env;
|
||
controller.devices = Devices;
|
||
bridge.scenarioProvider = () => controller.Scenario;
|
||
|
||
Devices.sun = SetupSun();
|
||
var orbit = SetupCamera();
|
||
QualitySettings.pixelLightCount = Mathf.Max(QualitySettings.pixelLightCount, 8);
|
||
|
||
if (style == RoomStyle.Ambient)
|
||
{
|
||
SetupAmbient(env, controller, bridge, weather, orbit);
|
||
return;
|
||
}
|
||
|
||
var hud = GetOrAdd<SmartRoomHUD>();
|
||
hud.env = env;
|
||
hud.devices = Devices;
|
||
hud.controller = controller;
|
||
hud.bridge = bridge;
|
||
hud.weather = weather;
|
||
hud.orbit = orbit;
|
||
|
||
Devices.projector.contentProvider = () =>
|
||
$"Innen {env.indoorTemp:0.0} °C · CO2 {env.co2:0} ppm · {env.indoorLux:0} lx\n" +
|
||
$"Außen {env.outdoorTemp:0.0} °C · Wind {env.windSpeed:0.0} m/s · AQI {env.outdoorAqi:0}\n" +
|
||
$"Fenster {Devices.WindowLabel} · Lüftung {Devices.VentLevel * 100f:0} % · Heizung {Devices.HeatingLevel * 100f:0} %\n" +
|
||
$"Szenario {controller.Scenario} · Quelle {(env.AnyMeasured ? "Arduino" : "Simulation")}";
|
||
}
|
||
|
||
/// <summary>Helles, weiches Licht und das Dashboard im Look des Web-Frontends.</summary>
|
||
void SetupAmbient(RoomEnvironment env, ClimateController controller, ArduinoBridge bridge, WeatherService weather, OrbitCamera orbit)
|
||
{
|
||
Devices.ambSky = new Color(0.93f, 0.96f, 0.94f);
|
||
Devices.ambEquator = new Color(0.86f, 0.89f, 0.87f);
|
||
Devices.ambGround = new Color(0.72f, 0.75f, 0.73f);
|
||
Devices.ambBase = 0.55f;
|
||
Devices.sun.shadowStrength = 0.35f;
|
||
|
||
var hud = GetOrAdd<AmbientHUD>();
|
||
hud.env = env;
|
||
hud.devices = Devices;
|
||
hud.controller = controller;
|
||
hud.bridge = bridge;
|
||
hud.weather = weather;
|
||
hud.orbit = orbit;
|
||
hud.roomCamera = orbit.GetComponent<Camera>();
|
||
|
||
Devices.projector.contentProvider = () =>
|
||
$"{hud.roomName.ToUpperInvariant()}\n" +
|
||
$"{env.indoorTemp:0.0} °C · CO2 {env.co2:0} ppm · Ziel {controller.AverageWish:0.0} °C\n" +
|
||
$"Szenario: {controller.Scenario} · Quelle: {(env.AnyMeasured ? "Arduino" : "Simulation")}";
|
||
}
|
||
|
||
T GetOrAdd<T>() where T : Component
|
||
{
|
||
var c = GetComponent<T>();
|
||
return c != null ? c : gameObject.AddComponent<T>();
|
||
}
|
||
|
||
static Light SetupSun()
|
||
{
|
||
Light sun = null;
|
||
foreach (var l in Object.FindObjectsByType<Light>(FindObjectsSortMode.None))
|
||
if (l.type == LightType.Directional) { sun = l; break; }
|
||
if (sun == null)
|
||
{
|
||
sun = new GameObject("Sonne").AddComponent<Light>();
|
||
sun.type = LightType.Directional;
|
||
}
|
||
// Sonne steht links (Fensterseite) und scheint schräg in den Raum
|
||
sun.transform.rotation = Quaternion.Euler(35f, 100f, 0f);
|
||
sun.shadows = LightShadows.Soft;
|
||
sun.shadowStrength = 0.85f;
|
||
RenderSettings.sun = sun;
|
||
return sun;
|
||
}
|
||
|
||
static OrbitCamera SetupCamera()
|
||
{
|
||
var cam = Camera.main;
|
||
if (cam == null)
|
||
{
|
||
var go = new GameObject("Main Camera", typeof(Camera), typeof(AudioListener)) { tag = "MainCamera" };
|
||
cam = go.GetComponent<Camera>();
|
||
}
|
||
cam.nearClipPlane = 0.05f;
|
||
cam.fieldOfView = 60f;
|
||
if (RenderSettings.skybox == null)
|
||
{
|
||
cam.clearFlags = CameraClearFlags.SolidColor;
|
||
cam.backgroundColor = new Color(0.62f, 0.76f, 0.9f);
|
||
}
|
||
var orbit = cam.GetComponent<OrbitCamera>();
|
||
return orbit != null ? orbit : cam.gameObject.AddComponent<OrbitCamera>();
|
||
}
|
||
}
|
||
}
|