xxxxxxxxxx
let canvasSize = 900;
//Maze
let cols, rows;
let mazeSize = 600;
let w = 100;
let posX = canvasSize / 2 - mazeSize / 2;
let posY = canvasSize / 2 - mazeSize / 2;
let grid = [];
let current;
let stack = [];
let steps = 1;
let percentage;
let wSteps = 10;
let mazeMap;
//Player
let spd = 1;
let pos;
let dir;
//Rays
let rays = [];
function setup() {
createCanvas(canvasSize*1.5, canvasSize);
mazeMap = createGraphics(width, height);
generateGrid();
current = grid[0];
pos = createVector(current.x + w/2, current.y + w/2);
dir = createVector(0, -spd);
//Ray
for (let i = 0; i < 100; i++) {
rays.push(new Ray((((HALF_PI * 0.5)/100)*i) - ((HALF_PI * 0.5)/2)));
}
}
function draw() {
background(51);
percentage = (steps / grid.length) * 100;
//Indicates the generation's completion
if (percentage < 100) {
rectMode(CORNER);
fill(0, 255, 0);
noStroke();
rect(10, 10, percentage * 5, 50);
stroke(0);
strokeWeight(5);
noFill();
rect(10, 10, 500, 50);
fill(0);
noStroke();
textSize(40);
textAlign(LEFT, TOP);
text(round(percentage) + " %", 20, 18);
}
noStroke();
strokeWeight(0);
rectMode(CENTER);
//Genetrates the maze
current.visited = true;
let next = current.checkNeighbors();
if (next) {
next.visited = true;
stack.push(current);
removeWalls(current, next);
current = next;
steps++;
} else if (stack.length > 0) {
current = stack.pop();
}
//When the maze generation is over
if (stack.length == 0) {
grid[grid.length - 1].walls[1] = false;
//Rays
for (let ray of rays) {
ray.update(pos, dir);
ray.look(grid);
}
//POV
mazeMap.background(51);
push();
translate(0, 0);
for (let i = 0; i < rays.length; i++) {
let a = dir.heading() - rays[i].rDir.heading();
if (a < 0) a += TWO_PI;
if (a > TWO_PI) a -= TWO_PI;
let dr = rays[i].rayLen * cos(a);
let x = map(i, 0, rays.length - 1, 0, width);
let y = height/2;
let wt = (width / rays.length) + 1;
let h = (w*height) / dr;
let c;
//let shadow = constrain(map(rays[i].rayLen, 0, w*8, 0, 255), 0, 255);
if (rays[i].wallDir == 0) c = lerpColor(color(200), color(150), map(rays[i].rayLen, 0, w*10, 0, 1));
else c = lerpColor(color(150), color(100), map(rays[i].rayLen, 0, w*10, 0, 1));
if(rays[i].noWall){
wt = 0;
h = 0;
c = color(0, 0);
}
mazeMap.rectMode(CENTER);
mazeMap.noStroke();
//Walls
mazeMap.fill(c);
mazeMap.rect(x + wt/2, y - 1, wt + 3, h);
}
pop();
image(mazeMap, 0, 0);
push();
translate(width - 200, height - 200);
scale(0.2);
showGrid();
//Player
fill(255, 0, 0);
noStroke();
push();
translate(pos.x, pos.y);
rotate(dir.heading() - PI*1.5);
triangle(-15, 15, 0, -15, 15, 15);
pop();
// circle(pos.x, pos.y, 30);
// stroke(0);
// strokeWeight(5);
// line(pos.x, pos.y, pos.x + (dir.x * 15), pos.y + (dir.y * 15));
movePlayer(dir, spd / 40);
pop();
if(pos.x > grid[grid.length - 1].x + w + 10){
rebuiltMaze();
}
}
}
function index(i, j) {
if (i < 0 || j < 0 || i > cols - 1 || j > rows - 1) {
return -1;
}
return i + j * cols;
}
function removeWalls(a, b) {
let x = a.i - b.i;
if (x === 1) {
a.walls[3] = false;
b.walls[1] = false;
} else if (x === -1) {
a.walls[1] = false;
b.walls[3] = false;
}
let y = a.j - b.j;
if (y === 1) {
a.walls[0] = false;
b.walls[2] = false;
} else if (y === -1) {
a.walls[2] = false;
b.walls[0] = false;
}
}
function generateGrid() {
cols = floor(mazeSize / w);
rows = floor(mazeSize / w);
for (let j = 0; j < rows; j++) {
for (let i = 0; i < cols; i++) {
let cell = new Cell(i, j);
grid.push(cell);
}
}
}
function showGrid() {
for (let i = 0; i < grid.length; i++) {
grid[i].show();
}
}
function rebuiltMaze() {
grid = [];
w -= wSteps;
generateGrid();
for (let i = 0; i < grid.length; i++) {
grid[i].resetWalls();
}
current = grid[0];
pos = createVector(current.x + w/2, current.y + w/2);
dir = createVector(0, -spd);
steps = 1;
}
let up, down, left, right;
function keyPressed(){
if (key == 'w') up = true;
if (key == 'd') right = true;
if (key == 's') down = true;
if (key == 'a') left = true;
}
function keyReleased(){
if (key == 'w') up = false;
if (key == 'd') right = false;
if (key == 's') down = false;
if (key == 'a') left = false;
}
function movePlayer(dir, spd) {
if (up) {
pos.add(dir);
let temp = createVector(0, 0);
for(let i = 0; i < grid.length; i++){
if(grid[i].collides(pos.x, pos.y, 0)) pos.sub(dir);
//if(grid[i].collideH(pos.x, pos.y, 15)) temp.x = dir.x; print("fdfs");
//if(grid[i].collideV(pos.x, pos.y, 15)) temp.y = dir.y;
}
//print(temp);
//pos.sub(temp);
}
if (down) {
pos.sub(dir);
for(let i = 0; i < grid.length; i++){
if(grid[i].collides(pos.x, pos.y, 0)) pos.add(dir);
}
}
if (left) dir.rotate(-spd);
if (right) dir.rotate(spd);
}
function lerpT(s, m, e, dir) {
if (dir == 0) return (m.x - s.x) / (e.x - s.x);
else return (m.y - s.y) / (e.y - s.y);
}