Пример #1
0
        /// <summary>
        /// This routine takes the selectedUnit and places it on the hex at the location passed
        /// </summary>
        /// <param name="selectedUnit"></param>
        /// <param name="xCoord"></param>
        /// <param name="yCoord"></param>
        public void GetUnitSetupDestination(GameObject selectedUnit, int xCoord, int yCoord)
        {
            GameObject targetHex;

            targetHex = GeneralHexRoutines.GetHexAtXY(xCoord, yCoord);
            if ((targetHex != null) && (VerifyValidHex(selectedUnit, targetHex)))
            {
                // If the unit already occupies a hex (not gauranteed since this is setup) need
                // to remove it from the hex so it isn't counting against stacking when it isn't there
                if (selectedUnit.GetComponent <UnitDatabaseFields>().occupiedHex != null)
                {
                    GeneralHexRoutines.RemoveUnitFromHex(selectedUnit, selectedUnit.GetComponent <UnitDatabaseFields>().occupiedHex);
                }

                GeneralHexRoutines.PutUnitOnHex(selectedUnit, targetHex);
            }
            GlobalDefinitions.UnhighlightUnit(selectedUnit);
            selectedUnit.transform.parent = GlobalDefinitions.allUnitsOnBoard.transform;
            selectedUnit.GetComponent <UnitDatabaseFields>().inBritain = false;
            selectedUnit = null;
        }
Пример #2
0
        /// <summary>
        /// Executes when the user indicates he wants to go to main menu
        /// </summary>
        public void YesMain()
        {
            List <GameObject> removeUnitList = new List <GameObject>();

            // If this is a network game I've already checked that the player is in control
            //if (GlobalDefinitions.gameMode == GlobalDefinitions.GameModeValues.Peer2PeerNetwork)
            //{
            //    GlobalDefinitions.WriteToLogFile("YesMain: Calling ResetConnection()");
            //    //byte error;
            //    //NetworkTransport.Disconnect(TransportScript.receivedHostId, TransportScript.gameConnectionId, out error);
            //    //Network.Disconnect();
            //    TransportScript.ResetConnection(TransportScript.computerId);
            //}

            // Copy list so the guis can be removed
            List <GameObject> removeList = new List <GameObject>();

            foreach (GameObject gui in GUIRoutines.guiList)
            {
                removeList.Add(gui);
            }


            // Get rid of all active guis
            foreach (GameObject gui in removeList)
            {
                GUIRoutines.RemoveGUI(gui);
            }

            // Put all the units back on the OOB sheet
            foreach (Transform unit in GlobalDefinitions.allUnitsOnBoard.transform)
            {
                unit.GetComponent <UnitDatabaseFields>().unitInterdiction      = false;
                unit.GetComponent <UnitDatabaseFields>().isCommittedToAnAttack = false;
                unit.GetComponent <UnitDatabaseFields>().hasMoved                    = false;
                unit.GetComponent <UnitDatabaseFields>().unitEliminated              = false;
                unit.GetComponent <UnitDatabaseFields>().occupiedHex                 = null;
                unit.GetComponent <UnitDatabaseFields>().beginningTurnHex            = null;
                unit.GetComponent <UnitDatabaseFields>().invasionAreaIndex           = -1;
                unit.GetComponent <UnitDatabaseFields>().inSupply                    = true;
                unit.GetComponent <UnitDatabaseFields>().supplySource                = null;
                unit.GetComponent <UnitDatabaseFields>().supplyIncrementsOutOfSupply = 0;
                unit.GetComponent <UnitDatabaseFields>().remainingMovement           = unit.GetComponent <UnitDatabaseFields>().movementFactor;
                if (unit.GetComponent <UnitDatabaseFields>().occupiedHex != null)
                {
                    GlobalDefinitions.UnhighlightUnit(unit.gameObject);
                    GeneralHexRoutines.RemoveUnitFromHex(unit.gameObject, unit.GetComponent <UnitDatabaseFields>().occupiedHex);
                    unit.GetComponent <UnitDatabaseFields>().occupiedHex = null;
                }

                removeUnitList.Add(unit.gameObject);
            }

            foreach (GameObject unit in removeUnitList)
            {
                GlobalDefinitions.ReturnUnitToOOBShet(unit);
            }

            // Clear out the lists keeping track of both side's units on board
            GlobalDefinitions.alliedUnitsOnBoard.Clear();
            GlobalDefinitions.germanUnitsOnBoard.Clear();

            // Go through the hexes and reset all highlighting
            foreach (GameObject hex in HexDefinitions.allHexesOnBoard)
            {
                hex.GetComponent <HexDatabaseFields>().riverInterdiction   = false;
                hex.GetComponent <HexDatabaseFields>().closeDefenseSupport = false;
                hex.GetComponent <HexDatabaseFields>().successfullyInvaded = false;
                hex.GetComponent <HexDatabaseFields>().alliedControl       = false;
                hex.GetComponent <HexDatabaseFields>().inAlliedZOC         = false;
                hex.GetComponent <HexDatabaseFields>().inGermanZOC         = false;
                hex.GetComponent <HexDatabaseFields>().occupyingUnit.Clear();
                hex.GetComponent <HexDatabaseFields>().unitsExertingZOC.Clear();
                hex.GetComponent <HexDatabaseFields>().availableForMovement       = false;
                hex.GetComponent <HexDatabaseFields>().strategicRemainingMovement = 0;
                hex.GetComponent <HexDatabaseFields>().remainingMovement          = 0;
                hex.GetComponent <HexDatabaseFields>().supplySources.Clear();
                hex.GetComponent <HexDatabaseFields>().unitsThatCanBeSupplied.Clear();
                hex.GetComponent <HexDatabaseFields>().closeDefenseSupport = false;
                hex.GetComponent <HexDatabaseFields>().riverInterdiction   = false;
                hex.GetComponent <HexDatabaseFields>().carpetBombingActive = false;

                GlobalDefinitions.UnhighlightHex(hex.gameObject);
            }

            GlobalDefinitions.WriteToLogFile("Putting Allied units in Britain");
            // When restarting a game the units won't have their Britain location loaded so this needs to be done before a restart file is read
            GameControl.createBoardInstance.GetComponent <CreateBoard>().ReadBritainPlacement(GlobalGameFields.britainUnitLocationFile);

            GlobalDefinitions.ResetAllGlobalDefinitions();

            // Turn the button back on
            GlobalDefinitions.mainMenuButton.GetComponent <Button>().interactable = true;

            MainMenuRoutines.GetGameModeUI();
        }
Пример #3
0
        private float initialTouch; // Used to check if the mouse click is a double click
        void Update()
        {
            if (GlobalDefinitions.gameStarted)
            {
                if (GlobalDefinitions.localControl || (GlobalDefinitions.gameMode == GlobalDefinitions.GameModeValues.Hotseat))
                {
                    if (!GlobalDefinitions.commandFileBeingRead)
                    {
                        // Left mouse button click
                        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
                        {
                            // Check if the user double clicked
                            if ((Time.time < initialTouch + 0.5f) &&
                                ((gameStateControlInstance.GetComponent <GameStateControl>().currentState.name == "alliedMovementStateInstance") ||
                                 (gameStateControlInstance.GetComponent <GameStateControl>().currentState.name == "germanMovementStateInstance") ||
                                 (gameStateControlInstance.GetComponent <GameStateControl>().currentState.name == "setUpStateInstance")))
                            {
                                // When we have a double click that means that there was already a single click that would have selected a unit
                                // Unhighlight it and then remove it
                                if (GlobalDefinitions.selectedUnit != null)
                                {
                                    GlobalDefinitions.UnhighlightUnit(GlobalDefinitions.selectedUnit);
                                }
                                foreach (Transform hex in GameObject.Find("Board").transform)
                                {
                                    GlobalDefinitions.UnhighlightHex(hex.gameObject);
                                }
                                GlobalDefinitions.selectedUnit = null;

                                GlobalDefinitions.WriteToCommandFile(GlobalDefinitions.SETCAMERAPOSITIONKEYWORD + " " + Camera.main.transform.position.x + " " + Camera.main.transform.position.y + " " + Camera.main.transform.position.z + " " + Camera.main.GetComponent <Camera>().orthographicSize);

                                // I had a bug where double clicking on an off-board unit causes an exception in the following line because it is assuming a hex is being clicked
                                if (GeneralHexRoutines.GetHexFromUserInput(Input.mousePosition) != null)
                                {
                                    GlobalDefinitions.WriteToCommandFile(GlobalDefinitions.MOUSEDOUBLECLICKIONKEYWORD + " " + GeneralHexRoutines.GetHexFromUserInput(Input.mousePosition).name + " " + gameStateControlInstance.GetComponent <GameStateControl>().currentState.currentNationality);
                                }

                                movementRoutinesInstance.GetComponent <MovementRoutines>().CallMultiUnitDisplay(GeneralHexRoutines.GetHexFromUserInput(Input.mousePosition),
                                                                                                                gameStateControlInstance.GetComponent <GameStateControl>().currentState.currentNationality);
                            }
                            // If not double click then process a normal click
                            else
                            {
                                inputMessage.GetComponent <InputMessage>().hex  = GeneralHexRoutines.GetHexFromUserInput(Input.mousePosition);
                                inputMessage.GetComponent <InputMessage>().unit = GeneralHexRoutines.GetUnitWithoutHex(Input.mousePosition);

                                RecordMouseClick(inputMessage.GetComponent <InputMessage>().unit, inputMessage.GetComponent <InputMessage>().hex);

                                gameStateControlInstance.GetComponent <GameStateControl>().currentState.executeMethod(inputMessage.GetComponent <InputMessage>());
                            }

                            initialTouch = Time.time;
                        }
                    }

                    // Note that the EventSystem check is to ensure the mouse isn't clicking a ui button
                    //else if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
                    //{
                    //    // This is a left mouse button click to select a unit or hex
                    //    inputMessage.GetComponent<InputMessage>().hex = GlobalDefinitions.getHexFromUserInput(Input.mousePosition);
                    //    inputMessage.GetComponent<InputMessage>().unit = GlobalDefinitions.getUnitWithoutHex(Input.mousePosition);

                    //    gameStateControlInstance.GetComponent<gameStateControl>().currentState.executeMethod(inputMessage.GetComponent<InputMessage>());
                    //    if ((GlobalDefinitions.gameMode == GlobalDefinitions.GameModeValues.Network) && (GlobalDefinitions.localControl))
                    //        sendMouseClickToNetwork(inputMessage.GetComponent<InputMessage>().unit, inputMessage.GetComponent<InputMessage>().hex);
                    //}

                    // Even though this is for when the player is in control, still need to check for chat messages
                    //if (GlobalDefinitions.gameMode == GlobalDefinitions.GameModeValues.Peer2PeerNetwork)
                    //{
                    //    string message;

                    //    NetworkEventType receivedNetworkEvent = TransportScript.checkForNetworkEvent(out message);

                    //    if (receivedNetworkEvent == NetworkEventType.DataEvent)
                    //    {
                    //        // The only message that is valid when in control is a chat message

                    //        char[] delimiterChars = { ' ' };
                    //        string[] switchEntries = message.Split(delimiterChars);

                    //        switch (switchEntries[0])
                    //        {
                    //            case GlobalDefinitions.CHATMESSAGEKEYWORD:
                    //                string chatMessage = "";
                    //                for (int index = 0; index < (switchEntries.Length - 1); index++)
                    //                    chatMessage += switchEntries[index + 1] + " ";
                    //                GlobalDefinitions.WriteToLogFile("Chat message received: " + chatMessage);
                    //                GlobalDefinitions.AddChatMessage(chatMessage);
                    //                break;
                    //            default:
                    //                GlobalDefinitions.WriteToLogFile("ERROR: unexpected data message received when in control (only chat message valid - message = " + message);
                    //                break;
                    //        }
                    //    }
                    //}

                    // Since I have enabled chat I have to do something to get hotkeys since chat will execute the hotkeys
                    //else if (Input.GetKeyDown(KeyCode.R))
                    //{
                    //    GUIButtonRoutinesInstance.GetComponent<GUIButtonRoutines>().executeCombatResolution();
                    //}

                    //else if (Input.GetKeyDown(KeyCode.Q))
                    //{
                    //    GUIButtonRoutinesInstance.GetComponent<GUIButtonRoutines>().goToNextPhase();
                    //}

                    //else if (Input.GetKeyDown(KeyCode.U))
                    //{
                    //    GUIButtonRoutinesInstance.GetComponent<GUIButtonRoutines>().executeUndo();
                    //}
                }

                //else if (!GlobalDefinitions.localControl && (GlobalDefinitions.gameMode == GlobalDefinitions.GameModeValues.Peer2PeerNetwork))
                //{
                //    string message;
                //    NetworkEventType receivedNetworkEvent = TransportScript.checkForNetworkEvent(out message);

                //    if (receivedNetworkEvent == NetworkEventType.DataEvent)
                //        ExecuteGameCommand.ProcessCommand(message);
                //}

                else if (GlobalDefinitions.gameMode == GlobalDefinitions.GameModeValues.AI)
                {
                    // The user side is controled by the hotseat section above.  The AI doesn't need anything during update since its states don't have input or transitions.
                }

                else if (GlobalDefinitions.gameMode == GlobalDefinitions.GameModeValues.ClientServerNetwork)
                {
                }
            }
        }
        /// <summary>
        /// This routine looks through the attackers and defenders and determines if there are defenders that need to be added to the mustBeAttackedUnits due to being in the ZOC of a defender being attacked cross river
        /// </summary>
        public static bool CheckIfDefenderToBeAddedDueToCrossRiverAttack(List <GameObject> attackingUnits, List <GameObject> defendingUnits, bool shouldHighlight)
        {
            bool foundUnit = false;
            List <GameObject> adjacentUnits = new List <GameObject>();

            if (attackingUnits.Count > 0)
            {
                // Get all adjacent defenders to the attacking units
                foreach (GameObject attackingUnit in attackingUnits)
                {
                    foreach (GameObject defendingUnit in GameControl.combatRoutinesInstance.GetComponent <CombatRoutines>().ReturnAdjacentEnemyUnits(attackingUnit.GetComponent <UnitDatabaseFields>().occupiedHex, GlobalDefinitions.ReturnOppositeNationality(attackingUnits[0].GetComponent <UnitDatabaseFields>().nationality)))
                    {
                        if (!adjacentUnits.Contains(defendingUnit))
                        {
                            adjacentUnits.Add(defendingUnit);
                        }
                    }
                }

                foreach (GameObject defendingUnit in defendingUnits)
                {
                    if (defendingUnit.GetComponent <UnitDatabaseFields>().isCommittedToAnAttack)
                    {
                        //check if the defender is across a river from a committed attacker
                        foreach (GameObject attackingUnit in attackingUnits)
                        {
                            if (attackingUnit.GetComponent <UnitDatabaseFields>().isCommittedToAnAttack)
                            {
                                // Go through each of the hexsides on the defending hexes and see if there is a river between the defender and the attacker
                                foreach (HexDefinitions.HexSides hexSide in Enum.GetValues(typeof(HexDefinitions.HexSides)))
                                {
                                    if ((defendingUnit.GetComponent <UnitDatabaseFields>().occupiedHex.GetComponent <HexDatabaseFields>().Neighbors[(int)hexSide] != null) &&
                                        (defendingUnit.GetComponent <UnitDatabaseFields>().occupiedHex.GetComponent <HexDatabaseFields>().Neighbors[(int)hexSide] == attackingUnit.GetComponent <UnitDatabaseFields>().occupiedHex) &&
                                        defendingUnit.GetComponent <UnitDatabaseFields>().occupiedHex.GetComponent <BooleanArrayData>().riverSides[(int)hexSide])
                                    {
                                        // At this point we know that the defender is across the river from a committed attacker

                                        // Now check if there is a friendly unit in the ZOC of the defender and is adjacent to the attacker.  If so, add it to must be attacked

                                        // Get all friendly units that are in the ZOC of the defender
                                        foreach (GameObject unit in ReturnFriendlyUnitsInZOC(defendingUnit))
                                        {
                                            // Now check if any of the units are adjacent to the attacker with a river between them.  If so, add it to must be attacked.
                                            if (adjacentUnits.Contains(unit) &&
                                                GeneralHexRoutines.CheckForRiverBetweenTwoHexes(attackingUnit.GetComponent <UnitDatabaseFields>().occupiedHex, unit.GetComponent <UnitDatabaseFields>().occupiedHex) &&
                                                !unit.GetComponent <UnitDatabaseFields>().isCommittedToAnAttack)
                                            {
                                                foundUnit = true;
                                                if (shouldHighlight)
                                                {
                                                    GlobalDefinitions.HighlightUnit(unit);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(foundUnit);
        }
Пример #5
0
        /// <summary>
        /// This routine reads a single record for a unit
        /// </summary>
        /// <param name="entries"></param>
        public void ProcessUnitRecord(string[] entries)
        {
            GameObject unit;

            unit = GameObject.Find(entries[0]);
            if (entries[1] == "null")
            {
                unit.GetComponent <UnitDatabaseFields>().occupiedHex = null;
            }
            else
            {
                unit.GetComponent <UnitDatabaseFields>().occupiedHex = GameObject.Find(entries[1]);
            }
            if (entries[2] == "null")
            {
                unit.GetComponent <UnitDatabaseFields>().beginningTurnHex = null;
            }
            else
            {
                unit.GetComponent <UnitDatabaseFields>().beginningTurnHex = GameObject.Find(entries[2]);
            }
            unit.GetComponent <UnitDatabaseFields>().inBritain                     = GlobalDefinitions.ReturnBoolFromSaveFormat(entries[3]);
            unit.GetComponent <UnitDatabaseFields>().unitInterdiction              = GlobalDefinitions.ReturnBoolFromSaveFormat(entries[4]);
            unit.GetComponent <UnitDatabaseFields>().invasionAreaIndex             = Convert.ToInt32(entries[5]);
            unit.GetComponent <UnitDatabaseFields>().availableForStrategicMovement = GlobalDefinitions.ReturnBoolFromSaveFormat(entries[6]);
            unit.GetComponent <UnitDatabaseFields>().inSupply = GlobalDefinitions.ReturnBoolFromSaveFormat(entries[7]);
            // Need to adjust the highlighting of the unit if it is out of supply
            GlobalDefinitions.UnhighlightUnit(unit);
            if (entries[8] == "null")
            {
                unit.GetComponent <UnitDatabaseFields>().supplySource = null;
            }
            else
            {
                unit.GetComponent <UnitDatabaseFields>().supplySource = GameObject.Find(entries[8]);
            }
            unit.GetComponent <UnitDatabaseFields>().supplyIncrementsOutOfSupply = Convert.ToInt32(entries[9]);
            unit.GetComponent <UnitDatabaseFields>().unitEliminated = GlobalDefinitions.ReturnBoolFromSaveFormat(entries[10]);


            if (unit.GetComponent <UnitDatabaseFields>().occupiedHex != null)
            {
                GeneralHexRoutines.PutUnitOnHex(unit, unit.GetComponent <UnitDatabaseFields>().occupiedHex);
                unit.transform.parent = GlobalDefinitions.allUnitsOnBoard.transform;
            }
            else if (unit.GetComponent <UnitDatabaseFields>().unitEliminated)
            {
                unit.transform.parent   = GameObject.Find("Units Eliminated").transform;
                unit.transform.position = unit.GetComponent <UnitDatabaseFields>().OOBLocation;
            }
            else if (unit.GetComponent <UnitDatabaseFields>().inBritain)
            {
                unit.transform.parent   = GameObject.Find("Units In Britain").transform;
                unit.transform.position = unit.GetComponent <UnitDatabaseFields>().locationInBritain;
            }
            else
            {
                GlobalDefinitions.WriteToLogFile("processUnitRecord: Unit read error - " + entries[1] + ": found no location to place this unit");
            }

            if (!unit.GetComponent <UnitDatabaseFields>().unitEliminated&& !unit.GetComponent <UnitDatabaseFields>().inBritain)
            {
                if (unit.GetComponent <UnitDatabaseFields>().nationality == GlobalDefinitions.Nationality.Allied)
                {
                    GlobalDefinitions.alliedUnitsOnBoard.Add(unit);
                }
                else
                {
                    GlobalDefinitions.germanUnitsOnBoard.Add(unit);
                }
            }
        }