Пример #1
0
    /// <summary>
    /// Creates appropriate graphic for a towers faction.
    /// </summary>
    /// <param name="tower">Tower to validate.</param>
    private static void ValidateTowerFactionGraphic(TowerBehavior tower)
    {
        //var graphic = CreateTowerGraphicForFaction(tower.Faction);
        var rangeGraphic = CreateTowerRangeGraphicForFaction(tower.Faction);

        tower.SetGraphic();
        //if (graphic != null)
        //{
        //    var oldGraphic = tower.ReplaceGraphic(graphic);
        //    if (oldGraphic != null)
        //    {
        //        oldGraphic.SetActive(false);
        //    }
        //    //GameObject.Destroy(oldGraphic);
        //}

        if (rangeGraphic != null)
        {
            var oldRangeGraphic = tower.ReplaceRangeGraphic(rangeGraphic);
            if (oldRangeGraphic != null)
            {
                oldRangeGraphic.SetActive(false);
            }
            //GameObject.Destroy(oldRangeGraphic);
        }
    }
Пример #2
0
    /// <summary>
    /// Converts the tower to faction.
    /// </summary>
    /// <param name="tower">Tower.</param>
    /// <param name="faction">Faction.</param>
    public static void ConvertTowerToFaction(TowerBehavior tower, int faction)
    {
        // Do not convert if tower's faction is same as desired faction
        if (tower.Faction == faction)
        {
            return;
        }

        // Convert tower
        int oldFaction = tower.Faction;

        tower.Faction = faction;

        // Reset and validate graphics
        tower.SetStats();
        ValidateTowerFactionGraphic(tower);

        // Raise event
        if (TowerConverted != null)
        {
            TowerConverted(tower, oldFaction, faction);
        }

        MapControl.TowerControlChangeEventTrigger();
    }
    ///two towers attack stuff
    public bool CanTwoAttack(TowerBehavior destination, TowerBehavior attackingTower, TowerBehavior queueTower,
                             float attackPercentage, out float queuePercent)
    {
        queuePercent = 0;
        if (queueTower.StationedUnits <= 0)
        {
            return(false);
        }


        int unitsLeft;

        int numOriginalAttacking = Mathf.CeilToInt(attackingTower.StationedUnits * attackPercentage);
        int maxAdditionAttackers = queueTower.StationedUnits;

        if (SimulateAttackUtil.SimulateAttack((numOriginalAttacking + maxAdditionAttackers),
                                              destination, attackingTower.Faction, out unitsLeft) == false)
        {
            //incase for overload attack
            maxAdditionAttackers -= unitsLeft - 1;
            queuePercent          = AIConstants.RoundPercentToClosestOption((float)maxAdditionAttackers / (float)queueTower.StationedUnits);
            return(false);
        }

        maxAdditionAttackers -= unitsLeft - 1;
        queuePercent          = AIConstants.RoundPercentToClosestOption((float)maxAdditionAttackers / (float)queueTower.StationedUnits);

        if (SimulateAttackUtil.DistanceCheck(queueTower, destination) == false)
        {
            return(false);
        }


        return(true);
    }
Пример #4
0
    private void TryPlaceBuilding(Vector2Int pos)
    {
        bool isBuildingPlacable = true;

        foreach (Vector2Int occupyingLocation in currentTower.occupyingLocations)
        {
            if (Grid.GetCell(pos + occupyingLocation).occupied)
            {
                isBuildingPlacable = false;
            }
        }
        if (!isBuildingPlacable)
        {
            return;
        }
        else //if building is placable
        {
            foreach (Vector2Int occupyingLocation in currentTower.occupyingLocations)
            {
                Grid.GetCell(pos + occupyingLocation).occupied = true;
            }

            // Transfer control to a new tower
            foreach (var tower in new List <TowerBehavior>(GameManager.Instance.controlledTowers))
            {
                tower.LoseControl();
            }

            currentTower.Build();
            currentTower = null;
            LastUiAnimator.DeselectButton();
        }
    }
Пример #5
0
    /// <summary>
    /// Creates a virtual button to represent pressing on the tower. Button is linked to tower.
    /// </summary>
    /// <param name="tower">Tower to create button for.</param>
    public static void CreateTowerButtonForTower(TowerBehavior tower)
    {
        Vector3 screenPoint = Camera.main.WorldToScreenPoint(tower.transform.position);
        var     towerBtn    = GameObject.Instantiate(current.TowerButtonFab) as GameObject;

        towerBtn.transform.position = screenPoint;
        towerBtn.transform.SetParent(current.TowerButtonParent.transform);
        var behavior = towerBtn.GetComponent <TowerButtonBehavior>();

        if (behavior == null)
        {
            Debug.LogError("TowerButtonFab of UIController does not have TowerButtonBehavior attached.");
            return;
        }
        behavior.Tower = tower;
        float camHeight = Camera.main.orthographicSize * 2;
        //Debug.Log("Cam Height: " + camHeight);
        //Debug.Log("Screen Height: " + Screen.height);
        float towerGraphicSize = tower.GraphicObj.GetComponent <SpriteRenderer>().bounds.size.x;
        //Debug.Log("GraphicSize: " + towerGraphicSize);
        float buttonSizeRatio = towerGraphicSize / camHeight;
        //Debug.Log("Scale Factor: " + Scaler.scaleFactor);
        float buttonPixelSize = Screen.height * buttonSizeRatio * (Scaler.referenceResolution.y / Screen.height);

        behavior.Size = buttonPixelSize;
        current.buttons.Add(behavior);

        // Visual representation; mainly for testing and debugging
        // Resize to 2 times TowerButtonRadius because the sprite scale ends up half size when using raw radius.
        var rectTrans = behavior.GetComponent <RectTransform>();

        rectTrans.localScale = new Vector3(1, 1, 1);
        rectTrans.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, behavior.Size);
        rectTrans.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, behavior.Size);
    }
Пример #6
0
    void Update()
    {
        if (selected_tower != null)
        {
            Vector3 mouse = GameObject.FindObjectOfType <Camera>().ScreenToWorldPoint(Input.mousePosition);
            selected_tower.transform.position = new Vector3(mouse.x, mouse.y, 11f);

            if (Input.GetMouseButtonDown(0))
            {
                if (gold >= selected_tower.cost)
                {
                    RemoveGold(selected_tower.cost);
                    selected_tower.Place();
                    selected_tower = null;
                }
                else
                {
                }
            }
            if (Input.GetMouseButtonDown(1))
            {
                GameObject.Destroy(selected_tower.gameObject);
                selected_tower = null;
            }
        }
    }
Пример #7
0
    /// <summary>
    /// Returns true if the from's units can reach the to tower
    /// </summary>
    public static bool DistanceCheck(TowerBehavior from, TowerBehavior to)
    {
        float   timeAlive = Game.TowerInfo.DefaultUnitKillTime;
        Vector3 destinationFinalPos;

        if (to.GetComponent <OrbitMotion>() != null)
        {
            destinationFinalPos = to.Orbit.CalculatePositionWithMoreUpTime(timeAlive);
        }

        else
        {
            destinationFinalPos = to.transform.position;
        }

        float distance = (to.transform.position - from.transform.position).magnitude;

        float maxDistanceTravel = timeAlive * FactionController.GetSpeedForFaction(from.Faction);

        if (maxDistanceTravel < distance)
        {
            return(false);
        }

        return(true);
    }
Пример #8
0
    private void TowerHasChangedFaction(TowerBehavior tower, int oldFaction, int newFaction)
    {
        //Add 1 to the faction that gained a tower
        if (newFaction == FactionController.PlayerFaction)
        {
            playerGameStateInfo.numTowers++;
        }
        else if (newFaction == FactionController.OtherFaction1)
        {
            AIGameStateInfo.numTowers++;
        }

        //subtract 1 from the faction that lost a tower
        if (oldFaction == FactionController.PlayerFaction)
        {
            playerGameStateInfo.numTowers--;
            if (tower.IsUpgraded)
            {
                playerGameStateInfo.numUpgrades--;
                AIGameStateInfo.numUpgrades++;
            }
        }
        else if (oldFaction == FactionController.OtherFaction1)
        {
            AIGameStateInfo.numTowers--;
            if (tower.IsUpgraded)
            {
                AIGameStateInfo.numUpgrades--;
                playerGameStateInfo.numUpgrades++;
            }
        }
    }
Пример #9
0
 /// <summary>
 /// Creates UnitPrepMotor with given UnitBehavior.
 /// </summary>
 /// <param name="unit"></param>
 public UnitPrepMotor(UnitBehavior unit)
 {
     this.unit   = unit;
     originTower = null;
     animTime    = Random.Range(.5f, 1.0f);
     deactivate  = false;
 }
    private bool HaveEnoughSoldiers(TowerBehavior AI, TowerBehavior destination, ref float percent)
    {
        //if the Ai doesn't have more units than the destination in general
        //then we can't take over the tower so there is no need to do more calculations
        if (AI.StationedUnits <= destination.StationedUnits)
        {
            percent = AIConstants.RoundPercentToClosestOption(((float)AI.StationedUnits / (float)AI.StationedUnits));
            return(false);
        }

        int AIunits = AI.StationedUnits;

        //how many Units are left after the attack
        int AIunitsLeft;

        //did the "attack" succeed?
        if (SimulateAttackUtil.SimulateAttack(AIunits, destination, AI.Faction, out AIunitsLeft) == false)
        {
            percent = AIConstants.RoundPercentToClosestOption((float)AIunits / (float)AI.StationedUnits);
            return(false);
        }


        AIunits -= AIunitsLeft;
        AIunits += 1;
        percent  = AIConstants.RoundPercentToClosestOption((float)AIunits / (float)AI.StationedUnits);
        return(true);
    }
Пример #11
0
    // Use this for initialization
    void Start()
    {
        tower     = GetComponent <TowerBehavior> ();
        rigidbody = GetComponent <Rigidbody> ();

        UIController.MarkTowerAsDynamic(tower);
    }
Пример #12
0
 // Use this for initialization
 void Start()
 {
     tower = GetComponent <TowerBehavior>();
     if (tower == null)
     {
         Debug.LogError("There is no TowerBehavior attached alongside ResourceGenerator.");
     }
 }
Пример #13
0
 public MovedUnitsInfo(TowerBehavior from, TowerBehavior to, int numberOfUnits)
 {
     From          = from;
     To            = to;
     FromFaction   = from.Faction;
     ToFaction     = to.Faction;
     NumberOfUnits = numberOfUnits;
 }
Пример #14
0
    public TowerBehavior SpawnTower(TowerBehavior prefab)
    {
        TowerBehavior tower = Instantiate(prefab, towerBase);

        tower.transform.localPosition = Vector3.zero;
        this.tower = tower;
        return(tower);
    }
Пример #15
0
 public void SetSelectedTower(TowerBehavior tower)
 {
     if (tower == null && selected_tower != null)
     {
         GameObject.Destroy(selected_tower.gameObject);
     }
     selected_tower = tower;
 }
Пример #16
0
    void OnClick()
    {
        TowerBehavior t = GameObject.Instantiate(select_tower);

        t.Init(tower, true);
        player.SetSelectedTower(null);
        player.SetSelectedTower(t);
    }
Пример #17
0
 public void SellTower(TowerBehavior selectedTower)
 {
     player.CollectCurrency(selectedTower.GetTowerValue() / 2);
     // deselect tower and deactivate tower-UI
     buildManager.DeselectTower();
     buildManager.DecreasePlayerTowers();
     Destroy(selectedTower.gameObject);
 }
Пример #18
0
    /// <summary>
    /// Calls Prepare method on all units in this group.
    /// </summary>
    /// <param name="origin"></param>
    public void PrepareUnits(TowerBehavior origin)
    {
        var units = UnitController.GetUnitsForGroup(ID);

        foreach (var u in units)
        {
            u.Prepare(origin);
        }
    }
Пример #19
0
    //function that is called for a tower that is going to attack, but is waiting on an AI to send units to it
    public IEnumerator StartOverloadAttack(TowerBehavior destination, float percent, float time)
    {
        yield return(new WaitForSeconds(time));

        UnitGroup currentGroup = UnitController.CreateUnitGroupForFaction(myTower.Faction, (int)(myTower.StationedUnits * percent));

        currentGroup.PrepareUnits(myTower);
        myCoroutine = this.WaittoAttack(currentGroup, destination, 1f);
        AIController.CallCoroutine(myCoroutine);
    }
Пример #20
0
    void SelectTower(TowerBehavior _selectedTower)
    {
        selectedTower = _selectedTower;

        rangeDrawer.Draw(selectedTower.transform.position, selectedTower.param.range, 50);
        GameManager.instance.interfaceManager.towerInterface.ShowTowerInformation
        (
            selectedTower,
            new Vector2(Screen.width / 10, Screen.height / 2)
        );
    }
Пример #21
0
 /// <summary>
 /// Starts process of unit moving toward a destination.
 /// </summary>
 /// <param name="destination">Destination tower to move toward.</param>
 public void MoveTo(TowerBehavior destination)
 {
     faction          = origin.Faction;
     this.destination = destination;
     orbitCounter     = 0;
     //prepMotor.Reset();
     //moveMotor.SetDestination(destination);
     //curMotor = moveMotor;
     attacking = true;
     StartRoutine(Attack());
 }
Пример #22
0
    // Replaced MoveUnits
    /// <summary>
    /// Sets a unit group's unit's to move toward a destination tower from an origin tower.
    /// </summary>
    /// <param name="group">Unit group to move.</param>
    /// <param name="origin">Tower were group should move from.</param>
    /// <param name="destination">Tower were group will move toward.</param>
    public static void MoveGroupFromTo(UnitGroup group, TowerBehavior origin, TowerBehavior destination)
    {
        origin.stationedGroup.SubtractUnits(group.UnitCount);
        group.MoveUnits(destination);
        var info = new MovedUnitsInfo(origin, destination, group.UnitCount);

        if (UnitsMoved != null)
        {
            UnitsMoved(info);
        }
    }
Пример #23
0
    //private void InitializeData() { }

    private void InitializeScripts()
    {
        Data       = GetComponent <TowerData>();
        TBehavior  = GetComponent <TowerBehavior>();
        Appearance = GetComponentInChildren <TowerAppearance>();
        Spawner    = GetComponent <UnitSpawner>();
        Circle     = GetComponentInChildren <SelectionCircle>();
        Particles  = GetComponentInChildren <TowerParticles>();
        FWTrigger  = GetComponentInChildren <FirewallTrigger>();
        UI         = GetComponentInChildren <TowerUI>();
    }
Пример #24
0
    private void DamageTower(ExplosionDamageData e, TowerBehavior t)
    {
        //Debug.Log("Tower " + t.Index);
        float reduction = GetDamageReductionForIndex(t.Index);
        //Debug.Log("Reduction Percent: " + reduction);
        float netPercent = Mathf.Clamp01(e.PercentOfUnitsDestroyed - reduction);
        //Debug.Log("Net Damage: " + netPercent);
        int numUnitsKilled = (int)(t.StationedUnits * netPercent);

        t.StationedGroup.SubtractUnits(numUnitsKilled);
        //Debug.Log("Lost " + numUnitsKilled + " units.");
    }
Пример #25
0
 private void TowerUpgraded(TowerBehavior tower, TowerUpgrade upgrade)
 {
     //Add 1 to the faction that gained a tower
     if (tower.Faction == FactionController.PlayerFaction)
     {
         playerGameStateInfo.numUpgrades++;
     }
     else if (tower.Faction == FactionController.OtherFaction1)
     {
         AIGameStateInfo.numUpgrades++;
     }
 }
Пример #26
0
    // Public Functions

    // Check if the tower can be upgraded
    public void UpgradeTowerUI(TowerBehavior selectedTower)
    {
        if (player.GetCurrentAmountOfCurrency() >= selectedTower.GetTowerValue())
        {
            player.DecreaseCurrency(selectedTower.GetTowerValue());
            selectedTower.UpgradeTower();
        }
        else
        {
            gameUI.UpdateUserMessage("Oh no! It looks you don't have enough money left. Damn capitalism!");
        }
    }
Пример #27
0
    public void Drive()
    {
        upTime += Time.deltaTime;
        float frac = Mathf.Clamp01(upTime / animTime);
        float x    = frac * (Mathf.PI / 2);

        unit.transform.position = Vector3.Lerp(origin, destination, Mathf.Sin(x)) + originTower.transform.position;
        if (Vector3.Distance(unit.transform.position, originTower.transform.position) <= .1f && deactivate)
        {
            unit.ImpactKill();
            originTower = null;
        }
    }
Пример #28
0
 public void TryToBuildTower(TileInfo tile)
 {
     if (towerToBuild != null && towerToBuild.BaseCost <= GameManager.Instance.mapManager.gold && !tile.IsOccupied)
     {
         tile.Occupant = Instantiate(towerToBuild.BaseTower, tile.gameObject.transform.position + (Vector3.up * tile.BaseTileInfo.Y), Quaternion.Euler(0f, 0f, 0f));
         TowerBehavior towerBehavior = tile.Occupant.GetComponent <TowerBehavior>();
         if (towerBehavior != null)
         {
             towerBehavior.SetInitialValues(towerToBuild, tile.gameObject);
         }
         GameManager.Instance.mapManager.gold -= towerToBuild.BaseCost;
     }
 }
Пример #29
0
 /// <summary>
 /// Prepares unit for moving to a destination tower.
 /// </summary>
 /// <param name="origin"></param>
 public void Prepare(TowerBehavior origin)
 {
     this.origin = origin;
     attacking   = false;
     gameObject.SetActive(true);
     GraphicObject.SetActive(true);
     GraphicObject.transform.localScale = Vector3.one;
     collider.enabled = true;
     orbitCounter     = 1;
     //prepMotor.SetOrigin(origin);
     //curMotor = prepMotor;
     StartRoutine(Prep());
 }
    //This is the function the AI calls to see if it should attack the chosen tower
    public bool ShouldAIAttack(TowerBehavior AI, TowerBehavior destination, out float percentage,
                               out AIConstants.ReasonFailed reason)
    {
        float chanceToAttackAnyway = AIGameStateManager.PercentToAttackAnyway;

        //setting our out variables(must be garunteed set before the function returns)
        reason     = AIConstants.ReasonFailed.None;
        percentage = 0;

        if (AI.StationedUnits <= 0)
        {
            //percentage is already set to zero
            reason = AIConstants.ReasonFailed.NotEnoughtoSend;
            return(false);
        }

        //random chance to attack anyway
        int randomNum = Random.Range(0, 101);

        if (randomNum <= chanceToAttackAnyway)
        {
            percentage = AIConstants.RoundPercentToClosestOption(((float)AI.StationedUnits / (float)AI.StationedUnits));
            return(true);
        }

        else
        {
            bool cantravel    = SimulateAttackUtil.DistanceCheck(AI, destination);
            bool enoughtUnits = HaveEnoughSoldiers(AI, destination, ref percentage);
            //if distance is ever a factor, than the we don't need to adjust the percentage
            if (enoughtUnits == false && cantravel == false)
            {
                reason = AIConstants.ReasonFailed.Both;
                return(false);
            }
            else if (enoughtUnits == false)
            {
                reason = AIConstants.ReasonFailed.Units;
                return(false);
            }

            else if (cantravel == false)
            {
                reason = AIConstants.ReasonFailed.Distance;
                return(false);
            }
        }


        return(true);
    }
 private PEASANTCLASS setClass(TowerBehavior tower)
 {
     switch(tower.getTowerType()){
     case TowerBehavior.TOWERTYPE.MAGICTOWER:
         return PEASANTCLASS.MAGIC;
         break;
     case TowerBehavior.TOWERTYPE.RANGEDTOWER:
         return PEASANTCLASS.RANGED;
         break;
     case TowerBehavior.TOWERTYPE.MELEETOWER:
         return PEASANTCLASS.MELEE;
         break;
     }
     return PEASANTCLASS.PEASANT;
 }
 private bool invadeTower(TowerBehavior tower)
 {
     if (tower.getTowerOwner() == TowerBehavior.TOWEROWNER.PEASANT)
     {
         if (Vector3.Distance(transform.position, tower.getExactTowerLoc()) < convertDistance)
         {
             myClass = setClass(tower);
             setColor();
             targetPos = middleEarth;
         }
         else
         {
             targetPos = tower.getExactTowerLoc();
         }
         return true;
     }
     return false;
 }
    // Use this for initialization
    void Start()
    {
        mMainCamera = Camera.main;

        myClass = PEASANTCLASS.PEASANT;
        MeleeTower = GameObject.Find("MeleeTower");
        RangedTower = GameObject.Find("RangedTower");
        MagicTower = GameObject.Find("MagicTower");
        //		EliteTower = GameObject.Find("EliteTower");
        //		BossTower = GameObject.Find("BossTower");
        HealTower = GameObject.Find("HealTower");

        MeleeTowerBehavior = MeleeTower.GetComponent<TowerBehavior>();
        RangedTowerBehavior = RangedTower.GetComponent<TowerBehavior>();
        MagicTowerBehavior = MagicTower.GetComponent<TowerBehavior>();
        //		EliteTowerBehavior = EliteTower.GetComponent<TowerBehavior>();
        //		BossTowerBehavior = BossTower.GetComponent<TowerBehavior>();
        UpdateWorldWindowBound();
        transform.position = new Vector3((float)(mWorldMin.x + mWorldMax.x),
                                         mWorldMin.y,0f);
        //transform.up = middleEarth - transform.position;
    }
    // Use this for initialization
    void Start()
    {
        mMainCamera = Camera.main;

        myClass = PEASANTCLASS.PEASANT;
        MeleeTower = GameObject.Find("MeleeTower");
        RangedTower = GameObject.Find("RangedTower");
        MagicTower = GameObject.Find("MagicTower");
        //		EliteTower = GameObject.Find("EliteTower");
        //		BossTower = GameObject.Find("BossTower");
        HealTower = GameObject.Find("HealTower");

        MeleeTowerBehavior = MeleeTower.GetComponent<TowerBehavior>();
        RangedTowerBehavior = RangedTower.GetComponent<TowerBehavior>();
        MagicTowerBehavior = MagicTower.GetComponent<TowerBehavior>();
        //		EliteTowerBehavior = EliteTower.GetComponent<TowerBehavior>();
        //		BossTowerBehavior = BossTower.GetComponent<TowerBehavior>();
        UpdateWorldWindowBound();
        transform.position = new Vector3((float)(mWorldMin.x + mWorldMax.x),
                                         mWorldMin.y,0f);

        float myTarget = Random.value;
        if (myTarget < 0.3f)
            targetPos = MeleeTowerBehavior.getExactTowerLoc();
        else if (myTarget < 0.6f)
            targetPos = RangedTowerBehavior.getExactTowerLoc();
        else
            targetPos = MagicTowerBehavior.getExactTowerLoc();

        targetPos.z = 0f;
    }