Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using static CorinnaMod.LogicUtilities;
- using static CorinnaMod.Graphics.Spritesheets.UI.UISpriteIDs;
- using static CorinnaMod.EntityData.PlayerData.PlayerInfo;
- namespace CorinnaMod.UI.UIData
- {
- /// <summary>
- /// Handles the logic and drawing of the item slots in the player ui.
- /// </summary>
- public class PlayerHotBar
- {
- /// <summary>
- /// Amount of item background inventory slots to be drawn.
- /// </summary>
- private static int AmountofItemSlots
- => 10;
- /// <summary>
- /// Horizontal offset to begin drawing the hotbar squares.
- /// </summary>
- private static int HotBarXOffset
- => (int)(0.1f * ScreenW);
- /// <summary>
- /// Vertical offset to begin drawing the hotbar Squares.
- /// </summary>
- private static float HotBarYOffSet
- => 0.8f * ScreenH;
- /// <summary>
- /// Coordinates of the left-top point of the hotbar.
- /// </summary>
- public static Vector2 HotBarPosition
- => new Vector2(HotBarXOffset, HotBarYOffSet);
- /// <summary>
- /// Space between the hotbar slots.
- /// </summary>
- private static int HotBarMargin
- => 6;
- /// <summary>
- /// Draws a background item slot graphic, then draws the inventory item related to that particular slot
- /// over it.
- /// </summary>
- /// <param name="spriteBatch">More like sprite bitch.</param>
- public static void DrawHotBar(SpriteBatch spriteBatch)
- {
- for (int Index = 0; Index < AmountofItemSlots; Index++)
- {
- DrawItemSlot(spriteBatch, Index);
- DrawItemOverSlot(spriteBatch, Index);
- }
- }
- /// <summary>
- /// Sets the cooordinates of each slot.
- /// </summary>
- /// <param name="Index">Iteration of the hotbar.</param>
- /// <returns></returns>
- public static Vector2 CalculateHotBarPosition(int Index)
- => new Vector2(((Index * HotBarMargin) + (Index * ItemSlot.Width)) + HotBarXOffset, HotBarYOffSet);
- /// <summary>
- /// Where to draw the item graphic and accounts for scaling to have it centered.
- /// </summary>
- /// <param name="Index">Iteration of the hotbar slot.</param>
- /// <param name="HotBarPosition">Top left coordinates of the current iteration of the hotbarslot./param>
- /// <returns></returns>
- public static Vector2 CalculateItemPosition(int Index, Vector2 HotBarPosition)
- => ScaleTexture(GetInventoryItemSlotTexture(Index)) == 0.5f ?
- HotBarPosition.AddXY(CenterMargins(ItemSlot.Width, GetInventoryItemSlotTexture(Index).Width * 0.5f),
- CenterMargins(ItemSlot.Height, GetInventoryItemSlotTexture(Index).Height * 0.5f)):
- HotBarPosition.AddXY(CenterMargins(ItemSlot.Width, GetInventoryItemSlotTexture(Index).Width),
- CenterMargins(ItemSlot.Height, GetInventoryItemSlotTexture(Index).Height));
- /// <summary>
- /// Scales down textures if they're too big to fit in the hotbar background icon.
- /// </summary>
- /// <param name="Texture">The texture.</param>
- /// <returns>A float to scale down the texture if needed.</returns>
- public static float ScaleTexture(Texture2D Texture)
- => Texture.Width < ItemSlot.Width && Texture.Height < ItemSlot.Height ? 1 : 0.5f;
- /// <summary>
- /// Calls the spritebatch to draw the background graphic of the item slot.
- /// </summary>
- /// <param name="Batch">The spritebatch object.</param>
- /// <param name="Index">The current iteration of the hotbar slot.</param>
- private static void DrawItemSlot(SpriteBatch Batch, int Index)
- => Batch.Draw(ItemSlot, CalculateHotBarPosition(Index), Color.White);
- /// <summary>
- /// Draws the item after the background slot has been drawn.
- /// </summary>
- /// <param name="Batch">The spritebatch object.</param>
- /// <param name="Index">The current iteration of the hotbar slot.</param>
- private static void DrawItemOverSlot(SpriteBatch Batch, int Index)
- => Batch.DrawtoScale(GetInventoryItemSlotTexture(Index),
- CalculateItemPosition(Index, CalculateHotBarPosition(Index)),
- ScaleTexture(GetInventoryItemSlotTexture(Index)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement