public List <EnviromentTile> FindTileDistance(EnviromentTile TileStart, EnviromentTile TileEnd) { List <EnviromentTile> Path = new List <EnviromentTile>(); if (!astar.PathAvailable(TileStart.GetComponent <Node>(), TileEnd.GetComponent <Node>(), GridNodes)) { return(Path); } List <Node> PathNodes = new List <Node>(); PathNodes = astar.ShowPathDistance(TileStart.GetComponent <Node>(), TileEnd.GetComponent <Node>(), GridNodes); //// Add Tile currently standing on Path.Add(TileStart); // Add rest of tiles if (PathNodes.Count > 0) { for (int i = 1; i <= PathNodes.Count; i++) { Path.Add(PathNodes[i - 1].GetComponent <EnviromentTile>()); } } return(Path); }
// Creates a path depending on a Start and end tile //TODO return a path public List <EnviromentTile> FindTilesBetween(EnviromentTile TileStart, EnviromentTile TileEnd, int MaxDistance) { List <Node> PathNodes = new List <Node>(); PathNodes = astar.FindPath(TileStart.GetComponent <Node>(), TileEnd.GetComponent <Node>(), GridNodes); List <EnviromentTile> Path = new List <EnviromentTile>(); //// Add Tile currently standing on Path.Add(TileStart); // Add rest of tiles if (PathNodes.Count > 0) { //Debug.Log(PathNodes.Count); if (PathNodes.Count < MaxDistance) { for (int i = 1; i <= PathNodes.Count; i++) { Path.Add(PathNodes[i - 1].GetComponent <EnviromentTile>()); } } // Path excedes the max move distance else { for (int i = 1; i <= MaxDistance; i++) { Path.Add(PathNodes[i - 1].GetComponent <EnviromentTile>()); } } } return(Path); }
// Look for Something to attack void CheckforAttackAvailable(CardObject cardObject) { Range = cardObject.FindAttackRange(); Debug.Log(Range.Count); if (Range.Count > 0) { EnviromentTile TileToAttack = null; foreach (EnviromentTile tile in Range) { if (tile.cardType == CardType.Player) { TileToAttack = tile; } } Debug.Log(TileToAttack); if (TileToAttack != null) { Debug.Log("InCombat"); SelectedObjectAttacked = true; cardObject.EngageCombat(CombatType.Attack, TileToAttack.ObjectHeld); return; } } StartCoroutine(SelectNextObject()); }
// Use this for initialization void Start() { currentHealthPoints = maxHealthPoints; terrainControl = FindObjectOfType <TerrainControl>(); EnviromentTile[] Tiles = FindObjectsOfType <EnviromentTile>(); foreach (EnviromentTile tile in Tiles) { if (tile.ObjectHeld == this.gameObject) { TileOn = tile; } } loseScreen = FindObjectOfType <LoseScreen>(); winScreen = FindObjectOfType <WinScreen>(); if (loseScreen != null) { loseScreen.gameObject.SetActive(false); } if (winScreen != null) { winScreen.gameObject.SetActive(false); } }
// TODO MOVE this to player controller or return a public List <EnviromentTile> FindAttackRange(EnviromentTile TileStart, int MaxDistance) { int layer = MaxDistance; List <EnviromentTile> TileRange = new List <EnviromentTile>(); while (layer > 0) { for (int j = -layer; j <= layer; j++) { int i = Mathf.Abs(j) - layer; int Y = Mathf.Clamp(TileStart.Z + j, 0, zGridLength - 1); if (i == 0) { int X = Mathf.Clamp(TileStart.X, 0, xGridLength - 1); TileRange.Add(GridTiles[X, Y]); } else { int X = Mathf.Clamp(TileStart.X + i, 0, xGridLength - 1); TileRange.Add(GridTiles[X, Y]); X = Mathf.Clamp(TileStart.X - i, 0, xGridLength - 1); TileRange.Add(GridTiles[X, Y]); } } layer--; } if (TileRange.Contains(TileStart)) { TileRange.Remove(TileStart); } return(TileRange); }
void OnSpellShowUse(Transform newTranform) { Debug.Log("Showing Spell Use"); CardSpell Spell = cardHand.CardUsing.GetComponent <CardSpell>(); EnviromentTile NewTile = newTranform.GetComponent <EnviromentTile>(); switch (Spell.spellType) { case SpellType.Buff: if (OldTileOver != null) { OldTileOver.ChangeColor(OldTileOver.MatColorOriginal); } if (NewTile.cardType == CardType.Player && NewTile.ObjectHeld.GetComponent <CardObject>() != null) { NewTile.ChangeColor(Color.cyan); } break; case SpellType.DeBuff: break; default: Debug.LogWarning("No Spell Type attached to Spell"); return; } OldTileOver = NewTile; }
void MoveToTile(CardObject cardObject, EnviromentTile tileToMove) { // Debug.Log("Moving"); // Debug.Log(tileToMove); Path = cardObject.MakePath(tileToMove); cardObject.enableMovement(Path); return; }
public bool CheckAttackInRange(EnviromentTile AttackTile, List <EnviromentTile> AttackRange) { if (terrainControl.FindEnemyInAttackRange(AttackTile, AttackRange)) { return(true); } return(false); }
//TODO Modify this to have the range as an input public bool FindEnemyInAttackRange(EnviromentTile TileOver, List <EnviromentTile> TileRange) { if (TileRange.Contains(TileOver)) { return(true); } return(false); }
public bool CheckIfPathAvailable(EnviromentTile TileStart, EnviromentTile TileEnd) { List <Node> PathNodes = new List <Node>(); PathNodes = astar.ShowPathDistance(TileStart.GetComponent <Node>(), TileEnd.GetComponent <Node>(), GridNodes); if (PathNodes.Count > 0) { return(true); } return(false); }
//Returns the position of the Tile in the path //If not in path returns -1 public int CheckPathPosition(EnviromentTile CurrentTile, List <EnviromentTile> path) { for (int i = 0; i < path.Count; i++) { if (path[i] == CurrentTile) { return(i); } } return(-1); }
//Returns the tile next in path public Transform FindNextTileInPath(EnviromentTile CurrentTile, List <EnviromentTile> path) { for (int i = 0; i < path.Count; i++) { if (path[i] == CurrentTile) { return(path[i + 1].transform); } } return(null); }
public bool CapableOfAttacking(EnviromentTile MoveTileEnd, EnviromentTile AttackTileEnd) { List <EnviromentTile> MoveRange = terrainControl.FindMoveRange(CurrentTile, MaxMoveDistance); if (MoveRange.Contains(MoveTileEnd)) { List <EnviromentTile> AttackRange = terrainControl.FindAttackRange(MoveTileEnd, MaxAttackDistance); if (AttackRange.Contains(AttackTileEnd)) { return(true); } } return(false); }
private void Start() { terrainControl = FindObjectOfType <TerrainControl>(); int layerMask = 1 << (int)Layer.LevelTerrain; RaycastHit hit; bool hasHit = Physics.Raycast(transform.position + Vector3.up, Vector3.down, out hit, 3f, layerMask); if (hasHit) { TileOn = hit.transform.GetComponent <EnviromentTile>(); } else { Debug.LogWarning("Spawner not over an enviroment tile"); } }
EnviromentTile FindClosestTileforCard(CardObject cardObject, CardObject cardObjectAgainst) { AttackArea = cardObjectAgainst.FindAttackRangeAround(cardObjectAgainst.GetCurrentTile, cardObject.MaxAttackDistance); int minDistancePlayer = 100; EnviromentTile ClosestPlayerTile = null; foreach (EnviromentTile tile in AttackArea) { if (tile.cardType == CardType.Open) { int Distance = cardObject.FindTileDistance(tile); // If Distance is 0 then a path is not available if (Distance < minDistancePlayer && Distance > 0) { minDistancePlayer = Distance; ClosestPlayerTile = tile; } } } return(ClosestPlayerTile); }
// Update is called once per frame void Update() { if (cardHand.CardUsing) { if (CrossPlatformInputManager.GetButtonUp("pointer1")) { if (cardHand.CardUsing.GetComponent <CardSummon>() != null) { OnItemCreate(); OldTileOver = null; } else if (cardHand.CardUsing.GetComponent <CardSpell>() != null) { if (OldTileOver != null) { onSpellUse(); } OldTileOver = null; } } if (CrossPlatformInputManager.GetButtonDown("Cancel")) { if (OldTileOver != null) { if (OldTileOver.cardType == CardType.Open) { OldTileOver.DestroyImage(); } OldTileOver = null; } SpawnTiles = mageSpawner.CheckTilesAround(); foreach (EnviromentTile tile in SpawnTiles) { tile.ChangeColor(tile.MatColorOriginal); } } } }
void OnItemCreateImage(Transform newTransform) { SpawnTiles = mageSpawner.CheckTilesAround(); foreach (EnviromentTile tile in SpawnTiles) { // Debug.Log("ColorTiles"); tile.ChangeColor(Color.cyan); } Tile = newTransform.GetComponent <EnviromentTile>(); GameObject newItem = cardHand.CardUsing.GetComponent <CardSummon>().SummonImageObject; if (OldTileOver != null) { // Debug.Log("DestroyedObject"); OldTileOver.DestroyImage(); } if (SpawnTiles.Contains(Tile)) { // Debug.Log("Tile In Spawn Area"); Tile.OnItemMake(newItem); OldTileOver = Tile; } }
public List <EnviromentTile> FindTilesOpenAround(EnviromentTile CurrentTile) { // x x x // x 0 x // x x x int Xhigh = Mathf.Clamp(CurrentTile.X + 1, 0, xGridLength - 1); int Xlow = Mathf.Clamp(CurrentTile.X - 1, 0, xGridLength - 1); int Zhigh = Mathf.Clamp(CurrentTile.Z + 1, 0, zGridLength - 1); int Zlow = Mathf.Clamp(CurrentTile.Z - 1, 0, zGridLength - 1); List <EnviromentTile> TilesAround = new List <EnviromentTile>(); for (int i = Xlow; i <= Xhigh; i++) { for (int j = Zlow; j <= Zhigh; j++) { if (GridTiles[i, j].cardType == CardType.Open) { TilesAround.Add(GridTiles[i, j]); } } } return(TilesAround); }
// MOVEMENT public void OnCurrentTile(EnviromentTile tileTransform) { CurrentTile = tileTransform; }
public int FindTileDistance(EnviromentTile Endtile) { return(terrainControl.FindTileDistance(CurrentTile, Endtile).Count); }
public bool CheckIfPathAvailable(EnviromentTile Endtile) { return(terrainControl.CheckIfPathAvailable(CurrentTile, Endtile)); }
// Control the card object to attack the closest object thats attackable void SelectObject(CardObject cardObject) { index++; Debug.Log("Selected " + SelectedCardObject.name + " " + index); cardObject.MoveChangeObservers += SelectedObjectMoveStateChange; cardObject.CombatChangeObservers += SelectedObjectCombatChange; Range = cardObject.FindMoveRange(); EnviromentTile ClosestPlayerTile = null; EnviromentTile ClosestSpawnerTile = null; // if in attack range of the spawner attack it AttackArea = mageSpawner.FindAttackRangeAround(mageSpawner.GetCurrentTile, cardObject.MaxAttackDistance); if (AttackArea.Contains(cardObject.GetCurrentTile)) { CheckforAttackAvailable(cardObject); return; } // check if there are any player objects out on the field if (playerCardObjectsOut.Count > 0) { // Find the closest player Object to attack int minDistancePlayer = 100; foreach (CardObject cardObjectplayer in playerCardObjectsOut) { AttackArea = cardObjectplayer.FindAttackRangeAround(cardObjectplayer.GetCurrentTile, cardObject.MaxAttackDistance); AttackAreaAroundPlayer = AttackArea; // CheckforPlayerinAttackArea if (AttackArea.Contains(cardObject.GetCurrentTile)) { CheckforAttackAvailable(cardObject); return; } int Distance = 100; EnviromentTile tile = FindClosestTileforCard(cardObject, cardObjectplayer); Debug.Log(tile); //Make sure a tile is available to make a path if (tile != null) { Distance = cardObject.FindTileDistance(tile); } if (Distance < minDistancePlayer) { //Set new minDistance and new closest tile minDistancePlayer = Distance; ClosestPlayerTile = tile; } } // Debug.Log(ClosestPlayerTile); // Debug.Log(minDistancePlayer); // If there is no path to an available object to attack dont move if (ClosestPlayerTile != null) { int minDistanceSpawner = 100; ClosestSpawnerTile = FindClosestTileforSpawner(cardObject, mageSpawner); //Make sure a tile is available to make a path if (ClosestSpawnerTile != null) { minDistanceSpawner = cardObject.FindTileDistance(ClosestSpawnerTile); } if (minDistancePlayer < minDistanceSpawner) { MoveToTile(cardObject, ClosestPlayerTile); return; } else { MoveToTile(cardObject, ClosestSpawnerTile); return; } } } // if no path available to the player objects go for the flag/spawner // If no player is found then go for the spawner ClosestSpawnerTile = FindClosestTileforSpawner(cardObject, mageSpawner); if (ClosestSpawnerTile != null) { MoveToTile(cardObject, ClosestSpawnerTile); } else { StartCoroutine(SelectNextObject()); } }
public List <EnviromentTile> FindAttackRangeAround(EnviromentTile AttackTile, int AttackDistance) { return(terrainControl.FindAttackRange(AttackTile, AttackDistance)); }
public List <EnviromentTile> CheckTilesAround() { EnviromentTile TileOn = cardObject.GetCurrentTile; return(terrainControl.FindTilesOpenAround(TileOn)); }
// Find the total range area that this object can move to and highlight it //TODO Return a range of tiles public List <EnviromentTile> FindMoveRange(EnviromentTile TileStart, int MaxDistance) { int layer = MaxDistance; List <EnviromentTile> TileRange = new List <EnviromentTile>(); List <Node> RangePath = new List <Node>(); // Loop through layers (Where layers is essentially the radius of a circle shrinking in) while (layer > 0) { // Start at the lowest Z Tile and move towards the highest Z Tile for (int j = -layer; j <= layer; j++) { int i = Mathf.Abs(j) - layer; // Dont allow to search for tiles outside of boundaries int Y = Mathf.Clamp(TileStart.Z + j, 0, zGridLength - 1); // if there is no change in the X position then only one tile is found // x // _ _ _ // _ S _ // _ _ _ // x if (i == 0) { int X = Mathf.Clamp(TileStart.X, 0, xGridLength - 1); // find the distance it would take //int range = Mathf.Abs(X - TileStart.X) + Mathf.Abs(Y - TileStart.Z); RangePath.Clear(); // find how long Astar calculates a path RangePath = astar.FindPath(TileStart.GetComponent <Node>(), GridTiles[X, Y].GetComponent <Node>(), GridNodes); // If it is equal then no obstacles decreasing range foreach (Node node in RangePath) { if (node == (GridTiles[X, Y]).GetComponent <Node>()) { if (RangePath.IndexOf(node) < MaxDistance) { { TileRange.Add(GridTiles[X, Y]); } } } } } // if there is a change in the X position then 2 tiles are found // _ // _ _ _ // _ S _ // x _ x // _ else { int X = Mathf.Clamp(TileStart.X + i, 0, xGridLength - 1); // int range = Mathf.Abs(X - TileStart.X) + Mathf.Abs(Y - TileStart.Z); RangePath.Clear(); RangePath = astar.FindPath(TileStart.GetComponent <Node>(), GridTiles[X, Y].GetComponent <Node>(), GridNodes); foreach (Node node in RangePath) { if (node == (GridTiles[X, Y]).GetComponent <Node>()) { if (RangePath.IndexOf(node) < MaxDistance) { { TileRange.Add(GridTiles[X, Y]); } } } } X = Mathf.Clamp(TileStart.X - i, 0, xGridLength - 1); //range = Mathf.Abs(X - TileStart.X) + Mathf.Abs(Y - TileStart.Z); RangePath.Clear(); RangePath = astar.FindPath(TileStart.GetComponent <Node>(), GridTiles[X, Y].GetComponent <Node>(), GridNodes); foreach (Node node in RangePath) { if (node == (GridTiles[X, Y]).GetComponent <Node>()) { if (RangePath.IndexOf(node) < MaxDistance) { { TileRange.Add(GridTiles[X, Y]); } } } } } } layer--; } if (TileRange.Contains(TileStart)) { TileRange.Remove(TileStart); } return(TileRange); }
//Change Path when a new tile is over cursor while an Object is Selected void OnPathChange(Transform newTransform) { if (selectedCardObject != null) { //Dont do anything if the selected object is an enemy if (selectedCardObject.cardType == CardType.Enemy) { } else if (allowPathChange) { switch (selectedCardObject.cardState) { case CardState.Move: if (newTransform.GetComponent <EnviromentTile>().cardType == CardType.Open) { if (LastTargetTileOver != null) { LastTargetTileOver.ChangeColor(LastTargetTileOver.MatColorOriginal); LastTargetTileOver = null; } if (Path != null) { foreach (EnviromentTile tile in Path) { if (Range.Contains(tile)) { tile.ChangeColor(Color.cyan); } else { tile.ChangeColor(tile.MatColorOriginal); } } } Path = selectedCardObject.MakePath(newTransform.GetComponent <EnviromentTile>()); if (Path.Contains(newTransform.GetComponent <EnviromentTile>())) { foreach (EnviromentTile tile in Path) { tile.ChangeColor(Color.blue); } LastMoveTileOver = newTransform.GetComponent <EnviromentTile>(); } else { selectedCardObject.GetCurrentTile.ChangeColor(Color.blue); LastMoveTileOver = null; } } else if (newTransform.GetComponent <EnviromentTile>().cardType == CardType.Enemy) { if (selectedCardObject.CapableOfAttacking(LastMoveTileOver, newTransform.GetComponent <EnviromentTile>())) { Debug.Log("In Range"); if (LastTargetTileOver != null) { LastTargetTileOver.ChangeColor(LastTargetTileOver.MatColorOriginal); } newTransform.GetComponent <EnviromentTile>().ChangeColor(Color.red); LastTargetTileOver = newTransform.GetComponent <EnviromentTile>(); } //newTransform.GetComponent<EnviromentTile>().ChangeColor(Color.red); } break; case CardState.Attack: Range = selectedCardObject.FindAttackRange(); if (Range != null) { foreach (EnviromentTile tile in Range) { if (Range.Contains(tile)) { tile.ChangeColor(Orange); } } } if (selectedCardObject.CheckAttackInRange(newTransform.GetComponent <EnviromentTile>(), Range)) { newTransform.GetComponent <EnviromentTile>().ChangeColor(Color.red); } break; default: return; } } } }
private void Start() { Tile = GetComponent <EnviromentTile>(); Location = new Point(Tile.X, Tile.Z); }
public List <EnviromentTile> MakePath(EnviromentTile EndTile) { return(terrainControl.FindTilesBetween(CurrentTile, EndTile, MaxMoveDistance)); }