Example #1
0
        /// <summary>
        /// This routine is used to adjust the offsets when a unit is removed from a hex that has more than 1 unit
        /// </summary>
        /// <param name="unit"></param>
        /// <param name="hex"></param>
        public static void RemoveUnitFromHex(GameObject unit, GameObject hex)
        {
            if ((unit != null) && (hex != null))
            {
                float offset = 0.5f;
                unit.GetComponent <SpriteRenderer>().sortingOrder = 0;
                hex.GetComponent <HexDatabaseFields>().occupyingUnit.Remove(unit);

                // If the hex exerts ZOC to neighbors, need to remove the unit to the list of exerting units
                foreach (HexDefinitions.HexSides hexSide in Enum.GetValues(typeof(HexDefinitions.HexSides)))
                {
                    if ((hex.GetComponent <BooleanArrayData>().exertsZOC[(int)hexSide]) && (hex.GetComponent <HexDatabaseFields>().Neighbors[(int)hexSide] != null))
                    {
                        if (hex.GetComponent <HexDatabaseFields>().Neighbors[(int)hexSide].GetComponent <HexDatabaseFields>().unitsExertingZOC.Contains(unit))
                        {
                            hex.GetComponent <HexDatabaseFields>().Neighbors[(int)hexSide].GetComponent <HexDatabaseFields>().unitsExertingZOC.Remove(unit);
                        }
                    }
                }

                // Remove the unit from exerting ZOC on the hex it is being removed from
                if (hex.GetComponent <HexDatabaseFields>().unitsExertingZOC.Contains(unit))
                {
                    hex.GetComponent <HexDatabaseFields>().unitsExertingZOC.Remove(unit);
                }

                if (hex.GetComponent <HexDatabaseFields>().occupyingUnit.Count > 0)
                {
                    for (int index = 0; index < hex.GetComponent <HexDatabaseFields>().occupyingUnit.Count; index++)
                    {
                        hex.GetComponent <HexDatabaseFields>().occupyingUnit[index].transform.position = hex.transform.position + new Vector3(index * offset, index * offset);
                        hex.GetComponent <HexDatabaseFields>().occupyingUnit[index].GetComponent <SpriteRenderer>().sortingOrder = index + 1;
                    }
                }

                // Need to update the ZOC of the hex since the unit that was removed was the last unit on the hex
                else
                {
                    // Before I created the common routines, I was calling UpdateZOC through a singleton.  I don't think I need this anymore so I'm making it a direct call.
                    // The original call is below
                    GameControl.movementRoutinesInstance.GetComponent <MovementRoutines>().UpdateZOC(hex);
                }
            }
            else
            {
                // Not sure why but I keep getting null exceptions in this routine
                if (unit == null)
                {
                    IORoutines.WriteToLogFile("removeUnitFromHex: unit passed is null");
                }
                if (hex == null)
                {
                    IORoutines.WriteToLogFile("removeUnitFromHex: hex passed is null");
                }
            }
        }
Example #2
0
 /// <summary>
 /// This routine will write out the fields of the hex
 /// </summary>
 public void WriteHexFields(StreamWriter fileWriter)
 {
     fileWriter.Write(name + " ");
     fileWriter.Write(IORoutines.WriteBooleanToSaveFormat(inGermanZOC) + " ");
     fileWriter.Write(IORoutines.WriteBooleanToSaveFormat(inAlliedZOC) + " ");
     fileWriter.Write(IORoutines.WriteBooleanToSaveFormat(alliedControl) + " ");
     fileWriter.Write(IORoutines.WriteBooleanToSaveFormat(successfullyInvaded) + " ");
     fileWriter.Write(IORoutines.WriteBooleanToSaveFormat(closeDefenseSupport) + " ");
     fileWriter.Write(IORoutines.WriteBooleanToSaveFormat(riverInterdiction) + " ");
     fileWriter.WriteLine();
 }
 /// <summary>
 /// Takes the string and returns the Unity color type that matches
 /// </summary>
 /// <param name="color"></param>
 /// <returns></returns>
 public static Color ConvertStringToColor(string color)
 {
     if (color == "Red")
     {
         return(Color.red);
     }
     else if (color == "Black")
     {
         return(Color.black);
     }
     else if (color == "Yellow")
     {
         return(Color.yellow);
     }
     else if (color == "Green")
     {
         return(Color.green);
     }
     else if (color == "Grey")
     {
         return(Color.grey);
     }
     else if (color == "Gray")
     {
         return(Color.gray);
     }
     else if (color == "Blue")
     {
         return(Color.blue);
     }
     else if (color == "Cyan")
     {
         return(Color.cyan);
     }
     else if (color == "Magenta")
     {
         return(Color.magenta);
     }
     else if (color == "White")
     {
         return(Color.white);
     }
     else
     {
         IORoutines.WriteToLogFile("ConvertStingToColor: unknown color passed - " + color);
     }
     return(Color.black);
 }
        /// <summary>
        /// This returns the combat odds of the attackers and defenders passed
        /// </summary>
        /// <param name="defendingUnits"></param>
        /// <param name="attackingUnits"></param>
        /// <param name="addCombatAirSupport"></param>
        /// <returns></returns>
        public static int ReturnCombatOdds(List <GameObject> defendingUnits, List <GameObject> attackingUnits, bool attackAirSupport)
        {
            // This routine returns a positive integer when the attackers are stronger than the defender and a negative number when the defenders are stronger than the attackers
            int odds;

            // Odds are always rounded to the defenders advantage.  For example an attack 4 to defender 7 is 1:2
            // while an attack 7 to a defender 4 is 1:1
            if ((CalculateAttackFactor(attackingUnits, attackAirSupport) == 0) || (CalculateDefenseFactor(defendingUnits, attackingUnits) == 0))
            {
                IORoutines.WriteToLogFile(
                    "returnCombatOdds: returning 0 - attackFactor = " +
                    CalculateAttackFactor(attackingUnits, attackAirSupport) +
                    " - defense factor = " + CalculateDefenseFactor(defendingUnits, attackingUnits));
                return(0);
            }
            if (CalculateDefenseFactor(defendingUnits, attackingUnits) >
                CalculateAttackFactor(attackingUnits, attackAirSupport))
            {
                if ((CalculateDefenseFactor(defendingUnits, attackingUnits) % CalculateAttackFactor(attackingUnits, attackAirSupport)) > 0)
                {
                    odds = (CalculateDefenseFactor(defendingUnits, attackingUnits) / (int)CalculateAttackFactor(attackingUnits, attackAirSupport)) + 1;
                }
                else
                {
                    odds = (CalculateDefenseFactor(defendingUnits, attackingUnits) / (int)CalculateAttackFactor(attackingUnits, attackAirSupport));
                }
                // 1:6 is the worst odds avaialble.  All odds greater than this will be returned as 7:1.
                if (odds > 6)
                {
                    odds = 7;
                }
                odds = -odds;
                return(odds);
            }
            else
            {
                odds = (int)CalculateAttackFactor(attackingUnits, attackAirSupport) / CalculateDefenseFactor(defendingUnits, attackingUnits);
                if (odds > 6)
                {
                    odds = 7;
                }
                return(odds);
            }
        }
        /// <summary>
        /// This routine will write out the fields of the unit
        /// </summary>
        public void WriteUnitFields(StreamWriter theWriter)
        {
            theWriter.Write(name + " ");
            if (occupiedHex != null)
            {
                theWriter.Write(occupiedHex.name + " ");
            }
            else
            {
                theWriter.Write("null ");
            }
            if (beginningTurnHex != null)
            {
                theWriter.Write(beginningTurnHex.name + " ");
            }
            else
            {
                theWriter.Write("null ");
            }
            theWriter.Write(IORoutines.WriteBooleanToSaveFormat(inBritain) + " ");
            theWriter.Write(IORoutines.WriteBooleanToSaveFormat(unitInterdiction) + " ");
            theWriter.Write(invasionAreaIndex + " ");
            theWriter.Write(IORoutines.WriteBooleanToSaveFormat(availableForStrategicMovement) + " ");
            theWriter.Write(IORoutines.WriteBooleanToSaveFormat(inSupply) + " ");
            if (supplySource != null)
            {
                theWriter.Write(supplySource.name + " ");
            }
            else
            {
                theWriter.Write("null ");
            }
            theWriter.Write(supplyIncrementsOutOfSupply + " ");
            theWriter.Write(IORoutines.WriteBooleanToSaveFormat(unitEliminated) + " ");

            theWriter.WriteLine();
        }
        /// <summary>
        /// Creates a canvas that is setup for scrolling
        /// </summary>
        /// <param name="name"></param>
        /// <param name="panelWidth"></param>
        /// <param name="panelHeight"></param>
        /// <param name="scrollContentPanel"></param>
        /// <param name="canvasObject"></param>
        /// <returns></returns>
        public static GameObject CreateScrollingGUICanvas(string name, float panelWidth, float panelHeight, ref GameObject scrollContentPanel, ref Canvas canvasObject)
        {
            IORoutines.WriteToLogFile("createScrollingGUICanvas: screen height = " + UnityEngine.Screen.height);

            if (panelHeight < (UnityEngine.Screen.height - 50))
            {
                IORoutines.WriteToLogFile("createScrollingGUICanvas: panel height = " + panelHeight);
                // The height is small enough that scrolling isn't needed
                return(CreateGUICanvas(name, panelWidth, panelHeight, ref canvasObject));
            }

            GameObject squareImage       = (GameObject)Resources.Load("SquareObject");
            GameObject sliderHandleImage = (GameObject)Resources.Load("SliderHandleObject");

            GameObject guiInstance = new GameObject(name);

            guiList.Add(guiInstance);
            canvasObject = guiInstance.AddComponent <Canvas>();
            guiInstance.AddComponent <CanvasScaler>();
            guiInstance.AddComponent <GraphicRaycaster>();
            canvasObject.renderMode = RenderMode.ScreenSpaceOverlay;

            GameObject scrollRect      = new GameObject("Scroll Rect");
            ScrollRect scrollable      = scrollRect.AddComponent <ScrollRect>();
            Image      scrollRectImage = scrollRect.AddComponent <Image>();

            scrollable.horizontal        = false;
            scrollable.vertical          = true;
            scrollable.movementType      = UnityEngine.UI.ScrollRect.MovementType.Clamped;
            scrollable.inertia           = false;
            scrollable.scrollSensitivity = -1;

            scrollRectImage.color = new Color32(0, 44, 255, 220);

            scrollRect.GetComponent <RectTransform>().sizeDelta = new Vector2(panelWidth, (UnityEngine.Screen.height - 50));
            scrollRect.GetComponent <RectTransform>().anchorMin = new Vector2(0.5f, 0.5f);
            scrollRect.GetComponent <RectTransform>().anchorMax = new Vector2(0.5f, 0.5f);
            scrollRect.transform.SetParent(guiInstance.transform, false);

            GameObject scrollViewport = new GameObject("ScrollViewport");

            scrollViewport.AddComponent <Mask>();
            scrollViewport.AddComponent <Image>();
            scrollViewport.GetComponent <RectTransform>().sizeDelta = new Vector2(panelWidth, (UnityEngine.Screen.height - 50));
            scrollViewport.transform.SetParent(scrollRect.transform);
            scrollViewport.GetComponent <RectTransform>().localPosition = new Vector2(0f, 0f);

            GameObject scrollContent = new GameObject("ScrollContent");

            scrollContent.AddComponent <RectTransform>();
            scrollContent.GetComponent <RectTransform>().sizeDelta = new Vector2(panelWidth, panelHeight);
            scrollContent.transform.SetParent(scrollViewport.transform, false);
            scrollContentPanel.transform.SetParent(scrollContent.transform, false);

            scrollable.content  = scrollContent.GetComponent <RectTransform>();
            scrollable.viewport = scrollViewport.GetComponent <RectTransform>();


            GameObject scrollHandle = new GameObject("ScrollHandle");

            scrollHandle.AddComponent <Image>();
            scrollHandle.GetComponent <Image>().sprite            = sliderHandleImage.GetComponent <Image>().sprite;
            scrollHandle.GetComponent <RectTransform>().sizeDelta = new Vector2(20, 400);

            GameObject scrollbarObject = new GameObject("ScrollbarObject");

            scrollbarObject.transform.SetParent(scrollRect.transform);
            Scrollbar verticalScroll = scrollbarObject.AddComponent <Scrollbar>();

            scrollbarObject.GetComponent <RectTransform>().sizeDelta     = new Vector2(20, (UnityEngine.Screen.height - 50));
            scrollbarObject.GetComponent <RectTransform>().localPosition = new Vector2(panelWidth / 2, 0f);
            scrollable.verticalScrollbar = verticalScroll;
            verticalScroll.GetComponent <Scrollbar>().direction = Scrollbar.Direction.BottomToTop;
            verticalScroll.targetGraphic = scrollHandle.GetComponent <Image>();
            verticalScroll.handleRect    = scrollHandle.GetComponent <RectTransform>();

            Image scrollbarImage = scrollbarObject.AddComponent <Image>();

            scrollbarImage.sprite = squareImage.GetComponent <Image>().sprite;

            GameObject scrollArea = new GameObject("ScrollArea");

            scrollArea.AddComponent <RectTransform>();
            scrollArea.transform.SetParent(scrollbarObject.transform, false);

            scrollHandle.transform.SetParent(scrollArea.transform, false);

            return(guiInstance);
        }