Rise
The Vieneo Province
Loading...
Searching...
No Matches
WindshieldWipers.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5
6// Event 29 and vehicle metadata bit 10 carry an on/off command. Every vehicle
7// advances its own phase at the same fixed rate; OFF returns the blades to park.
9{
10// DIX translations are kilometres. Use the same one-millimetre minimum for
11// rain clearing and audio; comparing squared travel to 1e-6 requires a metre.
12inline bool HasBladeMotion(int type, unsigned primitives, float axisLengthSq,
13 float hingeExtent, float travelLengthSq)
14{
15 constexpr float minTravelKm = .000001f;
16 return type == 47 && primitives > 0
17 && ((axisLengthSq > .000001f && std::fabs(hingeExtent) > .000001f)
18 || travelLengthSq > minTravelKm * minTravelKm);
19}
20
21// Local keybind/crash requests send an event only when the switch changes.
22// Received events and metadata assign the switch directly, without an echo.
23inline bool SetEnabled(bool& current, bool enabled)
24{
25 if (current == enabled) return false;
26 current = enabled;
27 return true;
28}
29
30struct Motion
31{
32 float phase = 0.f;
33 static constexpr float rate = 1.2f; // Complete out-and-back cycles per second.
34 bool moving = false;
35
36 void Update(float elapsed, bool enabled, bool powered = true)
37 {
38 moving = false;
39 if (!powered || !std::isfinite(elapsed) || elapsed <= 0.f) return;
40 if (!enabled && phase == 0.f) return;
41 phase += (std::min)(elapsed, .25f) * rate;
42 if (phase >= 1.f)
43 phase = enabled ? phase - std::floor(phase) : 0.f;
44 moving = enabled || phase != 0.f;
45 }
46};
47}
bool SetEnabled(bool &current, bool enabled)
bool HasBladeMotion(int type, unsigned primitives, float axisLengthSq, float hingeExtent, float travelLengthSq)
static constexpr float rate
void Update(float elapsed, bool enabled, bool powered=true)