示例#1
0
        private void DrawFullGame()
        {
            Console.Clear();
            Console.ResetColor();

            // Loop through all tiles and draw items
            for (int x = 0; x < levelWidth; x++)
            {
                for (int y = 0; y < levelHeight; y++)
                {
                    Point pos  = new Point(x, y);
                    Tile  tile = level.Map[x, y];
                    if (tile.HasMonster)
                    {
                        // Has monster - draw monster
                        Blitter.Add(pos, tile.Monster);
                    }
                    else if (tile.HasItems && !tile.HasMonster)
                    {
                        // Has item, no monster - draw item
                        Blitter.Add(pos, tile.Item);
                    }
                    else
                    {
                        // Just draw the floor tile
                        Blitter.Add(pos, tile);
                    }
                }
            }

            // Draw player last
            Blitter.Add(player.Position, player);
        }
示例#2
0
        private void AskForCommand()
        {
            ConsoleKeyInfo keyInfo         = Console.ReadKey(true);
            Tile           occupiedTile    = null;
            Tile           oldTile         = level.Map[player.Position.X, player.Position.Y];
            Point          affectedTilePos = new Point();
            Point          oldPos          = player.Position;
            MoveInfo       moveResult      = MoveInfo.Success;

            switch (keyInfo.Key)
            {
            case ConsoleKey.UpArrow:
            case ConsoleKey.NumPad8:
                moveResult = player.TryToMove(Direction.North, level.Map);
                if (moveResult == MoveInfo.Occupied)
                {
                    occupiedTile    = level.Map[player.Position.X, player.Position.Y - 1];
                    affectedTilePos = new Point(player.Position.X, player.Position.Y - 1);
                }
                break;

            case ConsoleKey.DownArrow:
            case ConsoleKey.NumPad2:
                moveResult = player.TryToMove(Direction.South, level.Map);
                if (moveResult == MoveInfo.Occupied)
                {
                    occupiedTile    = level.Map[player.Position.X, player.Position.Y + 1];
                    affectedTilePos = new Point(player.Position.X, player.Position.Y + 1);
                }
                break;

            case ConsoleKey.LeftArrow:
            case ConsoleKey.NumPad4:
                moveResult = player.TryToMove(Direction.West, level.Map);
                if (moveResult == MoveInfo.Occupied)
                {
                    occupiedTile    = level.Map[player.Position.X - 1, player.Position.Y];
                    affectedTilePos = new Point(player.Position.X - 1, player.Position.Y);
                }
                break;

            case ConsoleKey.RightArrow:
            case ConsoleKey.NumPad6:
                moveResult = player.TryToMove(Direction.East, level.Map);
                if (moveResult == MoveInfo.Occupied)
                {
                    occupiedTile    = level.Map[player.Position.X + 1, player.Position.Y];
                    affectedTilePos = new Point(player.Position.X + 1, player.Position.Y);
                }
                break;

            case ConsoleKey.P:
                PickupItem();
                break;
            }

            // Bonk if we walk into a wall
            if (moveResult == MoveInfo.BlockedByWall)
            {
                statusQueue.Add("Bonk!");
            }
            // Move succeeded, add blit for tile we moved from
            else if (moveResult == MoveInfo.Success)
            {
                // Blit old tile and new player position
                Blitter.Add(new Blixel(oldPos, oldTile));
                Blitter.Add(player.Position, player);
            }
            // Fight if we encountered a monster
            else if (occupiedTile?.Monster != null)
            {
                Monster monster    = occupiedTile.Monster;
                int     healthLeft = player.Fight(monster);
                if (healthLeft >= 1)
                {
                    statusQueue.Add($"Attacked {monster.Name} for {player.AttackValue} damage! It has {monster.Health} hp left.");

                    // Monster fights back
                    monster.Fight(player);
                }
                else
                {
                    // Monster is dead
                    statusQueue.Add($"{monster.Name} falls over dead!");
                    monster.Kill();

                    // Add corpse to backpack
                    player.Inventory.Add(monster);

                    // Remove from creature list
                    // TODO: remove creature list from game
                    creatures.Remove(monster);
                    // Remove from tile
                    if (occupiedTile != null)
                    {
                        occupiedTile.Monster = null;
                    }

                    // Blit floor on the tile
                    Blitter.Add(new Blixel(affectedTilePos, occupiedTile));
                }

                // Check player state
                if (player.Health <= 0 && monster != null)
                {
                    statusQueue.Add($"You have been slain by a {monster.Name}... Rest in pieces.");
                    Console.ReadKey();
                }
            }
        }