source: 2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Guis/GuiPlayer.cs @ 4694

Revision 4694, 7.8 KB checked in by dezhidki, 6 years ago (diff)

Guit lisätty, AIn viimestely.

Line 
1using CastleMaster.Players;
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using Microsoft.Xna.Framework;
7using CastleMaster.Graphics;
8using Microsoft.Xna.Framework.Graphics;
9
10namespace CastleMaster.Guis
11{
12    public class GuiPlayer : Gui
13    {
14        private const string NEWLINE = "\n";
15        private const string ICON_COINS = "&";
16        private const string ICON_WOOD = "#";
17        private const string KING_TEXT = "KING HEALTH: ";
18        private const string STORE_TEXT = "STORE HEALTH: ";
19        private const string ARMORY_TEXT = "ARMORY HEALTH: ";
20        public const int GUI_BAR_HEIGHT = 100;
21        private const int HEALTH_BAR_WIDTH = 100;
22        private const int SCALE = 2;
23
24        private Player player;
25        private StringBuilder itemStats;
26        private Color guiCol;
27        private Rectangle screenRect;
28        private Vector2 itemStatsPos;
29
30        private Rectangle kingHealthGreenRectangle, kingHealthRedRectangle;
31        private Vector2 kingHealthPos, kingRedHealthPos, kingTextPos;
32        private int oldKingHealthBarWidth = HEALTH_BAR_WIDTH;
33
34        private Rectangle storeGreenHealthRectangle, storeRedHealthRectangle;
35        private Vector2 storeHealthPos, storeRedHealthPos, storeTextPos;
36        private int oldStoreHealthBarWidth = HEALTH_BAR_WIDTH;
37
38        private Rectangle armoryGreenHealthRectangle, armoryRedHealthRectangle;
39        private Vector2 armoryHealthPos, armoryRedHealthPos, armoryTextPos;
40        private int oldArmoryHealthBarWidth = HEALTH_BAR_WIDTH;
41
42        public GuiPlayer(GuiManager manager, Player player)
43            : base(manager)
44        {
45            this.player = player;
46            itemStats = new StringBuilder();
47            guiCol = new Color(165, 89, 38);
48            screenRect = new Rectangle(0, Game.HEIGHT - GUI_BAR_HEIGHT, Game.WIDTH, GUI_BAR_HEIGHT);
49            itemStatsPos = new Vector2(0, Game.HEIGHT - GUI_BAR_HEIGHT + 5);
50
51            kingHealthGreenRectangle = new Rectangle(0, 0, HEALTH_BAR_WIDTH, 4);
52            kingHealthRedRectangle = new Rectangle(HEALTH_BAR_WIDTH, 4, 0, 4);
53            kingHealthPos = new Vector2(Game.WIDTH - HEALTH_BAR_WIDTH * SCALE - 10, Game.HEIGHT - GUI_BAR_HEIGHT + 20);
54            kingRedHealthPos = new Vector2(kingHealthPos.X + HEALTH_BAR_WIDTH, kingHealthPos.Y);
55            kingTextPos = new Vector2(kingHealthPos.X - KING_TEXT.Length * 13, kingHealthPos.Y - 5);
56
57            storeGreenHealthRectangle = new Rectangle(0, 0, HEALTH_BAR_WIDTH, 4);
58            storeRedHealthRectangle = new Rectangle(HEALTH_BAR_WIDTH, 4, 0, 4);
59            storeHealthPos = new Vector2(Game.WIDTH - HEALTH_BAR_WIDTH * SCALE - 10, Game.HEIGHT - GUI_BAR_HEIGHT + 40);
60            storeRedHealthPos = new Vector2(storeHealthPos.X + HEALTH_BAR_WIDTH, storeHealthPos.Y);
61            storeTextPos = new Vector2(storeHealthPos.X - STORE_TEXT.Length * 13, storeHealthPos.Y - 5);
62
63            armoryGreenHealthRectangle = new Rectangle(0, 0, HEALTH_BAR_WIDTH, 4);
64            armoryRedHealthRectangle = new Rectangle(HEALTH_BAR_WIDTH, 4, 0, 4);
65            armoryHealthPos = new Vector2(Game.WIDTH - HEALTH_BAR_WIDTH * SCALE - 10, Game.HEIGHT - GUI_BAR_HEIGHT + 60);
66            armoryRedHealthPos = new Vector2(armoryHealthPos.X + HEALTH_BAR_WIDTH, armoryHealthPos.Y);
67            armoryTextPos = new Vector2(armoryHealthPos.X - ARMORY_TEXT.Length * 13, armoryHealthPos.Y - 5);
68        }
69
70        public override int Importance
71        {
72            get
73            {
74                return IMPORTANCE_HIGH;
75            }
76        }
77
78        public override void Update()
79        {
80            itemStats.Clear();
81            itemStats.Append(ICON_COINS).Append(player.CoinsAmount).Append(NEWLINE);
82            itemStats.Append(ICON_WOOD).Append(player.LumberAmount);
83            kingHealthGreenRectangle.Width = (int)((player.King.Health / (float)player.King.MaxHealth) * HEALTH_BAR_WIDTH);
84            if (kingHealthGreenRectangle.Width != oldKingHealthBarWidth)
85            {
86                oldKingHealthBarWidth = kingHealthGreenRectangle.Width;
87                kingRedHealthPos.X = kingHealthPos.X + kingHealthGreenRectangle.Width * SCALE;
88                kingHealthRedRectangle.X = kingHealthGreenRectangle.Width;
89                kingHealthRedRectangle.Width = HEALTH_BAR_WIDTH - kingHealthGreenRectangle.Width;
90            }
91
92            storeGreenHealthRectangle.Width = (int)((player.Store.Health / (float)player.Store.MaxHealth) * HEALTH_BAR_WIDTH);
93            if (storeGreenHealthRectangle.Width != oldStoreHealthBarWidth)
94            {
95                oldStoreHealthBarWidth = storeGreenHealthRectangle.Width;
96                storeRedHealthPos.X = storeHealthPos.X + storeGreenHealthRectangle.Width * SCALE;
97                storeRedHealthRectangle.X = storeGreenHealthRectangle.Width;
98                storeRedHealthRectangle.Width = HEALTH_BAR_WIDTH - storeGreenHealthRectangle.Width;
99            }
100
101            armoryGreenHealthRectangle.Width = (int)((player.Armory.Health / (float)player.Armory.MaxHealth) * HEALTH_BAR_WIDTH);
102            if (armoryGreenHealthRectangle.Width != oldArmoryHealthBarWidth)
103            {
104                oldArmoryHealthBarWidth = armoryGreenHealthRectangle.Width;
105                armoryRedHealthPos.X = armoryHealthPos.X + armoryGreenHealthRectangle.Width * SCALE;
106                armoryRedHealthRectangle.X = armoryGreenHealthRectangle.Width;
107                armoryRedHealthRectangle.Width = HEALTH_BAR_WIDTH - armoryGreenHealthRectangle.Width;
108            }
109        }
110
111        public override void Render(RenderHelper renderer)
112        {
113            renderer.SpriteBatch.Draw(RenderHelper.EmptyTexture, screenRect, guiCol);
114            renderer.SpriteBatch.DrawString(Resources.FONT, itemStats, itemStatsPos, Color.White, 0.0F, Vector2.Zero, 1.5F, SpriteEffects.None, 0);
115           
116            renderer.SpriteBatch.DrawString(Resources.FONT, KING_TEXT, kingTextPos, Color.White);
117            if (kingHealthGreenRectangle.Width > 0)
118                renderer.SpriteBatch.Draw(renderer.RegisteredSpriteSheets[Resources.SPRITESHEET_HEALTH].SheetTexture, kingHealthPos, kingHealthGreenRectangle, Color.White, 0.0F, Vector2.Zero, SCALE, SpriteEffects.None, 0);
119            if (kingHealthRedRectangle.Width > 0)
120                renderer.SpriteBatch.Draw(renderer.RegisteredSpriteSheets[Resources.SPRITESHEET_HEALTH].SheetTexture, kingRedHealthPos, kingHealthRedRectangle, Color.White, 0.0F, Vector2.Zero, SCALE, SpriteEffects.None, 0);
121
122            renderer.SpriteBatch.DrawString(Resources.FONT, STORE_TEXT, storeTextPos, Color.White);
123            if (storeGreenHealthRectangle.Width > 0)
124                renderer.SpriteBatch.Draw(renderer.RegisteredSpriteSheets[Resources.SPRITESHEET_HEALTH].SheetTexture, storeHealthPos, storeGreenHealthRectangle, Color.White, 0.0F, Vector2.Zero, SCALE, SpriteEffects.None, 0);
125            if (storeRedHealthRectangle.Width > 0)
126                renderer.SpriteBatch.Draw(renderer.RegisteredSpriteSheets[Resources.SPRITESHEET_HEALTH].SheetTexture, storeRedHealthPos, storeRedHealthRectangle, Color.White, 0.0F, Vector2.Zero, SCALE, SpriteEffects.None, 0);
127
128            renderer.SpriteBatch.DrawString(Resources.FONT, ARMORY_TEXT, armoryTextPos, Color.White);
129            if (armoryGreenHealthRectangle.Width > 0)
130                renderer.SpriteBatch.Draw(renderer.RegisteredSpriteSheets[Resources.SPRITESHEET_HEALTH].SheetTexture, armoryHealthPos, armoryGreenHealthRectangle, Color.White, 0.0F, Vector2.Zero, SCALE, SpriteEffects.None, 0);
131            if (armoryRedHealthRectangle.Width > 0)
132                renderer.SpriteBatch.Draw(renderer.RegisteredSpriteSheets[Resources.SPRITESHEET_HEALTH].SheetTexture, armoryRedHealthPos, armoryRedHealthRectangle, Color.White, 0.0F, Vector2.Zero, SCALE, SpriteEffects.None, 0);
133        }
134    }
135}
Note: See TracBrowser for help on using the repository browser.