如何在使用Cocos2D中实现A星(A*)寻路算法

转载自

http://www.cocoachina.com/applenews/devnews/2013/0424/6064.html



这篇blog是由iOS Tutorial Team的成员  Johann Fradj发表的,他目前是一位全职的资深iOS开发工程师。他还是Hot Apps Factory的创始人,该公司开发了App Cooker

 

添加A星寻路算法到简单的Cocos2D游戏中!

在本篇教程中,你将学到如何在一款简单的cocos2D游戏中使用A星寻路算法。


在学习本篇教程之前,如果你先阅读《A星(A*)寻路算法在iOS开发的运用》将会有所帮助。该文章图文并茂的介绍了这个我们将要实现的算法的基本概念。


在学习本篇教程之前,如果你有Cocos2D的iOS开发经验,将会有所帮助。如果没有也没关系,因为你可以将这里讲解的例子迁移到其他的语言或者框架中。


Maze猫

首先介绍下我们将要在本篇教程中开发的简单游戏。

 

前往下载本篇教程的工程代码。编译运行工程,你将看到以下画面。


 

在这款游戏中,你扮演着一只小偷猫,在一个由危险的狗守护着的地牢里小心穿行。如果你试图穿过一只狗,他会把你吃掉 – 除非你可以用骨头去贿赂它!

 

所以在这款游戏中,你的任务是尝试以正确的顺序捡起骨头,然后 寻找路线 穿过狗逃离。

 

注意到猫只能水平或者垂直的移动(例如不能斜线移动),并且会从一个方块的中心点移动到另一个中心点。每个方块既可以是可通行的也可以是不可通行的。

 

尝试下这款游戏,看看你能否找到出路!建议你阅读代码以熟悉它的原理。这是一款相当普通的方块-地图式游戏,我们会在接下来的教程中修改它并使用上A星寻路算法。

 

Maze猫和A星概览

正如你所看到的,当你点击地图某处时,猫会沿着你点击的方向跳到相邻的方块上。我们想对程序做修改,让猫持续的往你点击的方块方向前进,就像许多RPGs或者point-and-click冒险类游戏。

 

让我们看下控制触摸事件代码的工作原理。如果你打开HelloWorldLayer.m文件,你将看到像下面这样去实现触摸操作:

 

1.- (void)registerWithTouchDispatcher {
2.    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
3.}
4.- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
5. 
6.    if (_gameOver) return NO;
7. 
8.    CGPoint touchLocation = [_tileMap convertTouchToNodeSpace:touch];
9.    [_cat moveToward:touchLocation];
10.    return YES;
11.}


你可以看到这里只是对猫精灵调用了一个方法,让猫在方块地图上往你点击的地方移动。

 

我们现在要做的是修改在CatSprite.m文件中的以下方法,寻找到达该点的最短路径,并且开始前进:
1.- (void)moveToward:(CGPoint)target {
2.    // Figure out the shortest path to the target, and start following it!
3.}


创建ShortestPathStep类

我们开始创建一个内部类,代表路径上的一步操作。在这种情况下,它是一个方块和由A星算法计算出来的的F,G和H scores。 现在添加以下代码到CatSprite.m文件的顶部(在CatSprite的 @implementation上面):
1.// A class that represents a step of the computed path
2.@interface ShortestPathStep : NSObject
3.{
4.    CGPoint position;
5.    int gScore;
6.    int hScore;
7.    ShortestPathStep *parent;
8.}
9.@property (nonatomic, assign) CGPoint position;
10.@property (nonatomic, assign) int gScore;
11.@property (nonatomic, assign) int hScore;
12.@property (nonatomic, assign) ShortestPathStep *parent;
13. 
14.- (id)initWithPosition:(CGPoint)pos;
15.- (int)fScore;
16.@end


正如你所看到的,这是一个的简单类,它对以下内容做了跟踪记录:
•方块的坐标
•G score(记住,这是开始点到当前点之间的方块数目)
•H score (记住,这是当前点到终点之间的方块估算值)
•ShortestPathStep是它自身的前继
•F score,是方块的score值(它是F和G的和).

 

