J2ME平台A-RPG游戏怪物寻路算法初探

来源:互联网 发布:新闻哪个软件好 编辑:程序博客网 时间:2024/05/15 04:39

            最近似乎在手机上A-RPG游戏很受欢迎,当然,我最近也在帮公司写一款A-RPG类的游戏。以前从没写过这样的游戏,从有到无的写出来了。也碰到不少问题,包括屏幕滚动时地图title数组索引的校验、整个游戏框架的搭建、地图数据的处理、分层的处理、主角技能的处理、碰状检测。

           这些问题有空我会做为小专题一个一个的写出来,这个话题里我们来一起讨论下A-RPG游戏里怪物寻路算法的确定。

          很显然,在手机这样的设备上,A*算法不适合。因为A*中大量的节点会把手机内存爆掉,这个是我直观的感觉,没亲自去实践,如果有人能在手机上精确的实现了A*算法的话,请告诉我一声。

        那么,在J2ME里该如何处理怪物寻路算法呢?

       我在这个游戏里用的是A*算法的简化版本,暂时称其为A* Simple算法。

      我们可以建立一个怪物的类,这个类里包括不同版本的怪物、相应的图片、相应的寻路算法、相应的攻击技能、相应的碰撞检测等。如果不建立这个类的话,实在很难想象这个游戏到后期如何扩展。

      那么我们就可以在这个类里面做如下的判断:

if(!walking){
      if(spriteX < theRoleX&&spriteY > theRoleY)
      {
        direction = 3;
        walking = true;
        drawByDirection(g,spriteImage,spriteX,spriteY,direction*3);
      }else if(spriteX < theRoleX&&spriteY < theRoleY)
      {
        direction = 2;
        walking = true;
        drawByDirection(g,spriteImage,spriteX,spriteY,direction*3);
      }else if(spriteY < theRoleY&&spriteX > theRoleX)
      {
        direction = 0;
        walking = true;
        drawByDirection(g,spriteImage,spriteX,spriteY,direction*3);
      }else if(spriteY > theRoleY&&spriteX > theRoleX)
      {
        direction = 1;
        walking = true;
        drawByDirection(g,spriteImage,spriteX,spriteY,direction*3);
      }else if(theRoleX-spriteX > 8+spriteWidth&&spriteY == theRoleY)
      {
        direction = 2;
        walking = true;
        drawByDirection(g,spriteImage,spriteX,spriteY,direction*3);
      }else if(theRoleX-spriteX <= 8+spriteWidth&&spriteY == theRoleY&&theRoleX-spriteX>0)
      {
        spriteX = theRoleX - 8 - spriteWidth;
        attackFlash(g,2);
      }else if(spriteX-theRoleX > 8+spriteWidth&&spriteY == theRoleY)
      {
        direction = 1;
        walking = true;
        drawByDirection(g,spriteImage,spriteX,spriteY,direction*3);
      }else if(spriteX-theRoleX <= 8+spriteWidth&&spriteY == theRoleY&&spriteX>theRoleX)
      {
        spriteX = theRoleX+8+spriteWidth;
        attackFlash(g,1);
      }
      else if(spriteY-theRoleY > spriteHeight&&spriteX == theRoleX)
      {
        direction = 3;
        walking = true;
        drawByDirection(g,spriteImage,spriteX,spriteY,direction*3);
      }else if(spriteY-theRoleY <= spriteHeight&&spriteX == theRoleX&&spriteY-theRoleY > 0)
      {
        spriteY = theRoleY+spriteHeight;
        attackFlash(g,3);
      }else if(theRoleY-spriteY > height&&spriteX == theRoleX)
      {
        direction = 0;
        walking = true;
        drawByDirection(g,spriteImage,spriteX,spriteY,direction*3);
      }else if(theRoleY-spriteY <= height&&spriteX == theRoleX)
      {
        spriteY = theRoleY-height;
        attackFlash(g,0);
      }

     在这个寻路算法中,怪物类会先判断“权值”,即横向和竖向到目标地的距离的大小。

    然后根据“权值”做出相应的动作。

   比如,怪物如果在主角的左下方,那么它会先逼近主角的正下方,然后从下方逼近主角。

   当然,这个算法可以写的更精确写,那要以牺牲性能为代价了。

   如何在精确和性能上做出平衡,也是我所要寻找的。

   欢迎有类似经验的同行一起讨论。



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=744476


原创粉丝点击