Ejemplo n.º 1
0
 /// <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);
     titleScreen = new TitleScreen();
     textureList = new TextureList(Content);
     map = new Map(spriteBatch, textureList, graphics.GraphicsDevice.Viewport);
     character = new Character(graphics.GraphicsDevice.Viewport, map.tileList.First());
 }
Ejemplo n.º 2
0
        public void Update(MouseState currentMouseState, MouseState previousMouseState, KeyboardState currentKeyState, KeyboardState previousKeyState, GameTime gameTime, Map map)
        {
            // Selects or deselects the character.
            if (Math.Abs(currentMouseState.X - (position.X + 15)) <= 15 
                && Math.Abs(currentMouseState.Y - (position.Y + 15)) <= 15
                && previousMouseState.LeftButton == ButtonState.Pressed
                && currentMouseState.LeftButton == ButtonState.Released)
            {
                selected = !selected;
            }

            // Deselects the character when clicking anywhere on the map.
            if (selected
                && (Math.Abs(currentMouseState.X - (position.X + 15)) > 15
                    || Math.Abs(currentMouseState.Y - (position.Y + 15)) > 15)
                && previousMouseState.LeftButton == ButtonState.Pressed
                && currentMouseState.LeftButton == ButtonState.Released)
            {
                selected = false;
            }

            // Finds the path from the current tile to the selected tile.
            if (selected
                && previousMouseState.RightButton == ButtonState.Pressed
                && currentMouseState.RightButton == ButtonState.Released)
            {
                foreach (Tile mapTile in map.tileList)
                {
                    if (!mapTile.restricted
                        && Math.Abs(currentMouseState.X - (mapTile.position.X + 15)) <= 15
                        && Math.Abs(currentMouseState.Y - (mapTile.position.Y + 15)) <= 15)
                    {
                        // Clear the old path.
                        if (path != null)
                        {
                            for (int i = 1; i < path.Length; i++)
                            {
                                path[i].isPath = false;
                            }
                        }

                        // Create the new path.
                        pathIndex = 0;
                        path = map.FindPath(currentTile, mapTile).ToArray();
                        for (int i = 1; i < path.Length; i++)
                        {
                            path[i].isPath = true;
                        }
                    }
                }
            }

            // Moves the character one tile along the path when the space bar is pressed.
            if (selected && path != null && pathIndex < path.Length-1 && previousKeyState.IsKeyDown(Keys.Space) && currentKeyState.IsKeyUp(Keys.Space))
            {
                Tile nextTile = path[++pathIndex];
                currentTile = nextTile;
                position = nextTile.position;
                nextTile.isPath = false;
            }

            // Clears the path if the character gets deselected.
            if (!selected)
            {
                path = null;
                foreach (Tile mapTile in map.tileList)
                {
                    mapTile.isPath = false;
                }
            }
        }