关于SLG战略游戏中,人物移动范围的算法(js实现)

来源:互联网 发布:seo跟sem的区别 编辑:程序博客网 时间:2024/05/27 00:48

关于SLG战略游戏中,人物移动范围的算法

战略游戏里作战单位都是有移动力的, 显示移动选区的时候会出现围绕单位的一个菱形选区,里面包含了对障碍物的计算.

  例: 假设目前有一单位坐标为   5,5

移动力为 2格时,出现的选区坐标!

关于SLG战略游戏中,人物移动范围的算法(js实现) - Nealson - Nealson的博客 关于SLG战略游戏中,人物移动范围的算法(js实现) - Nealson - Nealson的博客 下载 (10.38 KB)

2009-2-15 17:09

js函数实现

  1. function ScopeFinder()

  2. {

  3. this.sx=0; //中心点

  4. this.sy=0;

  5. this.step=5; //移动力

  6. this.maxX=10; //地图边界

  7. this.maxY=10;

  8. this.hinder={"6,5":true,"5,5":true}; //障碍点

  9. this.sp={}; //记录每个格子的移动力

  10. this.wp=[]; //等待处理的节点

  11. this.path=[]; //包含了结果

  12. this.opened={}; //包含了已经处理的点

  13. this.find=function()

  14. {

  15. this.path=[]; //清空

  16. this.opened={};

  17. this.wp=[];

  18. this.sp={};

  19. this.sp[this.sx+","+this.sy]=this.step; //将初始点的移动力设置为最高移动力

  20. this.wp.push([this.sx,this.sy]); //放进待处理队列

  21. this.opened[this.sx+","+this.sy]=true; //把起点放进已处理的点中

  22. while(this.wp.length)

  23. {

  24. this.getNode();

  25. }

  26. return this.path; //返回结果集合

  27. }

  28. //获取一个待拓展节点

  29. this.getNode=function()

  30. {

  31. var tmpNode=this.wp.pop();

  32. var x=tmpNode[0];

  33. var y=tmpNode[1];

  34. this.findNode(x,y,x+1,y); //右

  35. this.findNode(x,y,x-1,y); //左

  36. this.findNode(x,y,x,y+1); //下

  37. this.findNode(x,y,x,y-1); //上

  38. }

  39. //检查节点

  40. this.findNode=function(fx,fy,tx,ty)

  41. {

  42. if(tx<0 || ty<0 || tx>=this.maxX || ty>=this.maxY) { return; } //如果超出边界

  43. if(this.hinder[tx+","+ty]){return;} //如果是障碍

  44. if(this.opened[tx+","+ty]){return;} //如果是处理过的

  45. if(this.sp[fx+","+fy]-1<0){return;}; //如果已经没有移动力了

  46. //如果都不是,则代表是个可以拓展的节点

  47. this.sp[tx+","+ty]=this.sp[fx+","+fy]-1; //把新的节点的移动力继承并减1

  48. this.wp.push([tx,ty]); //加入等待队列

  49. this.opened[tx+","+ty]=true; //设置为已经处理的

  50. this.path.push([tx,ty]); //加入结果

  51. }

  52. }
0 0