public CubeStar FindWay(Cube origin, Cube target) { int nbTreat = 0; bool vertical = Mathf.Abs(origin.position.y - target.position.y) > Mathf.Abs(origin.position.x - target.position.x); toTreats = new List <CubeStar>(); treats = new List <CubeStar>(); toTreats.Add(new CubeStar(origin)); do { CubeStar actual = toTreats[0]; //actual.c.m.color = new Color(nbTreat/((float)(_map.WIDTH*_map.WIDTH)),0,0); ++nbTreat; toTreats.Remove(actual); treats.Add(actual); if (actual.c.position == target.position) { return(actual); } int x = (int)actual.c.position.x, y = (int)actual.c.position.y; if (Treatable(x - 1, y)) { toTreats.Add(new CubeStar(_map.GetCube(x - 1, y), actual, _map.GetCube(x - 1, y).position.Distance(target.position) - ((vertical)?0:0.01f))); } if (Treatable(x + 1, y)) { toTreats.Add(new CubeStar(_map.GetCube(x + 1, y), actual, _map.GetCube(x + 1, y).position.Distance(target.position) - ((vertical)?0:0.01f))); } if (Treatable(x, y - 1)) { toTreats.Add(new CubeStar(_map.GetCube(x, y - 1), actual, _map.GetCube(x, y - 1).position.Distance(target.position) - (!(vertical)?0:0.01f))); } if (Treatable(x, y + 1)) { toTreats.Add(new CubeStar(_map.GetCube(x, y + 1), actual, _map.GetCube(x, y + 1).position.Distance(target.position) - (!(vertical)?0:0.01f))); } toTreats.Sort(); } while (toTreats.Count > 0); return(new CubeStar(origin)); }
private void Move() { if (isTarget && !isGoTo) { direction = Goto(target); //direction.Draw(); isTarget = false; isGoTo = true; timeMove = moveSpeed; --direction.depth; next = direction.GetLast().c; } else if (isGoTo) { if (timeMove > 0) { isMobile = true; transform.position = Vector3.Lerp(pos.transform.position, next.transform.position, (moveSpeed - timeMove) / moveSpeed) + new Vector3(0, 1, 0); timeMove -= Time.deltaTime; } else { isMobile = false; pos = next; timeMove = moveSpeed; --direction.depth; CubeStar nextStar = direction.GetLast(); next = nextStar.c; } if (pos == target) { isGoTo = false; } } }