Advertisement
Rodrigo_Medeiros

HealthBar

Aug 19th, 2015
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.50 KB | None | 0 0
  1. // Code Created by Wellington Pires
  2.  
  3. using SiliconStudio.Core.Mathematics;
  4. using SiliconStudio.Paradox.Engine;
  5. using SiliconStudio.Paradox.Graphics;
  6. using SiliconStudio.Paradox.UI;
  7. using SiliconStudio.Paradox.UI.Controls;
  8. using SiliconStudio.Paradox.UI.Panels;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14.  
  15. namespace HealthBar
  16. {
  17.     public class UIScript : AsyncScript
  18.     {
  19.         private Canvas gameRoot;
  20.         private SpriteSheet uiImages;
  21.        // private UIImageGroup uiImages;
  22.         private ImageElement fillHP;
  23.         private ImageElement fillMP;
  24.         private SpriteFont spriteFont;
  25.  
  26.         private Button hpButtonIncrement;
  27.         private Button hpButtonDecrement;
  28.         private Button mpButtonIncrement;
  29.         private Button mpButtonDecrement;
  30.  
  31.         private float fillHPWidth = 551.0f;
  32.         private float fillHPHeight = 82.0f;
  33.  
  34.         private float fillMPWidth = 558.0f;
  35.         private float fillMPHeight = 86.0f;
  36.  
  37.         public void Start()
  38.         {
  39.             uiImages = Asset.Load<SpriteSheet>("UIImages");
  40.             spriteFont = Asset.Load<SpriteFont>("Font");
  41.  
  42.             createHealthBarUI();
  43.  
  44.             Entity.Get<UIComponent>().RootElement = gameRoot;
  45.         }
  46.  
  47.         public override async Task Execute()
  48.         {
  49.             Start();
  50.             hpButtonIncrement.Click += (s, e) => HPButtonIncrementClick();
  51.             hpButtonDecrement.Click += (s, e) => HPButtonDecrementClick();
  52.  
  53.             mpButtonIncrement.Click += (s, e) => MPButtonIncrementClick();
  54.             mpButtonDecrement.Click += (s, e) => MPButtonDecrementClick();
  55.  
  56.             while(Game.IsRunning) {
  57.                 await Script.NextFrame();
  58.             }
  59.         }
  60.  
  61.         private void HPButtonIncrementClick()
  62.         {
  63.  
  64.             if (fillHP.Source.Region.Width < fillHPWidth)
  65.             {
  66.                 var region = fillHP.Source.Region;
  67.  
  68.                 region.Width += 10.0f;
  69.  
  70.                 fillHP.Source.Region = region;
  71.             }
  72.  
  73.         }
  74.  
  75.         private void HPButtonDecrementClick()
  76.         {
  77.             if (fillHP.Source.Region.Width > 0)
  78.             {
  79.                 var region = fillHP.Source.Region;
  80.  
  81.                 region.Width -= 10.0f;
  82.  
  83.                 fillHP.Source.Region = region;
  84.             }
  85.         }
  86.  
  87.         private void MPButtonIncrementClick()
  88.         {
  89.  
  90.             if (fillMP.Source.Region.Width < fillMPWidth)
  91.             {
  92.                 var region = fillMP.Source.Region;
  93.  
  94.                 region.Width += 10.0f;
  95.  
  96.                 fillMP.Source.Region = region;
  97.             }
  98.  
  99.         }
  100.  
  101.         private void MPButtonDecrementClick()
  102.         {
  103.             if (fillMP.Source.Region.Width > 0)
  104.             {
  105.                 var region = fillMP.Source.Region;
  106.  
  107.                 region.Width -= 10.0f;
  108.  
  109.                 fillMP.Source.Region = region;
  110.             }
  111.         }
  112.  
  113.         private void createHealthBarUI()
  114.         {
  115.  
  116.             var charInfo = new ImageElement
  117.             {
  118.                 Source = uiImages["CharInfo"],
  119.             };
  120.             charInfo.Size = new Vector3(charInfo.Source.Region.Width, charInfo.Source.Region.Height, 1f);
  121.             charInfo.SetCanvasRelativePosition(new Vector3(0f, 0f, 1f));
  122.  
  123.             #region HP
  124.  
  125.             fillHP = new ImageElement
  126.             {
  127.                 Source = uiImages["FillHP"]
  128.             };
  129.             fillHP.Size = new Vector3(fillHPWidth, fillHPHeight, 1f);
  130.             fillHP.SetCanvasRelativePosition(new Vector3(0.148f, 0.039f, 1f));
  131.  
  132.             var hpText = new TextBlock
  133.             {
  134.                 Font = spriteFont,
  135.                 Text = "HP",
  136.                 TextColor = Color.White,
  137.                 HorizontalAlignment = HorizontalAlignment.Center,
  138.                 VerticalAlignment = VerticalAlignment.Center
  139.             };
  140.             hpText.SetCanvasRelativePosition(new Vector3(0.35f, 0.6f, 0f));
  141.  
  142.             hpButtonIncrement = new Button
  143.             {
  144.                 Content = new TextBlock
  145.                 {
  146.                     Font = spriteFont,
  147.                     Text = "+",
  148.                     TextColor = Color.White,
  149.                     HorizontalAlignment = HorizontalAlignment.Center,
  150.                     VerticalAlignment = VerticalAlignment.Center
  151.                 },
  152.                 Padding = new Thickness(5, 5, 5, 5),
  153.                 MinimumWidth = 50f,
  154.             };
  155.             hpButtonIncrement.SetCanvasRelativePosition(new Vector3(0.3f, 0.7f, 0f));
  156.  
  157.             hpButtonDecrement = new Button
  158.             {
  159.                 Content = new TextBlock
  160.                 {
  161.                     Font = spriteFont,
  162.                     Text = "-",
  163.                     TextColor = Color.White,
  164.                     HorizontalAlignment = HorizontalAlignment.Center,
  165.                     VerticalAlignment = VerticalAlignment.Center
  166.                 },
  167.                 Padding = new Thickness(5, 5, 5, 5),
  168.                 MinimumWidth = 50f,
  169.             };
  170.             hpButtonDecrement.SetCanvasRelativePosition(new Vector3(0.4f, 0.7f, 0f));
  171.  
  172.             #endregion
  173.  
  174.             #region MP
  175.  
  176.             fillMP = new ImageElement
  177.             {
  178.                 Source = uiImages["FillMP"]
  179.             };
  180.             fillMP.Size = new Vector3(fillMPWidth, fillMPHeight, 1f);
  181.             fillMP.SetCanvasRelativePosition(new Vector3(0.146f, 0.176f, 1f));
  182.  
  183.             var mpText = new TextBlock
  184.             {
  185.                 Font = spriteFont,
  186.                 Text = "MP",
  187.                 TextColor = Color.White,
  188.                 HorizontalAlignment = HorizontalAlignment.Center,
  189.                 VerticalAlignment = VerticalAlignment.Center
  190.             };
  191.             mpText.SetCanvasRelativePosition(new Vector3(0.55f, 0.6f, 0f));
  192.  
  193.  
  194.             mpButtonIncrement = new Button
  195.             {
  196.                 Content = new TextBlock
  197.                 {
  198.                     Font = spriteFont,
  199.                     Text = "+",
  200.                     TextColor = Color.White,
  201.                     HorizontalAlignment = HorizontalAlignment.Center,
  202.                     VerticalAlignment = VerticalAlignment.Center
  203.                 },
  204.                 Padding = new Thickness(5, 5, 5, 5),
  205.                 MinimumWidth = 50f,
  206.             };
  207.             mpButtonIncrement.SetCanvasRelativePosition(new Vector3(0.5f, 0.7f, 0f));
  208.  
  209.             mpButtonDecrement = new Button
  210.             {
  211.                 Content = new TextBlock
  212.                 {
  213.                     Font = spriteFont,
  214.                     Text = "-",
  215.                     TextColor = Color.White,
  216.                     HorizontalAlignment = HorizontalAlignment.Center,
  217.                     VerticalAlignment = VerticalAlignment.Center
  218.                 },
  219.                 Padding = new Thickness(5, 5, 5, 5),
  220.                 MinimumWidth = 50f,
  221.             };
  222.             mpButtonDecrement.SetCanvasRelativePosition(new Vector3(0.6f, 0.7f, 0f));
  223.  
  224.             #endregion
  225.  
  226.             gameRoot = new Canvas();
  227.             gameRoot.Children.Add(charInfo);
  228.             gameRoot.Children.Add(fillHP);
  229.             gameRoot.Children.Add(fillMP);
  230.  
  231.             gameRoot.Children.Add(hpText);
  232.             gameRoot.Children.Add(hpButtonIncrement);
  233.             gameRoot.Children.Add(hpButtonDecrement);
  234.  
  235.             gameRoot.Children.Add(mpText);
  236.             gameRoot.Children.Add(mpButtonIncrement);
  237.             gameRoot.Children.Add(mpButtonDecrement);
  238.         }
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement