144 lines
4.7 KiB
C#
144 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
using BoostComponents;
|
|
|
|
public class ControlsUI : MonoBehaviour {
|
|
[SerializeField]
|
|
ShipControls ship;
|
|
|
|
UIDocument uiDocument;
|
|
Label speed;
|
|
Label shields;
|
|
|
|
BoostBar boostBar;
|
|
InterfaceIcon shield;
|
|
|
|
VisualElement accelCtr;
|
|
VisualElement decelCtr;
|
|
VisualElement strafeCtr;
|
|
VisualElement shieldCtr;
|
|
|
|
BoostIndicator[] accelBoosts;
|
|
BoostIndicator[] decelBoosts;
|
|
BoostIndicator[] strafeBoosts;
|
|
BoostIndicator[] shieldBoosts;
|
|
|
|
void Awake() {
|
|
Init();
|
|
}
|
|
|
|
void OnEnable() {
|
|
Init();
|
|
}
|
|
|
|
void Init() {
|
|
accelBoosts = new BoostIndicator[ship.powerUnit.BoostCharges];
|
|
decelBoosts = new BoostIndicator[ship.powerUnit.BoostCharges];
|
|
strafeBoosts = new BoostIndicator[ship.powerUnit.BoostCharges];
|
|
shieldBoosts = new BoostIndicator[ship.powerUnit.BoostCharges];
|
|
|
|
uiDocument = GetComponent<UIDocument>();
|
|
speed = uiDocument.rootVisualElement.Q<Label>("speed");
|
|
shields = uiDocument.rootVisualElement.Q<Label>("shields");
|
|
shield = uiDocument.rootVisualElement.Q<InterfaceIcon>("ShieldIcon");
|
|
boostBar = uiDocument.rootVisualElement.Q<BoostBar>("boostBar");
|
|
|
|
accelCtr = uiDocument.rootVisualElement.Q<VisualElement>("accelCtr");
|
|
decelCtr = uiDocument.rootVisualElement.Q<VisualElement>("decelCtr");
|
|
strafeCtr = uiDocument.rootVisualElement.Q<VisualElement>("strafeCtr");
|
|
shieldCtr = uiDocument.rootVisualElement.Q<VisualElement>("shieldCtr");
|
|
|
|
SetupBars();
|
|
}
|
|
|
|
void SetupBars() {
|
|
// remove the test elements
|
|
accelCtr.Query<BoostIndicator>()
|
|
.ForEach(i => accelCtr.Remove(i));
|
|
decelCtr.Query<BoostIndicator>()
|
|
.ForEach(i => decelCtr.Remove(i));
|
|
strafeCtr.Query<BoostIndicator>()
|
|
.ForEach(i => strafeCtr.Remove(i));
|
|
shieldCtr.Query<BoostIndicator>()
|
|
.ForEach(i => shieldCtr.Remove(i));
|
|
|
|
// add a bar for the capacity of charges to each output icon ctr
|
|
for (int i = 0; i < ship.powerUnit.BoostCharges; i++) {
|
|
BoostIndicator a = new BoostIndicator();
|
|
BoostIndicator d = new BoostIndicator();
|
|
BoostIndicator s = new BoostIndicator();
|
|
BoostIndicator l = new BoostIndicator();
|
|
|
|
accelBoosts[i] = a;
|
|
decelBoosts[i] = d;
|
|
strafeBoosts[i] = s;
|
|
shieldBoosts[i] = l;
|
|
|
|
accelCtr.Add(a);
|
|
decelCtr.Add(d);
|
|
strafeCtr.Add(s);
|
|
shieldCtr.Add(l);
|
|
}
|
|
}
|
|
|
|
void FixedUpdate() {
|
|
if (speed == null) {
|
|
Debug.LogWarning("no speed");
|
|
Init();
|
|
return;
|
|
}
|
|
|
|
speed.text = $"{ship.Speed:0}";
|
|
shield.detail = ship.powerUnit.ShieldPct switch {
|
|
"100" => "",
|
|
string pct => pct,
|
|
};
|
|
|
|
boostBar.progress = ship.powerUnit.BoostProgress;
|
|
boostBar.charges = ship.powerUnit.BoostCharges;
|
|
|
|
BarNasty();
|
|
}
|
|
|
|
// yolo looping
|
|
void BarNasty() {
|
|
var boosts = ship.powerUnit.BoostActive.OrderByDescending(b => b.expiresAt);
|
|
|
|
var aB = boosts.Where(b => b.output == Output.Acceleration);
|
|
var dB = boosts.Where(b => b.output == Output.Deceleration);
|
|
var sB = boosts.Where(b => b.output == Output.Strafing);
|
|
var lB = boosts.Where(b => b.output == Output.Shield);
|
|
|
|
for (int i = 0; i < ship.powerUnit.BoostCharges; i++) {
|
|
var accelBoost = aB.ElementAtOrDefault(i);
|
|
accelBoosts[i].progress = (accelBoost == default((Output, double))) switch {
|
|
true => 0f,
|
|
false => (accelBoost.expiresAt - Time.timeAsDouble) / PowerUnit.boostDurationSeconds,
|
|
};
|
|
|
|
var decelBoost = dB.ElementAtOrDefault(i);
|
|
decelBoosts[i].progress = (decelBoost == default((Output, double))) switch {
|
|
true => 0f,
|
|
false => (decelBoost.expiresAt - Time.timeAsDouble) / PowerUnit.boostDurationSeconds,
|
|
};
|
|
|
|
var strafeBoost = sB.ElementAtOrDefault(i);
|
|
strafeBoosts[i].progress = (strafeBoost == default((Output, double))) switch {
|
|
true => 0f,
|
|
false => (strafeBoost.expiresAt - Time.timeAsDouble) / PowerUnit.boostDurationSeconds,
|
|
};
|
|
|
|
var shieldBoost = lB.ElementAtOrDefault(i);
|
|
shieldBoosts[i].progress = (shieldBoost == default((Output, double))) switch {
|
|
true => 0f,
|
|
false => (shieldBoost.expiresAt - Time.timeAsDouble) / PowerUnit.boostDurationSeconds,
|
|
};
|
|
}
|
|
}
|
|
}
|