public static void SetupHostileInRangeforTile(Unit unit, List<Tile> tileList){ List<Unit> allUnitList=FactionManager.GetAllUnit(); List<Unit> allHostileUnitList=new List<Unit>(); for(int i=0; i<allUnitList.Count; i++){ if(allUnitList[i].factionID!=unit.factionID) allHostileUnitList.Add(allUnitList[i]); } List<Unit> allFriendlyUnitList=new List<Unit>(); if(GameControl.EnableFogOfWar()) allFriendlyUnitList=FactionManager.GetAllUnitsOfFaction(unit.factionID); int range=unit.GetAttackRange(); int rangeMin=unit.GetAttackRangeMin(); int sight=unit.GetSight(); for(int i=0; i<tileList.Count; i++){ Tile srcTile=tileList[i]; List<Tile> hostileInRangeList=new List<Tile>(); for(int j=0; j<allHostileUnitList.Count; j++){ Tile targetTile=allHostileUnitList[j].tile; if(GridManager.GetDistance(srcTile, targetTile)>range) continue; if(GridManager.GetDistance(srcTile, targetTile)<rangeMin) continue; if(!GameControl.EnableFogOfWar() && !GameControl.AttackThroughObstacle()){ if(!FogOfWar.InLOS(srcTile, targetTile, 0)) continue; } bool inSight=GameControl.EnableFogOfWar() ? false : true; if(GameControl.EnableFogOfWar()){ if(FogOfWar.InLOS(srcTile, targetTile) && GridManager.GetDistance(srcTile, targetTile)<=sight){ inSight=true; } else if(!unit.requireDirectLOSToAttack){ for(int n=0; n<allFriendlyUnitList.Count; n++){ if(allFriendlyUnitList[n]==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) hostileInRangeList.Add(targetTile); } tileList[i].SetHostileInRange(hostileInRangeList); } }
public void AutoDeployFaction(int factionID=-1){ if(factionID==-1) factionID=deployingFactionID; bool setToInvisible=GameControl.EnableFogOfWar() & !factionList[factionID].isPlayerFaction; List<Unit> unitList=factionList[factionID].startingUnitList; List<Tile> tileList=GridManager.GetDeployableTileList(factionList[factionID].ID); for(int i=0; i<tileList.Count; i++){ if(!tileList[i].walkable || tileList[i].unit!=null || tileList[i].obstacleT!=null){ tileList.RemoveAt(i); i-=1; } } int count=0; for(int i=0; i<unitList.Count; i++){ if(tileList.Count==0) break; Unit unit=unitList[i]; int rand=Random.Range(0, tileList.Count); Tile tile=tileList[rand]; tileList.RemoveAt(rand); tile.unit=unit; unit.tile=tile; unit.transform.position=tile.GetPos(); unit.transform.rotation = Quaternion.Euler(0, factionList[factionID].spawnDirection, 0); unit.gameObject.SetActive(true); deployedUnitList.Add(unit); if(setToInvisible && !tile.IsVisible()){ unit.gameObject.layer=TBTK.GetLayerUnitInvisible(); Utilities.SetLayerRecursively(unit.transform, TBTK.GetLayerUnitInvisible()); } count+=1; unit.factionID=factionList[factionID].ID; } for(int i=0; i<count; i++) unitList.RemoveAt(0); TBTK.OnUnitDeployed(null); }
public static void InitGrid(List <Tile> tileList) { if (!GameControl.EnableFogOfWar()) { return; } for (int i = 0; i < tileList.Count; i++) { tileList[i].SetVisible(false); } List <Unit> unitList = FactionManager.GetAllPlayerUnits(); for (int i = 0; i < unitList.Count; i++) { unitList[i].SetupFogOfWar(true); } }
//public DurationCounter forcedVisible=new DurationCounter(); public void ForceVisible(int dur = 1) { if (!GameControl.EnableFogOfWar()) { return; } SetVisible(true); //if(fogOfWarObj!=null) fogOfWarObj.SetActive(!visible); if (forcedVisibleDuration.duration < dur) { forcedVisibleDuration.Set(dur); EffectTracker.TrackVisible(this); } //forcedVisible.Count(dur); //GameControl.onIterateTurnE += IterateForcedVisible; }
//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]); }
public void Init() { if (init) { return; } init = true; thisT = transform; Transform deploymentIndicator = null; Transform moveableIndicator = null; Transform hostileIndicator = null; Transform abRangeIndicator = null; Transform abTargetIndicator = null; if (GridManager.GetTileType() == _TileType.Hex) { deploymentIndicator = hexDeployment; moveableIndicator = hexMoveable; hostileIndicator = hexHostile; abRangeIndicator = hexAbRange; abTargetIndicator = hexAbTarget; cursorIndicatorF = (Transform)Instantiate(hexCursorF); cursorIndicatorH = (Transform)Instantiate(hexCursorH); selectIndicator = (Transform)Instantiate(hexSelected); } else if (GridManager.GetTileType() == _TileType.Square) { deploymentIndicator = sqDeployment; moveableIndicator = sqMoveable; hostileIndicator = sqHostile; abRangeIndicator = sqAbRange; abTargetIndicator = sqAbTarget; cursorIndicatorF = (Transform)Instantiate(sqCursorF); cursorIndicatorH = (Transform)Instantiate(sqCursorH); selectIndicator = (Transform)Instantiate(sqSelected); } cursorIndicatorF.parent = thisT; cursorIndicatorH.parent = thisT; selectIndicator.parent = thisT; for (int i = 0; i < 20; i++) { deploymentIndicatorList.Add((Transform)Instantiate(deploymentIndicator)); deploymentIndicatorList[i].gameObject.SetActive(false); deploymentIndicatorList[i].parent = thisT; } for (int i = 0; i < 20; i++) { moveableIndicatorList.Add((Transform)Instantiate(moveableIndicator)); moveableIndicatorList[i].gameObject.SetActive(false); moveableIndicatorList[i].parent = thisT; } for (int i = 0; i < 10; i++) { hostileIndicatorList.Add((Transform)Instantiate(hostileIndicator)); hostileIndicatorList[i].gameObject.SetActive(false); hostileIndicatorList[i].parent = thisT; } for (int i = 0; i < 20; i++) { abRangeIndicatorList.Add((Transform)Instantiate(abRangeIndicator)); abRangeIndicatorList[i].gameObject.SetActive(false); abRangeIndicatorList[i].parent = thisT; } for (int i = 0; i < 20; i++) { abTargetIndicatorList.Add((Transform)Instantiate(abTargetIndicator)); abTargetIndicatorList[i].gameObject.SetActive(false); abTargetIndicatorList[i].parent = thisT; } if (TurnControl.GetTurnMode() == _TurnMode.FactionPerTurn) { //create the moved indicator for (int i = 0; i < 10; i++) { movedIndicatorList.Add((Transform)Instantiate(movedIndicator)); movedIndicatorList[i].gameObject.SetActive(false); movedIndicatorList[i].parent = thisT; } } if (GameControl.EnableFogOfWar()) { Transform fogObj = null; if (GridManager.GetTileType() == _TileType.Hex) { fogObj = hexFogObj; } else if (GridManager.GetTileType() == _TileType.Square) { fogObj = sqFogObj; } List <Tile> tileList = GridManager.GetTileList(); for (int i = 0; i < tileList.Count; i++) { //if(!tileList[i].walkable) continue; tileList[i].SetFogOfWarObj((Transform)Instantiate(fogObj)); } } if (GameControl.EnableCover()) { float scaleOffset = GridManager.GetTileType() == _TileType.Hex ? 0.5f : 0.8f; float tileSize = GridManager.GetTileSize(); for (int i = 0; i < 5; i++) { coverOverlayHList.Add((Transform)Instantiate(coverOverlayH)); coverOverlayFList.Add((Transform)Instantiate(coverOverlayF)); coverOverlayHList[i].localScale *= tileSize * scaleOffset; coverOverlayHList[i].parent = thisT; coverOverlayHList[i].gameObject.SetActive(false); coverOverlayFList[i].localScale *= tileSize * scaleOffset; coverOverlayFList[i].parent = thisT; coverOverlayFList[i].gameObject.SetActive(false); } } }
//~ 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); }