Exemplo n.º 1
0
    private static void BuildCluster(ref List <Unit> cluster, ref List <Unit> points)
    {
        while (points.Count != 0)
        {
            instance.combatPoints.Remove(points[0].CurrentHex);
            cluster.Add(points[0]);

            Unit center = points[0];
            foreach (HexCell h in MapMaster.CellsWithinArea(center.CurrentHex, 1))
            {
                if (h.Passable)
                {
                    continue;
                }
                if (cluster.Contains(h.Unit))
                {
                    continue;
                }
                if (h.Unit != null)
                {
                    if (h.Unit.UName != "Cannon")
                    {
                        if (h.Unit.Player == PlayerMaster.PriorTurn)
                        {
                            cluster.Add(h.Unit);
                        }
                    }
                }
            }
            points.RemoveAt(0);
        }
    }
Exemplo n.º 2
0
    public static void SetCannon(Cannon c)
    {
        int fI = 0;
        int fC = 0;
        int speed;

        foreach (HexCell h in MapMaster.CellsWithinArea(c.CurrentHex, 1))
        {
            if (h.passable)
            {
                continue;
            }
            if (h.Unit != null)
            {
                if (h.Unit.Player == c.player)
                {
                    string name = h.Unit.UName;
                    if (name == "Infantry")
                    {
                        fI++;
                    }
                    else if (name == "Cavalry")
                    {
                        fC++;
                    }
                }
            }
        }

        c.Manned = DecideManned(fI, fC, out speed);
        c.shots  = Cannon.Clip;
        c.SetMove(speed);
        c.Moved = speed == 0 ? true : false;
    }
Exemplo n.º 3
0
    public static void BuildCombatPoints(HexCell checkCell)
    {
        if (checkCell.Unit != null && checkCell.Unit.UName == "Cannon")
        {
            return;
        }

        foreach (HexCell h in MapMaster.CellsWithinArea(checkCell, 1))
        {
            if (h.Passable)
            {
                continue;
            }
            else if (h.Unit != null)
            {
                if (h.Unit.Player == PlayerMaster.PriorTurn && h.Unit.UName != "Cannon")
                {
                    Debug.Log("PointAdded" + h.Unit.Player);
                    instance.combatPoints.Add(checkCell);
                    break;
                }
            }
        }

        //  foreach(Unit u in PlayerMaster.CurrentPlayer)
    }
Exemplo n.º 4
0
    static IEnumerator MoveFollowers()
    {
        moving = true;
        target = otherlist[0];

        for (int n = 1; n < otherlist.Count; ++n)
        {
            MoveMaster.SetTarget(otherlist[n]);

            moveTo = otherlist[n].CurrentHex;
            moves  = otherlist[n].MoveSpeed;

            openSpaces = MapMaster.CellsWithinArea(moveTo, moves);                                                           //finds nearby hexcells
            distance   = Vector3.Distance(target.CurrentHex.transform.position, otherlist[n].CurrentHex.transform.position); //distance between current hex and target hex.

            for (int i = 1; i < openSpaces.Count; ++i)                                                                       //loops through the rest of the cells
            {
                if (openSpaces[i].Unit == null && distance > Vector3.Distance(target.CurrentHex.transform.position, openSpaces[i].transform.position))
                {
                    moveTo   = openSpaces[i];
                    distance = Vector3.Distance(target.CurrentHex.transform.position, openSpaces[i].transform.position);
                }
            }
            yield return(new WaitForSeconds(.2f));

            MoveMaster.EvaluateTile(moveTo); //Actually moves unit
        }
        Debug.Log("1");
        moving = false;
    }
