1
0

GunSystem.AmmoCounter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System.Numerics;
  2. using Content.Client.IoC;
  3. using Content.Client.Items;
  4. using Content.Client.Resources;
  5. using Content.Client.Stylesheets;
  6. using Content.Client.Weapons.Ranged.Components;
  7. using Content.Client.Weapons.Ranged.ItemStatus;
  8. using Robust.Client.Animations;
  9. using Robust.Client.Graphics;
  10. using Robust.Client.UserInterface;
  11. using Robust.Client.UserInterface.Controls;
  12. namespace Content.Client.Weapons.Ranged.Systems;
  13. public sealed partial class GunSystem
  14. {
  15. private void OnAmmoCounterCollect(EntityUid uid, AmmoCounterComponent component, ItemStatusCollectMessage args)
  16. {
  17. RefreshControl(uid, component);
  18. if (component.Control != null)
  19. args.Controls.Add(component.Control);
  20. }
  21. /// <summary>
  22. /// Refreshes the control being used to show ammo. Useful if you change the AmmoProvider.
  23. /// </summary>
  24. /// <param name="uid"></param>
  25. /// <param name="component"></param>
  26. private void RefreshControl(EntityUid uid, AmmoCounterComponent? component = null)
  27. {
  28. if (!Resolve(uid, ref component, false))
  29. return;
  30. component.Control?.Dispose();
  31. component.Control = null;
  32. var ev = new AmmoCounterControlEvent();
  33. RaiseLocalEvent(uid, ev, false);
  34. // Fallback to default if none specified
  35. ev.Control ??= new DefaultStatusControl();
  36. component.Control = ev.Control;
  37. UpdateAmmoCount(uid, component);
  38. }
  39. private void UpdateAmmoCount(EntityUid uid, AmmoCounterComponent component)
  40. {
  41. if (component.Control == null)
  42. return;
  43. var ev = new UpdateAmmoCounterEvent()
  44. {
  45. Control = component.Control
  46. };
  47. RaiseLocalEvent(uid, ev, false);
  48. }
  49. protected override void UpdateAmmoCount(EntityUid uid, bool prediction = true)
  50. {
  51. // Don't use resolves because the method is shared and there's no compref and I'm trying to
  52. // share as much code as possible
  53. if (prediction && !Timing.IsFirstTimePredicted ||
  54. !TryComp<AmmoCounterComponent>(uid, out var clientComp))
  55. {
  56. return;
  57. }
  58. UpdateAmmoCount(uid, clientComp);
  59. }
  60. /// <summary>
  61. /// Raised when an ammocounter is requesting a control.
  62. /// </summary>
  63. public sealed class AmmoCounterControlEvent : EntityEventArgs
  64. {
  65. public Control? Control;
  66. }
  67. /// <summary>
  68. /// Raised whenever the ammo count / magazine for a control needs updating.
  69. /// </summary>
  70. public sealed class UpdateAmmoCounterEvent : HandledEntityEventArgs
  71. {
  72. public Control Control = default!;
  73. }
  74. #region Controls
  75. private sealed class DefaultStatusControl : Control
  76. {
  77. private readonly BulletRender _bulletRender;
  78. public DefaultStatusControl()
  79. {
  80. MinHeight = 15;
  81. HorizontalExpand = true;
  82. VerticalAlignment = VAlignment.Center;
  83. AddChild(_bulletRender = new BulletRender
  84. {
  85. HorizontalAlignment = HAlignment.Right,
  86. VerticalAlignment = VAlignment.Bottom
  87. });
  88. }
  89. public void Update(int count, int capacity)
  90. {
  91. _bulletRender.Count = count;
  92. _bulletRender.Capacity = capacity;
  93. _bulletRender.Type = capacity > 50 ? BulletRender.BulletType.Tiny : BulletRender.BulletType.Normal;
  94. }
  95. }
  96. public sealed class BoxesStatusControl : Control
  97. {
  98. private readonly BatteryBulletRenderer _bullets;
  99. private readonly Label _ammoCount;
  100. public BoxesStatusControl()
  101. {
  102. MinHeight = 15;
  103. HorizontalExpand = true;
  104. VerticalAlignment = Control.VAlignment.Center;
  105. AddChild(new BoxContainer
  106. {
  107. Orientation = BoxContainer.LayoutOrientation.Horizontal,
  108. Children =
  109. {
  110. (_bullets = new BatteryBulletRenderer
  111. {
  112. Margin = new Thickness(0, 0, 5, 0),
  113. HorizontalExpand = true
  114. }),
  115. (_ammoCount = new Label
  116. {
  117. StyleClasses = { StyleNano.StyleClassItemStatus },
  118. HorizontalAlignment = HAlignment.Right,
  119. VerticalAlignment = VAlignment.Bottom
  120. }),
  121. }
  122. });
  123. }
  124. public void Update(int count, int max)
  125. {
  126. _ammoCount.Visible = true;
  127. _ammoCount.Text = $"x{count:00}";
  128. _bullets.Capacity = max;
  129. _bullets.Count = count;
  130. }
  131. }
  132. private sealed class ChamberMagazineStatusControl : Control
  133. {
  134. private readonly BulletRender _bulletRender;
  135. private readonly TextureRect _chamberedBullet;
  136. private readonly Label _noMagazineLabel;
  137. private readonly Label _ammoCount;
  138. public ChamberMagazineStatusControl()
  139. {
  140. MinHeight = 15;
  141. HorizontalExpand = true;
  142. VerticalAlignment = Control.VAlignment.Center;
  143. AddChild(new BoxContainer
  144. {
  145. Orientation = BoxContainer.LayoutOrientation.Horizontal,
  146. HorizontalExpand = true,
  147. Children =
  148. {
  149. new Control
  150. {
  151. HorizontalExpand = true,
  152. Margin = new Thickness(0, 0, 5, 0),
  153. Children =
  154. {
  155. (_bulletRender = new BulletRender
  156. {
  157. HorizontalAlignment = HAlignment.Right,
  158. VerticalAlignment = VAlignment.Bottom
  159. }),
  160. (_noMagazineLabel = new Label
  161. {
  162. Text = "No Magazine!",
  163. StyleClasses = {StyleNano.StyleClassItemStatus}
  164. })
  165. }
  166. },
  167. new BoxContainer
  168. {
  169. Orientation = BoxContainer.LayoutOrientation.Vertical,
  170. VerticalAlignment = VAlignment.Bottom,
  171. Margin = new Thickness(0, 0, 0, 2),
  172. Children =
  173. {
  174. (_ammoCount = new Label
  175. {
  176. StyleClasses = {StyleNano.StyleClassItemStatus},
  177. HorizontalAlignment = HAlignment.Right,
  178. }),
  179. (_chamberedBullet = new TextureRect
  180. {
  181. Texture = StaticIoC.ResC.GetTexture("/Textures/Interface/ItemStatus/Bullets/chambered.png"),
  182. HorizontalAlignment = HAlignment.Left,
  183. }),
  184. }
  185. }
  186. }
  187. });
  188. }
  189. public void Update(bool chambered, bool magazine, int count, int capacity)
  190. {
  191. _chamberedBullet.ModulateSelfOverride =
  192. chambered ? Color.FromHex("#d7df60") : Color.Black;
  193. if (!magazine)
  194. {
  195. _bulletRender.Visible = false;
  196. _noMagazineLabel.Visible = true;
  197. _ammoCount.Visible = false;
  198. return;
  199. }
  200. _bulletRender.Visible = true;
  201. _noMagazineLabel.Visible = false;
  202. _ammoCount.Visible = true;
  203. _bulletRender.Count = count;
  204. _bulletRender.Capacity = capacity;
  205. _bulletRender.Type = capacity > 50 ? BulletRender.BulletType.Tiny : BulletRender.BulletType.Normal;
  206. _ammoCount.Text = $"x{count:00}";
  207. }
  208. public void PlayAlarmAnimation(Animation animation)
  209. {
  210. _noMagazineLabel.PlayAnimation(animation, "alarm");
  211. }
  212. }
  213. private sealed class RevolverStatusControl : Control
  214. {
  215. private readonly BoxContainer _bulletsList;
  216. public RevolverStatusControl()
  217. {
  218. MinHeight = 15;
  219. HorizontalExpand = true;
  220. VerticalAlignment = Control.VAlignment.Center;
  221. AddChild((_bulletsList = new BoxContainer
  222. {
  223. Orientation = BoxContainer.LayoutOrientation.Horizontal,
  224. HorizontalExpand = true,
  225. VerticalAlignment = VAlignment.Center,
  226. SeparationOverride = 0
  227. }));
  228. }
  229. public void Update(int currentIndex, bool?[] bullets)
  230. {
  231. _bulletsList.RemoveAllChildren();
  232. var capacity = bullets.Length;
  233. string texturePath;
  234. if (capacity <= 20)
  235. {
  236. texturePath = "/Textures/Interface/ItemStatus/Bullets/normal.png";
  237. }
  238. else if (capacity <= 30)
  239. {
  240. texturePath = "/Textures/Interface/ItemStatus/Bullets/small.png";
  241. }
  242. else
  243. {
  244. texturePath = "/Textures/Interface/ItemStatus/Bullets/tiny.png";
  245. }
  246. var texture = StaticIoC.ResC.GetTexture(texturePath);
  247. var spentTexture = StaticIoC.ResC.GetTexture("/Textures/Interface/ItemStatus/Bullets/empty.png");
  248. FillBulletRow(currentIndex, bullets, _bulletsList, texture, spentTexture);
  249. }
  250. private void FillBulletRow(int currentIndex, bool?[] bullets, Control container, Texture texture, Texture emptyTexture)
  251. {
  252. var capacity = bullets.Length;
  253. var colorA = Color.FromHex("#b68f0e");
  254. var colorB = Color.FromHex("#d7df60");
  255. var colorSpentA = Color.FromHex("#b50e25");
  256. var colorSpentB = Color.FromHex("#d3745f");
  257. var colorGoneA = Color.FromHex("#000000");
  258. var colorGoneB = Color.FromHex("#222222");
  259. var altColor = false;
  260. var scale = 1.3f;
  261. for (var i = 0; i < capacity; i++)
  262. {
  263. var bulletFree = bullets[i];
  264. // Add a outline
  265. var box = new Control()
  266. {
  267. MinSize = texture.Size * scale,
  268. };
  269. if (i == currentIndex)
  270. {
  271. box.AddChild(new TextureRect
  272. {
  273. Texture = texture,
  274. TextureScale = new Vector2(scale, scale),
  275. ModulateSelfOverride = Color.LimeGreen,
  276. });
  277. }
  278. Color color;
  279. Texture bulletTexture = texture;
  280. if (bulletFree.HasValue)
  281. {
  282. if (bulletFree.Value)
  283. {
  284. color = altColor ? colorA : colorB;
  285. }
  286. else
  287. {
  288. color = altColor ? colorSpentA : colorSpentB;
  289. bulletTexture = emptyTexture;
  290. }
  291. }
  292. else
  293. {
  294. color = altColor ? colorGoneA : colorGoneB;
  295. }
  296. box.AddChild(new TextureRect
  297. {
  298. Stretch = TextureRect.StretchMode.KeepCentered,
  299. Texture = bulletTexture,
  300. ModulateSelfOverride = color,
  301. });
  302. altColor ^= true;
  303. container.AddChild(box);
  304. }
  305. }
  306. }
  307. #endregion
  308. }