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
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PacMan
{
    public class PacManGame : Game
    {
        SpriteBatch _spriteBatch;

        private Texture2D _texture;

        private Vector2 _position = new Vector2(32, 32);
        private const float Speed = 156;
        private Vector2 _direction = new Vector2(0, 1);
        private Vector2? _target;


        private readonly int[,] _map = 
        {
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
            { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
            { 1, 0, 1, 0, 0, 0, 0, 1, 0, 1 },
            { 1, 0, 1, 1, 0, 0, 0, 1, 0, 1 },
            { 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 },
            { 1, 0, 0, 1, 0, 0, 1, 0, 0, 1 },
            { 1, 1, 1, 1, 0, 1, 1, 0, 0, 1 },
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }

        };

        public PacManGame()
        {
            new GraphicsDeviceManager(this) {PreferredBackBufferWidth = 320, PreferredBackBufferHeight = 320};
            Content.RootDirectory = "Content";
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            _texture = new Texture2D(GraphicsDevice, 1, 1);
            _texture.SetData(new[] { Color.White });
        }

        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Down)) _direction = new Vector2(0, 1);
            if (Keyboard.GetState().IsKeyDown(Keys.Right)) _direction = new Vector2(1, 0);
            if (Keyboard.GetState().IsKeyDown(Keys.Up)) _direction = new Vector2(0, -1);
            if (Keyboard.GetState().IsKeyDown(Keys.Left)) _direction = new Vector2(-1, 0);

            // If there's no target and we can move in the chosen direction, set new target
            if (_target == null)
                if (CanMoveInDirection(_position, _direction))
                    _target = _position + _direction * 32;

            // If there's a target move towards that location, and clear target when destination is reached
            if (_target != null)
                if (MoveTowardsPoint(_target.Value, (float) gameTime.ElapsedGameTime.TotalSeconds))
                    _target = null;
            
            base.Update(gameTime);
        }

        private bool CanMoveInDirection(Vector2 position, Vector2 direction)
        {
            int nx = (int) (position.X / 32) + (int) direction.X;
            int ny = (int) (position.Y / 32) + (int) direction.Y;
            if (nx < 0 || ny < 0 || nx >= 10 || ny >= 10) return false;
            return _map[ny, nx] == 0;
        }

        private bool MoveTowardsPoint(Vector2 goal, float elapsed)
        {
            // If we're already at the goal return immediatly
            if (_position == goal) return true;

            // Find direction from current position to goal
            Vector2 direction = Vector2.Normalize(goal - _position);

            // Move in that direction
            _position += direction * Speed * elapsed;

            // If we moved PAST the goal, move it back to the goal
            if (Math.Abs(Vector2.Dot(direction, Vector2.Normalize(goal - _position)) + 1) < 0.1f)
                _position = goal;

            // Return whether we've reached the goal or not
            return _position == goal;
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.LightGray);
            _spriteBatch.Begin();
            for (int i = 0; i != 10; ++i)
                for (int j = 0; j != 10; ++j)
                    if(_map[j,i] == 1)
                        _spriteBatch.Draw(_texture, new Rectangle(i*32, j*32, 32, 32), null, Color.Black);
            _spriteBatch.Draw(_texture, new Rectangle((int) _position.X +8, (int) _position.Y + 8, 16, 16), Color.Yellow);
            _spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}