Example #1
0
        //class NodeAStar
        //{
        //    public GraphPoint<NaviPoint> point;
        //    public NodeAStar father;

        //    public float G;
        //    public float H;
        //    public float F;

        //    public NodeAStar ( GraphPoint<NaviPoint> point, NodeAStar father )
        //    {
        //        this.point = point;
        //        this.father = father;
        //        this.G = 0;
        //        this.H = 0;
        //        this.F = 0;
        //    }
        //}

        //private void CalPath ()
        //{
        //    if (naviMap == null)
        //        return;

        //    #region 复制导航图
        //    GraphPoint<NaviPoint>[] map = new GraphPoint<NaviPoint>[naviMap.Map.Length + 2];
        //    GraphPoint<NaviPoint>[] temp = GraphPoint<NaviPoint>.DepthCopy( naviMap.Map );
        //    for (int i = 0; i < temp.Length; i++)
        //    {
        //        map[i] = temp[i];
        //    }
        //    #endregion

        //    #region 将当前点和目标点加入到导航图中
        //    int prePointSum = temp.Length;
        //    Vector2 curPos = orderServer.Pos;
        //    GraphPoint<NaviPoint> curNaviPoint = new GraphPoint<NaviPoint>( new NaviPoint( null, -1, curPos ), new List<GraphPath<NaviPoint>>() );
        //    GraphPoint<NaviPoint> aimNaviPoint = new GraphPoint<NaviPoint>( new NaviPoint( null, -1, aimPos ), new List<GraphPath<NaviPoint>>() );
        //    AddCurPosToNaviMap( map, curNaviPoint, prePointSum, naviMap.GuardLines, naviMap.BorderLines );
        //    AddAimPosToNaviMap( map, aimNaviPoint, curNaviPoint, prePointSum, naviMap.GuardLines, naviMap.BorderLines );

        //    #endregion

        //    #region 计算最短路径,使用A*算法

        //    List<NodeAStar> open = new List<NodeAStar>();
        //    List<NodeAStar> close = new List<NodeAStar>();
        //    open.Add( new NodeAStar( curNaviPoint, null ) );

        //    NodeAStar cur = null;
        //    while (open.Count != 0)
        //    {
        //        cur = open[open.Count - 1];

        //        if (cur.point == aimNaviPoint)
        //            break;

        //        open.RemoveAt( open.Count - 1 );
        //        close.Add( cur );

        //        foreach (GraphPath<NaviPoint> path in cur.point.neighbors)
        //        {
        //            if (Contains( close, path.neighbor ))
        //            {
        //                continue;
        //            }
        //            else
        //            {
        //                NodeAStar inOpenNode;
        //                if (Contains( open, path.neighbor, out inOpenNode ))
        //                {
        //                    float G = cur.G + path.weight;
        //                    if (inOpenNode.G > G)
        //                    {
        //                        inOpenNode.G = G;
        //                        inOpenNode.F = G + inOpenNode.H;
        //                    }
        //                }
        //                else
        //                {
        //                    NodeAStar childNode = new NodeAStar( path.neighbor, cur );
        //                    childNode.G = cur.G + path.weight;
        //                    childNode.H = Vector2.Distance( aimPos, childNode.point.value.Pos );
        //                    childNode.F = childNode.G + childNode.H;
        //                    SortInsert( open, childNode );
        //                }
        //            }
        //        }
        //    }

        //    if (cur == null)
        //        return;

        //    Stack<NodeAStar> cahe = new Stack<NodeAStar>();
        //    while (cur.father != null)
        //    {
        //        cahe.Push( cur );
        //        cur = cur.father;
        //    }

        //    this.path = new NaviPoint[cahe.Count];
        //    int j = 0;
        //    foreach (NodeAStar node in cahe)
        //    {
        //        this.path[j] = node.point.value;
        //    }
        //    curPathIndex = 0;

        //    #endregion
        //}

        //private void SortInsert ( List<NodeAStar> open, NodeAStar childNode )
        //{
        //    int i = 0;
        //    while (i < open.Count && open[i].F > childNode.F)
        //    {
        //        i++;
        //    }
        //    if (i == open.Count)
        //        open.Add( childNode );
        //    else
        //        open.Insert( i, childNode );
        //}

        //private bool Contains ( List<NodeAStar> list, GraphPoint<NaviPoint> graphPoint, out NodeAStar findNode )
        //{
        //    if ((findNode = list.Find( new Predicate<NodeAStar>(
        //        delegate( NodeAStar node )
        //        {
        //            if (node.point == graphPoint)
        //                return true;
        //            else
        //                return false;
        //        } ) )) == null)
        //        return false;
        //    else
        //        return true;
        //}

        //private bool Contains ( List<NodeAStar> list, GraphPoint<NaviPoint> graphPoint )
        //{
        //    if (list.Find( new Predicate<NodeAStar>(
        //        delegate( NodeAStar node )
        //        {
        //            if (node.point == graphPoint)
        //                return true;
        //            else
        //                return false;
        //        } ) ) == null)
        //        return false;
        //    else
        //        return true;
        //}

        //private void AddCurPosToNaviMap ( GraphPoint<NaviPoint>[] map, GraphPoint<NaviPoint> curNaviP,
        //    int prePointSum, List<Segment> guardLines, List<Segment> borderLines )
        //{
        //    map[prePointSum] = curNaviP;
        //    for (int i = 0; i < prePointSum; i++)
        //    {
        //        Segment seg = new Segment( curNaviP.value.Pos, map[i].value.Pos );

        //        bool cross = false;
        //        foreach (Segment guardLine in guardLines)
        //        {
        //            if (Segment.IsCross( guardLine, seg ))
        //            {
        //                cross = true;
        //                break;
        //            }
        //        }

        //        if (!cross)
        //        {
        //            foreach (Segment borderLine in borderLines)
        //            {
        //                if (Segment.IsCross( borderLine, seg ))
        //                {
        //                    cross = true;
        //                    break;
        //                }

        //            }
        //        }

        //        if (!cross)
        //        {
        //            float weight = Vector2.Distance( curNaviP.value.Pos, map[i].value.Pos );
        //            GraphPoint<NaviPoint>.Link( map[i], curNaviP, weight );
        //        }
        //    }
        //}

        //private void AddAimPosToNaviMap ( GraphPoint<NaviPoint>[] map, GraphPoint<NaviPoint> aimNaviP, GraphPoint<NaviPoint> curNaviP,
        //    int prePointSum, List<Segment> guardLines, List<Segment> borderLines )
        //{
        //    map[prePointSum + 1] = aimNaviP;
        //    for (int i = 0; i < prePointSum; i++)
        //    {
        //        Segment seg = new Segment( aimNaviP.value.Pos, map[i].value.Pos );

        //        bool cross = false;
        //        foreach (Segment guardLine in guardLines)
        //        {
        //            if (Segment.IsCross( guardLine, seg ))
        //            {
        //                cross = true;
        //                break;
        //            }
        //        }

        //        if (!cross)
        //        {
        //            foreach (Segment borderLine in borderLines)
        //            {
        //                if (Segment.IsCross( borderLine, seg ))
        //                {
        //                    cross = true;
        //                    break;
        //                }

        //            }
        //        }

        //        if (!cross)
        //        {
        //            float weight = Vector2.Distance( aimNaviP.value.Pos, map[i].value.Pos );
        //            GraphPoint<NaviPoint>.Link( map[i], aimNaviP, weight );
        //        }
        //    }

        //    Segment curToAim = new Segment( curNaviP.value.Pos, aimNaviP.value.Pos );

        //    bool link = true;
        //    foreach (Segment guardLine in guardLines)
        //    {
        //        if (Segment.IsCross( guardLine, curToAim ))
        //        {
        //            if (MathTools.Vector2Cross( guardLine.endPoint - guardLine.startPoint, curNaviP.value.Pos - guardLine.endPoint ) < 0)
        //            {
        //                link = false;
        //                break;
        //            }
        //        }
        //    }

        //    if (link)
        //    {
        //        foreach (Segment borderLine in borderLines)
        //        {
        //            if (Segment.IsCross( borderLine, curToAim ))
        //            {
        //                link = false;
        //                break;
        //            }

        //        }
        //    }

        //    if (link)
        //    {
        //        float weight = Vector2.Distance( curNaviP.value.Pos, aimNaviP.value.Pos );
        //        GraphPoint<NaviPoint>.Link( curNaviP, aimNaviP, weight );
        //    }
        //}

        private void UpdateNaviMap()
        {
            this.naviMap = orderServer.CalNavigateMap(new NaviMapConsiderObj(
                                                          delegate(EyeableBorderObjInfo obj)
            {
                return(true);
            }), mapSize, orderServer.TankWidth * 0.6f);
        }
