Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using static CorinnaMod.LogicUtilities;
  4. using static CorinnaMod.Graphics.Spritesheets.UI.UISpriteIDs;
  5. using static CorinnaMod.EntityData.PlayerData.PlayerInfo;
  6.  
  7. namespace CorinnaMod.UI.UIData
  8. {
  9. /// <summary>
  10. /// Handles the logic and drawing of the item slots in the player ui.
  11. /// </summary>
  12. public class PlayerHotBar
  13. {
  14. /// <summary>
  15. /// Amount of item background inventory slots to be drawn.
  16. /// </summary>
  17. private static int AmountofItemSlots
  18. => 10;
  19.  
  20. /// <summary>
  21. /// Horizontal offset to begin drawing the hotbar squares.
  22. /// </summary>
  23. private static int HotBarXOffset
  24. => (int)(0.1f * ScreenW);
  25.  
  26. /// <summary>
  27. /// Vertical offset to begin drawing the hotbar Squares.
  28. /// </summary>
  29. private static float HotBarYOffSet
  30. => 0.8f * ScreenH;
  31.  
  32. /// <summary>
  33. /// Coordinates of the left-top point of the hotbar.
  34. /// </summary>
  35. public static Vector2 HotBarPosition
  36. => new Vector2(HotBarXOffset, HotBarYOffSet);
  37.  
  38. /// <summary>
  39. /// Space between the hotbar slots.
  40. /// </summary>
  41. private static int HotBarMargin
  42. => 6;
  43.  
  44. /// <summary>
  45. /// Draws a background item slot graphic, then draws the inventory item related to that particular slot
  46. /// over it.
  47. /// </summary>
  48. /// <param name="spriteBatch">More like sprite bitch.</param>
  49. public static void DrawHotBar(SpriteBatch spriteBatch)
  50. {
  51. for (int Index = 0; Index < AmountofItemSlots; Index++)
  52. {
  53. DrawItemSlot(spriteBatch, Index);
  54. DrawItemOverSlot(spriteBatch, Index);
  55. }
  56.  
  57. }
  58.  
  59. /// <summary>
  60. /// Sets the cooordinates of each slot.
  61. /// </summary>
  62. /// <param name="Index">Iteration of the hotbar.</param>
  63. /// <returns></returns>
  64. public static Vector2 CalculateHotBarPosition(int Index)
  65. => new Vector2(((Index * HotBarMargin) + (Index * ItemSlot.Width)) + HotBarXOffset, HotBarYOffSet);
  66.  
  67. /// <summary>
  68. /// Where to draw the item graphic and accounts for scaling to have it centered.
  69. /// </summary>
  70. /// <param name="Index">Iteration of the hotbar slot.</param>
  71. /// <param name="HotBarPosition">Top left coordinates of the current iteration of the hotbarslot./param>
  72. /// <returns></returns>
  73. public static Vector2 CalculateItemPosition(int Index, Vector2 HotBarPosition)
  74. => ScaleTexture(GetInventoryItemSlotTexture(Index)) == 0.5f ?
  75. HotBarPosition.AddXY(CenterMargins(ItemSlot.Width, GetInventoryItemSlotTexture(Index).Width * 0.5f),
  76. CenterMargins(ItemSlot.Height, GetInventoryItemSlotTexture(Index).Height * 0.5f)):
  77. HotBarPosition.AddXY(CenterMargins(ItemSlot.Width, GetInventoryItemSlotTexture(Index).Width),
  78. CenterMargins(ItemSlot.Height, GetInventoryItemSlotTexture(Index).Height));
  79.  
  80. /// <summary>
  81. /// Scales down textures if they're too big to fit in the hotbar background icon.
  82. /// </summary>
  83. /// <param name="Texture">The texture.</param>
  84. /// <returns>A float to scale down the texture if needed.</returns>
  85. public static float ScaleTexture(Texture2D Texture)
  86. => Texture.Width < ItemSlot.Width && Texture.Height < ItemSlot.Height ? 1 : 0.5f;
  87.  
  88. /// <summary>
  89. /// Calls the spritebatch to draw the background graphic of the item slot.
  90. /// </summary>
  91. /// <param name="Batch">The spritebatch object.</param>
  92. /// <param name="Index">The current iteration of the hotbar slot.</param>
  93. private static void DrawItemSlot(SpriteBatch Batch, int Index)
  94. => Batch.Draw(ItemSlot, CalculateHotBarPosition(Index), Color.White);
  95.  
  96. /// <summary>
  97. /// Draws the item after the background slot has been drawn.
  98. /// </summary>
  99. /// <param name="Batch">The spritebatch object.</param>
  100. /// <param name="Index">The current iteration of the hotbar slot.</param>
  101. private static void DrawItemOverSlot(SpriteBatch Batch, int Index)
  102. => Batch.DrawtoScale(GetInventoryItemSlotTexture(Index),
  103. CalculateItemPosition(Index, CalculateHotBarPosition(Index)),
  104. ScaleTexture(GetInventoryItemSlotTexture(Index)));
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement