Example #1
0
    public override PriorityPair <T> Remove(PriorityPair <T> data)
    {
        if (Count <= 0 || !Contains(data))
        {
            return(null);
        }

        BinaryCell <PriorityPair <T> > cell = finalCell.LeftCell;

        while (cell.Value != data)
        {
            cell.Index--;
            cell = cell.LeftCell;
        }

        BinaryCell <PriorityPair <T> > leftCell  = cell.LeftCell;
        BinaryCell <PriorityPair <T> > rightCell = cell.RightCell;

        leftCell.RightCell = rightCell;
        rightCell.LeftCell = leftCell;

        cell.RightCell = null;
        cell.LeftCell  = null;
        cell.Index     = 0;

        Count--;

        return(cell.Value);
    }
Example #2
0
    private void Awake()
    {
        construcionsList = new PriorityList <Transform>();

        foreach (GameObject tower in GameObject.FindGameObjectsWithTag("Tower"))
        {
            int towerOrder = tower.GetComponent <TowerAttackOrder>().orderInAttack;

            PriorityPair <Transform> towerPair = new PriorityPair <Transform>(tower.transform, towerOrder);

            construcionsList.Add(towerPair);
        }

        GameObject core           = GameObject.FindGameObjectWithTag("Core");
        int        lastTowerOrder = 0;

        if (construcionsList.GetFirst() != null)
        {
            lastTowerOrder = construcionsList.GetFirst().Priority;
        }

        PriorityPair <Transform> corePair = new PriorityPair <Transform>(core.transform, lastTowerOrder + 1);

        construcionsList.Add(corePair);

        troopData      = GetComponent <RunTimeTroopData>(); //TO:DO adapt to use Troop class with RunTimeData data
        movementAction = GetComponent <TroopMovementActions>();
        attackAction   = GetComponent <TroopAttackActions>();
    }
Example #3
0
    public override PriorityPair <T> Remove(int index)
    {
        if (Count <= 0 || index >= Count || index < 0)
        {
            return(null);
        }

        PriorityPair <T> searchedData = Find(index);

        if (searchedData == null)
        {
            return(null);
        }

        return(Remove(searchedData));
    }
Example #4
0
    public override void Add(PriorityPair <T> data)
    {
        if (Count == 0)
        {
            BinaryCell <PriorityPair <T> > newCell = new BinaryCell <PriorityPair <T> >(Count, data);

            finalCell.LeftCell  = newCell;
            startCell.RightCell = newCell;

            newCell.LeftCell  = startCell;
            newCell.RightCell = finalCell;
        }
        else
        {
            BinaryCell <PriorityPair <T> > newCell    = new BinaryCell <PriorityPair <T> >(Count, data);
            BinaryCell <PriorityPair <T> > graterCell = finalCell.LeftCell;

            while (!(graterCell.Value.CompareTo(newCell.Value) > 0))
            {
                graterCell.Index++;
                graterCell = graterCell.LeftCell;

                if (graterCell == startCell)
                {
                    break;
                }
            }

            BinaryCell <PriorityPair <T> > smallerCell = graterCell.RightCell;

            graterCell.RightCell = newCell;
            smallerCell.LeftCell = newCell;

            newCell.RightCell = smallerCell;
            newCell.LeftCell  = graterCell;

            if (smallerCell != finalCell)
            {
                newCell.Index = smallerCell.Index - 1;
            }
        }

        Count++;
    }
Example #5
0
    public override bool Contains(PriorityPair <T> data)
    {
        if (Count <= 0)
        {
            return(false);
        }

        BinaryCell <PriorityPair <T> > cell = finalCell.LeftCell;

        while (cell.Value != data)
        {
            cell = cell.LeftCell;

            if (cell == startCell)
            {
                return(false);
            }
        }

        return(cell.Value == data);
    }
Example #6
0
    public override int IndexOf(PriorityPair <T> data)
    {
        if (Count <= 0)
        {
            return(-1);
        }

        BinaryCell <PriorityPair <T> > cell = finalCell.LeftCell;

        while (cell.Value != data)
        {
            cell = cell.LeftCell;

            if (cell == startCell)
            {
                return(-1);
            }
        }

        return(cell.Index);
    }
Example #7
0
    public int CompareTo(PriorityPair <T> other)
    {
        if (other == null)
        {
            return(1);
        }

        int otherPriority = other.priority;

        if (Priority > otherPriority)
        {
            return(1);
        }
        else if (Priority < otherPriority)
        {
            return(-1);
        }
        else
        {
            return(0);
        }
    }
Example #8
0
    public void Add(T key, int priority)
    {
        PriorityPair <T> pair = new PriorityPair <T>(key, priority);

        Add(pair);
    }
Example #9
0
    private Transform GetNextCloseTarget()
    {
        PriorityList <string> tagsPriority = new PriorityList <string>();

        foreach (Transform target in closeTargets)
        {
            if (target != null)
            {
                if (!tagsPriority.ContainsKey(target.tag))
                {
                    PriorityPair <string> pair = new PriorityPair <string>(target.tag, GetTargetPriority(target));
                    tagsPriority.Add(pair);
                }
            }
        }

        int    randomNumber = Random.Range(0, TroopPriorityWeight + TowerPriorityWeight + HeroPriorityWeight);
        int    lastPriority = 0;
        string selectedTag  = "";

        for (int i = tagsPriority.Count - 1; i >= 0; i--)
        {
            PriorityPair <string> pair = tagsPriority.Find(i);

            if (i == 0)
            {
                if (randomNumber >= lastPriority)
                {
                    selectedTag = pair.Key;
                }
            }
            else
            {
                if (randomNumber >= lastPriority && randomNumber < pair.Priority)
                {
                    selectedTag = pair.Key;
                    break;
                }
            }

            lastPriority = pair.Priority;
        }

        List <Transform> possibleTargets = new List <Transform>();

        foreach (Transform target in closeTargets)
        {
            if (target != null)
            {
                if (target.tag == selectedTag)
                {
                    possibleTargets.Add(target);
                }
            }
        }

        Transform nextTarget;

        randomNumber = Random.Range(0, possibleTargets.Count - 1);
        nextTarget   = possibleTargets.ToArray()[randomNumber];
        closeTargets.Remove(nextTarget);

        return(nextTarget);
    }