Exemplo n.º 1
0
 public void UpdateMove()
 {
     lock (typeof(MoveObject))
     {
         if (!GetCollisionArea().Contains(touchX, touchY))
         {
             if (findPath != null)
             {
                 CollectionUtils.Clear(findPath);
             }
             findPath = AStarFinder
                        .Find(heuristic,
                              tiles.GetField(),
                              tiles.PixelsToTilesWidth(X()),
                              tiles.PixelsToTilesHeight(Y()),
                              tiles.PixelsToTilesWidth(touchX
                                                       - tiles.GetOffset().x),
                              tiles.PixelsToTilesHeight(touchY
                                                        - tiles.GetOffset().y), allDirection);
         }
         else if (findPath != null)
         {
             CollectionUtils.Clear(findPath);
         }
     }
 }
Exemplo n.º 2
0
        public override void Enter()
        {
            var pathFinder = new AStarFinder(Heuristic.Manhattan, npc.IsWalkable);

            pathFinder.FindPathAsync(npc.CurrentPosition, npc.HomePosition).ContinueWith(t =>
            {
                var path = t.Result;
                if (path == null)
                {
                    WriteLog("Path not found! (" + npc.CurrentPosition + " => " + npc.HomePosition + ")");

                    var f = new AStarFinder(Heuristic.Manhattan, (x, y) => true);
                    path  = f.FindPath(npc.CurrentPosition, npc.HomePosition);

                    if (path == null)
                    {
                        WriteLog("Safe path not found! (" + npc.CurrentPosition + " => " + npc.HomePosition + ")");
                    }
                }

                _movement = new PathMovement(path);
                _movement.Start(npc);
            });

            base.Enter();
        }
Exemplo n.º 3
0
        //function that determines which loads are valid to keep and which are not
        private void KeepValidLoads(GridPos EndPoint)
        {
            int  list_index = 0;
            bool removed;

            for (int i = 0; i < loadPos.Count; i++)
            {
                searchGrid.SetWalkableAt(loadPos[i], true); //assumes that all loads are walkable
            }
            //and only walls are in fact the only obstacles in the grid

            do
            {
                removed = false;
                jumpParam.Reset(loadPos[list_index], EndPoint);                   //tries to find path between each Load and the exit
                if (AStarFinder.FindPath(jumpParam, nud_weight.Value).Count == 0) //if no path is found
                {
                    isLoad[loadPos[list_index].x, loadPos[list_index].y] = 2;     //mark the corresponding load as NOT available
                    loadPos.RemoveAt(list_index);                                 //remove that load from the list
                    removed = true;
                }
                if (!removed)
                {
                    list_index++;
                }
            } while (list_index < loadPos.Count); //loop repeats untill all loads are checked

            if (loadPos.Count == 0)
            {
                mapHasLoads = false;
            }
        }
Exemplo n.º 4
0
        public void Run()
        {
            _graph = new Graph <Pos>();
            _graph.Clear();
            _positions = new List <Pos>();
            _rng       = new Random();

            CreateNodes();
            CreateLinks();

            Console.WriteLine(_graph);

            var finder = new AStarFinder <Pos>();

            finder.SetCallback(this);
            var path = finder.Search(_graph, new Pos(0, Bounds / 2), new Pos(Bounds - 1, Bounds / 2));

            if (path == null)
            {
                Console.WriteLine("No solution! Try again...");
                return;
            }

            DrawMap(path.ToList());
        }
Exemplo n.º 5
0
 /// <summary>
 /// Returns the AStarFinder static singleton instance
 /// </summary>
 /// <returns></returns>
 public static AStarFinder getInstance()
 {
     if (finder == null)
     {
         finder = new AStarFinder();
     }
     return(finder);
 }
