84 lines
3.3 KiB
C#
84 lines
3.3 KiB
C#
using UnityEngine;
|
|
|
|
namespace SmartRoom
|
|
{
|
|
public enum WindowState { Closed, Tilted, Open }
|
|
|
|
public static class WindowStateExt
|
|
{
|
|
public static string Label(this WindowState s)
|
|
=> s == WindowState.Open ? "offen" : s == WindowState.Tilted ? "gekippt" : "geschlossen";
|
|
|
|
/// <summary>Kennung im Arduino-Protokoll.</summary>
|
|
public static string Code(this WindowState s)
|
|
=> s == WindowState.Open ? "open" : s == WindowState.Tilted ? "tilted" : "closed";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dreh-Kipp-Fenster. Ablauf wie in echt: erst Griff drehen, dann Flügel bewegen.
|
|
/// Beim Wechsel zwischen Kipp- und Drehstellung wird der Flügel zuerst geschlossen.
|
|
/// </summary>
|
|
public class WindowActuator : MonoBehaviour
|
|
{
|
|
public WindowState target = WindowState.Closed;
|
|
public Transform hinge, tilt, handle;
|
|
public float maxSwing = 80f, maxTilt = 12f;
|
|
[Tooltip("Normierte Geschwindigkeit pro Sekunde")] public float swingSpeed = 0.3f, tiltSpeed = 0.6f;
|
|
public float handleSpeed = 300f;
|
|
|
|
float swing, tiltV, handleAngle;
|
|
|
|
public bool IsMoving { get; private set; }
|
|
|
|
/// <summary>0 = dicht, 1 = vollständig geöffnet (gekippt ≈ 0,3).</summary>
|
|
public float Airflow => Mathf.Max(Ease(swing), 0.3f * Ease(tiltV));
|
|
|
|
public WindowState Current
|
|
{
|
|
get
|
|
{
|
|
if (swing > 0.98f) return WindowState.Open;
|
|
if (tiltV > 0.98f) return WindowState.Tilted;
|
|
return WindowState.Closed;
|
|
}
|
|
}
|
|
|
|
static float Ease(float x) => x * x * (3f - 2f * x);
|
|
|
|
static float HandleFor(WindowState s) => s == WindowState.Open ? 90f : s == WindowState.Tilted ? 180f : 0f;
|
|
|
|
void Update()
|
|
{
|
|
float dt = Time.deltaTime;
|
|
float tiltGoal = target == WindowState.Tilted ? 1f : 0f;
|
|
float swingGoal = target == WindowState.Open ? 1f : 0f;
|
|
float before = swing + tiltV + handleAngle;
|
|
|
|
if (swing <= 0f && tiltV <= 0f)
|
|
{
|
|
// Flügel liegt im Rahmen: erst Griff in Zielstellung, dann bewegen
|
|
float h = HandleFor(target);
|
|
handleAngle = Mathf.MoveTowards(handleAngle, h, handleSpeed * dt);
|
|
if (Mathf.Approximately(handleAngle, h))
|
|
{
|
|
tiltV = Mathf.MoveTowards(tiltV, tiltGoal, tiltSpeed * dt);
|
|
swing = Mathf.MoveTowards(swing, swingGoal, swingSpeed * dt);
|
|
}
|
|
}
|
|
else if (tiltV > 0f && tiltGoal == 0f) tiltV = Mathf.MoveTowards(tiltV, 0f, tiltSpeed * dt);
|
|
else if (swing > 0f && swingGoal == 0f) swing = Mathf.MoveTowards(swing, 0f, swingSpeed * dt);
|
|
else
|
|
{
|
|
tiltV = Mathf.MoveTowards(tiltV, tiltGoal, tiltSpeed * dt);
|
|
swing = Mathf.MoveTowards(swing, swingGoal, swingSpeed * dt);
|
|
}
|
|
|
|
IsMoving = !Mathf.Approximately(before, swing + tiltV + handleAngle);
|
|
|
|
hinge.localRotation = Quaternion.Euler(0f, maxSwing * Ease(swing), 0f);
|
|
tilt.localRotation = Quaternion.Euler(0f, 0f, -maxTilt * Ease(tiltV));
|
|
handle.localRotation = Quaternion.Euler(handleAngle, 0f, 0f);
|
|
}
|
|
}
|
|
}
|