private void Start() { //BuildGrid(); //grid = GetComponent<RectTileGridBuilder>().Grid; RectTileGridBuilder builder = GetComponent <RectTileGridBuilder>(); start = builder.Grid.First(); goal = builder.Grid.Last(); }
void ClearPath() { RectTileGridBuilder builder = GetComponent <RectTileGridBuilder>(); Color color = new Color(0, 0, 0, 0); foreach (var point in builder.Grid) { builder.Grid[point].transform.FindChild("Sprite").GetComponent <SpriteRenderer>().color = color; } }
private IEnumerable <RectPoint> GetGridPath() { RectTileGridBuilder builder = GetComponent <RectTileGridBuilder>(); var path = Algorithms.AStar(builder.Grid, start, goal, (p, q) => p.DistanceFrom(q), c => ((ShipCell)c).IsAccessible, CalculateCost); return(path); }
void OnDrawGizmos() { //if (Application.isPlaying) Gizmos.color = Color.blue; RectTileGridBuilder builder = GetComponent <RectTileGridBuilder>(); if (builder != null) { var origin = builder.Grid[start].transform.position; var target = builder.Grid[goal].transform.position; Gizmos.DrawRay(origin, target - origin); } }
private float CalculateCost(RectPoint p, RectPoint q) { RectTileGridBuilder builder = GetComponent <RectTileGridBuilder>(); var pCell = (ShipCell)builder.Grid[p]; var qCell = (ShipCell)builder.Grid[q]; float cost = 1; if (p + RectPoint.North == q) { // q // p if (pCell.GetHasTopWall() || qCell.GetHasBottomWall()) { cost = float.MaxValue; } } else if (p + RectPoint.South == q) { // p // q if (pCell.GetHasBottomWall() || qCell.GetHasTopWall()) { cost = float.MaxValue; } } else if (p + RectPoint.East == q) { // p q if (pCell.GetHasRightWall() || qCell.GetHasLeftWall()) { cost = float.MaxValue; } } else if (p + RectPoint.West == q) { // q p if (pCell.GetHasLeftWall() || qCell.GetHasRightWall()) { cost = float.MaxValue; } } else { Debug.LogWarningFormat("Not neighbors!: {0} and {1}", p, q); } return(cost); }
/// <summary> /// Draws lines via DDA. Does NOT support vertical lines.. /// </summary> /// <returns></returns> IEnumerable <RectPoint> DrawLine() { RectTileGridBuilder builder = GetComponent <RectTileGridBuilder>(); Color color = new Color(1.0f, 1.0f, 0.0f, 0.5f); var line = new List <RectPoint>(); float dydx = (goal.Y - start.Y) / (goal.X - start.X); float y = start.Y; for (int x = start.X; x <= goal.X; x++) { var point = new RectPoint(x, Mathf.RoundToInt(y)); line.Add(point); builder.Grid[point].transform.FindChild("Sprite").GetComponent <SpriteRenderer>().color = color; y = y + dydx; } return(line); }
private void UpdatePath() { RectTileGridBuilder builder = GetComponent <RectTileGridBuilder>(); IEnumerable <RectPoint> path = GetGridPath(); if (path == null) { return; //then there is no path between the start and goal. } Color color = new Color(1.0f, 1.0f, 0.0f, 0.5f); if (!IsInLineOfSight(start, goal)) { color = new Color(1.0f, 0.0f, 0.0f, 0.5f); } foreach (var point in path) { //grid[point].Color = Color.black; builder.Grid[point].transform.FindChild("Sprite").GetComponent <SpriteRenderer>().color = color; } }
public bool IsInLineOfSight(RectPoint viewer, RectPoint target) { RectTileGridBuilder builder = GetComponent <RectTileGridBuilder>(); return(IsInLineOfSight(builder.Grid[viewer].transform.position, builder.Grid[target].transform.position)); }
IEnumerable <RectPoint> DrawLine2() { RectTileGridBuilder builder = GetComponent <RectTileGridBuilder>(); Color color = new Color(1.0f, 1.0f, 0.0f, 0.5f); var line = new List <RectPoint>(); int dy = goal.Y - start.Y; int dx = goal.X - start.X; int stepx, stepy; if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; } if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; } dy <<= 1; // dy is now 2*dy dx <<= 1; // dx is now 2*dx float x = start.X; float y = start.Y; //drawpixel(x1, y1, color); var point = new RectPoint(start.X, start.Y); line.Add(point); builder.Grid[point].transform.FindChild("Sprite").GetComponent <SpriteRenderer>().color = color; if (dx > dy) { int fraction = dy - (dx >> 1); // same as 2*dy - dx while (x != goal.X) { if (fraction >= 0) { y += stepy; fraction -= dx; // same as fraction -= 2*dx } x += stepx; fraction += dy; // same as fraction -= 2*dy //drawpixel(x1, y1, color); point = new RectPoint(Mathf.RoundToInt(x), Mathf.RoundToInt(y)); line.Add(point); builder.Grid[point].transform.FindChild("Sprite").GetComponent <SpriteRenderer>().color = color; } } else { int fraction = dx - (dy >> 1); while (y != goal.Y) { if (fraction >= 0) { x += stepx; fraction -= dy; } y += stepy; fraction += dx; //drawpixel(x1, y1, color); point = new RectPoint(Mathf.RoundToInt(x), Mathf.RoundToInt(y)); line.Add(point); builder.Grid[point].transform.FindChild("Sprite").GetComponent <SpriteRenderer>().color = color; } } return(line); }