Ignore:
Timestamp:
2015-06-09 21:12:52 (8 years ago)
Author:
sieerinn
Message:

Reitinlöytö toimii.

Location:
2015/24/ohjaajat/Dungeon/Dungeon/Dungeon
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 2015/24/ohjaajat/Dungeon/Dungeon/Dungeon/Dungeon.cs

    r5968 r5969  
    164164        Keyboard.Listen(Key.F1, ButtonState.Pressed, Begin, null); 
    165165 
    166         // Testi haku huoneesta (0, 0) -> (5, 5). 
    167         Keyboard.Listen(Key.F2, ButtonState.Pressed, delegate 
    168         { 
    169             FindPath(new Point(0, 0), new Point(5, 5)); 
    170         }, null); 
    171  
    172166        Keyboard.Listen(Key.Space, ButtonState.Pressed, SeuraavaAalto, "Anna kivan barbaariaallon tulla"); 
    173167    } 
     
    292286            digArrowHead.Angle = suunta.Angle - Angle.FromDegrees(90); 
    293287        } 
     288 
    294289        base.Update(time); 
    295290    } 
    296291 
     292    protected override void Paint(Canvas canvas) 
     293    { 
     294        canvas.BrushColor = Color.Purple; 
     295 
     296        var points = FindPath(new Point(6, 0), MuunnaJypelista(Mouse.PositionOnWorld)); 
     297        for (int i = 0; i < points.Count - 1; i++) 
     298        { 
     299            Vector v = new Vector(points[i].X - huoneet.GetLength(0) / 2, points[i].Y - huoneet.GetLength(1) / 2) * RUUDUN_KOKO; 
     300            Vector v2 = new Vector(points[i+1].X - huoneet.GetLength(0) / 2, points[i+1].Y - huoneet.GetLength(1) / 2) * RUUDUN_KOKO; 
     301            canvas.DrawLine(v, v2); 
     302        } 
     303 
     304        base.Paint(canvas); 
     305    } 
     306 
    297307    #region Reitinlöytö 
    298308 
    299     void FindPath(Point alku, Point loppu) 
    300     { 
     309    List<Point> FindPath(Point alku, Point loppu) 
     310    { 
     311        if (GetRoom(alku) == null || GetRoom(loppu) == null) 
     312            return new List<Point>(); 
     313 
    301314        bool[,] walkable = new bool[huoneet.GetLength(0), huoneet.GetLength(1)]; 
    302315        foreach (var paikka in RuutujenPaikat()) 
     
    304317            walkable[paikka.X, paikka.Y] = huoneet[paikka.X, paikka.Y].Dug; 
    305318        } 
    306         var finder = new AStar.PathFinder(new AStar.SearchParameters(alku, loppu, walkable)); 
    307         List<Point> path = finder.FindPath(); 
    308         // Ei ota huomioon ovia :( 
     319        var finder = new AStar.PathFinder(new AStar.SearchParameters(alku, loppu, walkable), OviTarkistus); 
     320        return finder.FindPath(); 
     321    } 
     322 
     323    bool OviTarkistus(Point a, Point b) 
     324    { 
     325        Room ra = GetRoom(a); 
     326        Room rb = GetRoom(b); 
     327        Direction suunta = (new Vector(b.X, b.Y) - new Vector(a.X, a.Y)).Angle.MainDirection; 
     328        if (ra != null && rb != null) 
     329        { 
     330            return ra.Walls[suunta].Image == reikaSeinaKuva && rb.Walls[Direction.Inverse(suunta)].Image == reikaSeinaKuva; 
     331        } 
     332        return false; 
    309333    } 
    310334 
  • 2015/24/ohjaajat/Dungeon/Dungeon/Dungeon/Pathfinding/PathFinder.cs

    r5967 r5969  
    1616        private Node endNode; 
    1717        private SearchParameters searchParameters; 
     18        private Func<Point, Point, bool> extraCheck; 
    1819 
    1920        /// <summary> 
     
    2122        /// </summary> 
    2223        /// <param name="searchParameters"></param> 
    23         public PathFinder(SearchParameters searchParameters) 
     24        public PathFinder(SearchParameters searchParameters, Func<Point, Point, bool> extraCheck) 
    2425        { 
     26            this.extraCheck = extraCheck; 
    2527            this.searchParameters = searchParameters; 
    2628            InitializeNodes(searchParameters.Map); 
     
    134136                    continue; 
    135137 
     138                if (!extraCheck(fromNode.Location, location)) 
     139                    continue; 
     140 
    136141                // Already-open nodes are only added to the list if their G-value is lower going via this route. 
    137142                if (node.State == NodeState.Open) 
     
    166171            return new Point[] 
    167172            { 
    168                 new Point(fromLocation.X-1, fromLocation.Y-1), 
     173                //new Point(fromLocation.X-1, fromLocation.Y-1), 
    169174                new Point(fromLocation.X-1, fromLocation.Y  ), 
    170                 new Point(fromLocation.X-1, fromLocation.Y+1), 
     175                //new Point(fromLocation.X-1, fromLocation.Y+1), 
    171176                new Point(fromLocation.X,   fromLocation.Y+1), 
    172                 new Point(fromLocation.X+1, fromLocation.Y+1), 
     177                //new Point(fromLocation.X+1, fromLocation.Y+1), 
    173178                new Point(fromLocation.X+1, fromLocation.Y  ), 
    174                 new Point(fromLocation.X+1, fromLocation.Y-1), 
     179                //new Point(fromLocation.X+1, fromLocation.Y-1), 
    175180                new Point(fromLocation.X,   fromLocation.Y-1) 
    176181            }; 
Note: See TracChangeset for help on using the changeset viewer.