Exemplo n.º 6
0
 public override void OnLoad()
 {
     if (layerMap == null || original == null)
     {
         return;
     }
     if (!original.GetRectBox().Contains(endLocation.X(), endLocation.Y()))
     {
         if (useCache)
         {
             lock (pathCache)
             {
                 if (pathCache.Count > Loon.Core.LSystem.DEFAULT_MAX_CACHE_SIZE * 10)
                 {
                     pathCache.Clear();
                 }
                 Int32           key        = GetHashCode();
                 List <Vector2f> final_path = (List <Vector2f>)CollectionUtils.Get(pathCache, key);
                 if (final_path == null)
                 {
                     final_path = AStarFinder.Find(heuristic,
                                                   layerMap,
                                                   layerMap.PixelsToTilesWidth(startLocation
                                                                               .X()),
                                                   layerMap.PixelsToTilesHeight(startLocation
                                                                                .Y()),
                                                   layerMap.PixelsToTilesWidth(endLocation
                                                                               .X()),
                                                   layerMap.PixelsToTilesHeight(endLocation
                                                                                .Y()), flag);
                     CollectionUtils.Put(pathCache, key, final_path);
                 }
                 pActorPath = new List <Vector2f>();
                 CollectionUtils.AddAll(final_path, pActorPath);
             }
         }
         else
         {
             pActorPath = Loon.Action.Map.AStarFinder.Find(heuristic, layerMap,
                                                           layerMap.PixelsToTilesWidth(startLocation.X()),
                                                           layerMap.PixelsToTilesHeight(startLocation.Y()),
                                                           layerMap.PixelsToTilesWidth(endLocation.X()),
                                                           layerMap.PixelsToTilesHeight(endLocation.Y()), flag);
         }
     }
 }
Exemplo n.º 7
0
    /// <summary>
    /// 开始一场战斗
    /// </summary>
    /// <param name="isPvp">比赛性质,pve还是pvp</param>
    /// <param name="treasures">宝箱数量</param>
    public void CreateFight(bool isPvp, int treasures = 0)
    {
        this.State = FightState.Init;
        if (this.Fighters == null)
        {
            EDebug.LogError("FightLogic.CreateFight failed, fighters is null");
            return;
        }
        if (this.EnemyFighters == null)
        {
            EDebug.LogError("FightLogic.CreateFight failed, fighters is null");
            return;
        }
        if (this.Fighters.Count == 0)
        {
            EDebug.LogError("FightLogic.CreateFight failed, fighters is empty");
            return;
        }
        if (this.EnemyFighters.Count == 0)
        {
            EDebug.LogError("FightLogic.CreateFight failed, enemyFighters is empty");
            return;
        }

        this.TotalRound = this.EnemyFighters.Count;
        this.CurRound   = 0;
        this.IsPvp      = isPvp;
        this.Treasures  = treasures;
        this.DropMgrObj = new DropMgr();
        aStarFinder     = new AStarFinder(PathFinder.V_GRID, (this.TotalRound * 3 + 1) * PathFinder.H_GRID);

        List <FightUnit> allUnit = new List <FightUnit>();

        for (int idx = 0; idx < Fighters.Count; ++idx)
        {
            allUnit.Add(Fighters[idx]);
        }
        for (int idx = 0; idx < EnemyFighters.Count; ++idx)
        {
            for (int idx2 = 0; idx2 < EnemyFighters[idx].Count; ++idx2)
            {
                allUnit.Add(EnemyFighters[idx][idx2]);
            }
        }

        for (int idx = 0; idx < allUnit.Count; ++idx)
        {
            allUnit[idx].UID = ++UID;
        }
        ZEventSystem.Dispatch(EventConst.OnCreateFight, allUnit);

        //Test~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        for (int idx = 0; idx < Fighters.Count; ++idx)
        {
            FightUnit unit = Fighters[idx];
            _createFightUnitView(unit);
        }

        MapMgr.Instance.CreateFightMap(1, TotalRound);
        CamMgrObj = GameObject.Find("Main Camera").GetComponent <CamMgr>();

        //EndTest~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        //注册事件
        ZEventSystem.Register(EventConst.OnFightUnitDie, this, "OnUnitDie");
        ZEventSystem.Register(EventConst.OnFightStateChange, this, "OnFightStateChange");
        ZEventSystem.Register(EventConst.OnRequestUnitPause, this, "OnRequestUnitPause");
        ZEventSystem.Register(EventConst.OnFightMaskOver, this, "OnFightMaskOver");
        ZEventSystem.Register(EventConst.OnGamePause, this, "OnGamePause");

        //CamMgrObj.StartDissolve();
        CamMgrObj.PlayStartEffect();
        NextRound();
        ProcessCtrl.Instance.AddUpdate(this);
    }