Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class MazeMatrixS{
- private int _width;
- private int _height;
- private int _startX;
- private int _startY;
- private int _finishX;
- private int _finishY;
- private ArrayList[] matrix;
- public MazeIndexS[] activeRoute;
- private int currentStep ;
- private const byte WALL_AROUND = 15;
- public const byte WALL_TOP = 1; //Возможно, нужно static
- public const byte WALL_RIGHT = 2;
- public const byte WALL_BOTTOM = 4;
- public const byte WALL_LEFT = 8;
- public const byte CELL_VISITED = 128;
- private const float RAND_PROBABILITY = 0.5f;
- public MazeMatrixS MazeMatrix(int w, int h, int startX = 0, int startY = 0, bool autogenerate = true)
- {
- _width = w;
- _height = h;
- _startX = startX;
- _startY = startY;
- _finishX = w - 1;
- _finishY = h - 1;
- matrix = new Array(w);
- for (int i = 0; i < w; ++i)
- {
- matrix[i] = new Array(h);
- (matrix[i] as Array).forEach(fillMatrix);
- }
- activeRoute = new Array(new MazeIndex(startX, startY));
- matrix[startX][startY] |= CELL_VISITED;
- currentStep = 0;
- if (autogenerate)
- {
- createRoute();
- }
- }
- public int width
- {
- get {return _width;}
- }
- public int height
- {
- get {return _height;}
- }
- public int startX
- {
- get {return _startX;}
- }
- public int startY
- {
- get {return _startY;}
- }
- public int finishX
- {
- get {return _finishX;}
- }
- public int finishY
- {
- get {return _finishY;}
- }
- public int getCell(int x, int y)
- {
- return matrix[x][y];
- }
- /*public bool isCompleted
- {
- get {return activeRoute.length == 0;}
- }*/
- private void fillMatrix(/*var element, */int index, byte[] arr)
- {
- arr[index] = WALL_AROUND;
- }
- public void createRoute()
- {
- while(doStep()) {}
- }
- public bool doStep()
- {
- // If now way (maze is completed)
- if (activeRoute.length == 0) return false;
- MazeIndex p = activeRoute[currentStep];
- bool wayExist = (canMove(p, WALL_TOP) || canMove(p, WALL_RIGHT) || canMove(p, WALL_BOTTOM) || canMove(p, WALL_LEFT));
- if (wayExist)
- {
- switch (Math.floor(Math.random() * 4))
- {
- case 0:
- if (canMove(p, WALL_TOP)) { addRoute(p, WALL_TOP); }
- break;
- case 1:
- if (canMove(p, WALL_RIGHT)) { addRoute(p, WALL_RIGHT); }
- break;
- case 2:
- if (canMove(p, WALL_BOTTOM)) { addRoute(p, WALL_BOTTOM); }
- break;
- case 3:
- if (canMove(p, WALL_LEFT)) { addRoute(p, WALL_LEFT); }
- break;
- }
- // Continue route
- currentStep = activeRoute.length - 1;
- }
- else
- {
- // Remove deadlock route point
- activeRoute.splice(currentStep, 1);
- // Start a new route
- currentStep = 0;
- // Return false, if maze is completed
- return (activeRoute.length > 0);
- }
- return true;
- }
- private void addRoute(MazeIndexS pos, int direct)
- {
- matrix[pos.x][pos.y] = matrix[pos.x][pos.y] & (0xFFFF ^ direct);
- MazeIndex newIndex = new MazeIndex(
- pos.x + (direct == WALL_LEFT ? -1 : (direct == WALL_RIGHT ? 1 : 0)),
- pos.y + (direct == WALL_TOP ? -1 : (direct == WALL_BOTTOM ? 1 : 0))
- );
- matrix[newIndex.x][newIndex.y] = (matrix[newIndex.x][newIndex.y] | CELL_VISITED) & (0xFFFF ^ backDirect(direct));
- if ((newIndex.x == _finishX) && (newIndex.y == _finishY)) { return; }
- activeRoute.push(newIndex);
- }
- // Returns inverse direction
- private int backDirect(int direct)
- {
- switch (direct)
- {
- case WALL_TOP: return WALL_BOTTOM;
- case WALL_RIGHT: return WALL_LEFT;
- case WALL_BOTTOM: return WALL_TOP;
- case WALL_LEFT: return WALL_RIGHT;
- }
- return 0;
- }
- private bool canMove(MazeIndexS pos, int direct)
- {
- switch (direct)
- {
- case (WALL_TOP):
- return (pos.y > 0) && ((matrix[pos.x][pos.y - 1] & CELL_VISITED) == 0);
- case (WALL_RIGHT):
- return (pos.x < _width - 1) && ((matrix[pos.x + 1][pos.y] & CELL_VISITED) == 0);
- case (WALL_BOTTOM):
- return (pos.y < _height - 1) && ((matrix[pos.x][pos.y + 1] & CELL_VISITED) == 0);
- case (WALL_LEFT):
- return (pos.x > 0) && ((matrix[pos.x - 1][pos.y] & CELL_VISITED) == 0);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement