|
| 1 | +using Epstein_Fusion_DS.Communication; |
| 2 | +using Epstein_Fusion_DS.FusionParts; |
| 3 | +using Epstein_Fusion_DS.HeatParts; |
| 4 | +using RichHudFramework.UI; |
| 5 | +using RichHudFramework.UI.Rendering; |
| 6 | +using RichHudFramework.UI.Rendering.Client; |
| 7 | +using Sandbox.Game.Entities; |
| 8 | +using Sandbox.ModAPI; |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Linq; |
| 12 | +using System.Text; |
| 13 | +using System.Threading.Tasks; |
| 14 | +using VRage.Game.Entity; |
| 15 | +using VRage.Game.ModAPI; |
| 16 | +using VRageMath; |
| 17 | + |
| 18 | +namespace Epstein_Fusion_DS.HudHelpers |
| 19 | +{ |
| 20 | + internal class FusionWindow : CamSpaceNode |
| 21 | + { |
| 22 | + private readonly TexturedBox _backgroundBox, _foregroundBox, _heatBox, _storBox; |
| 23 | + private readonly Label _heatLabel, _storageLabel, _infoLabelLeft; |
| 24 | + |
| 25 | + private readonly GlyphFormat _stdTextFormat = new GlyphFormat(color: HudConstants.HudTextColor, alignment: TextAlignment.Center, font: FontManager.GetFont("BI_Monospace")); |
| 26 | + private readonly GlyphFormat _stdTextFormatInfo = new GlyphFormat(color: HudConstants.HudTextColor, textSize: 0.85f, alignment: TextAlignment.Left, font: FontManager.GetFont("BI_Monospace")); |
| 27 | + |
| 28 | + public FusionWindow(HudParentBase parent) : base(parent) |
| 29 | + { |
| 30 | + RotationAxis = new Vector3(0, 1, 0); |
| 31 | + RotationAngle = 0.25f; |
| 32 | + TransformOffset = new Vector3D(-0.0675, -0.04, -0.05); |
| 33 | + |
| 34 | + _backgroundBox = new TexturedBox(this) |
| 35 | + { |
| 36 | + Material = new Material("WhiteSquare", HudConstants.HudSize), |
| 37 | + Size = HudConstants.HudSize, |
| 38 | + Color = HudConstants.HudBackgroundColor, |
| 39 | + IsMasking = true, |
| 40 | + ZOffset = sbyte.MinValue, |
| 41 | + Padding = Vector2.Zero, |
| 42 | + }; |
| 43 | + _foregroundBox = new TexturedBox(this) |
| 44 | + { |
| 45 | + Material = new Material("HudBackground", new Vector2(400, 136)), |
| 46 | + Size = HudConstants.HudSize, |
| 47 | + ZOffset = sbyte.MaxValue, |
| 48 | + }; |
| 49 | + |
| 50 | + _heatLabel = new Label(this) |
| 51 | + { |
| 52 | + Text = "00% HEAT", |
| 53 | + Offset = new Vector2(0, 0), |
| 54 | + Format = _stdTextFormat, |
| 55 | + ZOffset = sbyte.MaxValue, |
| 56 | + }; |
| 57 | + _storageLabel = new Label(this) |
| 58 | + { |
| 59 | + Text = "00% STOR", |
| 60 | + Offset = new Vector2(0, -_backgroundBox.Size.Y/3), |
| 61 | + Format = _stdTextFormat, |
| 62 | + ZOffset = sbyte.MaxValue, |
| 63 | + }; |
| 64 | + |
| 65 | + _infoLabelLeft = new Label(this) |
| 66 | + { |
| 67 | + Text = "100% INTEGRITY - ALL SYSTEMS NOMINAL", |
| 68 | + Offset = new Vector2(0, _backgroundBox.Size.Y/3), |
| 69 | + Format = _stdTextFormat, |
| 70 | + ZOffset = sbyte.MaxValue, |
| 71 | + }; |
| 72 | + |
| 73 | + _heatBox = new TexturedBox(_backgroundBox) |
| 74 | + { |
| 75 | + Material = new Material("WhiteSquare", new Vector2(384, 38) * HudConstants.HudSizeRatio), |
| 76 | + Size = new Vector2(384, 38) * HudConstants.HudSizeRatio, |
| 77 | + ParentAlignment = ParentAlignments.Left | ParentAlignments.Top | ParentAlignments.Inner, |
| 78 | + Offset = new Vector2(8, -49) * HudConstants.HudSizeRatio, |
| 79 | + ZOffset = 0, |
| 80 | + Color = Color.Red, |
| 81 | + }; |
| 82 | + |
| 83 | + _storBox = new TexturedBox(_backgroundBox) |
| 84 | + { |
| 85 | + Material = new Material("WhiteSquare", new Vector2(384, 38) * HudConstants.HudSizeRatio), |
| 86 | + Size = new Vector2(384, 38) * HudConstants.HudSizeRatio, |
| 87 | + ParentAlignment = ParentAlignments.Left | ParentAlignments.Top | ParentAlignments.Inner, |
| 88 | + Offset = new Vector2(8, -95) * HudConstants.HudSizeRatio, |
| 89 | + ZOffset = 0, |
| 90 | + Color = Color.Orange, |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + private static ModularDefinitionApi ModularApi => Epstein_Fusion_DS.ModularDefinition.ModularApi; |
| 96 | + private int _ticks = 0; |
| 97 | + private bool _shouldHide; |
| 98 | + private MyEntity3DSoundEmitter _soundEmitter = null; |
| 99 | + private readonly MySoundPair _alertSound = new MySoundPair("ArcSoundBlockAlert2"); |
| 100 | + |
| 101 | + public void Update() |
| 102 | + { |
| 103 | + _ticks++; |
| 104 | + var playerCockpit = MyAPIGateway.Session?.Player?.Controller?.ControlledEntity?.Entity as IMyShipController; |
| 105 | + |
| 106 | + // Pulling the current HudState is SLOOOOWWWW, so we only pull it when tab is just pressed. |
| 107 | + //if (MyAPIGateway.Input.IsNewKeyPressed(MyKeys.Tab)) |
| 108 | + // _shouldHide = MyAPIGateway.Session?.Config?.HudState != 1; |
| 109 | + |
| 110 | + // Hide HUD element if the player isn't in a cockpit |
| 111 | + if (playerCockpit == null || _shouldHide) |
| 112 | + { |
| 113 | + if (Visible) Visible = false; |
| 114 | + |
| 115 | + if (_soundEmitter != null) |
| 116 | + { |
| 117 | + _soundEmitter.StopSound(true); |
| 118 | + _soundEmitter = null; |
| 119 | + } |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + var playerGrid = playerCockpit.CubeGrid; |
| 124 | + |
| 125 | + float totalFusionCapacity = 0; |
| 126 | + float totalFusionGeneration = 0; |
| 127 | + float totalFusionStored = 0; |
| 128 | + float reactorIntegrity = 0; |
| 129 | + int reactorCount = 0; |
| 130 | + |
| 131 | + foreach (var system in SFusionManager.I.FusionSystems) |
| 132 | + { |
| 133 | + if (playerGrid != ModularApi.GetAssemblyGrid(system.Key)) |
| 134 | + continue; |
| 135 | + |
| 136 | + totalFusionCapacity += system.Value.MaxPowerStored; |
| 137 | + totalFusionGeneration += system.Value.PowerGeneration; |
| 138 | + totalFusionStored += system.Value.PowerStored; |
| 139 | + foreach (var reactor in system.Value.Reactors) |
| 140 | + { |
| 141 | + reactorIntegrity += reactor.Block.SlimBlock.Integrity/reactor.Block.SlimBlock.MaxIntegrity; |
| 142 | + reactorCount++; |
| 143 | + } |
| 144 | + foreach (var thruster in system.Value.Thrusters) |
| 145 | + { |
| 146 | + reactorIntegrity += thruster.Block.SlimBlock.Integrity/thruster.Block.SlimBlock.MaxIntegrity; |
| 147 | + reactorCount++; |
| 148 | + } |
| 149 | + } |
| 150 | + reactorIntegrity /= reactorCount; |
| 151 | + |
| 152 | + // Hide HUD element if the grid has no fusion systems (capacity is always >0 for a fusion system) |
| 153 | + if (totalFusionCapacity == 0) |
| 154 | + { |
| 155 | + if (Visible) Visible = false; |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + // Show otherwise |
| 160 | + if (!Visible) Visible = true; |
| 161 | + |
| 162 | + var heatPct = HeatManager.I.GetGridHeatLevel(playerGrid); |
| 163 | + |
| 164 | + _heatBox.Width = 384 * HudConstants.HudSizeRatio.X * heatPct; |
| 165 | + _heatBox.Color = new Color(heatPct, 1-heatPct, 0, 0.75f); |
| 166 | + |
| 167 | + _storBox.Width = 384 * HudConstants.HudSizeRatio.X * (totalFusionStored / totalFusionCapacity); |
| 168 | + |
| 169 | + _infoLabelLeft.Text = new RichText |
| 170 | + { |
| 171 | + {(reactorIntegrity*100).ToString("N0") + "%", _stdTextFormatInfo.WithColor(reactorIntegrity > 0.6 ? Color.White : Color.Red)}, |
| 172 | + {" INTEGRITY - ", _stdTextFormatInfo}, |
| 173 | + {GetNoticeText(heatPct, reactorIntegrity), GetNoticeFormat(heatPct, reactorIntegrity)}, |
| 174 | + }; |
| 175 | + |
| 176 | + _heatLabel.Text = $"{heatPct*100:N0}% HEAT"; |
| 177 | + _storageLabel.Text = $"{(totalFusionStored / totalFusionCapacity) * 100:N0}% STOR"; |
| 178 | + |
| 179 | + if (heatPct > 0.8f) |
| 180 | + { |
| 181 | + if (_soundEmitter == null) |
| 182 | + { |
| 183 | + _soundEmitter = new MyEntity3DSoundEmitter((MyEntity) playerCockpit.Entity) |
| 184 | + { |
| 185 | + CanPlayLoopSounds = true |
| 186 | + }; |
| 187 | + _soundEmitter.PlaySound(_alertSound); |
| 188 | + } |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + if (_soundEmitter != null) |
| 193 | + { |
| 194 | + _soundEmitter.StopSound(true); |
| 195 | + _soundEmitter = null; |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private string GetNoticeText(float heatPct, float integrityPct) |
| 201 | + { |
| 202 | + if (integrityPct < 0.1) |
| 203 | + return "I DON'T WANT TO DIE."; |
| 204 | + if (integrityPct < 0.5) |
| 205 | + return "-!- REACTOR FAILURE -!-"; |
| 206 | + if (integrityPct < 0.6) |
| 207 | + return "-!- SHUTDOWN IMMINENT -!-"; |
| 208 | + if (heatPct > 0.8) |
| 209 | + return "! THERMAL DAMAGE !"; |
| 210 | + return "ALL SYSTEMS NOMINAL"; |
| 211 | + } |
| 212 | + |
| 213 | + private GlyphFormat GetNoticeFormat(float heatPct, float integrityPct) |
| 214 | + { |
| 215 | + if (integrityPct < 0.6 || heatPct > 0.8) |
| 216 | + return _stdTextFormatInfo.WithColor(Color.Red); |
| 217 | + return _stdTextFormatInfo; |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments