Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Fish_and_Bread_Kingdoms
  11. {
  12. /// <summary>
  13. /// Class responsible for button logic, activates event based on type when clicked
  14. /// </summary>
  15. class Button : GameObject
  16. {
  17. private bool pressed;
  18. private string type;
  19. static private int unitCount;
  20.  
  21. private Vector2 spawnPos = new Vector2(65.36f * 128, 60.5f * 128);
  22.  
  23. /// <summary>
  24. /// Area where button can be clicked
  25. /// </summary>
  26. public override Rectangle CollisionBox {
  27. get { return new Rectangle((int)position.X + 3, (int)position.Y + 3, sprite.Width - 6, sprite.Height - 6); }
  28. }
  29.  
  30. /// <summary>
  31. /// Button constructor
  32. /// </summary>
  33. /// <param name="pos">Position</param>
  34. /// <param name="sprite">Button sprite</param>
  35. /// <param name="type">Button type</param>
  36. public Button(Vector2 pos, Texture2D sprite, string type) : base(pos)
  37. {
  38. this.type = type;
  39. this.sprite = sprite;
  40. layer = 0.2f;
  41. DrawAsUI = true;
  42. }
  43.  
  44. /// <summary>
  45. /// Runs button logic when panel is shown and updates hidden state
  46. /// </summary>
  47. public override void Update()
  48. {
  49. if (GameManager.ShowBreadStorageUI || GameManager.ShowFishStorageUI || GameManager.ShowTownhallUI) { ButtonLogic(); }
  50. if (type.Contains("Fish")) { hidden = !GameManager.ShowFishStorageUI; }
  51. if (type.Contains("Bread")) { hidden = !GameManager.ShowBreadStorageUI; }
  52. if (type.Contains("Hire")) { hidden = !GameManager.ShowTownhallUI; }
  53. }
  54.  
  55. /// <summary>
  56. /// Activatates events when buttons are clicked
  57. /// Also highlights button when mouse is over it
  58. /// </summary>
  59. private void ButtonLogic()
  60. {
  61. Point point = GameWorld.Mousestate.Position;
  62.  
  63. if (CollisionBox.Contains(point))
  64. {
  65. tint = new Color(240, 240, 240);
  66. if (GameWorld.Mousestate.LeftButton == ButtonState.Pressed)
  67. {
  68. if (!pressed)
  69. {
  70. if(!hidden)
  71. {
  72. // Do something different for every button type
  73. switch (type)
  74. {
  75. case "HireFarmer":
  76. if(GameManager.Money >= 10)
  77. {
  78. GameManager.Money -= 10;
  79. GameManager.Units.Add(GameWorld.Instantiate(new Unit(spawnPos, "farmer", unitCount)) as Unit);
  80. unitCount++;
  81. }
  82. else
  83. {
  84. // Error sound
  85. }
  86.  
  87. break;
  88.  
  89. case "HireFisherman":
  90. if (GameManager.Money >= 30)
  91. {
  92. GameManager.Money -= 30;
  93. GameManager.Units.Add(GameWorld.Instantiate(new Unit(spawnPos, "fisherman", unitCount)) as Unit);
  94. unitCount++;
  95. }
  96. else
  97. {
  98. // Error sound
  99. }
  100.  
  101. break;
  102.  
  103. case "HireMerchant":
  104. if (GameManager.Money >= 50)
  105. {
  106. GameManager.Money -= 50;
  107. GameManager.Units.Add(GameWorld.Instantiate(new Unit(spawnPos, "merchant", unitCount)) as Unit);
  108. unitCount++;
  109. }
  110. else
  111. {
  112. // Error sound
  113. }
  114.  
  115. break;
  116.  
  117.  
  118. case "Send5Bread":
  119. // Send bread to market
  120. if(GameManager.StorageInstance.GetBread() - GameManager.StorageInstance.RequestedBread >= 5)
  121. GameManager.StorageInstance.RequestedBread += 5;
  122. break;
  123.  
  124. case "Send10Bread":
  125. // Send bread to market
  126. if (GameManager.StorageInstance.GetBread() - GameManager.StorageInstance.RequestedBread >= 10)
  127. GameManager.StorageInstance.RequestedBread += 10;
  128. break;
  129.  
  130. case "Send20Bread":
  131. // Send bread to market
  132. if (GameManager.StorageInstance.GetBread() - GameManager.StorageInstance.RequestedBread >= 20)
  133. GameManager.StorageInstance.RequestedBread += 20;
  134. break;
  135. }
  136. layer = 0;
  137. }
  138. }
  139. pressed = true;
  140. }
  141. else
  142. {
  143. pressed = false;
  144. layer = 0.2f;
  145. }
  146. }
  147. else
  148. {
  149. tint = Color.White;
  150. layer = 0.2f;
  151. }
  152. }
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement