Exemplo n.º 1
0
    public void Init(int row, int col, MapCtr map, AStarMap astarMap, PlayerCtrEx target, bool isLeft)
    {
        startRow = row;
        startCol = col;
        m_data   = GetComponent <PlayerData> ();
        m_data.Init(row, col, map);
        m_astarMap = astarMap;
        m_target   = target;

        m_data.RegisterMoveOverEvent(AutoMoveOver);
        if (isLeft)
        {
            if (m_astarMap.GetCell(m_data.Row, m_data.Column).IsToLeft)
            {
                m_data.Move(-1, 0);
            }
            else
            {
                m_data.Move(1, 0);
            }
        }
        else
        {
            if (m_astarMap.GetCell(m_data.Row, m_data.Column).IsToRight)
            {
                m_data.Move(1, 0);
            }
            else
            {
                m_data.Move(-1, 0);
            }
        }
    }
    static void SetAStarMapCell_Second(MapData map, MapCellData cell, ref AStarMap astarMap)
    {
        // check second time
        CELL_TYPE curType = (CELL_TYPE)cell.cellType;

        if (curType == CELL_TYPE.STONE || curType == CELL_TYPE.WALL)
        {
            return;
        }
        if (cell.row - 1 >= 0)
        {
            MapCellData downCell = map.cells [(cell.row - 1) * map.column + cell.col];
            CELL_TYPE   downType = (CELL_TYPE)downCell.cellType;
            if (downType == CELL_TYPE.WALL || downType == CELL_TYPE.STONE ||
                downType == CELL_TYPE.LADDER)
            {
                //right
                if (cell.col + 1 < map.column)
                {
                    MapCellData rightCell = map.cells [(cell.row) * map.column + cell.col + 1];
                    CELL_TYPE   rightType = (CELL_TYPE)rightCell.cellType;
                    if (rightType != CELL_TYPE.WALL && rightType != CELL_TYPE.STONE)
                    {
                        //astarMapCell.IsToRight = true;
                        astarMap.GetCell(rightCell.row, rightCell.col).IsToLeft = true;
                    }
                }
                //left
                if (cell.col - 1 >= 0)
                {
                    MapCellData leftCell = map.cells [(cell.row) * map.column + cell.col - 1];
                    CELL_TYPE   leftType = (CELL_TYPE)leftCell.cellType;
                    if (leftType != CELL_TYPE.WALL && leftType != CELL_TYPE.STONE)
                    {
                        //astarMapCell.IsToLeft = true;
                        astarMap.GetCell(leftCell.row, leftCell.col).IsToRight = true;
                    }
                }
            }
        }
    }
Exemplo n.º 3
0
 void FromMap(AStarMap map)
 {
     ClearMap();
     m_column = map.Column;
     m_row    = map.Row;
     CreateMap();
     for (int i = 0; i < m_row; i++)
     {
         for (int j = 0; j < m_column; j++)
         {
             m_cellArray [j, i].FromMapCell(map.GetCell(i, j));
         }
     }
 }
Exemplo n.º 4
0
    public void AutoMoveOver(PlayerData pd)
    {
        if (!CanControl)
        {
            return;
        }
        object[] p = new object[2];
        p[0] = (object)pd.Row;
        p[1] = (object)pd.Column;
//		if(pd.Row == m_target.Row && pd.Column == m_target.Column){
//			GameStateManager.Instance().FSM.CurrentState.Message("PlayerDead", p);
//			return;
//		}

        if (m_data.IsLookLeft)
        {
            if (m_astarMap.GetCell(m_data.Row, m_data.Column).IsToLeft)
            {
                m_data.Move(-1, 0);
            }
            else
            {
                m_data.Move(1, 0);
            }
        }
        else
        {
            if (m_astarMap.GetCell(m_data.Row, m_data.Column).IsToRight)
            {
                m_data.Move(1, 0);
            }
            else
            {
                m_data.Move(-1, 0);
            }
        }
    }