Exemplo n.º 5
0
    private void ClickHex() // checks if a valid hex was clicked
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        HexCell h;

        if (Physics.Raycast(ray, out rayHit))
        {
            h = rayHit.transform.gameObject.GetComponent <HexCell>();
            if (h != null) // it might be tempting to but this and the nested if statment together with an &&, but that'll throw an error if h is null when it check h.Unit
            {
                if (h.Unit == null && h.Passable)
                {
                    List <HexCell> CellsToChange = MapMaster.CellsWithinArea(h, multCellRadius);

                    foreach (HexCell cell in CellsToChange)
                    {
                        if (cell.R < 2 || cell.R > 3 && cell.R < MapMaster.MapRadius - 4 || cell.R > MapMaster.MapRadius - 3)
                        {
                            if (cell.Passable)
                            {
                                ChangeHex(cell);
                            }
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 6
0
    void ITerrain.ResetTiles()
    {
        List <HexCell> cells = MapMaster.CellsWithinArea(currentHex, moveSpeed);

        foreach (HexCell h in cells)
        {
            h.Passable = true;
        }
    }
Exemplo n.º 7
0
    public static void ClearMoveVariables()
    {
        if (target != null)
        {
            HighlightMaster.ToggleTileHighlights(false, MapMaster.CellsWithinArea(target.CurrentHex, target.MoveSpeed));
        }

        target = null;
    }
Exemplo n.º 8
0
    public static void EvaluateTile(HexCell h)
    {
        if (target != null && h.passable)
        {
            List <HexCell> path = HexStar.GetPath(target.CurrentHex, h, target.MoveSpeed);

            HighlightMaster.ToggleTileHighlights(false, MapMaster.CellsWithinArea(target.CurrentHex, target.MoveSpeed));

            if (path.Count > 0)
            {
                target.StartCoroutine(FollowPath(path, target));
                SoundMaster.SelectUnit();
            }

            target = null;
        }
    }
Exemplo n.º 9
0
    private static void FeelForPoints(ref List <Unit> points)
    {
        List <Unit> open = new List <Unit>()
        {
            points[0]
        };
        List <Unit> closed = new List <Unit>()
        {
            points[0]
        };

        while (open.Count != 0)
        {
            Unit    center = open[0];
            HexCell origin = points[0].CurrentHex;
            foreach (HexCell h in MapMaster.CellsWithinArea(center.CurrentHex, 1))
            {
                if (h.Passable)
                {
                    continue;
                }
                if (closed.Contains(h.Unit))
                {
                    continue;
                }
                if (h.Unit != null)
                {
                    if (h.Unit.Player != -1)
                    {
                        open.Add(h.Unit);
                        closed.Add(h.Unit);
                        if (instance.combatPoints.Contains(h))
                        {
                            points.Add(h.Unit);
                        }
                    }
                }
            }

            open.RemoveAt(0);
        }
    }
Exemplo n.º 10
0
    static void MoveCannon()
    {
        Debug.Log("Move Cannons");
        HexCell     moveTo;
        List <Unit> inRangeTargets = new List <Unit>();

        for (int i = 0; i < otherlist.Count; ++i) //searches squad list for the cannon.
        {
            if (otherlist[i].gameObject.tag == "cannon")
            {
                Debug.Log("Found Cannon");
                if (GameBrain.turnNum < 4)
                {
                    otherlist[i].Moved     = false;
                    otherlist[i].MoveSpeed = 2;
                }

                HexCell        cannonCell   = otherlist[i].CurrentHex;
                List <HexCell> inRangeCells = MapMaster.CellsWithinArea(cannonCell, 11); //Gets all HexCells within "range" of the current cannon
                for (int j = 0; j < inRangeCells.Count; ++j)
                {
                    //If the cell has an enemy unit on it that is not a cannon it is added to the list
                    if (inRangeCells[j].Unit != null && inRangeCells[j].Unit.Player != PlayerMaster.CurrentTurn && inRangeCells[j].Unit.gameObject.tag != "Cannon")
                    {
                        inRangeTargets.Add(inRangeCells[j].Unit);
                    }
                }


                //if(inRangeTargets.Count < 0)
                // {
                MoveMaster.SetTarget(otherlist[i]);
                target = otherlist[i];
                moveTo = MapMaster.Map[target.CurrentHex.R - 2, target.CurrentHex.Q];
                MoveMaster.EvaluateTile(moveTo); //Actually moves unit
                // }
            }
        }
    }
Exemplo n.º 11
0
    private static void MoveToTile(HexCell h, ref Unit u)
    {
        HighlightMaster.ToggleTileHighlights(false, MapMaster.CellsWithinArea(u.CurrentHex, u.MoveSpeed));

        MapMaster.Map[u.CurrentHex.R, u.CurrentHex.Q].Passable = true;
        MapMaster.Map[u.CurrentHex.R, u.CurrentHex.Q].Unit     = null;

        u.gameObject.transform.position = h.SpawnVector;

        MapMaster.Map[h.R, h.Q].Unit     = u;
        MapMaster.Map[h.R, h.Q].Passable = false;
        u.Moved = true;

        u.CurrentHex = MapMaster.Map[h.R, h.Q];

        HighlightMaster.HighlightUnitToggle(false, u);

        AttackMaster.BuildCombatPoints(u.CurrentHex);

        u = null;

        UnitsToMove--;
    }
Exemplo n.º 12
0
    static void MoveLeadSoldier()
    {
        MoveMaster.SetTarget(otherlist[0]); //Which unit it's about to move
        //moveTo is hexcell unit is trying to move to. Starts as the cell the Unit is already on
        moveTo = otherlist[0].CurrentHex;
        moves  = otherlist[0].MoveSpeed;

        openSpaces = MapMaster.CellsWithinArea(moveTo, moves);                                                           //finds nearby hexcells
        distance   = Vector3.Distance(target.CurrentHex.transform.position, otherlist[0].CurrentHex.transform.position); //distance between current hex and target hex.

        for (int i = 1; i < openSpaces.Count; ++i)                                                                       //loops through the rest of the cells
        {
            if (openSpaces[i].Unit == null && distance > Vector3.Distance(target.CurrentHex.transform.position, openSpaces[i].transform.position))
            {
                moveTo   = openSpaces[i];
                distance = Vector3.Distance(target.CurrentHex.transform.position, openSpaces[i].transform.position);
            }
        }

        openSpaces.Clear();

        MoveMaster.EvaluateTile(moveTo); //Actually moves unit
    }
Exemplo n.º 13
0
    public static void SetTarget(Unit u)
    {
        if (!u.Moved && u.Player == PlayerMaster.CurrentTurn)
        {
            if (target != null)
            {
                HighlightMaster.ToggleTileHighlights(false, MapMaster.CellsWithinArea(target.CurrentHex, target.MoveSpeed));
                if (target.UName != "Cannon" && !target.Equals(u))
                {
                    target.GetComponentInChildren <Animator>().Play("GoToIdle");
                }
            }

            target = u;
            instance.timePressed = 0;
            SoundMaster.SelectUnit();

            if (u.UName != "Cannon")
            {
                u.GetComponentInChildren <Animator>().Play("AtenHut");
            }

            foreach (HexCell h in MapMaster.CellsWithinArea(u.CurrentHex, u.MoveSpeed))
            {
                if (HexStar.GetPath(u.CurrentHex, h, u.MoveSpeed).Count > 0)
                {
                    HighlightMaster.HighlightTile(h);
                }
            }

            if (instance.uQue.Count != 0)
            {
                instance.uQue.Clear();
            }
            //playthatfunkysound(h.Unit.UName);
        }
    }
Exemplo n.º 14
0
    private static void ExpandCluster(ref List <Unit> area, ref List <Unit> cluster)
    {
        List <Unit> open = new List <Unit>(cluster);

        area = new List <Unit>(cluster);
        while (open.Count != 0)
        {
            Unit center = open[0];
            foreach (HexCell h in MapMaster.CellsWithinArea(center.CurrentHex, 1))
            {
                if (h.Passable)
                {
                    continue;
                }
                if (area.Contains(h.Unit))
                {
                    continue;
                }
                if (h.Unit != null)
                {
                    if (h.Unit.UName != "Cannon")
                    {
                        if (h.Unit.Player != -1)
                        {
                            if (WithinClusterRange(ref cluster, h, 1))
                            {
                                open.Add(h.Unit);
                                area.Add(h.Unit);
                            }
                        }
                    }
                }
            }

            open.RemoveAt(0);
        }
    }
Exemplo n.º 15
0
    //Finds all nearby enemies and figures out where to aim. Called from Camera Control during Cannon phase and after a single cannon shot
    public void FindTargets(Cannon can)
    {
        if (can == null)
        {
            return;
        }

        HexCell        cannonCell   = can.CurrentHex;
        List <HexCell> inRangeCells = MapMaster.CellsWithinArea(cannonCell, range); //Gets all HexCells within "range" of the current cannon

        //     int l = 0;
        for (int i = 0; i < inRangeCells.Count; ++i)
        {
            //If the cell has an enemy unit on it that is not a cannon it is added to the list
            if (inRangeCells[i].Unit != null && inRangeCells[i].Unit.Player != 1 && inRangeCells[i].Unit.gameObject.tag != "cannon")
            {
                inRangeTargets.Add(inRangeCells[i].Unit);
            }
        }

        //For loop for inrange

        while (inRangeTargets.Count != 0) //runs through all aquired nearby targets
        {
            Debug.Log("Looping");
            currentCluster.Add(inRangeTargets[0]);                                                                                                     //adds first one to current cluster
            inRangeTargets.RemoveAt(0);                                                                                                                //removes that same one from inRangeTargets to prevent overlap
            inRangeCells = MapMaster.CellsWithinArea(currentCluster[0].CurrentHex, 1);                                                                 //gets all cells that are 1 cell within the first target

            for (int i = 0; i < inRangeCells.Count; ++i)                                                                                               //loops through previously aquired list of cells
            {
                if (inRangeCells[i].Unit != null && inRangeCells[i].Unit.gameObject.tag != "cannon" && !currentCluster.Contains(inRangeCells[i].Unit)) //if that cell has a unit that is not in the cluster
                {
                    currentCluster.Add(inRangeCells[i].Unit);                                                                                          //adds it too the current cluster
                    if (inRangeTargets.Contains(inRangeCells[i].Unit))                                                                                 //if that unit was in the inRangeTarget list
                    {
                        inRangeTargets.Remove(inRangeCells[i].Unit);                                                                                   //remove that unit
                    }
                }

                //This loop is similiar to the one above. After it gets one enemy, it checks the cells around it for more
                //Then, after adding a new enemy, it checks the cells around THAT one for even more enemies, countinously adding them to the cluster
                for (int j = 0; j < currentCluster.Count; ++j)
                {
                    List <HexCell> inRangeCellsTwo = MapMaster.CellsWithinArea(currentCluster[j].CurrentHex, 1); //Cells surrounding the unit

                    for (int k = 0; k < inRangeCellsTwo.Count; ++k)
                    {
                        if (inRangeCellsTwo[k].Unit != null && !currentCluster.Contains(inRangeCellsTwo[k].Unit) && inRangeCellsTwo[k].Unit.gameObject.tag != "cannon") //if that cell has a unit that is not in the cluster
                        {
                            currentCluster.Add(inRangeCellsTwo[k].Unit);                                                                                                //adds it too the current cluster
                            if (inRangeTargets.Contains(inRangeCellsTwo[k].Unit))                                                                                       //if that unit was in the inRangeTarget list
                            {
                                inRangeTargets.Remove(inRangeCellsTwo[k].Unit);                                                                                         //remove that unit
                            }
                        }
                    }
                }
            }

            //When the cluster is finished, we compare it to the biggest cluster we've found so far
            if (biggestCluster.Count < currentCluster.Count) //If the current cluster has more enemies than the biggest cluster, we make that cluster the new biggest
            {
                biggestCluster.Clear();
                for (int i = 0; i < currentCluster.Count; ++i)
                {
                    biggestCluster.Add(currentCluster[i]);
                }
            }
            currentCluster.Clear();
        }

        if (biggestCluster.Count == 0)
        {
            Debug.Log("reached 1");
            inRangeTargets.Clear();
            currentCluster.Clear();
            biggestCluster.Clear();

            while (currentCannon < PlayerMaster.CurrentPlayer.Can.Count && PlayerMaster.CurrentPlayer.Can[currentCannon].shots == 0) //Moves through cannons until we are out of available cannons or we find one with enough shots
            {
                ++currentCannon;
            }
            Debug.Log("reached 2");
            if (currentCannon < PlayerMaster.CurrentPlayer.Can.Count && (PlayerMaster.OtherPlayer.Inf.Count > 0 || PlayerMaster.OtherPlayer.Cav.Count > 0)) //If we found a cannon that can be shot
            {
                FindTargets(PlayerMaster.CurrentPlayer.Can[currentCannon]);                                                                                 //Find the targets within it's range and continue from there
            }
            else
            {
                Debug.Log("Reached 3");
                //StartCoroutine(CannonMaster.LeaveCannon()); //If we are out of cannons then zoom back out
                // GameBrain.ChangeTurn();
            }
        }

        else
        {
            StartCoroutine(HandleCannons(can));
        }
        //AimAndShoot(can);
    }
Exemplo n.º 16
0
    public static void DecideMove()
    {
        if (target != null)
        {
            if (Input.GetKey(KeyCode.Mouse0))
            {
                instance.BuildMarker();
            }
            else if (Input.GetKeyUp(KeyCode.Mouse0) && instance.radiusMarker != null)
            {
                instance.timePressed = 0;

                foreach (HexCell h in MapMaster.CellsWithinArea(target.CurrentHex, instance.uSize))
                {
                    Unit u = h.Unit;
                    if (u != null)
                    {
                        if (!u.Moved && u.Player == PlayerMaster.CurrentTurn)
                        {
                            instance.uQue.Add(u);
                        }
                    }
                }

                GameObject.Destroy(instance.radiusMarker);
                instance.radiusMarker = null;
                instance.uSize        = 0;
                // target = null;
            }
        }

        if (Input.GetKeyDown(KeyCode.Mouse0) /*&& Events.GetComponent<Pause>().paused == false*/)
        {
            Ray        ray;
            RaycastHit hit;
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit))
            {
                Unit    u = hit.transform.gameObject.GetComponent <Unit>();
                HexCell h = hit.transform.gameObject.GetComponent <HexCell>();
                //Debug.Log(u == null ? "" : u.UName);

                if (u != null)
                {
                    SetTarget(u);
                }
                else if (h != null)
                {
                    if (instance.uQue.Count == 0)
                    {
                        EvaluateTile(h);
                    }
                    else
                    {
                        if (target != null)
                        {
                            HighlightMaster.ToggleTileHighlights(false, MapMaster.CellsWithinArea(target.CurrentHex, target.MoveSpeed));

                            Vector3 directionVector = HexCell.TravelDirection(target.CurrentHex.Cube, h.Cube);
                            Vector2 endpoint;
                            HexCell goal;

                            List <HexCell> newPath;

                            List <KeyValuePair <Unit, List <HexCell> > > paths = new List <KeyValuePair <Unit, List <HexCell> > >();

                            foreach (Unit un in instance.uQue)
                            {
                                un.CurrentHex.passable = true;
                            }
                            foreach (Unit un in instance.uQue)
                            {
                                endpoint = HexCell.NewLocationInOffset(un.CurrentHex.Cube, directionVector);
                                if (!MapMaster.IsCellOnMap((int)endpoint.y, (int)endpoint.x))
                                {
                                    continue;
                                }
                                goal    = MapMaster.Map[(int)endpoint.y, (int)endpoint.x];
                                newPath = HexStar.GetPath(un.CurrentHex, goal, un.MoveSpeed);
                                if (newPath.Count > 0)
                                {
                                    paths.Add(new KeyValuePair <Unit, List <HexCell> >(un, newPath));
                                }
                                else
                                {
                                    un.CurrentHex.passable = false;
                                }
                            }

                            EvaluateTileGroup(paths);
                            instance.uQue.Clear();
                            if (!target.Moved && target.UName == "Infantry")
                            {
                                target.GetComponentInChildren <Animator>().Play("GoToIdle");
                            }
                            target = null;
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 17
0
    public static void CheckCannonCapture()
    {
        for (int i = 0; i < PlayerMaster.CurrentPlayer.Can.Count; i++)
        {
            List <HexCell> adj   = MapMaster.CellsWithinArea(PlayerMaster.CurrentPlayer.Can[i].CurrentHex, 1);
            int            finfC = 0;
            int            fcalC = 0;
            int            einfC = 0;
            int            ecalC = 0;
            foreach (HexCell h in adj)
            {
                if (h.Unit != null)
                {
                    if (h.Unit.UName == "Infantry")
                    {
                        if (h.Unit.Player == PlayerMaster.CurrentTurn)
                        {
                            finfC++;
                        }

                        if (h.Unit.Player != PlayerMaster.CurrentTurn)
                        {
                            einfC++;
                        }
                    }

                    else if (h.Unit.UName == "Cavalry")
                    {
                        if (h.Unit.Player == PlayerMaster.CurrentTurn)
                        {
                            fcalC++;
                        }

                        if (h.Unit.Player != PlayerMaster.CurrentTurn)
                        {
                            ecalC++;
                        }
                    }
                }
            }

            if (finfC + fcalC == 0 && einfC + ecalC >= Cannon.ActiveThreshold)
            {
                PlayerMaster.CurrentPlayer.Can[i].Shots  = Cannon.Clip;
                PlayerMaster.CurrentPlayer.Can[i].Moved  = false;
                PlayerMaster.CurrentPlayer.Can[i].Manned = true;

                if (ecalC >= Cannon.ActiveThreshold)
                {
                    PlayerMaster.CurrentPlayer.Can[i].SetMove(Cavalry.MaxMoveSpeed);
                }
                else
                {
                    PlayerMaster.CurrentPlayer.Can[i].SetMove(Infantry.MaxMoveSpeed);
                }
                PlayerMaster.CurrentPlayer.Can[i].Player = PlayerMaster.PriorTurn;
                PlayerMaster.OtherPlayer.Can.Add(PlayerMaster.CurrentPlayer.Can[i]);
                ScoreMaster.SwapPoints(UnitTypes.Cannon, PlayerMaster.CurrentTurn);
                PlayerMaster.CurrentPlayer.Can.Remove(PlayerMaster.CurrentPlayer.Can[i]);
                i--;
            }
        }
    }
Exemplo n.º 18
0
    static void Cluster(Unit u) //Clusters Units together into squads. Pushes squads to list for storage and management.
    {
        if (!u.InSquad)         //if unit is not in a squad
        {
            //Make a list to store the squad into and a list to hold the hexes to be searched for units for said squad.
            List <Unit>    squad = new List <Unit>(); //single squad
            List <HexCell> cellsToSearch;             //area looked at for more squad members

            squad.Add(u);                             //adds unit to squad
            u.InSquad = true;
            //Debug.Log("Position of first thing is " + u.transform.position);
            //Use CellsWithinArea to obtain first three units within one move of current unit. Push said units to a new List. Push said List to master list of all current squads.
            for (int i = 1; squad.Count <= 3 && i < 4; ++i)
            {
                cellsToSearch = MapMaster.CellsWithinArea(u.CurrentHex, i);
                foreach (HexCell cell in cellsToSearch)
                {
                    if (cell.Unit != null && cell.Unit.InSquad == false && squad.Count < 4)
                    {
                        squad.Add(cell.Unit);
                        cell.Unit.inSquad = true;
                    }
                }
            }

            SquadMaster.Add(squad);
        }

        #region

        /*if (u.tag == "cannon")
         *  {
         *      switch (count)
         *      {
         *          case 1:
         *              list1 = squad;
         *              break;
         *          case 2:
         *              list2 = squad;
         *              break;
         *          case 3:
         *              list3 = squad;
         *              break;
         *          case 4:
         *              list4 = squad;
         *              break;
         *      }
         ++count;
         *  }
         *
         *
         * //Take the average of all the positions of the units in the squad list and set a cluster goal for them to move to.
         * int avgQ = 0, avgR = 0; //Holds the total for the coordinates of all the hexcells in the squad. Used for calculating the goal position
         *
         * foreach(Unit n in squad)
         * {
         *  avgQ += n.CurrentHex.Q;
         *  avgR += n.CurrentHex.R;
         * }
         * avgQ /= squad.Capacity;
         * avgR /= squad.Capacity;
         *
         * if(MapMaster.Map[avgR, avgQ] != null && MapMaster.Map[avgR, avgQ].Unit == null)
         * {
         *  //pass cell (MapMaster.Map[avgR, avgQ]) as goal to Move() or MoveMaster methods
         * }
         * else
         * {
         *  //Use BestFirst to find a goal cell. H = manhattan distance to middle of enemies DZ.
         *  cellsToSearch.Clear();
         *  cellsToSearch = MapMaster.CellsWithinArea(MapMaster.Map[avgR, avgQ], 10);
         *
         *  foreach(HexCell c in cellsToSearch)
         *  {
         *
         *  }
         * }
         */


        //Use Move() with the cluster goal.

        #endregion
    }