现在我们在CatSprite.m文件中添加实现代码(在@end上面).
1.@implementation ShortestPathStep
2.@synthesize position;
3.@synthesize gScore;
4.@synthesize hScore;
5.@synthesize parent;
6.- (id)initWithPosition:(CGPoint)pos
7.{
8.    if ((self = [super init])) {
9.        position = pos;
10.        gScore = 0;
11.        hScore = 0;
12.        parent = nil;
13.    }
14.    return self;
15.}
16.- (NSString *)description
17.{
18.    return [NSString stringWithFormat:@"%@  pos=[%.0f;%.0f]  g=%d  h=%d  f=%d", [super description], self.position.x, self.position.y, self.gScore, self.hScore, [self fScore]];
19.}
20.- (BOOL)isEqual:(ShortestPathStep *)other
21.{
22.    return CGPointEqualToPoint(self.position, other.position);
23.}
24.- (int)fScore
25.{
26.    return self.gScore + self.hScore;
27.}
28.@end


这方法的思路相当直接。我们重新定义了description方法,以方便debugging操作,然后创建了isEquals方法,当且仅当两个 ShortestPathSteps的CGPoint值相等时,它们相等(例如它们都代表同一个方块)。

 

创建Open和Closed列表

接下来我们使用两个NSMutableArray去记录open和closed列表。

 

你可能会奇怪为什么不使用NSMutableSet. 这里有两个原因:
1.NSMutableSet 的内部项不是排序的,但是我们希望列表按照F score的值去排列,以便于快速搜索。
2.NSMutableSet 不会在ShortestPathStep上调用isEqual方法去测试两个项是否相等(但是我们需要这么做)。

 

现在我们在CatSprite.h文件中添加那些数组的定义:
1.@interface CatSprite : CCSprite {
2.    //...
3. 
4.@private
5.    NSMutableArray *spOpenSteps;
6.    NSMutableArray *spClosedSteps;
7.}


然后在CatSprite.m文件中做以下修改:
1.// Add to top of file
2.// Private properties and methods
3.@interface CatSprite ()
4.@property (nonatomic, retain) NSMutableArray *spOpenSteps;
5.@property (nonatomic, retain) NSMutableArray *spClosedSteps;
6.@end
7.// Add after @implementation CatSprite
8.@synthesize spOpenSteps;
9.@synthesize spClosedSteps;
10.// Add inside initWithLayer
11.self.spOpenSteps = nil;
12.self.spClosedSteps = nil;
13.//Add dealloc method to CatSprite
14.- (void)dealloc
15.{
16.    [spOpenSteps release]; spOpenSteps = nil;
17.    [spClosedSteps release]; spClosedSteps = nil;
18.    [super dealloc];
19.}


检查开始和结束点

准备步骤结束了,现在重新实现moveToward方法吧。首先我们在在方块坐标中获取当前位置(point A)和目标位置(point B)。然后检查是否需要去计算一条路径,最后测试目标位置是否可到达(目前只有墙壁是不可以通行的)。

现在用以下代码替换掉moveToward方法中的内容:
1.- (void)moveToward:(CGPoint)target
2.{
3.    // Get current tile coordinate and desired tile coord
4.    CGPoint fromTileCoord = [_layer tileCoordForPosition:self.position];
5.    CGPoint toTileCoord = [_layer tileCoordForPosition:target];
6.    // Check that there is a path to compute ;-)
7.    if (CGPointEqualToPoint(fromTileCoord, toTileCoord)) {
8.        NSLog(@"You're already there! :P");
9.        return;
10.    }
11.    // Must check that the desired location is walkable
12.    // In our case it's really easy, because only wall are unwalkable
13.    if ([_layer isWallAtTileCoord:toTileCoord]) {
14.        [[SimpleAudioEngine sharedEngine] playEffect:@"hitWall.wav"];
15.        return;
16.    }
17.    NSLog(@"From: %@", NSStringFromCGPoint(fromTileCoord));
18.    NSLog(@"To: %@", NSStringFromCGPoint(toTileCoord));
19.}


编译运行,在地图上点击。如果你不是点击墙壁,在console界面你将看到“from”等于{24,0},这就是猫的位置。你同时将看到“to”的坐标是 (0,24),代表你在地图上点击的方块坐标。

 

实现A星算法

根据我们的算法,第一步是添加当前位置到open列表中。

我们还需要三个辅助方法:
1.一个方法是在open列表的恰当位置(由F scroe值控制排列)插入一个ShortestPathStep对象。
2.一个方法是计算从一个方块到相邻方块的移动数值。
3.一个方法是根据“city block”算法,计算一个方块的H score值。

我们打开CatSprite.m文件并作以下修改:

=”p213159″>
1.// In "private properties and methods" section
2.- (void)insertInOpenSteps:(ShortestPathStep *)step;
3.- (int)computeHScoreFromCoord:(CGPoint)fromCoord toCoord:(CGPoint)toCoord;
4.- (int)costToMoveFromStep:(ShortestPathStep *)fromStep toAdjacentStep:(ShortestPathStep *)toStep;
5.// Add these new methods after moveToward
6.// Insert a path step (ShortestPathStep) in the ordered open steps list (spOpenSteps)
7.- (void)insertInOpenSteps:(ShortestPathStep *)step
8.{
9.    int stepFScore = [step fScore]; // Compute the step's F score
10.    int count = [self.spOpenSteps count];
11.    int i = 0; // This will be the index at which we will insert the step
12.    for (; i >  count; i++) {
13.        if (stepFScore // Compute the H score from a position to another (from the current position to the final desired position
14.- (int)computeHScoreFromCoord:(CGPoint)fromCoord toCoord:(CGPoint)toCoord
15.{
16.    // Here we use the Manhattan method, which calculates the total number of step moved horizontally and vertically to reach the
17.    // final desired step from the current step, ignoring any obstacles that may be in the way
18.    return abs(toCoord.x - fromCoord.x) + abs(toCoord.y - fromCoord.y);
19.}
20.// Compute the cost of moving from a step to an adjacent one
21.- (int)costToMoveFromStep:(ShortestPathStep *)fromStep toAdjacentStep:(ShortestPathStep *)toStep
22.{
23.    // Because we can't move diagonally and because terrain is just walkable or unwalkable the cost is always the same.
24.    // But it have to be different if we can move diagonally and/or if there is swamps, hills, etc...
25.    return 1;
26.}


以上方法中的注释很好的介绍了相关原理,请花时间看下。

接下来,我们需要一个方法去提供方块所有可通行的相邻方块。因为在这款游戏中,HelloWorldLayer管理着地图,我们需要在那里添加方法。

现在在HelloWorldLayer.h文件中添加方法申明,在@interface后面做修改:
>
1.- (NSArray *)walkableAdjacentTilesCoordForTileCoord:(CGPoint)tileCoord;


然后在HelloWorldLayer.m文件中添加以下实现:
>
1.- (NSArray *)walkableAdjacentTilesCoordForTileCoord:(CGPoint)tileCoord
2.{
3.    NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:4];
4. 
5.    // Top
6.    CGPoint p = CGPointMake(tileCoord.x, tileCoord.y - 1);
7.    if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
8.        [tmp addObject:[NSValue valueWithCGPoint:p]];
9.    }
10. 
11.    // Left
12.    p = CGPointMake(tileCoord.x - 1, tileCoord.y);
13.    if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
14.        [tmp addObject:[NSValue valueWithCGPoint:p]];
15.    }
16. 
17.    // Bottom
18.    p = CGPointMake(tileCoord.x, tileCoord.y + 1);
19.    if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
20.        [tmp addObject:[NSValue valueWithCGPoint:p]];
21.    }
22. 
23.    // Right
24.    p = CGPointMake(tileCoord.x + 1, tileCoord.y);
25.    if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
26.        [tmp addObject:[NSValue valueWithCGPoint:p]];
27.    }
28. 
29.    return [NSArray arrayWithArray:tmp];
30.}


