// this creates a GameObject with a corresponding Trap on it, and stores it inside of the trapPositionalData public void SetTrap(Vector2Int position, TrapType trap, Unit source, UnitAbility ability) { // parse the spawn location and spawn a new object there Vector3 pos = MapMath.MapToWorld(position.x, position.y); GameObject shell = new GameObject(); GameObject newTrap = Instantiate(shell, pos, Quaternion.identity); Destroy(shell); // add the correct inherited member of Trap to the object Trap newTrapComponent = null; switch (trap) { case TrapType.Claymore: newTrapComponent = newTrap.AddComponent <ClaymoreTrap>() as ClaymoreTrap; break; default: break; } newTrapComponent.mapPosition = position; newTrapComponent.sourceUnit = source; newTrapComponent.placingAbility = ability; newTrap.AddComponent <SpriteRenderer>(); Sprite newSprite = Resources.Load <Sprite>(newTrapComponent.GetResourcePath()); newTrap.GetComponent <SpriteRenderer>().sprite = newSprite; newTrap.GetComponent <SpriteRenderer>().sortingOrder = 98; trapPositionalData.AddTrap(position, newTrapComponent); }
public List <Vector2Int> GetAreaOfEffect(Unit source, Direction direction, int pushbackDist) { List <Vector2Int> result = new List <Vector2Int>(); Vector2Int origin = source.GetMapPosition(); origin += MapMath.DirToRelativeLoc(direction); if (!MapMath.InMapBounds(origin)) { return(result); } /* * Debug.Log( "Unit at (" + source.GetMapPosition().x + ", " + source.GetMapPosition().y + ") " + source.GetDirection() + " is Cleaving at Origin Point (" + origin.x + ", " + origin.y + ")" ); */ //one tile forward result.Add(origin); for (int i = 0; i < pushbackDist - 1; i++) { origin += MapMath.DirToRelativeLoc(direction); result.Add(origin); } return(result); }
private IEnumerator SpawnUnit(int unit, Vector2Int position) { // parse the spawn location and spawn a new object there Vector3 playerPos = MapMath.MapToWorld(position.x, position.y); GameObject shell = new GameObject(); GameObject newUnit = Instantiate(shell, playerPos, Quaternion.identity); Destroy(shell); newUnit.AddComponent <SpriteRenderer>(); newUnit.GetComponent <SpriteRenderer>().enabled = false; Unit newUnitComponent = newUnit.AddComponent <EnemyUnit>() as EnemyUnit; newUnitComponent.StartData = units[unit]; newUnitComponent.globalPositionalData = TurnController.instance.globalPositionalData; newUnitComponent.LoadData(); newUnitComponent.SetHealth(0); EventsManager.instance.AddEvent("spawning"); CameraController.instance.targetPos = playerPos; yield return(new WaitForSecondsRealtime(0.5f)); Graveyard.NewEnemiesEvent.Invoke(new List <Unit> { newUnitComponent }); yield return(new WaitForSecondsRealtime(0.5f)); EventsManager.instance.RemoveEvent("spawning"); }
protected override Size ArrangeOverride(Size finalSize) { foreach (UIElement child in Children) { var finalRect = new Rect(0.0, 0.0, _ViewportSize.Width, _ViewportSize.Height); var positionRectangle = GetPositionRectangle(child); if (positionRectangle is object) { finalRect = MapMath.LocationToViewportPoint(ref _NormalizedMercatorToViewport, positionRectangle); } else { var position = GetPosition(child); if (position is object && MapMath.TryLocationToViewportPoint(ref _NormalizedMercatorToViewport, position, out var viewportPosition)) { var positionOrigin = GetPositionOrigin(child); viewportPosition.X -= positionOrigin.X * child.DesiredSize.Width; viewportPosition.Y -= positionOrigin.Y * child.DesiredSize.Height; finalRect = new Rect(viewportPosition.X, viewportPosition.Y, child.DesiredSize.Width, child.DesiredSize.Height); } } var positionOffset = GetPositionOffset(child); finalRect.X += positionOffset.X; finalRect.Y += positionOffset.Y; child.Arrange(finalRect); } return(_ViewportSize); }
protected override Size MeasureOverride(Size availableSize) { foreach (UIElement child1 in Children) { var positionRectangle = GetPositionRectangle(child1); if (positionRectangle is object) { var viewportPoint = MapMath.LocationToViewportPoint(ref _NormalizedMercatorToViewport, positionRectangle); child1.Measure(new Size(viewportPoint.Width, viewportPoint.Height)); } else { if (child1 is ContentPresenter && VisualTreeHelper.GetChildrenCount(child1) > 0) { if (VisualTreeHelper.GetChild(child1, 0) is IProjectable child2) { child2.SetView(_ViewportSize, _NormalizedMercatorToViewport, _ViewportToNormalizedMercator); (child2 as UIElement)?.InvalidateMeasure(); } } (child1 as IProjectable)?.SetView(_ViewportSize, _NormalizedMercatorToViewport, _ViewportToNormalizedMercator); child1.Measure(_ViewportSize); } } return(_ViewportSize); }
public Stack <Vector2Int> GetMovementPath(Dictionary <Vector2Int, Direction> possibleMoveLocs, Vector2Int dest) { Stack <Vector2Int> path = new Stack <Vector2Int>(); if (!possibleMoveLocs.ContainsKey(dest)) { return(null); } Vector2Int currLoc = dest; while (currLoc != mapPosition) { path.Push(currLoc); Direction dir = MapMath.GetOppositeDirection(possibleMoveLocs[currLoc]); switch (dir) { case Direction.N: currLoc = currLoc + MapMath.RelativeNorth; break; case Direction.S: currLoc = currLoc + MapMath.RelativeSouth; break; case Direction.E: currLoc = currLoc + MapMath.RelativeEast; break; case Direction.W: currLoc = currLoc + MapMath.RelativeWest; break; } } return(path); }
// We're just doing straight damage here public override void DealEffects(Unit target, Unit source) { if (target != null) { //deal core damage to central target target.ChangeHealth((GetDamage() * (-1)), source, this); } Vector2Int origin = source.GetMapPosition(); origin += MapMath.DirToRelativeLoc(source.GetDirection()); List <Vector2Int> area = AttackHelper.GetCircleAOE(origin, source.GetDirection(), GetRange()); //skip first one(center) for (int i = 0; i < area.Count; i++) { if (area[i] == source.GetMapPosition()) { continue; } Unit searchResult = source.globalPositionalData.SearchLocation(area[i]); if (searchResult != null) { searchResult.ChangeHealth((GetDamage() * -1) / 3, source, this); Vector2Int diff = searchResult.GetMapPosition() - origin; // AttackHelper.DisplaceUnit(searchResult, source, this, 1, MapMath.LocToDirection(diff)); } } source.ChangeHealth((GetDamage() * (-1)), source, this); }
/// <summary> /// Takes a single tile on the map and calculates all paths to every other tile, starting from that initial tile. /// returns those pathways as a Dictionary /// it's keys are ints representing the distance required to travel /// its values are dictionaries with keys representing the destination and values representing the Queue path to follow /// </summary> /// <param name="start"> (x, y) coordinate to start from</param> public Dictionary <int, Dictionary <Vector2Int, Queue <Vector2Int> > > ScanFromStart(Vector2Int start) { // create two data structures: tiles that need to be visited and tiles already visited // visited is just a set of Vector2Int // toVisit is a queue, storing the tile to visit, the path taken to get there, and the total movement spent to reach the tile HashSet <Vector2Int> visited = new HashSet <Vector2Int>(); Queue <MutableTuple <Vector2Int, MutableTuple <Queue <Vector2Int>, int> > > toVisit = new Queue <MutableTuple <Vector2Int, MutableTuple <Queue <Vector2Int>, int> > >(); Dictionary <int, Dictionary <Vector2Int, Queue <Vector2Int> > > result = new Dictionary <int, Dictionary <Vector2Int, Queue <Vector2Int> > >(); toVisit.Enqueue(new MutableTuple <Vector2Int, MutableTuple <Queue <Vector2Int>, int> >(start, new MutableTuple <Queue <Vector2Int>, int>(new Queue <Vector2Int>(), 0))); while (toVisit.Count != 0) { // get the next tile in line to visit MutableTuple <Vector2Int, MutableTuple <Queue <Vector2Int>, int> > current = toVisit.Dequeue(); // make sure that this tile is valid to be visited. an Invalid tile: // has already been visited does not exist within weightedMap or has a weight of OBSTRUCTED AND is not the starting tile if (visited.Contains(current.Item1) || (!weightedMap.ContainsKey(current.Item1)) || (weightedMap[current.Item1] == (int)TileWeight.OBSTRUCTED && current.Item1 != start)) { continue; } // this is the first time that we've reached a tile this much movement away. set it up. if (!result.ContainsKey(current.Item2.Item2)) { result.Add(current.Item2.Item2, new Dictionary <Vector2Int, Queue <Vector2Int> >()); } // This is the first time that we've reached this tile with this amount of movement. it is the shortest path. store it if (!result[current.Item2.Item2].ContainsKey(current.Item1)) { result[current.Item2.Item2].Add(current.Item1, current.Item2.Item1); } // check each neighbor of this tile Dictionary <Vector2Int, Direction> neighbors = MapMath.GetNeighbors(current.Item1); foreach (Vector2Int neighbor in neighbors.Keys) { int movement = current.Item2.Item2; if (weightedMap.ContainsKey(neighbor)) { movement += weightedMap[neighbor]; } Queue <Vector2Int> newPath = new Queue <Vector2Int>(current.Item2.Item1.ToArray()); newPath.Enqueue(neighbor); // Debug.Log("Need to visit Tile " + neighbor); toVisit.Enqueue(new MutableTuple <Vector2Int, MutableTuple <Queue <Vector2Int>, int> >(neighbor, new MutableTuple <Queue <Vector2Int>, int>(newPath, movement))); } // this tile has been officially 'visited' visited.Add(current.Item1); } return(result); }
/// <summary> /// Knocks target unit in a line with a given direction. /// </summary> /// <param name="target"></param> /// <param name="knockback"></param> /// <param name="kbDirection"></param> public static void DisplaceUnit(Unit target, Unit source, Attack ability, int knockback, Direction kbDirection) { //look for collisions Vector2Int pushbackLoc = target.GetMapPosition() + (MapMath.DirToRelativeLoc(kbDirection) * knockback); List <Vector2Int> pushbackArea = GetLineAOE(target.GetMapPosition(), kbDirection, knockback); Vector2Int? searchResult = null; foreach (Vector2Int tile in pushbackArea) { if (MapController.instance.weightedMap[tile] == (int)TileWeight.OBSTRUCTED) { searchResult = tile; break; } } if (searchResult != null) { //deal damage when we cant go full knockback dist target.ChangeHealth(-1, source, ability); Vector2Int diff = (Vector2Int)searchResult - target.GetMapPosition(); Vector2Int absDiff = new Vector2Int(Mathf.Abs(diff.x), Mathf.Abs(diff.y)); Debug.Assert(source.GetDirection() != Direction.NO_DIR); //Debug.Log(pushbackLoc); switch (source.GetDirection()) { case Direction.N: pushbackLoc = new Vector2Int(target.GetMapPosition().x, target.GetMapPosition().y + (absDiff.y - 1)); break; case Direction.S: pushbackLoc = new Vector2Int(target.GetMapPosition().x, target.GetMapPosition().y - (absDiff.y - 1)); break; case Direction.W: pushbackLoc = new Vector2Int(target.GetMapPosition().x - (absDiff.x - 1), target.GetMapPosition().y); break; case Direction.E: pushbackLoc = new Vector2Int(target.GetMapPosition().x + (absDiff.x - 1), target.GetMapPosition().y); break; } //Debug.Log(newLocation); } if (MapMath.InMapBounds(pushbackLoc)) { // ******************************** target.Move(pushbackLoc.x, pushbackLoc.y, MovementType.KNOCKBACK); target.hasMoved = false; } //else if() else { DeathData data = new DeathData(source, ability, ability.GetDamage(), target.GetMapPosition()); target.KillMe(data); } }
public void PathHighlight(Vector2Int mapPos, bool northSouth) { if (northSouth) { pathHighlightingNS.SetTile(MapMath.MapToGrid(mapPos), MapUIController.instance.pathingTileNS); } else { pathHighlightingEW.SetTile(MapMath.MapToGrid(mapPos), MapUIController.instance.pathingTileEW); } }
public override void Execute(Unit source, Direction direction) { abilitySoundEvent.start(); Vector2Int position = MapMath.DirToRelativeLoc(direction) + source.GetMapPosition(); // make sure the place this wants to be put is not obstructed if (MapController.instance.weightedMap[position] != (int)TileWeight.OBSTRUCTED && TurnController.instance != null) { TurnController.instance.SetTrap(position, TrapType.Claymore, source, this); } }
private void InitMap() { mostWest = 0; mostEast = 0; mostNorth = 0; mostSouth = 0; HashSet <Vector2Int> visited = new HashSet <Vector2Int>(); Queue <Vector2Int> frontier = new Queue <Vector2Int>(); Vector2Int origin = new Vector2Int(0, 0); frontier.Enqueue(origin); // Should only contain tiles in range weightedMap.Add(origin, (int)(walkableTiles.GetTile(new Vector3Int(origin.x, origin.y, 0)) as WeightedTile).weight); while (frontier.Count != 0) { Vector2Int visiting = frontier.Dequeue(); if (visited.Contains(visiting)) { continue; } // TODO: Implement changing priority in the PQ, and remove this. mostEast = Mathf.Max(mostEast, visiting.y); mostWest = Mathf.Min(mostWest, visiting.y); mostNorth = Mathf.Max(mostNorth, visiting.x); mostSouth = Mathf.Min(mostSouth, visiting.x); Dictionary <Vector2Int, Direction> neighbors = MapMath.GetNeighbors(visiting); foreach (Vector2Int neighbor in neighbors.Keys) { //check if there are tiles in that location, check if its not in weightedMap dict if (walkableTiles.GetTile(new Vector3Int(neighbor.x, neighbor.y, 0))) { if (!visited.Contains(neighbor) && !MapMath.InMapBounds(MapMath.GridToMap(neighbor))) { frontier.Enqueue(neighbor); if (walkableTiles.GetTile(new Vector3Int(neighbor.x, neighbor.y, 0)) is WeightedTile) { //Debug.Log(neighbor); weightedMap.Add(MapMath.GridToMap(neighbor), (int)(walkableTiles.GetTile(new Vector3Int(neighbor.x, neighbor.y, 0)) as WeightedTile).weight); } } } } visited.Add(visiting); } /* * Debug.Log("most east: " + mostEast); * Debug.Log("most west: " + mostWest); * Debug.Log("most south: " + mostSouth); * Debug.Log("most north: " + mostNorth); */ }
// This takes a 'dead' unit and gets it back in the world // refreshes stats, health to full, etc // NOT DONE public virtual void Revive(Vector2Int position) { //if (globalPositionalData.SearchLocation(position) != null || ) health = StartData.health; hasMoved = false; hasActed = false; this.gameObject.GetComponent <SpriteRenderer>().enabled = true; this.gameObject.transform.position = MapMath.MapToWorld(position); globalPositionalData.AddUnit(position, this); tile = (TileWeight)MapController.instance.weightedMap[position]; MapController.instance.weightedMap[position] = (int)TileWeight.OBSTRUCTED; SetDirection(Direction.S); }
private void createLimits() { // Super inefficient, but it'll only get called once. // also singletons lmao. Vector3 left = MapMath.MapToWorld(MapController.instance.mostWest, MapController.instance.mostSouth); Vector3 right = MapMath.MapToWorld(MapController.instance.mostEast, MapController.instance.mostNorth); Vector3 up = MapMath.MapToWorld(MapController.instance.mostEast, MapController.instance.mostSouth); Vector3 down = MapMath.MapToWorld(MapController.instance.mostWest, MapController.instance.mostNorth); // the extra space is twice as big in the y direction, but thats fine. negativeLimit = new Vector2(left.x - 0.5f, down.y) - Vector2.one * EXTRA_SPACE; positiveLimit = new Vector2(right.x - 0.5f, up.y) + Vector2.one * EXTRA_SPACE; }
public override void DealEffects(Unit target, Unit source) { if (target != null) { Vector2Int origin = target.GetMapPosition(); target.ChangeHealth((GetDamage() * (-1)), source, this); foreach (Vector2Int tile in MapMath.GetNeighbors(origin).Keys) { Unit searchResult = source.globalPositionalData.SearchLocation(tile); if (searchResult != null) { Vector2Int diff = tile - origin; AttackHelper.DisplaceUnit(searchResult, source, this, 1, MapMath.LocToDirection(diff)); } } } }
//variation of the findmovetiles alg to save time in devising an algorithm. public static List <Vector2Int> GetCircleAOE(Vector2Int source, Direction direction, int abilityRadius) { List <Vector2Int> circleList = new List <Vector2Int>(); Dictionary <Vector2Int, int> movementCost = new Dictionary <Vector2Int, int>(); HashSet <Vector2Int> visited = new HashSet <Vector2Int>(); PriorityQueue <Vector2Int> frontier = new PriorityQueue <Vector2Int>(); frontier.Enqueue(source, 0); // Should only contain tiles in range movementCost[source] = 0; // Contains frontier and visited circleList.Add(source); while (frontier.Count != 0) { Vector2Int visiting = frontier.Dequeue(); if (visited.Contains(visiting)) { continue; } Dictionary <Vector2Int, Direction> neighbors = MapMath.GetNeighbors(visiting); foreach (Vector2Int neighbor in neighbors.Keys) { if (visited.Contains(neighbor) || !MapMath.InMapBounds(neighbor)) { continue; } int nextDist = 1 + movementCost[visiting]; if (nextDist > abilityRadius) { continue; } if (!movementCost.ContainsKey(neighbor) || nextDist < movementCost[neighbor]) { frontier.Enqueue(neighbor, nextDist); movementCost[neighbor] = nextDist; circleList.Add(neighbor); } } visited.Add(visiting); } return(circleList); }
/* * public static Dictionary<Vector2Int, Direction> FindMoveableTilesStraight(int[,] map, Vector2Int mapPosition, int moveSpeed) * { * Dictionary<Vector2Int, Direction> shortestFrom = new Dictionary<Vector2Int, Direction>(); * Dictionary<Vector2Int, int> movementCost = new Dictionary<Vector2Int, int>(); * * Stack<Vector2Int> frontier = new Stack<Vector2Int>(); * frontier.Push(mapPosition); // Should only contain tiles in range * movementCost[mapPosition] = 0; // Contains frontier and visited * shortestFrom[mapPosition] = Direction.NO_DIR; * * while (frontier.Count != 0) * { * Vector2Int visiting = frontier.Pop(); * * Dictionary<Vector2Int, Direction> neighbors = GetNeighbors(visiting); * foreach (Vector2Int neighbor in neighbors.Keys) * { * if (shortestFrom[visiting] != Direction.NO_DIR && neighbors[neighbor] != shortestFrom[visiting]) { continue; } * if (!MapMath.InMapBounds(neighbor)) { continue; } * int nextDist = MapController.instance.map[neighbor.x, neighbor.y] + movementCost[visiting]; * if (nextDist > moveSpeed) { continue; } * frontier.Push(neighbor); * movementCost[neighbor] = nextDist; * shortestFrom[neighbor] = neighbors[neighbor]; * } * } * return shortestFrom; * * * }*/ public static Dictionary <Vector2Int, Direction> FindMoveableTiles(Dictionary <Vector2Int, int> map, Vector2Int mapPosition, int moveSpeed) { Dictionary <Vector2Int, Direction> shortestFrom = new Dictionary <Vector2Int, Direction>(); Dictionary <Vector2Int, int> movementCost = new Dictionary <Vector2Int, int>(); HashSet <Vector2Int> visited = new HashSet <Vector2Int>(); PriorityQueue <Vector2Int> frontier = new PriorityQueue <Vector2Int>(); frontier.Enqueue(mapPosition, 0); // Should only contain tiles in range movementCost[mapPosition] = 0; // Contains frontier and visited shortestFrom[mapPosition] = Direction.NO_DIR; while (frontier.Count != 0) { Vector2Int visiting = frontier.Dequeue(); if (visited.Contains(visiting)) { continue; } // TODO: Implement changing priority in the PQ, and remove this. Dictionary <Vector2Int, Direction> neighbors = MapMath.GetNeighbors(visiting); foreach (Vector2Int neighbor in neighbors.Keys) { if (visited.Contains(neighbor) || !MapMath.InMapBounds(neighbor)) { continue; } int nextDist = MapController.instance.weightedMap[neighbor] + movementCost[visiting]; if (nextDist > moveSpeed) { continue; } if (!movementCost.ContainsKey(neighbor) || nextDist < movementCost[neighbor]) { frontier.Enqueue(neighbor, nextDist); movementCost[neighbor] = nextDist; shortestFrom[neighbor] = neighbors[neighbor]; } } visited.Add(visiting); } return(shortestFrom); }
public override void DealEffects(Unit target, Unit source) { if (target != null) { Vector2Int origin = target.GetMapPosition(); target.ChangeHealth((GetDamage() * (-1)), source, this); target.Disable(1); foreach (Vector2Int tile in MapMath.GetNeighbors(origin).Keys) { Unit searchResult = source.globalPositionalData.SearchLocation(tile); if (searchResult != null) { searchResult.ChangeHealth((GetDamage() * (-1)), source, this); searchResult.Disable(1); } } } }
// Update is called once per frame void Update() { ray = Camera.main.ScreenPointToRay(Input.mousePosition); cursorPosition = MapMath.WorldToMap(ray.origin * -10f / ray.origin.z); //Debug.Log("Cursor Position(World Pos): " + ray.origin); //Debug.Log("Cursor Position(Grid Pos): " + MapController.instance.walkableTiles.WorldToCell(ray.origin)); /* * if(MapMath.InMapBounds(cursorPosition)) * debugText.text = "Cursor Position(Map Pos): " + cursorPosition + "Weight: " + MapController.instance.weightedMap[cursorPosition]; * else * debugText.text = "Cursor Position(Map Pos): " + cursorPosition + "Weight: NULL"; */ if (MapController.instance.walkableTiles.GetTile(MapMath.MapToGrid(cursorPosition)) != null) { tileSelector.transform.position = MapMath.MapToWorld(cursorPosition); } }
// Parses unitSpawnData, instantiates and initializes Units // adds the newly instantiated Units to units list protected void LoadUnits() { for (int i = 0; i < unitSpawnData.Count; i++) { // parse the spawn location and spawn a new object there Vector3 playerPos = MapMath.MapToWorld(unitSpawnData[i].spawnPosition.x, unitSpawnData[i].spawnPosition.y); GameObject shell = new GameObject(); GameObject newUnit = Instantiate(shell, playerPos, Quaternion.identity); Destroy(shell); newUnit.AddComponent <SpriteRenderer>(); // add the correct inherited member of Unit to the object Unit newUnitComponent = null; switch (unitSpawnData[i].data.unitType) { case UnitType.AlliedUnit: newUnitComponent = newUnit.AddComponent <AlliedUnit>() as AlliedUnit; break; case UnitType.EnemyUnit: newUnitComponent = newUnit.AddComponent <EnemyUnit>() as EnemyUnit; break; case UnitType.Civilian: break; default: newUnitComponent = newUnit.AddComponent <AlliedUnit>() as AlliedUnit; break; } // give this new Unit the raw data for creating it, set its direction newUnitComponent.StartData = unitSpawnData[i].data; newUnitComponent.SetDirection(Direction.S); newUnitComponent.globalPositionalData = this.globalPositionalData; newUnitComponent.globalPositionalData.AddUnit(unitSpawnData[i].spawnPosition, newUnitComponent); // i've given you the data you need to make yourself. now make yourself, please newUnitComponent.LoadData(); // add the brand-spankin-new and created unit to your units list units.Add(newUnit.GetComponent <Unit>()); } }
public override void Start() { mapPosition = MapMath.WorldToMap(this.transform.position); tile = (TileWeight)MapController.instance.weightedMap[mapPosition]; MapController.instance.weightedMap[mapPosition] = (int)TileWeight.OBSTRUCTED; // create the list of possible action categories to take possibleActions = new List <EnemyAction> { new Aggro(this) }; // create list of possible abilities to choose from possibleAbilities = new List <AbilityOption>(); foreach (UnitAbility ability in this.AvailableAbilities) { possibleAbilities.Add(new AbilityOption(this, ability)); } }
public override void DealEffects(Unit target, Unit source) { int pushback = 5; if (target != null) { target.ChangeHealth((GetDamage() * (-1)), source, this); Vector2Int diff = target.GetMapPosition() - source.GetMapPosition(); Vector2Int absDiff = new Vector2Int(Mathf.Abs(diff.x), Mathf.Abs(diff.y)); Debug.Assert(source.GetDirection() != Direction.NO_DIR); Vector2Int newLocation = source.GetMapPosition(); //Debug.Log(newLocation); switch (source.GetDirection()) { case Direction.N: newLocation = new Vector2Int(source.GetMapPosition().x, source.GetMapPosition().y + (absDiff.y - 1)); break; case Direction.S: newLocation = new Vector2Int(source.GetMapPosition().x, source.GetMapPosition().y - (absDiff.y - 1)); break; case Direction.W: newLocation = new Vector2Int(source.GetMapPosition().x - (absDiff.x - 1), source.GetMapPosition().y); break; case Direction.E: newLocation = new Vector2Int(source.GetMapPosition().x + (absDiff.x - 1), source.GetMapPosition().y); break; } //Debug.Log(newLocation); //check new location for issues, if so, stay at current loc // ********************************** source.Move(newLocation.x, newLocation.y, MovementType.DASH); AttackHelper.DisplaceUnit(target, source, this, pushback, source.GetDirection()); } else { Vector2Int newLocation = source.GetMapPosition() + (MapMath.DirToRelativeLoc(source.GetDirection()) * pushback); // ********************************** source.Move(newLocation.x, newLocation.y, MovementType.DASH); } }
public static List <Vector2Int> GetTShapedAOE(Vector2Int source, Direction direction, int abilityRange) { List <Vector2Int> result = new List <Vector2Int>(); Vector2Int origin = source; for (int i = 0; i < abilityRange; i++) { origin += MapMath.DirToRelativeLoc(direction); } if (!MapMath.InMapBounds(origin)) { return(result); } result.Add(origin); if (direction == Direction.N || direction == Direction.S) { if (MapMath.InMapBounds(new Vector2Int(origin.x + 1, origin.y))) { result.Add(new Vector2Int(origin.x + 1, origin.y)); } if (MapMath.InMapBounds(new Vector2Int(origin.x - 1, origin.y))) { result.Add(new Vector2Int(origin.x - 1, origin.y)); } } else if (direction == Direction.E || direction == Direction.W) { if (MapMath.InMapBounds(new Vector2Int(origin.x, origin.y + 1))) { result.Add(new Vector2Int(origin.x, origin.y + 1)); } if (MapMath.InMapBounds(new Vector2Int(origin.x, origin.y - 1))) { result.Add(new Vector2Int(origin.x, origin.y - 1)); } } return(result); }
public override List <Vector2Int> GetAreaOfEffect(Vector2Int source, Direction direction) { //get tile in front List <Vector2Int> result = new List <Vector2Int>(); Vector2Int origin = source; origin += MapMath.DirToRelativeLoc(direction); if (!MapMath.InMapBounds(origin)) { return(result); } /* * Debug.Log( "Unit at (" + source.GetMapPosition().x + ", " + source.GetMapPosition().y + ") " + source.GetDirection() + " is Cleaving at Origin Point (" + origin.x + ", " + origin.y + ")" ); */ result.Add(origin); return(result); }
public static List <Vector2Int> GetLineAOE(Vector2Int source, Direction direction, int abilityRange) { List <Vector2Int> result = new List <Vector2Int>(); Vector2Int origin = source; origin += MapMath.DirToRelativeLoc(direction); if (!MapMath.InMapBounds(origin)) { return(result); } //one tile forward result.Add(origin); for (int i = 0; i < abilityRange - 1; i++) { origin += MapMath.DirToRelativeLoc(direction); result.Add(origin); } return(result); }
// takes a list of Units and adds it to the controller's storage of Units protected void AddUnits(List <Unit> inputList) { foreach (Unit unit in inputList) { units.Add(unit); if (unit.GetHealth() <= 0) { // find the first tile (starting with the original position) that isn't occupied to revive the Unit onto through a breadth-first search Vector2Int spawnPosition = new Vector2Int(int.MaxValue, int.MaxValue); Queue <Vector2Int> toVisit = new Queue <Vector2Int>(); toVisit.Enqueue(unit.GetMapPosition()); do { spawnPosition = toVisit.Dequeue(); foreach (Vector2Int tile in MapMath.GetNeighbors(spawnPosition).Keys) { toVisit.Enqueue(tile); } }while (unit.globalPositionalData.SearchLocation(spawnPosition) != null || MapController.instance.weightedMap[spawnPosition] == (int)TileWeight.OBSTRUCTED); unit.Revive(spawnPosition); } } }
public IEnumerator Move(Queue <Vector2Int> path, MovementType type) { // I am moving. do not let any controllers do anything EventsManager.instance.AddEvent("move"); if (moveSoundEvent.isValid()) { if (this is AlliedUnit) { moveSoundEvent.stop(FMOD.Studio.STOP_MODE.IMMEDIATE); MapUIController.instance.ClearPathHighlight(); MapUIController.instance.ClearRangeHighlight(); (this as AlliedUnit).plannedPath.Clear(); } moveSoundEvent.start(); } attackBuffed = false; Vector2Int currentTile = GetMapPosition(); while (path.Count > 0) { // have the camera look here CameraController.instance.targetPos = this.transform.position; // get the next tile to move to currentTile = path.Dequeue(); // remove old coordinates from globalPositionalData globalPositionalData.RemoveUnit(mapPosition); // restore old tilevalue MapController.instance.weightedMap[mapPosition] = (int)tile; // set new coordinates mapPosition = currentTile; // update the globalPositionalData globalPositionalData.AddUnit(mapPosition, this); // remember the weight of the tile being occupied tile = (TileWeight)MapController.instance.weightedMap[mapPosition]; // set the occupied tile to OBSTRUCTED MapController.instance.weightedMap[mapPosition] = (int)TileWeight.OBSTRUCTED; // visualize the movement made from one tile to another, based on the type of movement switch (type) { case MovementType.DASH: this.transform.position = MapMath.MapToWorld(currentTile); yield return(new WaitForSecondsRealtime(0.1f)); break; case MovementType.KNOCKBACK: this.transform.position = MapMath.MapToWorld(currentTile); yield return(new WaitForSecondsRealtime(0.01f)); break; case MovementType.TELEPORT: this.transform.position = MapMath.MapToWorld(currentTile); CameraController.instance.targetPos = this.transform.position; yield return(new WaitForSecondsRealtime(0.1f)); break; case MovementType.WALK: this.transform.position = MapMath.MapToWorld(currentTile); CameraController.instance.targetPos = this.transform.position; yield return(new WaitForSecondsRealtime(0.1f)); break; default: this.transform.position = MapMath.MapToWorld(currentTile); CameraController.instance.targetPos = this.transform.position; yield return(new WaitForSecondsRealtime(0.1f)); break; } if (TurnController.instance != null) { TurnController.instance.trapPositionalData.CheckTraps(); } } this.transform.position = MapMath.MapToWorld(currentTile); hasMoved = true; EventsManager.instance.RemoveEvent("move"); }
public override void Update() { // if it's not currently this controller's turn, the controller has no Units, or there are active events, it's not allowed to do anything if (!myTurn || units.Count == 0 || EventsManager.instance.EventsExist()) { return; } AlliedUnit theUnit = units[activeUnit] as AlliedUnit; //left click to choose unit if (Input.GetMouseButtonDown(0)) { Vector2Int selectedLoc = MapUIController.instance.cursorPosition; int? selectedIndex = FindUnit(selectedLoc); if (selectedIndex != null) { ClearSpotlight(); SelectUnit((int)selectedIndex); } } //check for tab input //select next unit if (Input.GetKeyDown(KeyCode.Tab)) { GetNextUnit(); return; } if (theUnit.hasMoved && !theUnit.hasPivoted) { Vector2Int unitPosition = units[activeUnit].GetMapPosition(); //Vector3 truePosition = MapMath.MapToWorld(unitPosition); directionSelector.transform.position = MapMath.MapToWorld(unitPosition); directionSelector.transform.position = new Vector3(directionSelector.transform.position.x, directionSelector.transform.position.y + 1.506f, directionSelector.transform.position.z); directionSelector.SetActive(true); theUnit.ChooseDirection(); return; } // if the current unit has moved but hasn't attacked, it needs to select an ability if (theUnit.hasMoved && theUnit.hasPivoted && !theUnit.hasActed) { directionSelector.SetActive(false); abilityPanel.SetActive(true); theUnit.ChooseAbility(); return; } else { abilityPanel.SetActive(false); } // if the current unit has moved and attacked, get the next unit if (theUnit.hasMoved && theUnit.hasPivoted && theUnit.hasActed) { abilityPanel.SetActive(false); GetNextUnit(); return; } //Only process if the unit has not moved if (!theUnit.hasMoved) { // ----Do Movement---- Vector2Int dest = MapUIController.instance.cursorPosition; if (Input.GetMouseButtonDown(0)) { if (theUnit.plannedPath.Contains(dest)) // Commit to the path { Queue <Vector2Int> movementPath = new Queue <Vector2Int>(); for (int i = 0; i < theUnit.plannedPath.Count; i++) { movementPath.Enqueue(theUnit.plannedPath[i]); if (theUnit.plannedPath[i] == dest) { break; } } StartCoroutine(theUnit.Move(movementPath, MovementType.WALK)); ClearSpotlight(); } else { theUnit.DisplayShortestPath(dest); } } } }
public void RangeHighlight(Vector2Int mapPos) { tileHighlighting.SetTile(MapMath.MapToGrid(mapPos), MapUIController.instance.movementTile); }
public virtual void Start() { mapPosition = MapMath.WorldToMap(this.transform.position); tile = (TileWeight)MapController.instance.weightedMap[mapPosition]; MapController.instance.weightedMap[mapPosition] = (int)TileWeight.OBSTRUCTED; }