bool buildPath(GameObject target) { m_currentCellIndex = -1; setTarget(null); TDWorld world = TDWorld.getWorld(); TDGrid grid = world.m_grid; TDGrid.Cell startCell = grid.getCell(world.from3dTo2d(gameObject.transform.position)); TDGrid.Cell endCell = grid.getCell(world.from3dTo2d(target.transform.position)); bool pathExists = false; if (canFly()) { pathExists = grid.buildAirPath(startCell, endCell, out m_path); } else { pathExists = grid.buildPath(startCell, endCell, out m_path); } m_currentCellIndex = 0; setTarget(target); return(pathExists); }
// Returns false if target is not reachable public bool walkByPath() { if (null == m_path) { return(false); } if ((m_currentCellIndex < 0) || (m_currentCellIndex >= m_path.Length)) { return(false); } if (target() == null) // nowhere to go { cleanPath(); onTargetDestroyed(); return(true); } TDWorld world = TDWorld.getWorld(); int cellTo = (m_currentCellIndex == m_path.Length - 1) ? m_currentCellIndex : (m_currentCellIndex + 1); Vector3 nextCellPos = world.from2dTo3d(world.m_grid.getCenter(m_path[cellTo])); Vector3 dir = nextCellPos - transform.position; dir.y = 0; if (!canFly()) { if (Mathf.Abs(dir.x) > Mathf.Abs(dir.z)) { dir.z = 0; } else { dir.x = 0; } } dir.Normalize(); float effSpeed = m_momentalSpeedFactor * getStartSpeed(); Vector3 move = effSpeed * Time.deltaTime * dir; Vector3 nextPos = transform.position + move; if (!canFly()) { if (TDGrid.CellState.eBusy == TDWorld.getWorld().positionState(nextPos)) { return(buildPath(target())); // To make things easy walk there next step } } transform.Translate(move); if ((world.from3dTo2d(transform.position) - world.from3dTo2d(nextCellPos)).magnitude < world.m_configuration.hitDistance) { ++m_currentCellIndex; } if (m_currentCellIndex == m_path.Length) { onTargetReached(target()); cleanPath(); return(true); } return(true); }