既然我们定义好了这些辅助函数,可以继续实现CatSprite.m文件中的moveToward方法。在moveToward方法的下面添加以下内容:
>
1.BOOL pathFound = NO;
2.self.spOpenSteps = [[[NSMutableArray alloc] init] autorelease];
3.self.spClosedSteps = [[[NSMutableArray alloc] init] autorelease];
4.// Start by adding the from position to the open list
5.[self insertInOpenSteps:[[[ShortestPathStep alloc] initWithPosition:fromTileCoord] autorelease]];
6.do {
7.    // Get the lowest F cost step
8.    // Because the list is ordered, the first step is always the one with the lowest F cost
9.    ShortestPathStep *currentStep = [self.spOpenSteps objectAtIndex:0];
10. 
11.    // Add the current step to the closed set
12.    [self.spClosedSteps addObject:currentStep];
13. 
14.    // Remove it from the open list
15.    // Note that if we wanted to first removing from the open list, care should be taken to the memory
16.    [self.spOpenSteps removeObjectAtIndex:0];
17. 
18.    // If the currentStep is the desired tile coordinate, we are done!
19.    if (CGPointEqualToPoint(currentStep.position, toTileCoord)) {
20. 
21.        pathFound = YES;
22.        ShortestPathStep *tmpStep = currentStep;
23.        NSLog(@"PATH FOUND :");
24.        do {
25.            NSLog(@"%@", tmpStep);
26.            tmpStep = tmpStep.parent; // Go backward
27.        } while (tmpStep != nil); // Until there is not more parent
28. 
29.        self.spOpenSteps = nil; // Set to nil to release unused memory
30.        self.spClosedSteps = nil; // Set to nil to release unused memory
31.        break;
32.    }
33. 
34.    // Get the adjacent tiles coord of the current step
35.    NSArray *adjSteps = [_layer walkableAdjacentTilesCoordForTileCoord:currentStep.position];
36.    for (NSValue *v in adjSteps) {
37.        ShortestPathStep *step = [[ShortestPathStep alloc] initWithPosition:[v CGPointValue]];
38. 
39.        // Check if the step isn't already in the closed set
40.        if ([self.spClosedSteps containsObject:step]) {
41.            [step release]; // Must releasing it to not leaking memory ;-)
42.            continue; // Ignore it
43.        }       
44. 
45.        // Compute the cost from the current step to that step
46.        int moveCost = [self costToMoveFromStep:currentStep toAdjacentStep:step];
47. 
48.        // Check if the step is already in the open list
49.        NSUInteger index = [self.spOpenSteps indexOfObject:step];
50. 
51.        if (index == NSNotFound) { // Not on the open list, so add it
52. 
53.            // Set the current step as the parent
54.            step.parent = currentStep;
55. 
56.            // The G score is equal to the parent G score + the cost to move from the parent to it
57.            step.gScore = currentStep.gScore + moveCost;
58. 
59.            // Compute the H score which is the estimated movement cost to move from that step to the desired tile coordinate
60.            step.hScore = [self computeHScoreFromCoord:step.position toCoord:toTileCoord];
61. 
62.            // Adding it with the function which is preserving the list ordered by F score
63.            [self insertInOpenSteps:step];
64. 
65.            // Done, now release the step
66.            [step release];
67.        }
68.        else { // Already in the open list
69. 
70.            [step release]; // Release the freshly created one
71.            step = [self.spOpenSteps objectAtIndex:index]; // To retrieve the old one (which has its scores already computed ;-)
72. 
73.            // Check to see if the G score for that step is lower if we use the current step to get there
74.            if ((currentStep.gScore + moveCost) >  step.gScore) {
75. 
76.                // The G score is equal to the parent G score + the cost to move from the parent to it
77.                step.gScore = currentStep.gScore + moveCost;
78. 
79.                // Because the G Score has changed, the F score may have changed too
80.                // So to keep the open list ordered we have to remove the step, and re-insert it with
81.                // the insert function which is preserving the list ordered by F score
82. 
83.                // We have to retain it before removing it from the list
84.                [step retain];
85. 
86.                // Now we can removing it from the list without be afraid that it can be released
87.                [self.spOpenSteps removeObjectAtIndex:index];
88. 
89.                // Re-insert it with the function which is preserving the list ordered by F score
90.                [self insertInOpenSteps:step];
91. 
92.                // Now we can release it because the oredered list retain it
93.                [step release];
94.            }
95.        }
96.    }
97. 
98.} while ([self.spOpenSteps count] > (0);
99.if (!pathFound) { // No path found
100.    [[SimpleAudioEngine sharedEngine] playEffect:@"hitWall.wav"];
101.}


上面代码中的注释很好的解释了实现原理。阅读完后,编译运行它吧!如果你点击了一个方块,你将看到以下画面:
 

你将在console栏看到以下信息:
>
1.  pos=[22;3]  g=9  h=0  f=9
2.  pos=[21;3]  g=8  h=1  f=9
3.  pos=[20;3]  g=7  h=2  f=9
4.  pos=[20;2]  g=6  h=3  f=9
5.  pos=[20;1]  g=5  h=4  f=9
6.  pos=[21;1]  g=4  h=3  f=7
7.  pos=[22;1]  g=3  h=2  f=5
8.  pos=[23;1]  g=2  h=3  f=5
9.  pos=[24;1]  g=1  h=4  f=5
10.  pos=[24;0]  g=0  h=0  f=0


注意路径是从后面开始建立的,所以你必须从下往上看,观察猫选择了哪条路径。我建议尽量与方块保持一致,这样你就可以看到最短路径的效果了!

 

沿着黄色方块路径前进

既然我们找到了路径,我们只需要让猫跟着前进即可。

我们现在要做的是记住整条路径,然后让猫沿着它一步步前进。

在CatSprite.h文件的@interface private部分创建一个数组去存储路径:
>
1.NSMutableArray *shortestPath;


然后对 CatSprite.m文件做以下修改:
>
1.// Add inside the CatSprite private properties and methods section
2.@property (nonatomic, retain) NSMutableArray *shortestPath;
3.// After the CatSprite @implementation
4.@synthesize shortestPath;
5.// Inside initWithLayer
6.self.shortestPath = nil;
7.// Inside dealloc
8.[shortestPath release]; shortestPath = nil;


现在我们创建一个方法,存储整条路径并且负责动画的播放。对CatSprite.m文件做以下修改:
>
1.// Add inside the CatSprite private properties and methods section
2.- (void)constructPathAndStartAnimationFromStep:(ShortestPathStep *)step;
3.// Inside moveToward, comment out the pathFound BOOL
4.//BOOL pathFound = NO;
5.// Inside moveToward, replace pathFound = YES with this:
6.[self constructPathAndStartAnimationFromStep:currentStep];
7.// Also comment all of the debugging statements below that.
8.// Inside moveToward, replace if (!pathFound) with this:
9.if (self.shortestPath == nil) { // No path found
10.// Add this new method:
11.// Go backward from a step (the final one) to reconstruct the shortest computed path
12.- (void)constructPathAndStartAnimationFromStep:(ShortestPathStep *)step
13.{
14.    self.shortestPath = [NSMutableArray array];
15. 
16.    do {
17.        if (step.parent != nil) { // Don't add the last step which is the start position (remember we go backward, so the last one is the origin position ;-)
18.            [self.shortestPath insertObject:step atIndex:0]; // Always insert at index 0 to reverse the path
19.        }
20.        step = step.parent; // Go backward
21.    } while (step != nil); // Until there is no more parents
22. 
23.    for (ShortestPathStep *s in self.shortestPath) {
24.        NSLog(@"%@", s);
25.    }
26.}


注意在moveToward方法中,我们调用了新的方法,而不是在console栏打印出结果,我们还移除了pathFound布尔值。同样的,在constructPathAndStartAnimationFromStep方法中的注释解释了详细情况。

 

现在编译运行。如果你像之前一样点击了同样的位置,你将在console栏看到以下信息:
>
1.  pos=[24;1]  g=1  h=4  f=5
2.  pos=[23;1]  g=2  h=3  f=5
3.  pos=[22;1]  g=3  h=2  f=5
4.  pos=[21;1]  g=4  h=3  f=7
5.  pos=[20;1]  g=5  h=4  f=9
6.  pos=[20;2]  g=6  h=3  f=9
7.  pos=[20;3]  g=7  h=2  f=9
8.  pos=[21;3]  g=8  h=1  f=9
9.  pos=[22;3]  g=9  h=0  f=9


这些信息跟之前的相似,但是现在的信息是从开始到结束(不是反向的),并且每个步骤都被很好的存放在数组中以供我们使用。

 

最后要做的事情是遍历shortestPath数值,让猫按着路径前进。为了实现它,我们将创建一个方法,从数组中获取一步操作,让猫移动到那个位置,然后使用回调函数去重复调用这个方法直到路径完成。

 

对 CatSprite.m文件做以下操作:
>
1.// Add inside the CatSprite private properties and methods section
2.- (void)popStepAndAnimate;
3.// Add to bottom of constructPathAndStartAnimationFromStep
4.[self popStepAndAnimate];
5.// Add new method
6.- (void)popStepAndAnimate
7.{
8.    // Check if there remains path steps to go through
9.    if ([self.shortestPath count] == 0) {
10.        self.shortestPath = nil;
11.        return;
12.    }
13.    // Get the next step to move to
14.    ShortestPathStep *s = [self.shortestPath objectAtIndex:0];
15. 
16.    // Prepare the action and the callback
17.    id moveAction = [CCMoveTo actionWithDuration:0.4 position:[_layer positionForTileCoord:s.position]];
18.    id moveCallback = [CCCallFunc actionWithTarget:self selector:@selector(popStepAndAnimate)]; // set the method itself as the callback
19. 
20.    // Remove the step
21.    [self.shortestPath removeObjectAtIndex:0];
22. 
23.    // Play actions
24.    [self runAction:[CCSequence actions:moveAction, moveCallback, nil]];
25.}


>
1.编译运行,然后...
 

我们的猫自动移动到我们点击的终点位置了! :-)

然而,当你把玩它的时候,你会注意到以下问题:
•猫看起来有点僵硬
•猫没有带走骨头
•猫可以穿过狗(没有拿着骨头),而不会被咬死
•当你在猫走完路径之前点击新的位置创造新路径时,猫会有奇怪的行为。

 

为了解决猫的呆板外表,还有游戏逻辑(胜利/失败,狗,骨头,等等…)我们必须抛弃在第一次实现中使用的旧版游戏逻辑。接下来我们对它做改进。

 

重新添加游戏逻辑

为了修复这些问题,使用以下代码替换掉popStepAndAnimate方法中的内容:
>
1.- (void)popStepAndAnimate
2.{
3.    // Check if there is still shortestPath
4.    if (self.shortestPath == nil) {
5.        return;
6.    }
7. 
8.    // Game Logic (Win / Lose, dogs, bones, etc...)
9.    CGPoint currentPosition = [_layer tileCoordForPosition:self.position];
10. 
11.    if ([_layer isBoneAtTilecoord:currentPosition]) {
12.        [[SimpleAudioEngine sharedEngine] playEffect:@"pickup.wav"];
13.        _numBones++;
14.        [_layer showNumBones:_numBones];
15.        [_layer removeObjectAtTileCoord:currentPosition];
16.    }
17.    else if ([_layer isDogAtTilecoord:currentPosition]) {
18.        if (_numBones == 0) {
19.            [_layer loseGame];
20.            self.shortestPath = nil;
21.            return;
22.        }
23.        else {
24.            _numBones--;
25.            [_layer showNumBones:_numBones];
26.            [_layer removeObjectAtTileCoord:currentPosition];
27.            [[SimpleAudioEngine sharedEngine] playEffect:@"catAttack.wav"];
28.        }
29.    }
30.    else if ([_layer isExitAtTilecoord:currentPosition]) {
31.        [_layer winGame];
32.        self.shortestPath = nil;
33.        return;
34.    }
35.    else {
36.        [[SimpleAudioEngine sharedEngine] playEffect:@"step.wav"];
37.    }
38. 
39.    // Check if there remains path steps to go trough
40.    if ([self.shortestPath count] == 0) {
41.        self.shortestPath = nil;
42.        return;
43.    }
44. 
45.    // Get the next step to move to
46.    ShortestPathStep *s = [self.shortestPath objectAtIndex:0];
47. 
48.    CGPoint futurePosition = s.position;
49.    CGPoint diff = ccpSub(futurePosition, currentPosition);
50.    if (abs(diff.x) > (abs(diff.y)) {
51.        if (diff.x > (0) {
52.            [self runAnimation:_facingRightAnimation];
53.        }
54.        else {
55.            [self runAnimation:_facingLeftAnimation];
56.        }
57.    }
58.    else {
59.        if (diff.y > (0) {
60.            [self runAnimation:_facingForwardAnimation];
61.        }
62.        else {
63.            [self runAnimation:_facingBackAnimation];
64.        }
65.    }
66. 
67.    // Prepare the action and the callback
68.    id moveAction = [CCMoveTo actionWithDuration:0.4 position:[_layer positionForTileCoord:s.position]];
69.    id moveCallback = [CCCallFunc actionWithTarget:self selector:@selector(popStepAndAnimate)]; // set the method itself as the callback
70. 
71.    // Remove the step
72.    [self.shortestPath removeObjectAtIndex:0];
73. 
74.    // Play actions
75.    [self runAction:[CCSequence actions:moveAction, moveCallback, nil]];
76.}


这里没有施展什么魔法,只是对原来的代码做了重构。

编译运行,你会看到一切工作正常,除了猫在完成旧路径之前开始新路径时做出的奇怪行为。

因为它跟主题关系不大,我将不会对它的实现方法(相当简单)做详细解答。如果你很感兴趣,你可以下载最终的Cat Maze 工程仔细看下。恭喜你,你已经在一款简单的Cocos2D游戏中实现了A星寻路算法!:-)

 

如何实现对角线移动?

其实在A星算法中实现对角线移动相当简单。

你只需要更新两个函数:
•walkableAdjacentTilesCoordForTileCoord: 更新它,让它也包含对角线方块。
•costToMoveFromStep:toAdjacentStep: 更新它,让它提供一个跟水平/垂直移动不同的对角线移动cost。

 

你也许会对如何计算出在对角线方向上移动的cost值感到好奇。使用简单的数学可以很容易实现它!

 

猫从一个方块的中心移动到另一个方块,并且因为方块是正方形的,A,B和C构成了一个三角形,如下图所示:

根据勾股定理,C² = A² + B², 所以:
1.C = √(A² + B²)
2.且有 A = B = 1 (从一个正方形移动到另一个的cost = G cost)
3.C = √(2)
4.C ≈ 1.41


所以对角线移动的cost等于1.41, 这比先向左移动然后再向上移动的cost值2 (1 + 1)还要少。

正如你所知道的,使用整形数去计算要远比浮点数方便,所以与其使用floats去代表对角线运动的cost值,还不如简单的对cost值乘10,然后四舍五入,这样水平或者垂直移动会消耗10,而对角线移动会消耗14。

我们现在试试它吧!首先使用以下函数对CatSprite.m文件中的costToMoveFromSTep:toAdjacentStep函数做替换:
>
1.// Compute the cost of moving from a step to an adjecent one
2.- (int)costToMoveFromStep:(ShortestPathStep *)fromStep toAdjacentStep:(ShortestPathStep *)toStep
3.{
4.    return ((fromStep.position.x != toStep.position.x) && (fromStep.position.y != toStep.position.y)) ? 14 : 10;
5.}


然后修改HelloWorldLayer.m文件中的walkableAdjacentTilesCoordForTileCoord方法,返回对角线相邻的正方形:
>
1.- (NSArray *)walkableAdjacentTilesCoordForTileCoord:(CGPoint)tileCoord
2.{
3.    NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:8];
4. 
5.    BOOL t = NO;
6.    BOOL l = NO;
7.    BOOL b = NO;
8.    BOOL r = NO;
9. 
10.    // Top
11.    CGPoint p = CGPointMake(tileCoord.x, tileCoord.y - 1);
12.    if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
13.        [tmp addObject:[NSValue valueWithCGPoint:p]];
14.        t = YES;
15.    }
16. 
17.    // Left
18.    p = CGPointMake(tileCoord.x - 1, tileCoord.y);
19.    if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
20.        [tmp addObject:[NSValue valueWithCGPoint:p]];
21.        l = YES;
22.    }
23. 
24.    // Bottom
25.    p = CGPointMake(tileCoord.x, tileCoord.y + 1);
26.    if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
27.        [tmp addObject:[NSValue valueWithCGPoint:p]];
28.        b = YES;
29.    }
30. 
31.    // Right
32.    p = CGPointMake(tileCoord.x + 1, tileCoord.y);
33.    if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
34.        [tmp addObject:[NSValue valueWithCGPoint:p]];
35.        r = YES;
36.    }
37. 
38.    // Top Left
39.    p = CGPointMake(tileCoord.x - 1, tileCoord.y - 1);
40.    if (t && l && [self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
41.        [tmp addObject:[NSValue valueWithCGPoint:p]];
42.    }
43. 
44.    // Bottom Left
45.    p = CGPointMake(tileCoord.x - 1, tileCoord.y + 1);
46.    if (b && l && [self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
47.        [tmp addObject:[NSValue valueWithCGPoint:p]];
48.    }
49. 
50.    // Top Right
51.    p = CGPointMake(tileCoord.x + 1, tileCoord.y - 1);
52.    if (t && r && [self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
53.        [tmp addObject:[NSValue valueWithCGPoint:p]];
54.    }
55. 
56.    // Bottom Right
57.    p = CGPointMake(tileCoord.x + 1, tileCoord.y + 1);
58.    if (b && r && [self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
59.        [tmp addObject:[NSValue valueWithCGPoint:p]];
60.    }
61. 
62.    return [NSArray arrayWithArray:tmp];
63.}


重要事项:你会发现代码中添加对角线方块比添加水平/垂直的方块有些不同。

事实上,例如,只有当顶部和左侧的项被添加时左对角线移动才会被添加到数组中。这是为了防止猫穿过墙壁的角落。以下是所有的详细情况:
•O = Origin
•T = Top
•B = Bottom
•L = Left
•R = Right
•TL = Top – Left
•…


 

刚才引用的例子如上图的T L所示。

猫想要从原始点(O)移动到左下角的对角线方块。如果在左侧或者底部(或者都有)有一面墙,然后试着走对角线,算法将会封掉墙壁的角落(或者两面墙壁的角落)。所以只有当左侧或者底部没有墙壁时左方和下方的对角线方块才是可通行的。

提示:你可以通过更新costToMoveFromStep方法去模拟不同类型的地形。事实上,如果你减少G cost值,这表示猫会在那些方块上移动得更快,防止亦然。

接下来可以做什么?

这里是 Maze猫游戏的工程文件,包含了本教程的所有代码(包括对角线移动)。

恭喜,现在你知道了A星算法的基本内容,并且有了实现它的经验!你需要做以下准备:
•在你自己的游戏中实现A星算法
•有必要的话改善算法(运行不同种类的地形,更好的搜索方法,等等),使它最优化
•阅读其他相关文章,例如这篇不错的教程:Amit’s A* Pages

 

来源:ityran 


发布了83 篇原创文章 · 获赞 4 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/hope_worker/article/details/8847668