ATB.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using OpenVIII.Kernel;
  5. namespace OpenVIII.IGMDataItem.Gradient
  6. {
  7. public class ATB : Texture
  8. {
  9. #region Fields
  10. private static readonly object Locker = new object();
  11. private static Texture2D _common;
  12. #endregion Fields
  13. #region Constructors
  14. private ATB()
  15. {
  16. }
  17. #endregion Constructors
  18. #region Properties
  19. ///Restriction controls the bounds of the drawing. And Pos is where it will draw.
  20. ///So if one is set and the other is not than you see no bar.
  21. public override Rectangle Pos { get => base.Pos; set => Restriction = base.Pos = value; }
  22. #endregion Properties
  23. #region Methods
  24. public static ATB Create(Rectangle? pos = null)
  25. {
  26. var r = new ATB
  27. {
  28. _pos = pos ?? Rectangle.Empty,
  29. Restriction = pos ?? Rectangle.Empty
  30. };
  31. Memory.MainThreadOnlyActions.Enqueue(
  32. () =>
  33. {
  34. r.Data = ThreadUnsafeOperations(r.Width);
  35. r.Width = r.Data.Width;
  36. }
  37. );
  38. return r;
  39. }
  40. public static Texture2D ThreadUnsafeOperations(int width)
  41. {
  42. if (_common != null) return _common;
  43. lock (Locker)
  44. {
  45. const float dark = 0.067f;
  46. const float fade = 0.933f;
  47. var total = width;
  48. var lightLine = new Color(118, 118, 118, 255);
  49. var darkLine = new Color(58, 58, 58, 255);
  50. var cFade = new Color[total];
  51. int i;
  52. for (i = 0; i < cFade.Length - (dark * total); i++)
  53. cFade[i] = Color.Lerp(Color.Black, lightLine, i / (fade * total));
  54. for (; i < cFade.Length; i++)
  55. cFade[i] = darkLine;
  56. _common = new Texture2D(Memory.Graphics.GraphicsDevice, cFade.Length, 1, false, SurfaceFormat.Color);
  57. _common.SetData(cFade);
  58. }
  59. return _common;
  60. }
  61. public override void Refresh(Damageable damageable)
  62. {
  63. base.Refresh(damageable);
  64. damageable?.Refresh();
  65. }
  66. public override bool Update()
  67. {
  68. if (!Enabled) return false;
  69. if (Damageable == null) return true;
  70. X = Lerp(Restriction.X - Width, Restriction.X, Damageable.ATBPercent);
  71. if (Damageable.IsDead)
  72. {
  73. //Color = Faded_Color = Color.Red * .5f;
  74. X = 0;
  75. }
  76. else if (Damageable.IsPetrify)
  77. {
  78. Color = Faded_Color = Color.Gray * .8f;
  79. }
  80. else if ((Damageable.Statuses1 & BattleOnlyStatuses.Stop) != 0)
  81. {
  82. Color = Faded_Color = Color.DarkBlue * .8f;
  83. }
  84. else if ((Damageable.Statuses1 & BattleOnlyStatuses.Slow) != 0)
  85. {
  86. Color = Faded_Color = Color.DarkCyan * .8f;
  87. }
  88. else if ((Damageable.Statuses1 & BattleOnlyStatuses.Haste) != 0)
  89. {
  90. Color = Faded_Color = Color.Violet * .8f;
  91. }
  92. else Color = Faded_Color = Color.Orange * .8f;
  93. return true;
  94. int Lerp(int x, int y, float p) => (int)Math.Round(MathHelper.Lerp(x, y, p));
  95. }
  96. #endregion Methods
  97. }
  98. }