Advertisement
Guest User

MazeMatrixS.cs

a guest
Nov 9th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.71 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MazeMatrixS{
  5.  
  6.         private int _width;
  7.         private int _height;
  8.         private int _startX;
  9.         private int _startY;
  10.         private int _finishX;
  11.         private int _finishY;
  12.         private ArrayList[] matrix;
  13.         public  MazeIndexS[] activeRoute;
  14.         private  int  currentStep ;
  15.    
  16.         private const byte WALL_AROUND = 15;
  17.         public const byte WALL_TOP = 1;         //Возможно, нужно static
  18.         public const byte WALL_RIGHT = 2;
  19.         public const byte WALL_BOTTOM = 4;
  20.         public const byte WALL_LEFT = 8;
  21.         public const byte CELL_VISITED = 128;
  22.        
  23.         private const float RAND_PROBABILITY = 0.5f;
  24.    
  25.         public MazeMatrixS MazeMatrix(int w, int h, int startX = 0, int startY = 0, bool autogenerate = true)
  26.         {
  27.             _width = w;
  28.             _height = h;
  29.             _startX = startX;
  30.             _startY = startY;
  31.             _finishX = w - 1;
  32.             _finishY = h - 1;
  33.             matrix = new Array(w);
  34.  
  35.             for (int i = 0; i < w; ++i)
  36.             {
  37.                 matrix[i] = new Array(h);
  38.                 (matrix[i] as Array).forEach(fillMatrix);
  39.             }
  40.  
  41.             activeRoute = new Array(new MazeIndex(startX, startY));
  42.             matrix[startX][startY] |= CELL_VISITED;
  43.             currentStep = 0;
  44.  
  45.             if (autogenerate)
  46.             {
  47.                 createRoute();
  48.             }
  49.            
  50.         }
  51.        
  52.         public int width
  53.         {
  54.             get {return _width;}
  55.         }
  56.        
  57.         public int height
  58.         {
  59.             get {return _height;}
  60.         }
  61.        
  62.         public int startX
  63.         {
  64.             get {return _startX;}
  65.         }
  66.        
  67.         public int startY
  68.         {
  69.             get {return _startY;}
  70.         }
  71.        
  72.         public int finishX
  73.         {
  74.             get {return _finishX;}
  75.         }
  76.        
  77.         public int finishY
  78.         {
  79.             get {return _finishY;}
  80.         }
  81.         public int getCell(int x, int y)
  82.         {
  83.             return matrix[x][y];
  84.         }
  85.    
  86.         /*public bool isCompleted
  87.         {
  88.             get {return activeRoute.length == 0;}
  89.         }*/
  90.        
  91.         private void fillMatrix(/*var element, */int index, byte[] arr)
  92.         {
  93.             arr[index] = WALL_AROUND;
  94.         }
  95.    
  96.        
  97.         public void createRoute()
  98.         {
  99.             while(doStep()) {}
  100.         }
  101.    
  102.         public bool doStep()
  103.         {
  104.             // If now way (maze is completed)
  105.             if (activeRoute.length == 0) return false;
  106.            
  107.             MazeIndex p = activeRoute[currentStep];
  108.             bool wayExist = (canMove(p, WALL_TOP) || canMove(p, WALL_RIGHT) || canMove(p, WALL_BOTTOM) || canMove(p, WALL_LEFT));
  109.             if (wayExist)
  110.             {
  111.                 switch (Math.floor(Math.random() * 4))
  112.                 {
  113.                     case 0:
  114.                         if (canMove(p, WALL_TOP)) { addRoute(p, WALL_TOP); }
  115.                         break;
  116.                     case 1:
  117.                         if (canMove(p, WALL_RIGHT)) { addRoute(p, WALL_RIGHT); }
  118.                         break;
  119.                     case 2:
  120.                         if (canMove(p, WALL_BOTTOM)) { addRoute(p, WALL_BOTTOM);  }
  121.                         break;
  122.                     case 3:
  123.                         if (canMove(p, WALL_LEFT)) { addRoute(p, WALL_LEFT); }
  124.                         break;
  125.                 }                
  126.                 // Continue route
  127.                 currentStep = activeRoute.length - 1;
  128.             }
  129.             else
  130.             {
  131.                 // Remove deadlock route point
  132.                 activeRoute.splice(currentStep, 1);
  133.                
  134.                 // Start a new route
  135.                 currentStep = 0;
  136.                
  137.                 // Return false, if maze is completed
  138.                 return (activeRoute.length > 0);
  139.             }
  140.             return true;
  141.         }  
  142.    
  143.         private void addRoute(MazeIndexS pos, int direct)
  144.         {
  145.             matrix[pos.x][pos.y] = matrix[pos.x][pos.y] & (0xFFFF ^ direct);
  146.             MazeIndex newIndex = new MazeIndex(
  147.                 pos.x + (direct == WALL_LEFT ? -1 : (direct == WALL_RIGHT ? 1 : 0)),
  148.                 pos.y + (direct == WALL_TOP ? -1 : (direct == WALL_BOTTOM ? 1 : 0))
  149.             );
  150.             matrix[newIndex.x][newIndex.y] = (matrix[newIndex.x][newIndex.y] | CELL_VISITED) & (0xFFFF ^ backDirect(direct));
  151.             if ((newIndex.x == _finishX) && (newIndex.y == _finishY)) { return; }
  152.             activeRoute.push(newIndex);
  153.         }
  154.        
  155.        
  156.         // Returns inverse direction
  157.         private int backDirect(int direct)
  158.         {
  159.             switch (direct)
  160.             {
  161.                 case WALL_TOP: return WALL_BOTTOM;
  162.                 case WALL_RIGHT: return WALL_LEFT;
  163.                 case WALL_BOTTOM: return WALL_TOP;
  164.                 case WALL_LEFT: return WALL_RIGHT;
  165.             }
  166.             return 0;
  167.         }
  168.        
  169.         private bool canMove(MazeIndexS pos, int direct)
  170.         {
  171.             switch (direct)
  172.             {
  173.                 case (WALL_TOP):
  174.                     return (pos.y > 0) && ((matrix[pos.x][pos.y - 1] & CELL_VISITED) == 0);
  175.                 case (WALL_RIGHT):
  176.                     return (pos.x < _width - 1) && ((matrix[pos.x + 1][pos.y] & CELL_VISITED) == 0);
  177.                 case (WALL_BOTTOM):
  178.                     return (pos.y < _height - 1) && ((matrix[pos.x][pos.y + 1] & CELL_VISITED) == 0);
  179.                 case (WALL_LEFT):
  180.                     return (pos.x > 0) && ((matrix[pos.x - 1][pos.y] & CELL_VISITED) == 0);
  181.             }
  182.             return false;
  183.         }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement