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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace MyFirstGame
{
    public class Camera
    {
        private readonly Viewport _viewport;
        public Camera(Viewport viewport)
        {
            _viewport = viewport;
            Origin = new Vector2(viewport.Width / 2.0f, viewport.Height / 2.0f);
            Zoom = 1.0f;
        }
        public Vector2 _position;
        public Vector2 Position
        {
            get { return _position; }
            set
            {
                _position = value;

                // If there's a limit set and the camera is not transformed clamp position to limits
                if (Limits != null && Zoom == 1.0f && Rotation == 0.0f)
                {
                    _position.X = MathHelper.Clamp(_position.X, Limits.Value.X, Limits.Value.X + Limits.Value.Width - _viewport.Width);
                    _position.Y = MathHelper.Clamp(_position.Y, Limits.Value.Y, Limits.Value.Y + Limits.Value.Height - _viewport.Height);
                }
            }
        }
        
        public Vector2 Origin { get; set; }
        public float Zoom { get; set; }
        public float Rotation { get; set; }

        public Rectangle? Limits
        {
            get { return _limits; }
            set
            {
                if (value != null)
                {
                    // Assign limit but make sure it's always bigger than the viewport
                    _limits = new Rectangle
                    {
                        X = value.Value.X,
                        Y = value.Value.Y,
                        Width = System.Math.Max(_viewport.Width, value.Value.Width),
                        Height = System.Math.Max(_viewport.Height, value.Value.Height)
                    };

                    // Validate camera position with new limit
                    Position = Position;
                }
                else
                {
                    _limits = null;
                }
            }
        }



        public Rectangle? _limits;
        
        public void LookAt(Vector2 position)
        {
            Position = position - new Vector2(_viewport.Width / 2.0f, _viewport.Height / 2.0f);
        }
        public Matrix GetViewMatrix()
        {
            // To add parallax, simply multiply it by the position
            return Matrix.CreateTranslation(new Vector3(-Position, 0.0f)) *
                // The next line has a catch. See note below.
                   Matrix.CreateTranslation(new Vector3(-Origin, 0.0f)) *
                   Matrix.CreateRotationZ(Rotation) *
                   Matrix.CreateScale(Zoom, Zoom, 1) *
                   Matrix.CreateTranslation(new Vector3(Origin, 0.0f));
            
           
        }
    }


}


using System.Collections.Generic;
using System.Text;
using System;

using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Demina;





namespace MyFirstGame
{
    class Sprite : DrawableGameComponent
    {
        //The current position of the Sprite
        
        public Vector2 X;
        public int V = 640;
        public Vector2 Position;
        public AnimationPlayer animationPlayer;
        //SpriteBatch spriteBatch;     
        public Rectangle Size;      
        Rectangle mSource;
        Camera cam;
        //GraphicsDeviceManager graphics;
        
        /*
        public Rectangle Source
        {
            get { return mSource; }
            set
            {
                mSource = value;
               // Size = new Rectangle(0, 0, (int)(mSource.Width * Scale), (int)(mSource.Height * Scale));
            }
        }


        //The amount to increase/decrease the size of the original sprite. 
        private float mScale = 1.0f;

        //When the scale is modified throught he property, the Size of the 
        //sprite is recalculated with the new scale applied.
        public float Scale
        {
            get { return mScale; }
            set
            {
                mScale = value;
                //Recalculate the Size of the Sprite with the new scale
                Size = new Rectangle(0, 0, (int)(Source.Width * Scale), (int)(Source.Height * Scale));
            }
        }
        */
        
        public Sprite(Game game)
            : base(game)
        {            
        }  
        

        //Load the texture for the sprite using the Content Pipeline
        public void LoadContent(ContentManager theContentManager, GraphicsDevice graphics) //string theAssetName
        {
            cam = new Camera(graphics.Viewport);
            animationPlayer = new AnimationPlayer();
            
            X = new Vector2(700, 400);            
            
            animationPlayer.AddAnimation("runright", theContentManager.Load<Animation>("Animation/runright"));
            animationPlayer.AddAnimation("runleft", theContentManager.Load<Animation>("Animation/runleft"));
            animationPlayer.AddAnimation("combatright", theContentManager.Load<Animation>("Animation/combatanimation"));
            animationPlayer.AddAnimation("combatleft", theContentManager.Load<Animation>("Animation/combatanimationleft"));
            animationPlayer.AddAnimation("combatrunright", theContentManager.Load<Animation>("Animation/combatrunright"));
            animationPlayer.AddAnimation("combatrunleft", theContentManager.Load<Animation>("Animation/combatrunleft"));
            animationPlayer.AddAnimation("crouchright", theContentManager.Load<Animation>("Animation/crouch"));
            animationPlayer.AddAnimation("crouchleft", theContentManager.Load<Animation>("Animation/crouchleft"));
            animationPlayer.AddAnimation("idlecombatright", theContentManager.Load<Animation>("Animation/idlecombatright"));
            animationPlayer.AddAnimation("idlecombatleft", theContentManager.Load<Animation>("Animation/idlecombatleft"));
            animationPlayer.AddAnimation("jumpright", theContentManager.Load<Animation>("Animation/Jump"));
            animationPlayer.AddAnimation("jumpleft", theContentManager.Load<Animation>("Animation/jumpleft"));
            animationPlayer.AddAnimation("landright", theContentManager.Load<Animation>("Animation/Land"));
            animationPlayer.AddAnimation("landleft", theContentManager.Load<Animation>("Animation/landleft"));
            animationPlayer.AddAnimation("slowdownright", theContentManager.Load<Animation>("Animation/slowdown"));
            animationPlayer.AddAnimation("slowdownleft", theContentManager.Load<Animation>("Animation/slowdownleft"));
            animationPlayer.AddAnimation("standstillright", theContentManager.Load<Animation>("Animation/standstill"));
            animationPlayer.AddAnimation("standstillleft", theContentManager.Load<Animation>("Animation/standstillleft"));
            animationPlayer.AddAnimation("swordslash1right", theContentManager.Load<Animation>("Animation/swordslash1x"));
            animationPlayer.AddAnimation("swordslash1left", theContentManager.Load<Animation>("Animation/swordslash1left"));
            animationPlayer.AddAnimation("swordslash2right", theContentManager.Load<Animation>("Animation/swordslash2x"));
            animationPlayer.AddAnimation("swordslash2left", theContentManager.Load<Animation>("Animation/swordslash2left"));
            animationPlayer.AddAnimation("swordslash3right", theContentManager.Load<Animation>("Animation/swordslash3x"));
            animationPlayer.AddAnimation("swordslash3left", theContentManager.Load<Animation>("Animation/swordslash3left"));
            animationPlayer.AddAnimation("swordslash0right", theContentManager.Load<Animation>("Animation/swordslashstart"));
            animationPlayer.AddAnimation("swordslash0left", theContentManager.Load<Animation>("Animation/swordslashstartleft"));
            animationPlayer.StartAnimation("standstillright");
            
        }

      

        //Update the Sprite and change it's position based on the passed in speed, direction and elapsed time.
        public void Update(GameTime theGameTime, Vector2 theSpeed, Vector2 theDirection)
        {
           //KeyboardState aCurrentKeyboardState = Keyboard.GetState();
            X += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
            Position = X;
           
         }

       
        //Draw the sprite to the screen
        public void Draw(SpriteBatch theSpriteBatch)
        {
                      
            
            animationPlayer.Draw(theSpriteBatch, new Vector2(1000, X.Y),false,false,0,Color.White,new Vector2(1,1),cam.GetViewMatrix());
            
        }
       
        public static bool CharacterMapCollision(Vector2 PositionChar)
        {
           

            Rectangle RectangleCharacter = new Rectangle((int)PositionChar.X, (int)PositionChar.Y, 200, 200);
            Rectangle Rectanglefloor = new Rectangle(-100, 800, 6400, 850);
            if (RectangleCharacter.Intersects(Rectanglefloor))
               return false;


            
            return true;
            

        }


public class Game1 : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        Sprite sprite;
        public SpriteBatch spriteBatch;
        MyCharacter mMyCharacter;
        Enemy firstenemy;
        Map map;
        ScreenManager screenManager;
        public bool end;
        public Camera camera;
        
        static readonly string[] preloadAssets =
        {
            "gradient",
        };

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            // Create the screen manager component.
            screenManager = new ScreenManager(this);
           
            Components.Add(screenManager);          
            
            // Activate the first screens.
            screenManager.AddScreen(new BackgroundScreen(), null);
            screenManager.AddScreen(new MainMenuScreen(), null);          
           
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
                           
            // TODO: Add your initialization logic here
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            //graphics.PreferredBackBufferWidth = 1920;
            //graphics.PreferredBackBufferHeight = 1200;
            graphics.ApplyChanges();
            
            base.Initialize();
            
               
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            
            
            
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            camera = new Camera(GraphicsDevice.Viewport);
            base.LoadContent();
            foreach (string asset in preloadAssets)
            {
                Content.Load<object>(asset);
            }
            //blah.LoadContent(Content);
            //worldmatrix = Matrix
            sprite = new Sprite(this);
            Components.Add(sprite);
            map = new Map(this);
            map.LoadContent(this.Content);
            mMyCharacter = new MyCharacter(this);
            mMyCharacter.LoadContent(this.Content, GraphicsDevice);
            firstenemy = new Enemy();
            firstenemy.Load(this.Content);
            camera.Limits = new Rectangle(0, 0, 6100, 720);
           // camera.Limits = new Rectangle(650, 360, 6100, 0);

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
        
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>

       
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();            
            
              mMyCharacter.Update(gameTime);
              firstenemy.Update(gameTime);
             
              camera.LookAt(mMyCharacter.X);
            
             
            
            base.Update(gameTime);
        }
       
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        
       
        protected override void Draw(GameTime gameTime)
        {
          
            GraphicsDevice.Clear(Color.CornflowerBlue);

            Vector2 parallax = new Vector2(1f);//(0.5f);
            spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.GetViewMatrix());           
            
            map.Draw(mMyCharacter, this.spriteBatch, gameTime);    
            
            spriteBatch.End();

            mMyCharacter.Draw(this.spriteBatch);
           // firstenemy.Draw(this.spriteBatch);
            
           // spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.GetViewMatrix(parallax));
            //map.overlay(mMyCharacter, this.spriteBatch);


            //map.Drawdraw(mMyCharacter, this.spriteBatch, cam);
            //spriteBatch.End();
            
            
            base.Draw(gameTime);
        }

       
    }



    
}