Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Fish_and_Bread_Kingdoms
- {
- /// <summary>
- /// Class responsible for button logic, activates event based on type when clicked
- /// </summary>
- class Button : GameObject
- {
- private bool pressed;
- private string type;
- static private int unitCount;
- private Vector2 spawnPos = new Vector2(65.36f * 128, 60.5f * 128);
- /// <summary>
- /// Area where button can be clicked
- /// </summary>
- public override Rectangle CollisionBox {
- get { return new Rectangle((int)position.X + 3, (int)position.Y + 3, sprite.Width - 6, sprite.Height - 6); }
- }
- /// <summary>
- /// Button constructor
- /// </summary>
- /// <param name="pos">Position</param>
- /// <param name="sprite">Button sprite</param>
- /// <param name="type">Button type</param>
- public Button(Vector2 pos, Texture2D sprite, string type) : base(pos)
- {
- this.type = type;
- this.sprite = sprite;
- layer = 0.2f;
- DrawAsUI = true;
- }
- /// <summary>
- /// Runs button logic when panel is shown and updates hidden state
- /// </summary>
- public override void Update()
- {
- if (GameManager.ShowBreadStorageUI || GameManager.ShowFishStorageUI || GameManager.ShowTownhallUI) { ButtonLogic(); }
- if (type.Contains("Fish")) { hidden = !GameManager.ShowFishStorageUI; }
- if (type.Contains("Bread")) { hidden = !GameManager.ShowBreadStorageUI; }
- if (type.Contains("Hire")) { hidden = !GameManager.ShowTownhallUI; }
- }
- /// <summary>
- /// Activatates events when buttons are clicked
- /// Also highlights button when mouse is over it
- /// </summary>
- private void ButtonLogic()
- {
- Point point = GameWorld.Mousestate.Position;
- if (CollisionBox.Contains(point))
- {
- tint = new Color(240, 240, 240);
- if (GameWorld.Mousestate.LeftButton == ButtonState.Pressed)
- {
- if (!pressed)
- {
- if(!hidden)
- {
- // Do something different for every button type
- switch (type)
- {
- case "HireFarmer":
- if(GameManager.Money >= 10)
- {
- GameManager.Money -= 10;
- GameManager.Units.Add(GameWorld.Instantiate(new Unit(spawnPos, "farmer", unitCount)) as Unit);
- unitCount++;
- }
- else
- {
- // Error sound
- }
- break;
- case "HireFisherman":
- if (GameManager.Money >= 30)
- {
- GameManager.Money -= 30;
- GameManager.Units.Add(GameWorld.Instantiate(new Unit(spawnPos, "fisherman", unitCount)) as Unit);
- unitCount++;
- }
- else
- {
- // Error sound
- }
- break;
- case "HireMerchant":
- if (GameManager.Money >= 50)
- {
- GameManager.Money -= 50;
- GameManager.Units.Add(GameWorld.Instantiate(new Unit(spawnPos, "merchant", unitCount)) as Unit);
- unitCount++;
- }
- else
- {
- // Error sound
- }
- break;
- case "Send5Bread":
- // Send bread to market
- if(GameManager.StorageInstance.GetBread() - GameManager.StorageInstance.RequestedBread >= 5)
- GameManager.StorageInstance.RequestedBread += 5;
- break;
- case "Send10Bread":
- // Send bread to market
- if (GameManager.StorageInstance.GetBread() - GameManager.StorageInstance.RequestedBread >= 10)
- GameManager.StorageInstance.RequestedBread += 10;
- break;
- case "Send20Bread":
- // Send bread to market
- if (GameManager.StorageInstance.GetBread() - GameManager.StorageInstance.RequestedBread >= 20)
- GameManager.StorageInstance.RequestedBread += 20;
- break;
- }
- layer = 0;
- }
- }
- pressed = true;
- }
- else
- {
- pressed = false;
- layer = 0.2f;
- }
- }
- else
- {
- tint = Color.White;
- layer = 0.2f;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement