Exemplo n.º 1
0
        private void SetTargetModeHoveredTile(Tile tile)
        {
            ClearTargetModeHoveredTile();

            if (targetModeTileList.Count > 0 && !targetModeTileList.Contains(tile))
            {
                return;
            }

            targetModeHoveredTileList = new List <Tile>();
            if (targetModeAOE > 0)
            {
                targetModeHoveredTileList = GridManager.GetTilesWithinDistance(tile, targetModeAOE);
            }
            if (!targetModeHoveredTileList.Contains(tile))
            {
                targetModeHoveredTileList.Add(tile);
            }

            OverlayManager.ShowAbilityTargetIndicator(targetModeHoveredTileList);
        }
Exemplo n.º 2
0
        //analyse the grid to know where the unit should move to
        private Tile Analyse(Unit unit, _AIMode activeMode)
        {
            //get all wakable tiles in range first
            List <Tile> walkableTilesInRange = GridManager.GetTilesWithinDistance(unit.tile, unit.GetEffectiveMoveRange(), true);

            walkableTilesInRange.Add(unit.tile);

            //get all visible hostile
            List <Unit> allHostileInSight = FactionManager.GetAllHostileUnit(unit.factionID);

            if (GameControl.EnableFogOfWar())
            {
                for (int i = 0; i < allHostileInSight.Count; i++)
                {
                    if (!FogOfWar.IsTileVisibleToFaction(allHostileInSight[i].tile, unit.factionID))
                    {
                        allHostileInSight.RemoveAt(i);  i -= 1;
                    }
                }
            }

            //if cover system is in used
            if (GameControl.EnableCover())
            {
                Tile tile = AnalyseCoverSystem(unit, walkableTilesInRange, allHostileInSight);
                if (tile != null)
                {
                    return(tile);
                }
            }


            //if there are hostile
            if (allHostileInSight.Count > 0)
            {
                //fill up the walkableTilesInRange hostile list
                //then filter thru walkableTilesInRange, those that have a hostile in range will be add to a tilesWithHostileInRange
                List <Tile> tilesWithHostileInRange = new List <Tile>();
                GridManager.SetupHostileInRangeforTile(unit, walkableTilesInRange);
                for (int i = 0; i < walkableTilesInRange.Count; i++)
                {
                    if (walkableTilesInRange[i].GetHostileInRange().Count > 0)
                    {
                        tilesWithHostileInRange.Add(walkableTilesInRange[i]);
                    }
                }

                //if the tilesWithHostileInRange is not empty after the process, means there's tiles which the unit can move into and attack
                //return one of those in the tilesWithHostileInRange so the unit can attack
                if (tilesWithHostileInRange.Count > 0)
                {
                    //if the unit current tile is one of those tiles with hostile, just stay put and attack
                    if (tilesWithHostileInRange.Contains(unit.tile))
                    {
                        //randomize it a bit so the unit do move around but not stay in place all the time
                        if (Random.Range(0f, 1f) > 0.25f)
                        {
                            return(unit.tile);
                        }
                    }
                    return(tilesWithHostileInRange[Random.Range(0, tilesWithHostileInRange.Count)]);
                }
            }


            //if there's not potential target at all, check if the unit has any previous attacker
            //if there are, go after the last attacker
            if (unit.lastAttacker != null)
            {
                return(unit.lastAttacker.tile);
            }


            //for aggresive mode with FogOfWar disabled, try move towards the nearest unit
            if (activeMode == _AIMode.Aggressive && Random.Range(0f, 1f) > 0.25f)
            {
                List <Unit> allHostile = FactionManager.GetAllHostileUnit(unit.factionID);
                float       nearest = Mathf.Infinity;   int nearestIndex = 0;
                for (int i = 0; i < allHostile.Count; i++)
                {
                    float dist = GridManager.GetDistance(allHostile[i].tile, unit.tile);
                    if (dist < nearest)
                    {
                        nearest      = dist;
                        nearestIndex = i;
                    }
                }
                return(allHostile[nearestIndex].tile);
            }


            //if there's really no hostile to go after, then just move randomly in one of the walkable
            int rand = Random.Range(0, walkableTilesInRange.Count);

            //clear in hostileInRange list for all moveable tile so, just in case the list is not empty (hostileInRange dont clear after each move)
            //so the unit dont try to attack anything when it moves into the targetTile
            walkableTilesInRange[rand].SetHostileInRange(new List <Tile>());

            return(walkableTilesInRange[rand]);
        }
Exemplo n.º 3
0
        private bool movingUnit = false;                //set to true when a unit is being moved
        IEnumerator MoveUnitRoutine(Unit unit, _AIMode activeMode)
        {
            TBTK.OnUnitSelected(unit);

            movingUnit = true;
            //Debug.Log("moving unit");
            if (activeMode != _AIMode.Aggressive && !unit.trigger)
            {
                AIDebug("unit " + unit.gameObject.name + " is not triggered");
                if (!untriggeredUnitMove)
                {
                    StartCoroutine(EndMoveUnitRoutine());
                    unit.moveRemain = 0;      unit.attackRemain = 0;
                }
                else
                {
                    if (Random.value < 0.5f)
                    {
                        StartCoroutine(EndMoveUnitRoutine());
                        unit.moveRemain = 0;      unit.attackRemain = 0;
                    }
                    else
                    {
                        AIDebug("Randomly move unit " + unit.gameObject.name + " anyway");
                        List <Tile> walkableTilesInRange = GridManager.GetTilesWithinDistance(unit.tile, Mathf.Min(1, unit.GetEffectiveMoveRange() / 2), true);
                        if (walkableTilesInRange.Count > 0)
                        {
                            unit.Move(walkableTilesInRange[Random.Range(0, walkableTilesInRange.Count)]);
                        }
                    }
                }
                AIDebug("End unit " + unit.gameObject.name + " turn");
                StartCoroutine(EndMoveUnitRoutine());
                yield break;
            }

            Tile targetTile = Analyse(unit, activeMode);


            if (CameraControl.CenterOnSelectedUnit())
            {
                bool visible = false;
                if (unit.tile.IsVisible())
                {
                    visible = true;
                }
                else if (targetTile != unit.tile)
                {
                    List <Tile> path = unit.GetPathForAI(targetTile);
                    for (int i = 0; i < path.Count; i++)
                    {
                        if (path[i].IsVisible())
                        {
                            visible = true;
                            break;
                        }
                    }
                    targetTile = path[path.Count - 1];
                    Debug.DrawLine(unit.tile.GetPos(), targetTile.GetPos(), Color.red, 2);
                }

                if (visible)
                {
                    CameraControl.OnUnitSelected(unit, false);
                    while (CameraControl.IsLerping())
                    {
                        yield return(null);
                    }
                }
            }


            //first move to the targetTile
            if (targetTile != unit.tile)
            {
                unit.Move(targetTile);
                yield return(new WaitForSeconds(.1f));                  //wait until the unit has moved into the targetTile

                while (!TurnControl.ClearToProceed())
                {
                    //AIDebug("waiting, unit "+unit.gameObject.name+" is moving");
                    AIDebug("waiting while unit is moving");
                    yield return(null);
                }
            }

            if (unit == null || unit.HP <= 0)           //in case unit is destroyed by overwatch
            {
                StartCoroutine(EndMoveUnitRoutine());
                yield break;
            }

            for (int i = 0; i < targetTile.hostileInRangeList.Count; i++)
            {
                if (targetTile.hostileInRangeList[i].unit == null || targetTile.hostileInRangeList[i].unit.factionID == unit.factionID)
                {
                    targetTile.hostileInRangeList.RemoveAt(i);              i -= 1;
                }
            }

            //if there's hostile within range, attack it
            if (targetTile.hostileInRangeList.Count > 0)
            {
                if (unit.CanAttack())
                {
                    AIDebug("waiting, unit " + unit.gameObject.name + " is attacking");

                    //~ if(targetTile!=unit.tile){	//wait until the unit has moved into the targetTile
                    //~ yield return new WaitForSeconds(.25f);
                    //~ while(!TurnControl.ClearToProceed()) yield return null;
                    //~ }

                    int rand = Random.Range(0, targetTile.hostileInRangeList.Count);
                    unit.Attack(targetTile.hostileInRangeList[rand].unit);
                }
                else
                {
                    if (unit.moveRemain > 0)
                    {
                        unit.moveRemain -= 1;
                    }
                }
            }
            else
            {
                if (unit.moveRemain <= 0)
                {
                    unit.attackRemain = 0;
                }
            }

            AIDebug("End unit " + unit.gameObject.name + " turn");
            StartCoroutine(EndMoveUnitRoutine());

            yield return(null);
        }
Exemplo n.º 4
0
        //~ public void ActivateTargetModeUnit(Tile tile, int range, int AOE, bool normalAttack, bool requireDirectLOS, _TargetType type, TargetModeCallBack sCallBack, TargetModeCallBack eCallBack){
        public void ActivateTargetModeUnit(Tile tile, UnitAbility ability, int abIndex, TargetModeCallBack sCallBack, ExitTargetModeCallBack eCallBack)
        {
            TBTK.OnUnitABTargetMode(abIndex);
            _ActivateTargetMode(abIndex, ability.GetAOERange(), ability.targetType, sCallBack, eCallBack);

            if (!ability.AttackInLine())
            {
                if (!ability.normalAttack)
                {
                    if (targetModeType == _TargetType.EmptyTile)
                    {
                        targetModeTileList = GridManager.GetTilesWithinDistance(tile, ability.GetRange(), true);
                    }
                    else
                    {
                        targetModeTileList = GridManager.GetTilesWithinDistance(tile, ability.GetRange());
                    }
                }
                else
                {
                    targetModeTileList = new List <Tile>();
                    List <Tile> tilesInRangeList = GridManager.GetTilesWithinDistance(tile, ability.GetRange());

                    int         sight = tile.unit.GetSight();
                    List <Unit> allFriendlyUnitList = FactionManager.GetAllUnitsOfFaction(tile.unit.factionID);

                    for (int i = 0; i < tilesInRangeList.Count; i++)
                    {
                        Tile targetTile = tilesInRangeList[i];

                        if (!GameControl.EnableFogOfWar() && !GameControl.AttackThroughObstacle())
                        {
                            if (!FogOfWar.InLOS(tile, targetTile, 0))
                            {
                                continue;
                            }
                        }

                        bool inSight = GameControl.EnableFogOfWar() ? false : true;
                        if (GameControl.EnableFogOfWar())
                        {
                            if (FogOfWar.InLOS(tile, targetTile) && GridManager.GetDistance(tile, targetTile) <= sight)
                            {
                                inSight = true;
                            }
                            else if (!ability.requireDirectLOS)
                            {
                                for (int n = 0; n < allFriendlyUnitList.Count; n++)
                                {
                                    if (allFriendlyUnitList[n] == tile.unit)
                                    {
                                        continue;
                                    }
                                    if (GridManager.GetDistance(allFriendlyUnitList[n].tile, targetTile) > allFriendlyUnitList[n].GetSight())
                                    {
                                        continue;
                                    }
                                    if (FogOfWar.InLOS(allFriendlyUnitList[n].tile, targetTile))
                                    {
                                        inSight = true;
                                        break;
                                    }
                                }
                            }
                        }

                        if (inSight)
                        {
                            targetModeTileList.Add(targetTile);
                        }
                    }
                }
            }
            else
            {
                /*
                 * List<Tile> neighbourList=tile.GetNeighbourList();
                 * for(int i=0; i<neighbourList.Count; i++){
                 *      bool walkableOnly=(ability.type==UnitAbility._AbilityType.ChargeAttack);
                 *      List<Tile> tileList=GridManager.GetTilesInALine(tile, neighbourList[i], ability.GetRange(), walkableOnly);
                 *
                 *      if(tileList.Count>0){
                 *              if(targetModeType!=_TargetType.EmptyTile) targetModeTileList.Add(tileList[tileList.Count-1]);
                 *              else if (tileList[tileList.Count-1].unit==null) targetModeTileList.Add(tileList[tileList.Count-1]);
                 *      }
                 * }
                 */
            }

            //for(int i=0; i<targetModeTileList.Count; i++) targetModeTileList[i].SetState(_TileState.Range);
            OverlayManager.ShowAbilityRangeIndicator(targetModeTileList);
        }
Exemplo n.º 5
0
        IEnumerator _ApplyAbilityEffect(Tile targetTile, Ability ability, int type, Unit srcUnit = null, float critMult = 1.0f)
        {
            if (targetTile != null && ability.effectObject != null)
            {
                if (!ability.autoDestroyEffect)
                {
                    ObjectPoolManager.Spawn(ability.effectObject, targetTile.GetPos(), Quaternion.identity);
                }
                else
                {
                    ObjectPoolManager.Spawn(ability.effectObject, targetTile.GetPos(), Quaternion.identity, ability.effectObjectDuration);
                }
            }

            if (!ability.useDefaultEffect)
            {
                yield break;
            }
            if (ability.effectDelayDuration > 0)
            {
                yield return(new WaitForSeconds(ability.effectDelayDuration));
            }

            if (type == 1)              //_AbilityType.Generic

            {
                List <Tile> tileList = new List <Tile>();
                if (targetTile != null)
                {
                    if (ability.aoeRange > 0)
                    {
                        tileList = GridManager.GetTilesWithinDistance(targetTile, ability.aoeRange);
                    }
                    tileList.Add(targetTile);
                }
                else
                {
                    if (ability.effTargetType == _EffectTargetType.Tile)
                    {
                        tileList = GridManager.GetTileList();
                    }
                    else
                    {
                        List <Unit> unitList = FactionManager.GetAllUnit();
                        for (int i = 0; i < unitList.Count; i++)
                        {
                            tileList.Add(unitList[i].tile);
                        }
                    }
                }

                if (ability.effTargetType == _EffectTargetType.AllUnit)
                {
                    for (int i = 0; i < tileList.Count; i++)
                    {
                        if (tileList[i].unit == null)
                        {
                            continue;
                        }
                        tileList[i].unit.ApplyEffect(ability.CloneEffect(critMult), srcUnit);
                        SpawnEffectObjTarget(ability, tileList[i]);
                    }
                }
                else if (ability.effTargetType == _EffectTargetType.HostileUnit)
                {
                    for (int i = 0; i < tileList.Count; i++)
                    {
                        if (tileList[i].unit == null)
                        {
                            continue;
                        }
                        if (tileList[i].unit.factionID == ability.factionID)
                        {
                            continue;
                        }
                        tileList[i].unit.ApplyEffect(ability.CloneEffect(critMult), srcUnit);
                        SpawnEffectObjTarget(ability, tileList[i]);
                    }
                }
                else if (ability.effTargetType == _EffectTargetType.FriendlyUnit)
                {
                    for (int i = 0; i < tileList.Count; i++)
                    {
                        if (tileList[i].unit == null)
                        {
                            continue;
                        }
                        if (tileList[i].unit.factionID != ability.factionID)
                        {
                            continue;
                        }
                        tileList[i].unit.ApplyEffect(ability.CloneEffect(critMult), srcUnit);
                        SpawnEffectObjTarget(ability, tileList[i]);
                    }
                }
                else if (ability.effTargetType == _EffectTargetType.Tile)
                {
                    for (int i = 0; i < tileList.Count; i++)
                    {
                        tileList[i].ApplyEffect(ability.CloneEffect(critMult));
                        SpawnEffectObjTarget(ability, tileList[i]);
                    }
                }
            }
            else if (type == 2)                 //_AbilityType.SpawnNew
            {
                Quaternion rot     = srcUnit != null ? srcUnit.thisT.rotation : Quaternion.identity;
                GameObject unitObj = (GameObject)Instantiate(ability.spawnUnit.gameObject, targetTile.GetPos(), rot);
                Unit       unit    = unitObj.GetComponent <Unit>();

                unitObj.transform.position = targetTile.GetPos();
                if (srcUnit != null)
                {
                    unitObj.transform.rotation = srcUnit.thisT.rotation;
                }
                else
                {
                    Faction faction = FactionManager.GetFaction(ability.factionID);
                    if (faction != null)
                    {
                        unitObj.transform.rotation = Quaternion.Euler(0, faction.spawnDirection, 0);
                    }
                }

                FactionManager.InsertUnit(unit, ability.factionID);
                unit.SetNewTile(targetTile);
                //if(GridManager.GetDistance(targetTile, srcUnit.tile)<=srcUnit.GetMoveRange()) GameControl.SelectUnit(srcUnit);
            }
            else if (type == 3)                 //_AbilityType.ScanFogOfWar)
            {
                List <Tile> targetTileList = GridManager.GetTilesWithinDistance(targetTile, ability.GetAOERange());
                targetTileList.Add(targetTile);
                for (int i = 0; i < targetTileList.Count; i++)
                {
                    targetTileList[i].ForceVisible(ability.effect.duration);
                }
            }
            else if (type == 4)                 //_AbilityType.Overwatch
            {
                targetTile.unit.Overwatch(ability.CloneEffect(critMult));
            }
            else if (type == 5)                 //_AbilityType.Teleport
            {
                Quaternion wantedRot = Quaternion.LookRotation(targetTile.GetPos() - ability.unit.tile.GetPos());
                ability.unit.thisT.rotation = wantedRot;

                GameControl.ClearSelectedUnit();
                ability.unit.SetNewTile(targetTile);
                GameControl.SelectUnit(srcUnit);
            }



            else if (type == 6)                 //charge attack
            {
                List <Tile> tileList = GridManager.GetTilesInALine(srcUnit.tile, targetTile, ability.GetRange());

                for (int i = 0; i < tileList.Count; i++)
                {
                    while (true)
                    {
                        float dist = Vector3.Distance(srcUnit.thisT.position, tileList[i].GetPos());
                        if (dist < 0.05f)
                        {
                            if (tileList[i].unit != null)
                            {
                                tileList[i].unit.ApplyEffect(ability.CloneEffect(critMult));
                                SpawnEffectObjTarget(ability, tileList[i]);
                            }
                            break;
                        }

                        Quaternion wantedRot = Quaternion.LookRotation(tileList[i].GetPos() - srcUnit.thisT.position);
                        srcUnit.thisT.rotation = Quaternion.Slerp(srcUnit.thisT.rotation, wantedRot, Time.deltaTime * srcUnit.moveSpeed * 6);

                        Vector3 dir = (tileList[i].GetPos() - srcUnit.thisT.position).normalized;
                        srcUnit.thisT.Translate(dir * Mathf.Min(srcUnit.moveSpeed * 2 * Time.deltaTime, dist), Space.World);
                        yield return(null);
                    }
                }

                srcUnit.tile.unit = null;
                srcUnit.tile      = tileList[tileList.Count - 1];
                tileList[tileList.Count - 1].unit = srcUnit;
                GameControl.ReselectUnit();
            }
            else if (type == 7)                 //attack all unit in a straight line
            {
                List <Tile> tileList = GridManager.GetTilesInALine(srcUnit.tile, targetTile, ability.GetRange());
                for (int i = 0; i < tileList.Count; i++)
                {
                    if (tileList[i].unit == null)
                    {
                        continue;
                    }
                    tileList[i].unit.ApplyEffect(ability.CloneEffect(critMult));
                    SpawnEffectObjTarget(ability, tileList[i]);
                }
            }
            else
            {
                Debug.LogWarning("Error, unknown ability type (" + type + ")");
            }

            yield return(null);
        }