Пример #1
0
        // ------------------------------------------
        // Constructor that creates the initial map
        // ------------------------------------------
        public Map()
        {
            for (int ii = 0; ii < maxRows; ii++)
            {
                // Create the map
                for (int jj = 0; jj < maxColumns; jj++)
                {
                    if (ii == 0 ||
                        ii == maxRows - 1 ||
                        jj == 0 ||
                        jj == maxColumns - 1)
                    {
                        // The boundary of the map
                        MapItem wall = new Wall();
                        mapContent[ii, jj] = wall;
                    }

                    else
                    {
                        // The map area
                        MapItem mapItem = new MapItem();
                        mapContent[ii, jj] = mapItem;
                    }
                }
            }

            // Now add the other details , such as:
            // exit, doors, keys, monster, inner-walls

            // Create the sword (of destiny) and set it's position
            SwordRoom sword = new SwordRoom();

            mapContent[swordRoomPos[0, 0], swordRoomPos[0, 1]] = sword;

            // Create the doors and set their positions
            int doorNumber = 0;

            for (int ii = 0; ii < doorPos.Length / 2; ii++)
            {
                Door door = new Door();
                door.SetNumber(doorNumber);
                mapContent[doorPos[ii, 0], doorPos[ii, 1]] = door;
                doorNumber++;
            }

            // Create the exit door and set it's position
            for (int ii = 0; ii < exitPos.Length / 2; ii++)
            {
                MapItem exit = new Exit();
                mapContent[exitPos[ii, 0], exitPos[ii, 1]] = exit;
            }

            // Create the key rooms and set their positions
            for (int ii = 0; ii < keyRoomPos.Length / 2; ii++)
            {
                KeyRoom keyRoom = new KeyRoom();
                mapContent[keyRoomPos[ii, 0], keyRoomPos[ii, 1]] = keyRoom;
            }

            // Create the super key rooms, here with 2 keys and set their positions
            for (int ii = 0; ii < superKey2RoomPos.Length / 2; ii++)
            {
                MapItem keyRoom = new KeyRoom();
                mapContent[superKey2RoomPos[ii, 0], superKey2RoomPos[ii, 1]] = keyRoom;
            }

            // Create the monsterrooms and set their positions
            for (int ii = 0; ii < monsterRoomPos.Length / 2; ii++)
            {
                MapItem monsterRoom = new MonsterRoom();
                mapContent[monsterRoomPos[ii, 0], monsterRoomPos[ii, 1]] = monsterRoom;
            }

            // Create the inner-walls and set their positions
            for (int ii = 0; ii < wallPos.Length / 2; ii++)
            {
                MapItem wall = new Wall();
                mapContent[wallPos[ii, 0], wallPos[ii, 1]] = wall;
            }
        }         // End of constructor
Пример #2
0
        // --------------------------------------------------
        // Different actions when the player is moving around
        // --------------------------------------------------
        public bool MovePlayer(Player player, int moveDirection)
        {
            // enum backgroundColor { green, red, blue, yellow };

            Wall wall = new Wall();
            Exit exit = new Exit();
            Door door = new Door();

            MonsterRoom monster   = new MonsterRoom();
            KeyRoom     keyRoom   = new KeyRoom();
            SwordRoom   swordRoom = new SwordRoom();
            MapItem     mapItem   = new MapItem();

            bool exitFound  = false;
            int  playerPosX = player.GetPosX();
            int  playerPosY = player.GetPosY();

            int nextPosX = 0;
            int nextPosY = 0;

            // Check the move direction
            if (moveDirection == 0 && playerPosX > 1)
            {
                // UP
                nextPosX = playerPosX - 1;
                nextPosY = playerPosY;
            }
            else if (moveDirection == 1 && playerPosX < maxRows - 2)
            {
                // DOWN
                nextPosX = playerPosX + 1;
                nextPosY = playerPosY;
            }
            else if (moveDirection == 2 && playerPosY > 1)
            {
                // LEFT
                nextPosX = playerPosX;
                nextPosY = playerPosY - 1;
            }
            else if (moveDirection == 3 && playerPosY < maxColumns - 2)
            {
                // RIGHT
                nextPosX = playerPosX;
                nextPosY = playerPosY + 1;
            }

            // ---------------------------------------
            // Now check where the player has gone
            // ---------------------------------------
            // Have we reach the exit door?
            if (mapContent[nextPosX, nextPosY].GetType() == exit.GetType())
            {
                mapColor.setgbColor((int)backgroundColor.green);
                player.SetPosX(nextPosX);
                player.SetPosY(nextPosY);
                return(true);
            }

            // A wall stands in the way
            if (mapContent[nextPosX, nextPosY].GetType() == wall.GetType())
            {
                return(false);
            }

            // Can we come trough the door, we must have a key
            // when entered the door remove the key
            // the door can of course already be open
            if (mapContent[nextPosX, nextPosY].GetType() == door.GetType())
            {
                door = (Door)mapContent[nextPosX, nextPosY];
                if (door.IsOpen())
                {
                    mapColor.setgbColor((int)backgroundColor.white);
                }
                else if (!player.hasKey())
                {
                    return(false);
                }
                else if (player.hasKey())
                {
                    player.setKey(false);
                    door.SetOpen();
                    mapContent[nextPosX, nextPosY] = door;
                    mapColor.setgbColor((int)backgroundColor.white);
                }
            }
            // Is this a key-rooom? Pick up the key if so!
            else if (mapContent[nextPosX, nextPosY].GetType() == keyRoom.GetType())
            {
                keyRoom = (KeyRoom)mapContent[nextPosX, nextPosY];
                if (keyRoom.hasKey())
                {
                    keyRoom.takeKey();
                    player.setKey(true);
                    mapColor.setgbColor((int)backgroundColor.yellow);
                }
            }
            // Is this the sword room, get the sword if so!
            else if (mapContent[nextPosX, nextPosY].GetType() == swordRoom.GetType())
            {
                swordRoom = (SwordRoom)mapContent[nextPosX, nextPosY];
                if (swordRoom.Sword.Equals(true))
                {
                    //swordRoom.Sword.
                    player.setSword(true);
                    mapColor.setgbColor((int)backgroundColor.red);
                    mapContent[nextPosX, nextPosY] = swordRoom;
                }
            }
            // Have we entered a monster room? Not good!
            else if (mapContent[nextPosX, nextPosY].GetType() == monster.GetType())
            {
                monster = (MonsterRoom)mapContent[nextPosX, nextPosY];
                player.addMonsterPoints(monster.getPoints());
                monster.setPoints(0);
                mapContent[nextPosX, nextPosY] = monster;
            }

            // move the player
            player.SetPosX(nextPosX);
            player.SetPosY(nextPosY);

            // ~Door();

            return(exitFound);
        }