Exemplo n.º 5
0
 private static void ResetMap()
 {
     for (int i = 0; i < m_map.Row; i++)
     {
         for (int j = 0; j < m_map.Column; j++)
         {
             AStarMapCell mc = m_map.GetCell(i, j);
             if (mc != null)
             {
                 mc.IsInOpenList  = false;
                 mc.IsInCloseList = false;
                 mc.SetParent(null);
                 mc.H = 0;
                 mc.G = 0;
             }
         }
     }
 }
    static void SetAStarMapCell_First(MapData map, MapCellData cell, ref AStarMap astarMap)
    {
        AStarMapCell astarMapCell = astarMap.GetCell(cell.row, cell.col);

        astarMapCell.IsToLeftUp    = false;
        astarMapCell.IsToLeftDown  = false;
        astarMapCell.IsToRightUp   = false;
        astarMapCell.IsToRightDown = false;

        //check up
        if (cell.row + 1 < map.row)
        {
            MapCellData  upCell      = map.cells [(cell.row + 1) * map.column + cell.col];
            AStarMapCell upAStarCell = astarMap.GetCell(cell.row + 1, cell.col);
            switch ((CELL_TYPE)cell.cellType)
            {
            case CELL_TYPE.WALL:
                upAStarCell.IsToDown = false;
                astarMapCell.IsToUp  = false;
                break;

            case CELL_TYPE.STONE:
                upAStarCell.IsToDown = false;
                astarMapCell.IsToUp  = false;
                break;

            case CELL_TYPE.LADDER:
                switch ((CELL_TYPE)upCell.cellType)
                {
                case CELL_TYPE.WALL:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = false;
                    break;

                case CELL_TYPE.STONE:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = false;
                    break;

                case CELL_TYPE.LADDER:
                    upAStarCell.IsToDown = true;
                    astarMapCell.IsToUp  = true;
                    break;

                case CELL_TYPE.POLE:
                    upAStarCell.IsToDown = true;
                    astarMapCell.IsToUp  = true;
                    break;

                case CELL_TYPE.NONE:
                    upAStarCell.IsToDown = true;
                    astarMapCell.IsToUp  = true;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    upAStarCell.IsToDown = true;
                    astarMapCell.IsToUp  = true;
                    break;
                }
                break;

            case CELL_TYPE.POLE:
                switch ((CELL_TYPE)upCell.cellType)
                {
                case CELL_TYPE.WALL:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = false;
                    break;

                case CELL_TYPE.STONE:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = false;
                    break;

                case CELL_TYPE.LADDER:
                    upAStarCell.IsToDown = true;
                    astarMapCell.IsToUp  = true;
                    break;

                case CELL_TYPE.POLE:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = false;
                    break;

                case CELL_TYPE.NONE:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = true;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = true;
                    break;
                }
                break;

            case CELL_TYPE.NONE:
                switch ((CELL_TYPE)upCell.cellType)
                {
                case CELL_TYPE.WALL:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = false;
                    break;

                case CELL_TYPE.STONE:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = false;
                    break;

                case CELL_TYPE.LADDER:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = true;
                    break;

                case CELL_TYPE.POLE:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = true;
                    break;

                case CELL_TYPE.NONE:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = true;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = true;
                    break;
                }
                break;

            default:            //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                switch ((CELL_TYPE)upCell.cellType)
                {
                case CELL_TYPE.WALL:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = false;
                    break;

                case CELL_TYPE.STONE:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = false;
                    break;

                case CELL_TYPE.LADDER:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = true;
                    break;

                case CELL_TYPE.POLE:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = true;
                    break;

                case CELL_TYPE.NONE:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = true;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    upAStarCell.IsToDown = false;
                    astarMapCell.IsToUp  = true;
                    break;
                }
                break;
            }
        }
        //down
        if (cell.row - 1 >= 0)
        {
            MapCellData  downCell      = map.cells [(cell.row - 1) * map.column + cell.col];
            AStarMapCell downAStarCell = astarMap.GetCell(cell.row - 1, cell.col);
            switch ((CELL_TYPE)cell.cellType)
            {
            case CELL_TYPE.WALL:
                astarMapCell.IsToDown = false;
                downAStarCell.IsToUp  = false;
                break;

            case CELL_TYPE.STONE:
                astarMapCell.IsToDown = false;
                downAStarCell.IsToUp  = false;
                break;

            case CELL_TYPE.LADDER:
                switch ((CELL_TYPE)downCell.cellType)
                {
                case CELL_TYPE.WALL:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = false;
                    break;

                case CELL_TYPE.STONE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = false;
                    break;

                case CELL_TYPE.LADDER:
                    astarMapCell.IsToDown = true;
                    downAStarCell.IsToUp  = true;
                    break;

                case CELL_TYPE.POLE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;

                case CELL_TYPE.NONE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;
                }
                break;

            case CELL_TYPE.POLE:
                switch ((CELL_TYPE)downCell.cellType)
                {
                case CELL_TYPE.WALL:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = false;
                    break;

                case CELL_TYPE.STONE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = false;
                    break;

                case CELL_TYPE.LADDER:
                    astarMapCell.IsToDown = true;
                    downAStarCell.IsToUp  = true;
                    break;

                case CELL_TYPE.POLE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;

                case CELL_TYPE.NONE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;
                }
                break;

            case CELL_TYPE.NONE:
                switch ((CELL_TYPE)downCell.cellType)
                {
                case CELL_TYPE.WALL:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = false;
                    break;

                case CELL_TYPE.STONE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = false;
                    break;

                case CELL_TYPE.LADDER:
                    astarMapCell.IsToDown = true;
                    downAStarCell.IsToUp  = true;
                    break;

                case CELL_TYPE.POLE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;

                case CELL_TYPE.NONE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;
                }
                break;

            default:            //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                switch ((CELL_TYPE)downCell.cellType)
                {
                case CELL_TYPE.WALL:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = false;
                    break;

                case CELL_TYPE.STONE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = false;
                    break;

                case CELL_TYPE.LADDER:
                    astarMapCell.IsToDown = true;
                    downAStarCell.IsToUp  = true;
                    break;

                case CELL_TYPE.POLE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;

                case CELL_TYPE.NONE:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    astarMapCell.IsToDown = false;
                    downAStarCell.IsToUp  = true;
                    break;
                }
                break;
            }
        }
        //check left
        if (cell.col - 1 >= 0)
        {
            MapCellData  leftCell      = map.cells [(cell.row) * map.column + cell.col - 1];
            AStarMapCell leftAStarCell = astarMap.GetCell(cell.row, cell.col - 1);
            switch ((CELL_TYPE)cell.cellType)
            {
            case CELL_TYPE.WALL:
                leftAStarCell.IsToRight = false;
                astarMapCell.IsToLeft   = false;
                break;

            case CELL_TYPE.STONE:
                leftAStarCell.IsToRight = false;
                astarMapCell.IsToLeft   = false;
                break;

            case CELL_TYPE.LADDER:
                switch ((CELL_TYPE)leftCell.cellType)
                {
                case CELL_TYPE.WALL:
                    leftAStarCell.IsToRight = false;
                    astarMapCell.IsToLeft   = false;
                    break;

                case CELL_TYPE.STONE:
                    leftAStarCell.IsToRight = false;
                    astarMapCell.IsToLeft   = false;
                    break;

                case CELL_TYPE.LADDER:
                    leftAStarCell.IsToRight = true;
                    astarMapCell.IsToLeft   = true;
                    break;

                case CELL_TYPE.POLE:
                    leftAStarCell.IsToRight = true;
                    astarMapCell.IsToLeft   = true;
                    break;

                case CELL_TYPE.NONE:
                    leftAStarCell.IsToRight = true;
                    astarMapCell.IsToLeft   = false;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    leftAStarCell.IsToRight = true;
                    astarMapCell.IsToLeft   = false;
                    break;
                }
                break;

            case CELL_TYPE.POLE:
                switch ((CELL_TYPE)leftCell.cellType)
                {
                case CELL_TYPE.WALL:
                    leftAStarCell.IsToRight = false;
                    astarMapCell.IsToLeft   = false;
                    break;

                case CELL_TYPE.STONE:
                    leftAStarCell.IsToRight = false;
                    astarMapCell.IsToLeft   = false;
                    break;

                case CELL_TYPE.LADDER:
                    leftAStarCell.IsToRight = true;
                    astarMapCell.IsToLeft   = true;
                    break;

                case CELL_TYPE.POLE:
                    leftAStarCell.IsToRight = true;
                    astarMapCell.IsToLeft   = true;
                    break;

                case CELL_TYPE.NONE:
                    leftAStarCell.IsToRight = true;
                    astarMapCell.IsToLeft   = false;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    leftAStarCell.IsToRight = true;
                    astarMapCell.IsToLeft   = false;
                    break;
                }
                break;

            default:            //case CELL_TYPE.NONE || CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                switch ((CELL_TYPE)leftCell.cellType)
                {
                case CELL_TYPE.WALL:
                    leftAStarCell.IsToRight = false;
                    astarMapCell.IsToLeft   = false;
                    break;

                case CELL_TYPE.STONE:
                    leftAStarCell.IsToRight = false;
                    astarMapCell.IsToLeft   = false;
                    break;

                case CELL_TYPE.LADDER:
                    leftAStarCell.IsToRight = false;
                    astarMapCell.IsToLeft   = true;
                    break;

                case CELL_TYPE.POLE:
                    leftAStarCell.IsToRight = false;
                    astarMapCell.IsToLeft   = true;
                    break;

                case CELL_TYPE.NONE:
                    leftAStarCell.IsToRight = false;
                    astarMapCell.IsToLeft   = false;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    leftAStarCell.IsToRight = false;
                    astarMapCell.IsToLeft   = false;
                    break;
                }
                break;
            }
        }
        //check right
        if (cell.col + 1 < map.column)
        {
            MapCellData  rightCell      = map.cells [(cell.row) * map.column + cell.col + 1];
            AStarMapCell rightAStarCell = astarMap.GetCell(cell.row, cell.col + 1);
            switch ((CELL_TYPE)cell.cellType)
            {
            case CELL_TYPE.WALL:
                astarMapCell.IsToRight  = false;
                rightAStarCell.IsToLeft = false;
                break;

            case CELL_TYPE.STONE:
                astarMapCell.IsToRight  = false;
                rightAStarCell.IsToLeft = false;
                break;

            case CELL_TYPE.LADDER:
                switch ((CELL_TYPE)rightCell.cellType)
                {
                case CELL_TYPE.WALL:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = false;
                    break;

                case CELL_TYPE.STONE:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = false;
                    break;

                case CELL_TYPE.LADDER:
                    astarMapCell.IsToRight  = true;
                    rightAStarCell.IsToLeft = true;
                    break;

                case CELL_TYPE.POLE:
                    astarMapCell.IsToRight  = true;
                    rightAStarCell.IsToLeft = true;
                    break;

                case CELL_TYPE.NONE:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = true;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = true;
                    break;
                }
                break;

            case CELL_TYPE.POLE:
                switch ((CELL_TYPE)rightCell.cellType)
                {
                case CELL_TYPE.WALL:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = false;
                    break;

                case CELL_TYPE.STONE:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = false;
                    break;

                case CELL_TYPE.LADDER:
                    astarMapCell.IsToRight  = true;
                    rightAStarCell.IsToLeft = true;
                    break;

                case CELL_TYPE.POLE:
                    astarMapCell.IsToRight  = true;
                    rightAStarCell.IsToLeft = true;
                    break;

                case CELL_TYPE.NONE:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = true;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = true;
                    break;
                }
                break;

            default:            //case CELL_TYPE.NONE || CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                switch ((CELL_TYPE)rightCell.cellType)
                {
                case CELL_TYPE.WALL:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = false;
                    break;

                case CELL_TYPE.STONE:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = false;
                    break;

                case CELL_TYPE.LADDER:
                    astarMapCell.IsToRight  = true;
                    rightAStarCell.IsToLeft = false;
                    break;

                case CELL_TYPE.POLE:
                    astarMapCell.IsToRight  = true;
                    rightAStarCell.IsToLeft = false;
                    break;

                case CELL_TYPE.NONE:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = false;
                    break;

                default:                //case CELL_TYPE.BRA || CELL_TYPE.UNDERPANT || CELL_TYPE.SOCKS:
                    astarMapCell.IsToRight  = false;
                    rightAStarCell.IsToLeft = false;
                    break;
                }
                break;
            }
        }
    }
Exemplo n.º 7
0
 // Use this for initialization
 void Start()
 {
     map = AStarMapStream.Read(Application.dataPath + "/scene1.txt");
     map.CalculateCellPosition(new Vector2(2.56f, 2.56f), Vector2.zero);
     path = AStar.CalculatePath(map.GetCell(0, 0), map.GetCell(map.Row - 1, map.Column - 1), map);
 }
Exemplo n.º 8
0
    public static List <AStarMapCell> CalculatePath(AStarMapCell start, AStarMapCell end, AStarMap map)
    {
        m_openList.Clear();
        m_closeList.Clear();
        m_map = map;
        ResetMap();

        start.G = 0;
        start.CalculateH(end);

        m_openList.Add(start);
        start.IsInOpenList = true;

        while (true)
        {
            int          nearestIndex = FindNearestCellInOpenList();
            AStarMapCell cur;
            if (nearestIndex >= 0)
            {
                cur = m_openList [nearestIndex];
                if (cur.Equals(end))
                {
                    return(BacktrackPath(cur));
                }
            }
            else
            {
                return(new List <AStarMapCell> ());
            }

            for (int i = cur.Row - 1; i <= cur.Row + 1; i++)
            {
                for (int j = cur.Column - 1; j <= cur.Column + 1; j++)
                {
                    if (i >= 0 && i < m_map.Row && j >= 0 && j < m_map.Column)
                    {
                        AStarMapCell mc = m_map.GetCell(i, j);

                        if (mc != null && !mc.IsInCloseList && mc.CanArrive(cur))
                        {
                            if (mc.IsInOpenList)
                            {
                                int dist = cur.CalculateNeighborDist(mc);
                                if (cur.G + dist < mc.G)
                                {
                                    mc.G = cur.G + dist;
                                    mc.SetParent(cur);
                                }
                            }
                            else
                            {
                                mc.SetParent(cur);
                                mc.CalculateH(end);
                                mc.CalculateG(cur);
                                mc.IsInOpenList = true;
                                m_openList.Add(mc);
                            }
                        }
                    }
                }
            }
            m_openList.RemoveAt(nearestIndex);
            cur.IsInOpenList = false;
            m_closeList.Add(cur);
            cur.IsInCloseList = true;
        }
        return(new List <AStarMapCell> ());
    }