Example #2
0
        public void Update(float seconds)
        {
            if (InputHandler.IsKeyDown(Keys.W))
            {
                orderServer.ForwardSpeed = 1000;
            }
            else if (InputHandler.IsKeyDown(Keys.S))
            {
                orderServer.ForwardSpeed = -1000;
            }
            else
            {
                orderServer.ForwardSpeed = 0;
            }

            if (InputHandler.IsKeyDown(Keys.D))
            {
                orderServer.TurnRightSpeed = 20;
            }
            else if (InputHandler.IsKeyDown(Keys.A))
            {
                orderServer.TurnRightSpeed = -20;
            }
            else
            {
                orderServer.TurnRightSpeed = 0;
            }


            if (InputHandler.MouseJustPressRight)
            {
                action.AddOrder(new OrderRotaRaderToPos(InputHandler.GetCurMousePosInLogic(BaseGame.RenderEngine)));
            }

            action.AddOrder(new OrderRotaTurretToPos(InputHandler.GetCurMousePosInLogic(BaseGame.RenderEngine)));


            //if (InputHandler.JustPressKey( Keys.C ))
            //{
            foreach (EyeableBorderObjInfo borderObj in orderServer.EyeableBorderObjInfos)
            {
                borderObj.UpdateConvexHall(10);
            }
            //}

            //if (InputHandler.JustPressKey( Keys.N ))
            //{

            naviMap = orderServer.CalNavigateMap(
                delegate(EyeableBorderObjInfo obj)
            {
                //if (((SceneCommonObjInfo)(obj.EyeableInfo.ObjInfo.SceneInfo)).isTankObstacle)
                return(true);
                //else
                //return false;
            }, commonServer.MapBorder, 5);
            //}

            action.Update(seconds);

            itemDisappeared = false;
            foreach (EyeableBorderObjInfo borderObjInfo in orderServer.EyeableBorderObjInfos)
            {
                if (borderObjInfo.EyeableInfo.ObjInfo.ObjClass == "Item")
                {
                    if (borderObjInfo.IsDisappeared)
                    {
                        itemDisappeared = true;
                    }
                }
            }

            seeItem = false;
            foreach (IEyeableInfo eyeableInfo in orderServer.GetEyeableInfo())
            {
                if (eyeableInfo.ObjInfo.ObjClass == "Item")
                {
                    seeItem = true;
                }
            }
        }