Exemplo n.º 1
0
        /// <summary>
        /// Dungeon Constructor
        /// </summary>
        /// <param name="npcDictionary">List of npcs</param>
        public Dungeon(NPCDictionary npcDictionary)
        {
            this._grid = new Tile[SIZE, SIZE];
            this._npcs.Add(new NPC(npcDictionary.GetNPC("mons_asmodeus")));
            this._npcs.Add(new NPC(npcDictionary.GetNPC("mons_cerebov")));
            this._npcs.Add(new NPC(npcDictionary.GetNPC("mons_glowing_shapeshifter")));
            for (int x = 0; x < SIZE; x++)
            {
                for (int y = 0; y < SIZE; y++)
                {
                    _grid[x, y] = new Tile(new Vector2(x, y));
                    if (x == 0 || y == 0 || x == SIZE - 1 || y == SIZE - 1)
                    {
                        _grid[x, y].isEdge = true;
                    }
                }
            }
            MakeRoom(MakeRectangle());

            while (((walls / dungeonArea) > .50) && attempts != 5000000)
            {
                TestRoom();
                attempts++;
            }

            while (true)
            {
                _upStairs = _grid[rand.Next(SIZE), rand.Next(SIZE)];
                if (_upStairs.tileName == "dngn_floor" && _upStairs.entities.Count() == 0)
                {
                    _upStairs.AddEntity("dngn_stone_stairs_up");
                    break;
                }
            }
            while (true)
            {
                _downStairs = _grid[rand.Next(SIZE), rand.Next(SIZE)];
                if (_downStairs.tileName == "dngn_floor" && _downStairs.entities.Count() == 0)
                {
                    _downStairs.AddEntity("dngn_stone_stairs_down");
                    break;
                }
            }

            DTGeneration();
            MSTGeneration();

            foreach (Edge edge in edgesMST)
            {
                Rectangle      roomA    = new Rectangle(0, 0, 0, 0);
                Rectangle      roomB    = new Rectangle(0, 0, 0, 0);
                List <Vector2> roomPath = new List <Vector2>();
                foreach (Rectangle room in rooms)
                {
                    if (room.Contains((int)edge.vA.X, (int)edge.vA.Y))
                    {
                        roomA = room;
                        continue;
                    }
                    if (room.Contains((int)edge.vB.X, (int)edge.vB.Y))
                    {
                        roomB = room;
                    }
                }
                roomPath = Pathing.FindPath(edge.vB, edge.vA, this._grid);
                Tile nextTile = this._grid[(int)edge.vA.X, (int)edge.vA.Y];
                foreach (Vector2 pos in roomPath)
                {
                    if (nextTile.tileName == "dngn_rock_wall_dark_gray")
                    {
                        nextTile.tileName = "dngn_floor";
                    }

                    nextTile = this._grid[(int)(nextTile.tilePos.X + pos.X), (int)(nextTile.tilePos.Y + pos.Y)];
                }
            }

            for (int x = 0; x < SIZE; x++)
            {
                for (int y = 0; y < SIZE; y++)
                {
                    if (this._grid[x, y].tileName == "dngn_rock_wall_dark_gray")
                    {
                        this._grid[x, y].isWall = true;
                    }
                }
            }

            foreach (NPC monster in _npcs)
            {
                while (true)
                {
                    _monsTile = _grid[rand.Next(SIZE), rand.Next(SIZE)];
                    if (_monsTile.tileName == "dngn_floor" && _monsTile.entities.Count() == 0 && _monsTile.npc == null)
                    {
                        monster.location = _monsTile.tilePos;
                        _monsTile.npc    = monster;
                        break;
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get input for commands
        /// </summary>
        private void UpdateInput()
        {
            KeyboardState newState      = Keyboard.GetState();
            MouseState    newMouseState = Mouse.GetState();

            //Checks if the same key has been held for a period of time.
            if (newState.Equals(oldState))
            {
                if (!held)
                {
                    oldtime = time;
                    held    = true;
                }
                else
                {
                    if (!repeat && time.TotalMilliseconds - oldtime.TotalMilliseconds > 250)
                    {
                        repeat = true;
                    }
                }
            }
            else
            {
                held   = false;
                repeat = false;
            }

            // Is the SPACE key down?
            if (newState.IsKeyDown(Keys.Space))
            {
                if (!oldState.IsKeyDown(Keys.Space))
                {
                    dungeon.Clear();
                    level = new Dungeon(npcDicationary);
                    dungeon.Add(level);
                    npcs = level.npcs;
                    currentdngn_floor = 0;
                    player            = new Player(level.upStairs);
                    fov = new FOV(level.grid, player.location);
                    fov.GetVisibility();
                    Log.Write("New floor generated.");
                    playerActed = false;
                }
            }
            if (newState.IsKeyDown(Keys.Left) || newState.IsKeyDown(Keys.NumPad4))
            {
                if (!oldState.IsKeyDown(Keys.Left) && !oldState.IsKeyDown(Keys.NumPad4) || repeat)
                {
                    if (doorWait != "none")
                    {
                        doorTile = level.grid[(int)player.location.X - 1, (int)player.location.Y];
                        switch (doorWait)
                        {
                        case ("close"):
                            playerActed = doorTile.CloseDoor();
                            break;

                        case ("open"):
                            playerActed = doorTile.OpenDoor();
                            break;
                        }
                        doorWait = "none";
                    }
                    else
                    {
                        moveToList = player.Movement(new Vector2(-1, 0), level.grid, moveType, moveToList);
                        moveType   = "one";
                    }
                    fov = new FOV(level.grid, player.location);
                    fov.GetVisibility();
                }
            }
            if (newState.IsKeyDown(Keys.NumPad7))
            {
                if (!oldState.IsKeyDown(Keys.NumPad7) || repeat)
                {
                    if (doorWait != "none")
                    {
                        doorTile = level.grid[(int)player.location.X - 1, (int)player.location.Y - 1];
                        switch (doorWait)
                        {
                        case ("close"):
                            playerActed = doorTile.CloseDoor();
                            break;

                        case ("open"):
                            playerActed = doorTile.OpenDoor();
                            break;
                        }
                        doorWait = "none";
                    }
                    else
                    {
                        moveToList = player.Movement(new Vector2(-1, -1), level.grid, moveType, moveToList);
                        moveType   = "one";
                    }
                    fov = new FOV(level.grid, player.location);
                    fov.GetVisibility();
                }
            }
            if (newState.IsKeyDown(Keys.Up) || newState.IsKeyDown(Keys.NumPad8))
            {
                if (!oldState.IsKeyDown(Keys.Up) && !oldState.IsKeyDown(Keys.NumPad8) || repeat)
                {
                    if (doorWait != "none")
                    {
                        doorTile = level.grid[(int)player.location.X, (int)player.location.Y - 1];
                        switch (doorWait)
                        {
                        case ("close"):
                            playerActed = doorTile.CloseDoor();
                            break;

                        case ("open"):
                            playerActed = doorTile.OpenDoor();
                            break;
                        }
                        doorWait = "none";
                    }
                    else
                    {
                        moveToList = player.Movement(new Vector2(0, -1), level.grid, moveType, moveToList);
                        moveType   = "one";
                    }
                    fov = new FOV(level.grid, player.location);
                    fov.GetVisibility();
                }
            }
            if (newState.IsKeyDown(Keys.NumPad9))
            {
                if (!oldState.IsKeyDown(Keys.NumPad9) || repeat)
                {
                    if (doorWait != "none")
                    {
                        doorTile = level.grid[(int)player.location.X + 1, (int)player.location.Y - 1];
                        switch (doorWait)
                        {
                        case ("close"):
                            playerActed = doorTile.CloseDoor();
                            break;

                        case ("open"):
                            playerActed = doorTile.OpenDoor();
                            break;
                        }
                        doorWait = "none";
                    }
                    else
                    {
                        moveToList = player.Movement(new Vector2(1, -1), level.grid, moveType, moveToList);
                        moveType   = "one";
                    }
                    fov = new FOV(level.grid, player.location);
                    fov.GetVisibility();
                }
            }
            if (newState.IsKeyDown(Keys.Right) || newState.IsKeyDown(Keys.NumPad6))
            {
                if (!oldState.IsKeyDown(Keys.Right) && !oldState.IsKeyDown(Keys.NumPad6) || repeat)
                {
                    if (doorWait != "none")
                    {
                        doorTile = level.grid[(int)player.location.X + 1, (int)player.location.Y];
                        switch (doorWait)
                        {
                        case ("close"):
                            playerActed = doorTile.CloseDoor();
                            break;

                        case ("open"):
                            playerActed = doorTile.OpenDoor();
                            break;
                        }
                        doorWait = "none";
                    }
                    else
                    {
                        moveToList = player.Movement(new Vector2(1, 0), level.grid, moveType, moveToList);
                        moveType   = "one";
                    }
                    fov = new FOV(level.grid, player.location);
                    fov.GetVisibility();
                }
            }
            if (newState.IsKeyDown(Keys.NumPad3))
            {
                if (!oldState.IsKeyDown(Keys.NumPad3) || repeat)
                {
                    if (doorWait != "none")
                    {
                        doorTile = level.grid[(int)player.location.X + 1, (int)player.location.Y + 1];
                        switch (doorWait)
                        {
                        case ("close"):
                            playerActed = doorTile.CloseDoor();
                            break;

                        case ("open"):
                            playerActed = doorTile.OpenDoor();
                            break;
                        }
                        doorWait = "none";
                    }
                    else
                    {
                        moveToList = player.Movement(new Vector2(1, 1), level.grid, moveType, moveToList);
                        moveType   = "one";
                    }
                    fov = new FOV(level.grid, player.location);
                    fov.GetVisibility();
                }
            }
            if (newState.IsKeyDown(Keys.Down) || newState.IsKeyDown(Keys.NumPad2))
            {
                if (!oldState.IsKeyDown(Keys.Down) && !oldState.IsKeyDown(Keys.NumPad2) || repeat)
                {
                    if (doorWait != "none")
                    {
                        doorTile = level.grid[(int)player.location.X, (int)player.location.Y + 1];
                        switch (doorWait)
                        {
                        case ("close"):
                            playerActed = doorTile.CloseDoor();
                            break;

                        case ("open"):
                            playerActed = doorTile.OpenDoor();
                            break;
                        }
                        doorWait = "none";
                    }
                    else
                    {
                        moveToList = player.Movement(new Vector2(0, 1), level.grid, moveType, moveToList);
                        moveType   = "one";
                    }
                    fov = new FOV(level.grid, player.location);
                    fov.GetVisibility();
                }
            }
            if (newState.IsKeyDown(Keys.NumPad1))
            {
                if (!oldState.IsKeyDown(Keys.NumPad1) || repeat)
                {
                    if (doorWait != "none")
                    {
                        doorTile = level.grid[(int)player.location.X - 1, (int)player.location.Y + 1];
                        switch (doorWait)
                        {
                        case ("close"):
                            playerActed = doorTile.CloseDoor();
                            break;

                        case ("open"):
                            playerActed = doorTile.OpenDoor();
                            break;
                        }
                        doorWait = "none";
                    }
                    else
                    {
                        moveToList = player.Movement(new Vector2(-1, 1), level.grid, moveType, moveToList);
                        moveType   = "one";
                    }
                    fov = new FOV(level.grid, player.location);
                    fov.GetVisibility();
                }
            }
            if (newState.IsKeyDown(Keys.OemPeriod))
            {
                if (!oldState.IsKeyDown(Keys.OemPeriod))
                {
                    if (player.MoveDown(level.grid))
                    {
                        if (currentdngn_floor == dungeon.Count - 1)
                        {
                            level = new Dungeon(npcDicationary);
                            dungeon.Add(level);
                            npcs = level.npcs;
                            currentdngn_floor++;
                        }
                        else
                        {
                            currentdngn_floor++;
                            level = dungeon[currentdngn_floor];
                        }
                        player.location = level.upStairs.tilePos;
                        fov             = new FOV(level.grid, player.location);
                        fov.GetVisibility();
                    }
                }
            }
            if (newState.IsKeyDown(Keys.OemComma))
            {
                if (!oldState.IsKeyDown(Keys.OemComma))
                {
                    if (currentdngn_floor > 0)
                    {
                        if (player.MoveUp(level.grid))
                        {
                            currentdngn_floor--;
                            level           = dungeon[currentdngn_floor];
                            npcs            = level.npcs;
                            player.location = level.downStairs.tilePos;
                            fov             = new FOV(level.grid, player.location);
                            fov.GetVisibility();
                        }
                    }
                }
            }
            if (newState.IsKeyDown(Keys.C))
            {
                if (!oldState.IsKeyDown(Keys.C))
                {
                    doorWait = "close";
                    Log.Write("Close which door?");
                }
            }
            if (newState.IsKeyDown(Keys.O))
            {
                if (!oldState.IsKeyDown(Keys.O))
                {
                    doorWait = "open";
                    Log.Write("Open which door?");
                }
            }
            if (newState.IsKeyDown(Keys.Divide))
            {
                if (!oldState.IsKeyDown(Keys.Divide))
                {
                    moveType = "five";
                }
            }
            if (newState.IsKeyDown(Keys.Multiply))
            {
                if (!oldState.IsKeyDown(Keys.Multiply))
                {
                    moveType = "end";
                }
            }

            if (newState.IsKeyDown(Keys.I))
            {
                if (!oldState.IsKeyDown(Keys.I))
                {
                    menu = !menu;
                }
            }

            /*if (newState.IsKeyDown(Keys.X))
             * {
             *  if (!oldState.IsKeyDown(Keys.X))
             *  {
             *      moveToList = Pathing.FindPath(new Vector2(12,12),level.grid);
             *  }
             * }*/

            if (newState.IsKeyDown(Keys.Escape))
            {
                if (!oldState.IsKeyDown(Keys.Escape))
                {
                    Exit();
                }
            }

            if (newState.IsKeyDown(Keys.Enter))
            {
                if (!oldState.IsKeyDown(Keys.Enter))
                {
                    Log.Write("Test message!");
                }
            }

            if (newState.IsKeyDown(Keys.F10))
            {
                if (!oldState.IsKeyDown(Keys.F10))
                {
                    for (int x = 0; x < 50; x++)
                    {
                        for (int y = 0; y < 50; y++)
                        {
                            level.grid[x, y].visible = true;
                        }
                    }
                }
            }

            if (newMouseState.LeftButton == ButtonState.Pressed)
            {
                if (oldMouseState.LeftButton != ButtonState.Pressed &&
                    GraphicsDevice.Viewport.Bounds.Contains(new Point((int)newMouseState.X, (int)newMouseState.Y)))
                {
                    Vector2 clickedTile = new Vector2();
                    Vector2 moveToTile  = new Vector2();
                    clickedTile.X = newMouseState.X / 32;  //Get on screen tile coords
                    clickedTile.Y = newMouseState.Y / 32;

                    if (clickedTile.X >= 0 && clickedTile.X < 25 && clickedTile.Y >= 0 && clickedTile.Y < 25)
                    {
                        moveToTile = viewport.GetTile(clickedTile);  //Get whole dungeon tile coords
                        if (level.grid[(int)moveToTile.X, (int)moveToTile.Y].seen ||
                            level.grid[(int)moveToTile.X, (int)moveToTile.Y].visible)
                        {
                            moveToList = Pathing.FindPath(moveToTile, player.location, level.grid);
                        }
                    }
                }
            }

            // Update saved state.
            oldState      = newState;
            oldMouseState = newMouseState;
        }