boost bar and cds

This commit is contained in:
ntr committed 2023-08-15 18:42:40 +10:00
1 parent 0e8a7a65ad
commit 3aacbed784
7 files changed
+139 -16

No files matched your search

+17 -7
View File
@@ -47,10 +47,11 @@ public class PowerUnit {
static double boostFullChargeSeconds = 10f; static double boostFullChargeSeconds = 10f;
static double boostDurationSeconds = 2f; static double boostDurationSeconds = 2f;
double boostMax = 2f * boostDurationSeconds; double boostMax = 2f * boostDurationSeconds;
double boostAvailable = 0f; // in seconds double boostProgress = 0f;
double boostAvailable = 0f;
public string BoostAvailable => $"{boostAvailable / 2f:0.0}"; public double BoostProgress => boostProgress / boostMax;
// public string BoostAvailable => $"{((boostAvailable / boostMax) * 100f):0}"; public int BoostCharges => (int)(boostMax / boostDurationSeconds);
public float Acceleration => public float Acceleration =>
acceleration + boostActive.Count(b => b.output == Output.Acceleration) * baseOutput; acceleration + boostActive.Count(b => b.output == Output.Acceleration) * baseOutput;
@@ -69,18 +70,21 @@ public class PowerUnit {
strafing = initialStrafing; strafing = initialStrafing;
boostActive = new(); boostActive = new();
boostAvailable = 0f; boostProgress = 0f;
shieldMax = initialShieldMax; shieldMax = initialShieldMax;
shieldCurrent = initialShieldMax; shieldCurrent = initialShieldMax;
} }
public void Deploy(Output output) { public void Deploy(Output output) {
if (boostAvailable < boostDurationSeconds * (boostActive.Count + 1)) return; if (boostAvailable < boostDurationSeconds) return;
boostActive.Add((output, Time.timeAsDouble + boostDurationSeconds)); boostActive.Add((output, Time.timeAsDouble + boostDurationSeconds));
boostAvailable -= boostDurationSeconds;
} }
public void Tick() { public void Tick() {
if (boostProgress <= 0f) boostActive.Clear();
// remove expired boosts // remove expired boosts
boostActive = boostActive.Where(b => b.expiresAt > Time.timeAsDouble).ToList(); boostActive = boostActive.Where(b => b.expiresAt > Time.timeAsDouble).ToList();
@@ -91,8 +95,14 @@ public class PowerUnit {
int count => count * Time.fixedDeltaTime * -1f, int count => count * Time.fixedDeltaTime * -1f,
}; };
double next = Math.Min(boostAvailable + delta, boostMax); // boost available gets dragged up by the clock
boostAvailable = Math.Max(0f, next); // when the delta is positive
// which can only happen when there are no active boosts
// check it vvvv
// Debug.Log($"Progress={boostProgress} actualAvailability={boostAvailable}");
double next = Math.Min(boostProgress + delta, boostMax);
boostProgress = Math.Max(0f, next);
if (delta > 0f) boostAvailable = boostProgress;
shieldMax = initialShieldMax + boostActive.Count(b => b.output == Output.Shield) * 1000f; shieldMax = initialShieldMax + boostActive.Count(b => b.output == Output.Shield) * 1000f;
shieldCurrent = Math.Min(shieldCurrent + ShieldRegen, shieldMax); shieldCurrent = Math.Min(shieldCurrent + ShieldRegen, shieldMax);
-2
View File
@@ -27,8 +27,6 @@ public class ShipControls : MonoBehaviour {
float clampAngle = 90.0f; float clampAngle = 90.0f;
public PowerUnit powerUnit = new(); public PowerUnit powerUnit = new();
int boostDurationSeconds = 2;
int boostCooldownSeconds = 5;
void Start() { void Start() {
Cursor.lockState = CursorLockMode.Locked; Cursor.lockState = CursorLockMode.Locked;
+96
View File
@@ -0,0 +1,96 @@
using System;
using Unity.Collections;
using UnityEngine;
using UnityEngine.UIElements;
namespace BoostComponents {
public class BoostBar : VisualElement {
// Need this uxml traits and factory for it to show up in editor
public new class UxmlTraits : VisualElement.UxmlTraits {
UxmlDoubleAttributeDescription m_ProgressAttribute = new UxmlDoubleAttributeDescription() {
name = "progress"
};
UxmlIntAttributeDescription m_ChargesAttribute = new UxmlIntAttributeDescription() {
name = "charges"
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
base.Init(ve, bag, cc);
(ve as BoostBar).progress = m_ProgressAttribute.GetValueFromBag(bag, cc);
(ve as BoostBar).charges = m_ChargesAttribute.GetValueFromBag(bag, cc);
}
}
public new class UxmlFactory : UxmlFactory<BoostBar, UxmlTraits> { }
public static readonly string ussClassName = "boostBar";
public static readonly string ussLabelClassName = "boostBar";
double m_Progress = 0f;
int m_Charges = 2;
Color strokeColor = Color.white;
public double progress {
get => m_Progress;
set {
m_Progress = value;
MarkDirtyRepaint();
}
}
public int charges {
get => m_Charges;
set {
m_Charges = value;
MarkDirtyRepaint();
}
}
public BoostBar() {
AddToClassList(ussClassName);
RegisterCallback<CustomStyleResolvedEvent>(evt => CustomStylesResolved(evt));
generateVisualContent += GenerateVisualContent;
}
static void CustomStylesResolved(CustomStyleResolvedEvent evt) {
BoostBar element = (BoostBar)evt.currentTarget;
element.UpdateCustomStyles();
}
void UpdateCustomStyles() {
MarkDirtyRepaint();
}
void GenerateVisualContent(MeshGenerationContext context) {
float width = contentRect.width;
float height = contentRect.height;
double length = width * m_Progress;
Vector2 left = new Vector2(0f, height / 2);
Vector2 end = new Vector2((float)length, height / 2);
var painter = context.painter2D;
painter.lineWidth = 4.0f;
painter.lineCap = LineCap.Butt;
painter.strokeColor = strokeColor;
painter.BeginPath();
painter.MoveTo(left);
painter.LineTo(end);
painter.Stroke();
float chevGap = width / charges;
for (int i = 0; i < charges + 1; i++) {
painter.BeginPath();
painter.MoveTo(new Vector2(i * chevGap, height));
painter.LineTo(new Vector2(i * chevGap, 0f));
painter.Stroke();
}
}
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4bc4f30c8e6df2a739c0909bbb9e1c7d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+2 -3
View File
@@ -11,9 +11,8 @@
<ui:Label tabindex="-1" display-tooltip-when-elided="true" name="shields" text="100" style="min-width: 5%; -unity-text-align: upper-right; flex-basis: 10%;" /> <ui:Label tabindex="-1" display-tooltip-when-elided="true" name="shields" text="100" style="min-width: 5%; -unity-text-align: upper-right; flex-basis: 10%;" />
<ui:Label tabindex="-1" text="%" display-tooltip-when-elided="true" name="units" style="flex-basis: 10%; -unity-text-align: upper-left;" /> <ui:Label tabindex="-1" text="%" display-tooltip-when-elided="true" name="units" style="flex-basis: 10%; -unity-text-align: upper-left;" />
</ui:VisualElement> </ui:VisualElement>
<ui:VisualElement name="boostCtr" style="align-items: center; flex-direction: row; justify-content: center;"> <ui:VisualElement name="boostBarCtr" style="height: 2.5%;">
<ui:Label tabindex="-1" display-tooltip-when-elided="true" name="boosts" text="1" style="min-width: 5%; -unity-text-align: upper-right; flex-basis: 10%;" /> <BoostComponents.BoostBar progress="0.66" name="boostBar" charges="2" style="height: 100%;" />
<ui:Label tabindex="-1" text="Boosts" display-tooltip-when-elided="true" name="units" style="flex-basis: 10%; -unity-text-align: upper-left;" />
</ui:VisualElement> </ui:VisualElement>
<ui:VisualElement name="puCtr" style="flex-direction: row;"> <ui:VisualElement name="puCtr" style="flex-direction: row;">
<ui:Label tabindex="-1" display-tooltip-when-elided="true" name="puAccel" text="100" /> <ui:Label tabindex="-1" display-tooltip-when-elided="true" name="puAccel" text="100" />
+9 -3
View File
@@ -5,6 +5,8 @@ using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
using BoostComponents;
public class ControlsUI : MonoBehaviour { public class ControlsUI : MonoBehaviour {
[SerializeField] [SerializeField]
ShipControls ship; ShipControls ship;
@@ -12,12 +14,13 @@ public class ControlsUI : MonoBehaviour {
UIDocument uiDocument; UIDocument uiDocument;
Label speed; Label speed;
Label shields; Label shields;
Label boosts;
Label puAccel; Label puAccel;
Label puDecel; Label puDecel;
Label puStrafe; Label puStrafe;
Label puShield; Label puShield;
BoostBar boostBar;
void OnEnable() { void OnEnable() {
Init(); Init();
} }
@@ -28,11 +31,12 @@ public class ControlsUI : MonoBehaviour {
shields = uiDocument.rootVisualElement.Q<Label>("shields"); shields = uiDocument.rootVisualElement.Q<Label>("shields");
// PowerUnit // PowerUnit
boosts = uiDocument.rootVisualElement.Q<Label>("boosts");
puAccel = uiDocument.rootVisualElement.Q<Label>("puAccel"); puAccel = uiDocument.rootVisualElement.Q<Label>("puAccel");
puDecel = uiDocument.rootVisualElement.Q<Label>("puDecel"); puDecel = uiDocument.rootVisualElement.Q<Label>("puDecel");
puStrafe = uiDocument.rootVisualElement.Q<Label>("puStrafe"); puStrafe = uiDocument.rootVisualElement.Q<Label>("puStrafe");
puShield = uiDocument.rootVisualElement.Q<Label>("puShield"); puShield = uiDocument.rootVisualElement.Q<Label>("puShield");
boostBar = uiDocument.rootVisualElement.Q<BoostBar>("boostBar");
} }
void FixedUpdate() { void FixedUpdate() {
@@ -45,10 +49,12 @@ public class ControlsUI : MonoBehaviour {
speed.text = $"{ship.Speed():0}"; speed.text = $"{ship.Speed():0}";
shields.text = ship.powerUnit.ShieldPct; shields.text = ship.powerUnit.ShieldPct;
boosts.text = ship.powerUnit.BoostAvailable;
puAccel.text = ship.powerUnit.Acceleration.ToString(); puAccel.text = ship.powerUnit.Acceleration.ToString();
puDecel.text = ship.powerUnit.Deceleration.ToString(); puDecel.text = ship.powerUnit.Deceleration.ToString();
puStrafe.text = ship.powerUnit.Strafing.ToString(); puStrafe.text = ship.powerUnit.Strafing.ToString();
puShield.text = ship.powerUnit.ShieldMax.ToString(); puShield.text = ship.powerUnit.ShieldMax.ToString();
boostBar.progress = ship.powerUnit.BoostProgress;
boostBar.charges = ship.powerUnit.BoostCharges;
} }
} }
+4 -1
View File
@@ -1,6 +1,9 @@
# SpaceRace! # SpaceRace!
- boost UI - controls UI
- boost charge coloring chevron things
- shield icon
- output boost indicators
- controls diagram - controls diagram