-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.js
108 lines (94 loc) · 2.6 KB
/
cell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
function Cell(row, column, width, height) {
this.visited = false;
this.row = row;
this.column = column;
this.width = width;
this.height = height;
this.walls = [true, true, true, true];
var canvas = document.getElementById("drawArea");
this.ctx = canvas.getContext('2d');
}
Cell.prototype.draw = function () {
var x = this.column * this.width;
var y = this.row * this.height;
this.ctx.beginPath();
this.ctx.moveTo(x, y);
//Top Wall
if (this.walls[0])
this.ctx.lineTo(x + this.width, y);
else
this.ctx.moveTo(x + this.width, y);
//Right Wall
if (this.walls[1])
this.ctx.lineTo(x + this.width, y + this.width);
else
this.ctx.moveTo(x + this.width, y + this.width);
//Bottom Wall
if (this.walls[2])
this.ctx.lineTo(x, y + this.width);
else
this.ctx.moveTo(x, y + this.width);
//Left Wall
if (this.walls[3])
this.ctx.lineTo(x, y);
else
this.ctx.moveTo(x, y);
this.ctx.strokeStyle = "#CCC";
this.ctx.stroke();
if (this.visited) {
this.ctx.fillStyle = "#7d4ec2";
this.ctx.fillRect(x, y, this.width, this.width);
}
};
Cell.prototype.getNextNeighbor = function () {
var neighbours = [];
var top = grid[getGridIndex(this.row - 1, this.column)];
var right = grid[getGridIndex(this.row, this.column + 1)];
var bottom = grid[getGridIndex(this.row + 1, this.column)];
var left = grid[getGridIndex(this.row, this.column - 1)];
if (top && !top.visited)
neighbours.push(top);
if (right && !right.visited)
neighbours.push(right);
if (bottom && !bottom.visited)
neighbours.push(bottom);
if (left && !left.visited)
neighbours.push(left);
if (neighbours.length > 0) {
var nextIndex = Math.floor(Math.random() * (neighbours.length));
return neighbours[nextIndex];
} else
return undefined;
};
Cell.prototype.collapseWall = function (relativeTo) {
console.log("Current");
console.log(this);
console.log("Next");
console.log(relativeTo);
var xDirection = this.column - relativeTo.column;
if (xDirection === 1) {
//the neighbor is to the left
this.walls[3] = false;
relativeTo.walls[1] = false;
} else if (xDirection === -1) {
//the neighbor is to the left
this.walls[1] = false;
relativeTo.walls[3] = false;
}
var yDirection = this.row - relativeTo.row;
if (yDirection === 1) {
//the neighbor is above
this.walls[0] = false;
relativeTo.walls[2] = false;
} else if (yDirection === -1) {
//the neighbor is below
this.walls[2] = false;
relativeTo.walls[0] = false;
}
};
Cell.prototype.highlight = function () {
var x = this.column * this.width;
var y = this.row * this.height;
this.ctx.fillStyle = "#5a1480"
this.ctx.fillRect(x, y, this.width, this.height);
};