예제 #1
0
        // Beginning of turn logic
        public void UpdateTurn()
        {
            var adjRooms = getAdjacentRooms(Player.Position);

            // Update Wumpus first
            Wumpus.Update();

            // Check for adjacent hazards (in entity updates)
            foreach (Entity entity in Entities)
            {
                entity.Update();
            }

            // Tell the player where they are
            FakeConsole.WriteLine("You are in room " + Player.Position);
            FakeConsole.WriteLine("Tunnels lead to " + adjRooms[0] + ", " + adjRooms[1] + ", and " + adjRooms[2]);
            FakeConsole.WriteLine("Your move:");
            FakeConsole.WriteLine("  Left/Right to select a room");
            FakeConsole.WriteLine("  press Enter to move to selected room");
            FakeConsole.WriteLine("  press S to shoot");
        }
예제 #2
0
        public void Update()
        {
            // Define camera positions for INIT and WAITING_ON_MOVE states
            Vector3 position, target;

            position = Player.PositionCoords + cameraPositionOffset;
            target   = new Vector3(roomPositions[facingRoom], 0);
            Vector3 direction = target - position;

            direction.Normalize();
            direction *= 200;
            position  -= direction;

            // Update keyboard state
            oldKeyboardState     = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            // If just beginning, start a turn and wait for player's move
            if (currentState == GameState.INIT)
            {
                UpdateTurn();
                currentState = GameState.WAITING_ON_MOVE;
            }
            // If waiting on player's move, see if they did anything yet
            if (currentState == GameState.WAITING_ON_MOVE)
            {
                // Left and Right keys change the selected/facing room
                if (wasKeyPressed(Keys.Left))
                {
                    int[] adjRooms = getAdjacentRooms(Player.Position);
                    int   index    = Array.IndexOf(adjRooms, facingRoom) - 1;
                    if (index < 0)
                    {
                        index = adjRooms.Length - 1;
                    }
                    facingRoom = adjRooms[index];
                }
                else if (wasKeyPressed(Keys.Right))
                {
                    int[] adjRooms = getAdjacentRooms(Player.Position);
                    int   index    = Array.IndexOf(adjRooms, facingRoom) + 1;
                    if (index >= adjRooms.Length)
                    {
                        index = 0;
                    }
                    facingRoom = adjRooms[index];
                }

                // Enter makes player go to selected/facing room
                int roomClicked = (wasKeyPressed(Keys.Enter)) ? facingRoom : -1;
                int oldRoom     = Player.Position;
                if (roomClicked != -1)
                {
                    // Valid room movement input
                    if (isAdjacent(Player.Position, roomClicked))
                    {
                        lastHazardRoom = -1;
                        arrowPath      = new List <int>();
                        lastFiredPath  = new List <int>();
                        Player.Move(roomClicked);
                        HazardCheck();
                        if (playing)
                        {
                            UpdateTurn();
                        }
                        facingRoom = getAdjacentRooms(Player.Position)[0];
                        if (facingRoom == oldRoom)
                        {
                            facingRoom = getAdjacentRooms(Player.Position)[1];
                        }
                    }
                    else // Invalid room
                    {
                        FakeConsole.WriteLine("Not possible.");
                    }
                }

                // Check if user pressed S to go into shoot mode
                if (wasKeyPressed(Keys.S))
                {
                    currentState     = GameState.PICKING_SHOOT_ROOMS;
                    arrowPath        = new List <int>();
                    lastFiredPath    = new List <int>();
                    arrowPathValid   = true;
                    currentArrowRoom = Player.Position;
                    FakeConsole.WriteLine("Select up to 5 rooms to shoot through");
                    FakeConsole.WriteLine("Press S to choose a room to shoot");
                    FakeConsole.WriteLine("Press enter when finished");
                }
            }
            // If in shoot mode, see what user has inputed
            if (currentState == GameState.PICKING_SHOOT_ROOMS)
            {
                // Update camera for picking rooms to shoot
                position  = new Vector3(roomPositions[currentArrowRoom], 0) + cameraPositionOffset;
                target    = new Vector3(roomPositions[facingRoom], 0);
                direction = target - position;
                direction.Normalize();
                direction *= 200;
                position  -= direction;

                int roomClicked = wasKeyPressed(Keys.S) ? facingRoom : -1;

                // Check if user hit enter, if so, shoot the arrow and finish the turn
                if (wasKeyPressed(Keys.Enter))
                {
                    if (arrowPath.Count == 0)
                    {
                        FakeConsole.WriteLine("Okay, suit yourself...");
                    }

                    lastFiredPath = arrowPath;
                    Player.ShootArrow(arrowPath.ToArray());
                    HazardCheck();
                    if (playing)
                    {
                        UpdateTurn();
                        currentState = GameState.WAITING_ON_MOVE;
                        facingRoom   = getAdjacentRooms(Player.Position)[0];
                    }
                }
                // Left and Right to select room to shoot. Makes sure not to allow selecting room that causes A-B-A path
                if (wasKeyPressed(Keys.Left))
                {
                    var adjRooms = getAdjacentRooms(currentArrowRoom);
                    int index    = 0;
                    if (arrowPath.Count > 0)
                    {
                        var forbiddenRoom = (arrowPath.Count == 1) ? Player.Position : arrowPath[arrowPath.Count - 2];
                        if (adjRooms.Contains(forbiddenRoom)) // prevent A-B-A path
                        {
                            var tempRooms = new int[adjRooms.Length - 1];
                            int j         = 0;
                            foreach (var adjRoom in adjRooms)
                            {
                                if (adjRoom != forbiddenRoom)
                                {
                                    tempRooms[j++] = adjRoom;
                                }
                            }
                            adjRooms = tempRooms;
                        }
                        index = Array.IndexOf(adjRooms, facingRoom) - 1;
                        if (index < 0)
                        {
                            index = adjRooms.Length - 1;
                        }
                    }
                    facingRoom = adjRooms[index];
                }
                if (wasKeyPressed(Keys.Right))
                {
                    var adjRooms = getAdjacentRooms(currentArrowRoom);
                    int index    = 0;
                    if (arrowPath.Count > 0)
                    {
                        var forbiddenRoom = (arrowPath.Count == 1) ? Player.Position : arrowPath[arrowPath.Count - 2];
                        if (adjRooms.Contains(forbiddenRoom)) // prevent A-B-A path
                        {
                            var tempRooms = new int[adjRooms.Length - 1];
                            int j         = 0;
                            foreach (var adjRoom in adjRooms)
                            {
                                if (adjRoom != forbiddenRoom)
                                {
                                    tempRooms[j++] = adjRoom;
                                }
                            }
                            adjRooms = tempRooms;
                        }
                        index = Array.IndexOf(adjRooms, facingRoom) + 1;
                        if (index >= adjRooms.Length)
                        {
                            index = 0;
                        }
                    }
                    facingRoom = adjRooms[index];
                }
                // If a room was selected, try to add it to the arrow path
                else if (roomClicked != -1)
                {
                    int oldArrowRoom = currentArrowRoom;
                    // Player inputed room that would cause A-B-A path, don't accept
                    if (arrowPath.Count > 1 && arrowPath[arrowPath.Count - 2] == roomClicked || arrowPath.Count == 1 && roomClicked == Player.Position)
                    {
                        FakeConsole.WriteLine("Arrows aren't that crooked - try another room");
                    }
                    // Player has entered valid path so far, so accept his input if correct
                    else if (arrowPathValid && isAdjacent(currentArrowRoom, roomClicked))
                    {
                        arrowPath.Add(roomClicked);
                        currentArrowRoom = roomClicked;
                    }
                    // Player inputted an invalid room, so pick one at random
                    else
                    {
                        arrowPathValid = false;
                        var adjRooms = getAdjacentRooms(currentArrowRoom);
                        if (arrowPath.Count > 0)
                        {
                            var forbiddenRoom = (arrowPath.Count == 1) ? Player.Position : arrowPath[arrowPath.Count - 2];
                            if (adjRooms.Contains(forbiddenRoom)) // prevent A-B-A path
                            {
                                var tempRooms = new int[adjRooms.Length - 1];
                                int j         = 0;
                                foreach (var adjRoom in adjRooms)
                                {
                                    if (adjRoom != forbiddenRoom)
                                    {
                                        tempRooms[j++] = adjRoom;
                                    }
                                }
                                adjRooms = tempRooms;
                            }
                            var pickRoom = adjRooms[random.Next(adjRooms.Length)];
                            arrowPath.Add(pickRoom);
                            currentArrowRoom = pickRoom;
                        }
                    }
                    facingRoom = getAdjacentRooms(currentArrowRoom)[0];
                    if (facingRoom == oldArrowRoom)
                    {
                        facingRoom = getAdjacentRooms(currentArrowRoom)[1];
                    }
                    // Max rooms hit, fire the arrow
                    if (arrowPath.Count == 5)
                    {
                        lastFiredPath = arrowPath;
                        Player.ShootArrow(arrowPath.ToArray());
                        HazardCheck();
                        if (playing)
                        {
                            UpdateTurn();
                            currentState = GameState.WAITING_ON_MOVE;
                            facingRoom   = getAdjacentRooms(Player.Position)[0];
                        }
                    }
                }
            }
            // When the game is over, switch to overview camera position to see the whole map
            if (!playing)
            {
                position = new Vector3();
                for (int k = 1; k < roomPositions.Length; k++)
                {
                    position.X += roomPositions[k].X;
                    position.Y += roomPositions[k].Y;
                }
                position   /= 20;
                position.Z  = 500;
                target      = new Vector3(position.X, position.Y, 0);
                position.Y -= 10;
            }
            camera.update(position, target);
        }