private Vector2 getNewTarget() { headIndex += 1; if (!Head) { GameObject player = GameObject.FindGameObjectWithTag("Player"); if (player) { HoneycombPos playerHex = Utility.WorldPointToHoneycombGrid(player.transform.position); HoneycombPos snakeHex = Utility.WorldPointToHoneycombGrid(transform.position); if (true && player && Utility.DistanceBetweenHoneycomb(playerHex, snakeHex) < AttackRadius && Map.StaticMap.GetHoneycomb((int)playerHex.x, (int)playerHex.y).LocationType == MapHoneycomb.LocationTypes.Chamber) { HoneycombPos playerGridHex = playerHex; // Utility.WorldPointToHoneycombGrid(player.transform.position); HoneycombPos startGridHex = snakeHex; // Utility.WorldPointToHoneycombGrid(transform.position); Vector2 playerGridPos = findPathToHoneycomb(startGridHex, playerGridHex); return(playerGridPos); } else { return(getRandomLoc()); } } else { return(getRandomLoc()); } } else { // headIndex += 1; return(Head.GetNextHeadTarget(headIndex)); } }
public static MapChunk GetMapChunk(Vector2 worldPos) { HoneycombPos honeyIndex = WorldPointToHoneycombGrid(worldPos); Map map = Map.StaticMap; int xChunk = (int)honeyIndex.x / map.ChunkWidth; int yChunk = (int)honeyIndex.y / (map.ChunkHeight / 2); //int chunkIndex = xChunk * yChunk + xChunk; return(map.GetChunk(xChunk, yChunk)); }
public void DamageAdjecentHoneycomb(int depth) { HoneycombPos hexPos = Utility.WorldPointToHoneycombGrid(honeyGrid.position); Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(0, 1)).DamageHoneycomb(depth); Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(1, 1)).DamageHoneycomb(depth); Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(1, -1)).DamageHoneycomb(depth); Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(0, -1)).DamageHoneycomb(depth); Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(-1, -1)).DamageHoneycomb(depth); Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(-1, 1)).DamageHoneycomb(depth); }
public static List <MapHoneycomb> GetHoneycombPath(HoneycombPos start, HoneycombDir dir, int honeyDistance) { List <MapHoneycomb> path = new List <MapHoneycomb>(); //start = WorldToHoneycomb(start); for (int i = 1; i <= honeyDistance; i += 1) { HoneycombPos honeyCell = GetHoneycombDirection(start, dir, i); //Debug.Log(honeyCell); path.Add(Map.StaticMap.GetHoneycomb((int)honeyCell.x, (int)honeyCell.y)); } return(path); }
public static Vector2 HoneycombGridToWorldPostion(HoneycombPos honeyPos) { Map map = Map.StaticMap; float xPos = honeyPos.x * map.HorizontalSpacing; float yPos = yPos = honeyPos.y * map.VerticalSpacing * 2; if (honeyPos.x % 2 == 0) { yPos += map.VerticalSpacing; } return(new Vector2(xPos, yPos) + new Vector2(map.MapOrigin.x * map.HorizontalSpacing, map.MapOrigin.y * map.VerticalSpacing)); }
private HoneycombPos FindShortestPath(HoneycombPos startHex, HoneycombDir hexDir, HoneycombPos targetHex) { int distance = 1; HoneycombPos pathHex = startHex; MapHoneycomb newTarget = Utility.GetHoneycombFreePath(startHex, hexDir, distance); MapHoneycomb nextTarget = Utility.GetHoneycombFreePath(startHex, hexDir, distance + 1); while (newTarget != null && nextTarget != null && Utility.DistanceBetweenHoneycomb(Utility.WorldPointToHoneycombGrid(newTarget.position), targetHex) > Utility.DistanceBetweenHoneycomb(Utility.WorldPointToHoneycombGrid(nextTarget.position), targetHex)) { newTarget = nextTarget; distance += 1; nextTarget = Utility.GetHoneycombFreePath(startHex, hexDir, distance + 1); } //Debug.Log("Closets HexPos: " + Utility.WorldPointToHoneycombGrid(newTarget.position)); return(Utility.WorldPointToHoneycombGrid(newTarget.position)); }
private HoneycombPos compareShortestPaths(HoneycombDir dirOne, HoneycombDir dirTwo, HoneycombPos startHoneycomb, HoneycombPos targetHoneycomb) { //Vector2 targetPos = Utility.HoneycombGridToWorldPostion(targetHoneycomb); //Vector2 startPos = Utility.HoneycombGridToWorldPostion(startHoneycomb); HoneycombPos closestHex = startHoneycomb; HoneycombPos hexOne = FindShortestPath(startHoneycomb, dirOne, targetHoneycomb); HoneycombPos hexTwo = FindShortestPath(startHoneycomb, dirTwo, targetHoneycomb); if (Utility.DistanceBetweenHoneycomb(closestHex, targetHoneycomb) > Utility.DistanceBetweenHoneycomb(hexOne, targetHoneycomb)) { closestHex = hexOne; } if (Utility.DistanceBetweenHoneycomb(closestHex, targetHoneycomb) > Utility.DistanceBetweenHoneycomb(hexTwo, targetHoneycomb)) { closestHex = hexTwo; } return(closestHex); }
public static MapHoneycomb GetHoneycombFreePath(HoneycombPos startHex, HoneycombDir hexDir, int hexDistance) { List <MapHoneycomb> path = GetHoneycombPath(startHex, hexDir, hexDistance); MapHoneycomb newTarget = null; foreach (MapHoneycomb honeycomb in path) { //Debug.Log(honeycomb.position); if ((!honeycomb.display || honeycomb.isFloor) && honeycomb.LocationType == MapHoneycomb.LocationTypes.Chamber) { newTarget = honeycomb; } else { //Debug.Log(honeycomb.LocationType); break; } } return(newTarget); }
public HoneycombPos GetAdjecentHoneycomb(HoneycombDir dir) { HoneycombPos end = new HoneycombPos(x, y); end.x += dir.x; if (dir.x == 0) { end.y += dir.y; } else if (x % 2 == 0 && dir.y > 0 || x % 2 != 0 && dir.y < 0) { //end.y += Mathf.Sign(dir.y) * Mathf.Ceil((float)honeyDistance / 2); end.y += (int)(Mathf.Sign(dir.y));// * Mathf.Ceil((float)honeyDistance / 2)); } else { //end.y += Mathf.Sign(dir.y) * Mathf.Ceil(((float)honeyDistance - 1) / 2); //end.y += (int)(Mathf.Sign(dir.y) * Mathf.Ceil(((float)honeyDistance - 1) / 2)); } return(end); }
/// <summary> /// Returns the coordinates of a target honeycomb starting from the coordinates of a honeycomb in a honeycomb vector (honeycomb direction and distance) /// </summary> /// <param name="start"></param> /// <param name="dir"></param> /// <param name="honeyDistance"></param> /// <returns></returns> public static HoneycombPos GetHoneycombDirection(HoneycombPos start, HoneycombDir dir, int honeyDistance) { //start = Utility.WorldPointToHoneycombGrid(start); HoneycombPos end = start; end.x += dir.x * honeyDistance; if (dir.x == 0) { end.y += dir.y * honeyDistance; } else if (start.x % 2 == 0 && dir.y > 0 || start.x % 2 != 0 && dir.y < 0) { //end.y += Mathf.Sign(dir.y) * Mathf.Ceil((float)honeyDistance / 2); end.y += (int)(Mathf.Sign(dir.y) * Mathf.Ceil((float)honeyDistance / 2)); } else { //end.y += Mathf.Sign(dir.y) * Mathf.Ceil(((float)honeyDistance - 1) / 2); end.y += (int)(Mathf.Sign(dir.y) * Mathf.Ceil(((float)honeyDistance - 1) / 2)); } return(end); }
private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Honeycomb")) { //Destroy(collision.gameObject); if (honeycombBounce) { Vector2 velocity = GetComponent <Rigidbody2D>().velocity; Vector2 normal = collision.transform.position - transform.position; } else { HoneycombPos hexPos = Utility.WorldPointToHoneycombGrid(collision.GetComponent <Honeycomb>().honeyGrid.position); collision.GetComponent <Honeycomb>().DamageHoneycomb(Damage); if (false) //for testing HoneycombPos.GetAdjecentHoneycomb() { Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(0, 1)).honeycomb.GetComponent <Honeycomb>().DamageHoneycomb(10); Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(1, 1)).honeycomb.GetComponent <Honeycomb>().DamageHoneycomb(10); Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(1, -1)).honeycomb.GetComponent <Honeycomb>().DamageHoneycomb(10); Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(0, -1)).honeycomb.GetComponent <Honeycomb>().DamageHoneycomb(10); Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(-1, -1)).honeycomb.GetComponent <Honeycomb>().DamageHoneycomb(10); Map.StaticMap.GetHoneycomb(hexPos.GetAdjecentHoneycomb(-1, 1)).honeycomb.GetComponent <Honeycomb>().DamageHoneycomb(10); } Destroy(gameObject); } } if (collision.transform.CompareTag("Enemy") && collision.transform.GetComponent <Insect>()) { Insect collider = collision.transform.GetComponent <Insect>(); collider.TakeDamage(Damage); FindObjectOfType <LevelHandler>().UpdatePlayerStats(1, 0); Destroy(gameObject); } }
public static float DistanceBetweenHoneycomb(HoneycombPos hexOne, HoneycombPos hexTwo) { return(Vector2.Distance(HoneycombGridToWorldPostion(hexOne), HoneycombGridToWorldPostion(hexTwo))); }
public MapHoneycomb GetHoneycomb(HoneycombPos pos) { return(GetHoneycomb(pos.x, pos.y)); }
private Vector2 findPathToHoneycomb(HoneycombPos startHoneycomb, HoneycombPos targetHoneycomb) { //Vector2 targetPos = Utility.HoneycombGridToWorldPostion(targetHoneycomb); //Vector2 startPos = Utility.HoneycombGridToWorldPostion(startHoneycomb); HoneycombPos closestHex = startHoneycomb; if (startHoneycomb.x < targetHoneycomb.x && startHoneycomb.y > targetHoneycomb.y) { //(1,-1) || (0,-1) HoneycombDir dirOne = new HoneycombDir(1, -1); HoneycombDir dirTwo = new HoneycombDir(0, -1); closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb); } else if (startHoneycomb.x < targetHoneycomb.x && startHoneycomb.y < targetHoneycomb.y) { //(1,1) || (0,1) HoneycombDir dirOne = new HoneycombDir(1, 1); HoneycombDir dirTwo = new HoneycombDir(0, 1); closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb); } else if (startHoneycomb.x < targetHoneycomb.x && startHoneycomb.y == targetHoneycomb.y) { //(1,1) || (1,-1) HoneycombDir dirOne = new HoneycombDir(1, 1); HoneycombDir dirTwo = new HoneycombDir(1, -1); closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb); } else if (startHoneycomb.x > targetHoneycomb.x && startHoneycomb.y > targetHoneycomb.y) { //(-1,-1) || (0,-1) HoneycombDir dirOne = new HoneycombDir(-1, -1); HoneycombDir dirTwo = new HoneycombDir(0, -1); closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb); } else if (startHoneycomb.x > targetHoneycomb.x && startHoneycomb.y < targetHoneycomb.y) { //(-1,1) || (0,1) HoneycombDir dirOne = new HoneycombDir(-1, 1); HoneycombDir dirTwo = new HoneycombDir(0, 1); closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb); } else if (startHoneycomb.x > targetHoneycomb.x && startHoneycomb.y == targetHoneycomb.y) { //(-1,1) || (-1,-1) HoneycombDir dirOne = new HoneycombDir(-1, 1); HoneycombDir dirTwo = new HoneycombDir(-1, -1); closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb); } else if (startHoneycomb.x == targetHoneycomb.x && startHoneycomb.y < targetHoneycomb.y) { // (0,1) HoneycombPos hexOne = FindShortestPath(startHoneycomb, new HoneycombDir(0, 1), targetHoneycomb); if (Utility.DistanceBetweenHoneycomb(closestHex, targetHoneycomb) > Utility.DistanceBetweenHoneycomb(hexOne, targetHoneycomb)) { closestHex = hexOne; } } else if (startHoneycomb.x == targetHoneycomb.x && startHoneycomb.y > targetHoneycomb.y) { // (0,-1) HoneycombPos hexOne = FindShortestPath(startHoneycomb, new HoneycombDir(0, -1), targetHoneycomb); if (Utility.DistanceBetweenHoneycomb(closestHex, targetHoneycomb) > Utility.DistanceBetweenHoneycomb(hexOne, targetHoneycomb)) { closestHex = hexOne; } } else { //(0,0) Debug.Log("Snake Follow Player Error"); } return(Utility.HoneycombGridToWorldPostion(closestHex)); }