Exemplo n.º 1
0
    // 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);
    }