aboutsummaryrefslogtreecommitdiff
path: root/V3/Screens/DeathScreen.cs
blob: 230d40a0c1a0dfaf621bb1c62266cd70647e21f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ninject;
using V3.Input;
using V3.Widgets;

namespace V3.Screens
{
    // ReSharper disable once ClassNeverInstantiated.Global
    public sealed class DeathScreen : AbstractScreen, IInitializable
    {
        private static TimeSpan sTotalDelay = TimeSpan.FromSeconds(2);

        private readonly ContentManager mContentManager;
        private readonly GraphicsDeviceManager mGraphicsDeviceManager;
        private readonly MenuActions mMenuActions;
        private readonly WidgetFactory mWidgetFactory;

        private Button mMenuButton;
        private Button mCloseGameButton;
        private Vector2 mButtonPosition;
        private Vector2 mCenter;
        private Vector2 mFontCenter;
        private SpriteFont mDeathFont;
        private Texture2D mRectangle;

        private TimeSpan mDelayTimer = sTotalDelay;

        /// <summary>
        /// Creates a death screen if the players health reaches 0.
        /// </summary>

        public DeathScreen(ContentManager contentManager,
            GraphicsDeviceManager graphicsDeviceManager,
            MenuActions menuActions,
            WidgetFactory widgetFactory)
            : base(false, true)
        {
            mContentManager = contentManager;
            mGraphicsDeviceManager = graphicsDeviceManager;
            mMenuActions = menuActions;
            mWidgetFactory = widgetFactory;
        }

        public override bool HandleKeyEvent(IKeyEvent keyEvent)
        {
            return false;
        }

        public override bool HandleMouseEvent(IMouseEvent mouseEvent)
        {
            if (mDelayTimer <= TimeSpan.Zero)
            {
                mMenuButton.HandleMouseEvent(mouseEvent);
                mCloseGameButton.HandleMouseEvent(mouseEvent);
            }
            return false;
        }

        public void Initialize()
        {
            mRectangle = mContentManager.Load<Texture2D>("Sprites/WhiteRectangle");
            mDeathFont = mContentManager.Load<SpriteFont>("Fonts/DeathFont");

            mMenuButton = mWidgetFactory.CreateButton("Zum Hauptmenü");
            mMenuButton.Position = Vector2.Zero;
            mMenuButton.PaddingX = 10;
            mMenuButton.PaddingY = 0;
            mMenuButton.Size = mMenuButton.GetMinimumSize();
            mMenuButton.BackgroundColor = Color.Gray * 0.7f;

            mCloseGameButton = mWidgetFactory.CreateButton("Spiel schließen");
            mCloseGameButton.Position = Vector2.Zero;
            mCloseGameButton.PaddingX = 10;
            mCloseGameButton.PaddingY = 0;
            mCloseGameButton.Size = mCloseGameButton.GetMinimumSize();
            mCloseGameButton.BackgroundColor = Color.Gray * 0.7f;
        }

        public override void Update(GameTime gameTime)
        {
            var viewport = mGraphicsDeviceManager.GraphicsDevice.Viewport;

            mCenter = new Vector2(viewport.Width / 2f, viewport.Height / 2f);
            mFontCenter = mDeathFont.MeasureString("Ihr        seid        tot") / 2;
            mButtonPosition = new Vector2(mCenter.X - mFontCenter.X * 2/3, mCenter.Y + mFontCenter.Y * 2);

            if (mDelayTimer > TimeSpan.Zero)
                mDelayTimer -= gameTime.ElapsedGameTime;

            mMenuButton.Position = mButtonPosition;
            mCloseGameButton.Position = mMenuButton.Position + new Vector2(mFontCenter.X * 3/ 4, 0);

            if (mMenuButton.IsClicked)
            {
                mMenuActions.OpenMainScreen();
            }
            else if (mCloseGameButton.IsClicked)
            {
                mMenuActions.Exit();
            }

            mMenuButton.IsSelected = mMenuButton.CheckSelected(Mouse.GetState().Position);
            mMenuButton.IsClicked = false;

            mCloseGameButton.IsSelected = mCloseGameButton.CheckSelected(Mouse.GetState().Position);
            mCloseGameButton.IsClicked = false;
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            float displayRatio = (float) (1 - mDelayTimer.TotalMilliseconds / sTotalDelay.TotalMilliseconds);

            spriteBatch.Begin();
            spriteBatch.Draw(mRectangle,
                mGraphicsDeviceManager.GraphicsDevice.Viewport.Bounds,
                Color.Black * 0.5f * displayRatio);

            spriteBatch.DrawString(mDeathFont,
                "Ihr     seid     tot",
                mCenter,
                Color.Firebrick * displayRatio,
                0,
                mFontCenter,
                1.0f,
                SpriteEffects.None,
                0.5f);

            if (mDelayTimer <= TimeSpan.Zero)
            {
                mMenuButton.Draw(spriteBatch);
                mCloseGameButton.Draw(spriteBatch);
            }
            spriteBatch.End();
        }
    }
}