admin 发表于 2010-8-17 20:32:04

在例程63的基础增加了段判断四方向障碍点代码

经测试还存在BUG,但目的只是交流思路:本打算想着写段自动过障碍点的,到了这里还在研究算法   引擎:置随机数种子()
    self.目的坐标.x = 引擎:取随机整数(50,800)
    self.目的坐标.y = 引擎:取随机整数(50,800)
    self.格子.x = 取整( self.坐标.x / 32 ) + 1
    self.格子.y = 取整( self.坐标.y / 32 ) + 1
    --=====================以下判断目的坐标路径中是否有障碍===============
      
      self.起步格子.x = 取整( self.起步坐标.x / 32 ) + 1
      self.起步格子.y = 取整( self.起步坐标.y / 32 ) + 1
      
      self.目标格子.x = 取整( self.目的坐标.x / 32 ) + 1
      self.目标格子.y = 取整( self.目的坐标.y / 32 ) + 1
      
      
      if(self.目标格子.x <=self.起步格子.x) then            --左
            
            if(self.目标格子.y<=self.起步格子.y )then         --左上
            
                if(self.格子.x~=self.目标格子.x ) then          --X方向判断障碍点
                  for n=self.目标格子.x,math.abs(self.起步格子.x - self.目标格子.x ) do
                        if(Q_全地图数据_路径层 == 1) then
                            self.障碍点格子.x=n
                            self.障碍点格子.y=self.起步格子.y
                            break
                        end
                  end
                else
                  if(self.格子.y~=self.目标格子.y) then    --Y方向判断障碍点            
                        for n=self.目标格子.y,math.abs(self.起步格子.y - self.目标格子.y) do
                            if(Q_全地图数据_路径层 == 1) then
                              self.障碍点格子.x=self.格子.x
                              self.障碍点格子.y=n
                              break
                            end
                        end
                  end                  
                end
               
            else                                                                                 --左下
               
                if(self.格子.x~=self.目标格子.x ) then         --X方向判断障碍点
                  for n=self.目标格子.x,math.abs(self.起步格子.x - self.目标格子.x ) do
                        if(Q_全地图数据_路径层 == 1) then
                            self.障碍点格子.x=n
                            self.障碍点格子.y=self.起步格子.y
                            break
                        end
                  end
                else
                  if(self.格子.y~=self.目标格子.y) then    --Y方向判断障碍点            
                        for n=self.起步格子.y,math.abs(self.目标格子.y-self.起步格子.y) do
                            if(Q_全地图数据_路径层 == 1) then
                              self.障碍点格子.x=self.格子.x
                              self.障碍点格子.y=n
                              break
                            end
                        end
                  end                  
                end
            
            end
      
      else                                                                                    --右
      
            if(self.目标格子.y<=self.起步格子.y )then       --右上
            
                if(self.格子.x~=self.目标格子.x ) then
                  for n=self.起步格子.x,math.abs(self.目标格子.x - self.起步格子.x) do
                        if(Q_全地图数据_路径层 == 1) then
                            self.障碍点格子.x=n
                            self.障碍点格子.y=self.起步格子.y
                            break
                        end
                  end
                else
                  if(self.格子.y~=self.目标格子.y) then               
                        for n=self.目标格子.y,math.abs(self.起步格子.y-self.目标格子.y) do
                            if(Q_全地图数据_路径层 == 1) then
                              self.障碍点格子.x=self.格子.x
                              self.障碍点格子.y=n
                              break
                            end
                        end
                  end                  
                end
               
            else                                                                           --右下
               
                if(self.格子.x~=self.目标格子.x ) then
                  for n=self.起步格子.x,math.abs(self.目标格子.x - self.起步格子.x )do
                        if(Q_全地图数据_路径层 == 1) then
                            self.障碍点格子.x=n
                            self.障碍点格子.y=self.起步格子.y
                            break
                        end
                  end
                else
                  if(self.格子.y~=self.目标格子.y) then               
                        for n=self.起步格子.y,math.abs(self.目标格子.y - self.起步格子.y) do
                            if(Q_全地图数据_路径层 == 1) then
                              self.障碍点格子.x=self.格子.x
                              self.障碍点格子.y=n
                              break
                            end
                        end
                  end                  
                end
            
            end
            
      end
页: [1]
查看完整版本: 在例程63的基础增加了段判断四方向障碍点代码