aboutsummaryrefslogtreecommitdiff
path: root/V3/Screens/TechdemoScreen.cs
blob: 748c9a1c86ccc185cd3a98803b7a77fb53ca3d5f (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ninject;
using V3.Camera;
using V3.Data;
using V3.Effects;
using V3.Input;
using V3.Map;
using V3.Objects;

namespace V3.Screens
{
    // ReSharper disable once ClassNeverInstantiated.Global
    public class TechdemoScreen : AbstractScreen, IInitializable
    {
        private enum Creatures
        {
            Empty, Zombie, Skeleton, Peasant, Knight
        }

        private Creatures mCreatePerKey;

        private readonly CameraManager mCameraManager;
        private readonly ContentManager mContentManager;
        private readonly CreatureFactory mCreatureFactory;
        private readonly GraphicsDeviceManager mGraphicsDeviceManager;
        private readonly MenuActions mMenuActions;
        private readonly IOptionsManager mOptionsManager;
        private readonly IEffectsManager mEffectsManager;
        private readonly Selection mSelection;
        private readonly Transformation mTransformation;
        private readonly IMapManager mMapManager;
        private readonly IObjectsManager mObjectsManager;
        private readonly Pathfinder mPathfinder;
        private readonly Texture2D mOnePixelTexture;
        // Fields for handling mouse input.
        private Point mInitialClickPosition;
        private bool mLeftButtonPressed;
        private bool mRightButtonPressed;
        private Vector2 mRightButtonPosition;

        /// <summary>
        /// Creates a new game screen.
        /// </summary>
        public TechdemoScreen(IOptionsManager optionsManager, CameraManager cameraManager,
                ContentManager contentManager, CreatureFactory creatureFactory,
                GraphicsDeviceManager graphicsDeviceManager, IMapManager mapManager,
                MenuActions menuActions, IObjectsManager objectsManager,
                Pathfinder pathfinder, Selection selection, Transformation transformation,
                IEffectsManager effectsManager) : base(false, false)
        {
            mMapManager = mapManager;
            mObjectsManager = objectsManager;
            mCameraManager = cameraManager;
            mOptionsManager = optionsManager;
            mContentManager = contentManager;
            mCreatureFactory = creatureFactory;
            mEffectsManager = effectsManager;
            mTransformation = transformation;
            mGraphicsDeviceManager = graphicsDeviceManager;
            mMenuActions = menuActions;
            mPathfinder = pathfinder;
            mSelection = selection;
            mOnePixelTexture = contentManager.Load<Texture2D>("Sprites/WhiteRectangle");
        }


        public override bool HandleMouseEvent(IMouseEvent mouseEvent)
        {
            if (mouseEvent.MouseButton == MouseButton.Left && mouseEvent.ButtonState == ButtonState.Pressed)
            {
                if (!mLeftButtonPressed)
                {
                    mLeftButtonPressed = true;
                    mInitialClickPosition = mouseEvent.PositionPressed;
                }
            }
            else
            {
                if (mLeftButtonPressed)
                {
                    mSelection.Select(mInitialClickPosition + mCameraManager.GetCamera().Location.ToPoint(),
                        mouseEvent.PositionReleased.GetValueOrDefault() + mCameraManager.GetCamera().Location.ToPoint());
                    mLeftButtonPressed = false;
                }
            }
            if (mouseEvent.MouseButton == MouseButton.Right && mouseEvent.ButtonState == ButtonState.Pressed)
            {
                if (!mRightButtonPressed)
                {
                    mRightButtonPressed = true;
                    mInitialClickPosition = mouseEvent.PositionPressed;
                }
            }
            if (mouseEvent.MouseButton == MouseButton.Right && mouseEvent.ButtonState == ButtonState.Released)
            {
                if (mouseEvent.PositionReleased != null && mouseEvent.ReleasedOnScreen)
                {
                    mRightButtonPressed = false;
                    mRightButtonPosition = mouseEvent.PositionReleased.Value.ToVector2() + mCameraManager.GetCamera().Location;
                    mSelection.Move(mouseEvent.PositionReleased.Value.ToVector2()
                         + mCameraManager.GetCamera().Location);
                }
            }
            if (mouseEvent.ReleasedOnScreen)
            {
                var position = mouseEvent.PositionPressed.ToVector2() + mCameraManager.GetCamera().Location;
                switch (mCreatePerKey)
                {
                    case Creatures.Zombie:
                        mObjectsManager.CreateCreature(mCreatureFactory.CreateZombie(position, MovementDirection.S));
                        break;
                    case Creatures.Skeleton:
                        mObjectsManager.CreateCreature(mCreatureFactory.CreateSkeleton(position, MovementDirection.S));
                        break;
                    case Creatures.Peasant:
                        mObjectsManager.CreateCreature(mCreatureFactory.CreateMalePeasant(position, MovementDirection.S));
                        break;
                    case Creatures.Knight:
                        mObjectsManager.CreateCreature(mCreatureFactory.CreateKnight(position, MovementDirection.S));
                        break;
                }
            }
            return true;
        }

        public void Initialize()
        {
            mMapManager.Load("techdemo");
            mObjectsManager.Initialize(mMapManager);
            mCameraManager.Initialize(mMapManager.SizeInPixel);
            mPathfinder.LoadGrid(mMapManager.GetPathfindingGrid());
            mTransformation.mSelection = mSelection;
            mTransformation.LoadArea(mContentManager);

            // Add creatures for testing purposes.
            var necromancer = mCreatureFactory.CreateNecromancer(new Vector2(1592, 1609), MovementDirection.S);
            var king = mCreatureFactory.CreateKing(new Vector2(1500, 200), MovementDirection.S);
            mObjectsManager.CreatePlayerCharacter(necromancer);
            mObjectsManager.CreateBoss(king);
            int makeCreatureStrongerModifier = 100;
            int makePeasantsNotSoMuchStrongerModifier = 10;
            AddCreature<MalePeasant>(new Point(4), new Point(200, 2000), new Point(2800, 3000), makePeasantsNotSoMuchStrongerModifier);
            AddCreature<FemalePeasant>(new Point(4), new Point(232, 2032), new Point(2800, 3000), makePeasantsNotSoMuchStrongerModifier);
            AddCreature<Knight>(new Point(4), new Point(200, 200), new Point(2800, 1000), makeCreatureStrongerModifier);
            AddCreature<KingsGuard>(new Point(4), new Point(232, 232), new Point(2800, 1000), makeCreatureStrongerModifier);
            AddCreature<Zombie>(new Point(3), new Point(264, 264), new Point(2800, 1000), makeCreatureStrongerModifier);
            AddCreature<Skeleton>(new Point(3), new Point(290, 290), new Point(2800, 1000), makeCreatureStrongerModifier);
            AddCreature<Zombie>(new Point(4), new Point(264, 2064), new Point(2800, 3000), makeCreatureStrongerModifier);

            mTransformation.ObjectsManager = mObjectsManager;
        }

        /// <summary>
        /// Add a generic creature to Techdemo.
        /// Using pixel coordinates for calculating start and end point of map.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="density">Density on X and Y axis. More creatures when lower.</param>
        /// <param name="start">Start point for creating the creature batallion.</param>
        /// <param name="end">At which point to stop creating creatures.</param>
        /// <param name="makeStronger">Give more HP. Better for testing.</param>
        private void AddCreature<T>(Point density, Point start, Point end, int makeStronger) where T:ICreature
        {
            var type = typeof(T);
            for (int i = start.Y; i < end.Y; i += 32 * density.Y)
            {
                for (int j = start.X; j < end.X; j += 32 * density.X)
                {
                    Vector2 position = new Vector2(j, i);
                    ICreature creature;
                    if (type == typeof(MalePeasant))
                    {
                        creature = mCreatureFactory.CreateMalePeasant(position, MovementDirection.S);
                    }
                    else if (type == typeof(FemalePeasant))
                    {
                        creature = mCreatureFactory.CreateFemalePeasant(position, MovementDirection.S);
                    }
                    else if (type == typeof(Skeleton))
                    {
                        creature = mCreatureFactory.CreateSkeleton(position, MovementDirection.S);
                    }
                    else if (type == typeof(Zombie))
                    {
                        creature = mCreatureFactory.CreateZombie(position, MovementDirection.S);
                    }
                    else if (type == typeof(Knight))
                    {
                        creature = mCreatureFactory.CreateKnight(position, MovementDirection.S);
                    }
                    else if (type == typeof(SkeletonHorse))
                    {
                        creature = mCreatureFactory.CreateSkeletonHorse(position, MovementDirection.S);
                    }
                    else if (type == typeof(Meatball))
                    {
                        creature = mCreatureFactory.CreateMeatball(position, MovementDirection.S);
                    }
                    else if (type == typeof(KingsGuard))
                    {
                        creature = mCreatureFactory.CreateKingsGuard(position, MovementDirection.S);
                    }
                    else if (type == typeof(SkeletonElite))
                    {
                        creature = mCreatureFactory.CreateSkeletonElite(position, MovementDirection.S);
                    }
                    else
                    {
                        creature = mCreatureFactory.CreateZombie(position, MovementDirection.S);
                    }
                    creature.Empower(makeStronger);
                    mObjectsManager.CreateCreature(creature);
                }
            }
            
        }

        public override bool HandleKeyEvent(IKeyEvent keyEvent)
        {
            if (keyEvent.KeyState == KeyState.Down)
            {
                Creatures createPerKey = Creatures.Empty;
                switch (keyEvent.Key)
                {
                    case Keys.Escape:
                        mMenuActions.OpenPauseScreen();
                        break;
                    case Keys.F1:
                        createPerKey = Creatures.Zombie;
                        break;
                    case Keys.F2:
                        createPerKey = Creatures.Skeleton;
                        break;
                    case Keys.F3:
                        createPerKey = Creatures.Peasant;
                        break;
                    case Keys.F4:
                        createPerKey = Creatures.Knight;
                        break;
                    case Keys.F5:
                        Rectangle cameraRectangle = mCameraManager.GetCamera().ScreenRectangle;
                        mEffectsManager.PlayOnce(new SmokeBig(), cameraRectangle.Center, cameraRectangle.Size);
                        mObjectsManager.ExposeTheLiving();
                        break;
                    case Keys.F6:
                        (mObjectsManager.PlayerCharacter as Necromancer)?.ChangeSex();
                        mEffectsManager.PlayOnce(new SmokeSmall(), mObjectsManager.PlayerCharacter.Position.ToPoint(), new Point(256));
                        break;
                }
                if (createPerKey == mCreatePerKey)
                {
                    mCreatePerKey = Creatures.Empty;
                }
                else
                {
                    mCreatePerKey = createPerKey;
                }
            }
            return true;
        }

        /// <summary>
        /// Updates the status of this object.
        /// </summary>
        /// <param name="gameTime">a snapshot of the game time</param>
        public override void Update(GameTime gameTime)
        {
#if DEBUG
#else
            try
            {
#endif
                mObjectsManager.Update(gameTime, mRightButtonPressed, mRightButtonPosition, mCameraManager.GetCamera());
                mCameraManager.Update(mObjectsManager.PlayerCharacter);
                mEffectsManager.Update(gameTime);

                // Call for Transformations
                mTransformation.Transform();

                mSelection.UpdateSelection();
#if DEBUG
#else
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
#endif
        }

        /// <summary>
        /// Draws this object using the given sprite batch.
        /// </summary>
        /// <param name="gameTime">a snapshot of the game time</param>
        /// <param name="spriteBatch">the sprite batch to use for drawing
        /// this object</param>
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            mGraphicsDeviceManager.GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, mCameraManager.GetCamera().Transform);
            mMapManager.DrawFloor(spriteBatch, mCameraManager.GetCamera());
            mObjectsManager.Draw(spriteBatch, mCameraManager.GetCamera());
            mTransformation.DrawNecroArea(spriteBatch);
            mEffectsManager.Draw(spriteBatch);
            if (mLeftButtonPressed)
                mSelection.Draw(spriteBatch, mInitialClickPosition + mCameraManager.GetCamera().Location.ToPoint(), Mouse.GetState().Position + mCameraManager.GetCamera().Location.ToPoint());
            if (mOptionsManager.Options.DebugMode == DebugMode.Full)
            {
                mMapManager.DrawPathfindingGrid(spriteBatch, mCameraManager.GetCamera());
                mObjectsManager.DrawQuadtree(spriteBatch);
                DrawLastSelection(spriteBatch, mSelection.LastSelection);
            }
            // Draws the selection rectangle when left mouse button is pressed.
            spriteBatch.End();
        }

        private void DrawLastSelection(SpriteBatch spriteBatch, Rectangle selection)
        {
            spriteBatch.Draw(mOnePixelTexture, selection, new Color(Color.Black, 100));
        }
    }
}