1+ let maze ;
2+
3+ function setup ( ) {
4+ createCanvas ( windowWidth , windowHeight ) ;
5+ frameRate ( 60 ) ;
6+ angleMode ( DEGREES ) ;
7+
8+ maze = new Maze ( 20 , 20 ) ;
9+
10+ }
11+
12+ function draw ( ) {
13+ background ( 220 ) ;
14+
15+ maze . display ( ) ;
16+ }
17+
18+ class Maze {
19+ constructor ( cols , rows ) {
20+ this . cols = cols ;
21+ this . rows = rows ;
22+
23+ this . containerParameters = { x : 0 , y : 0 , contWidth : 600 , contHeight : 600 }
24+
25+ this . squareSize = this . containerParameters . contWidth / this . cols ;
26+
27+ this . circleD = 10 ;
28+ this . circlePos = createVector ( 1.5 * this . squareSize , 1.5 * this . squareSize ) ;
29+ this . circleVel = createVector ( 3 , 3 ) ;
30+ this . circleAccel = createVector ( 0 , 0 ) ;
31+
32+ this . mazeMap = [ ] ;
33+ this . generateMapArray ( ) ;
34+ this . done = false ;
35+ }
36+
37+ generateMapArray ( ) {
38+ for ( let i = 0 ; i < this . rows + 1 ; i ++ ) {
39+ this . mazeMap . push ( [ ] ) ;
40+ for ( let j = 0 ; j < this . cols + 1 ; j ++ ) {
41+ // randomize wall or not
42+ if ( Math . random ( ) > 0.25 ) {
43+ this . mazeMap [ i ] . push ( 0 ) // not wall
44+ } else {
45+ if ( i < 3 && j < 3 ) {
46+ this . mazeMap [ i ] . push ( 0 ) // not wall
47+ } else if ( this . rows - 4 < i && this . cols - 4 < j ) {
48+ this . mazeMap [ i ] . push ( 0 ) // not wall
49+ } else {
50+ this . mazeMap [ i ] . push ( 1 ) // wall
51+ }
52+ }
53+ }
54+ }
55+ // set perimeter to be walls
56+ for ( let i = 0 ; i < this . cols ; i ++ ) {
57+ this . mazeMap [ 0 ] [ i ] = 1 ;
58+ this . mazeMap [ this . rows - 1 ] [ i ] = 1 ;
59+ }
60+
61+ for ( let j = 0 ; j < this . cols ; j ++ ) {
62+ this . mazeMap [ j ] [ 0 ] = 1 ;
63+ this . mazeMap [ j ] [ this . rows - 1 ] = 1 ;
64+ }
65+ }
66+
67+ display ( ) {
68+ push ( ) ;
69+ fill ( '#808080' ) ;
70+ stroke ( '#222222' ) ;
71+ translate ( width / 2 - 300 , height / 2 - 300 ) ;
72+ rect ( this . containerParameters . x , this . containerParameters . y , this . containerParameters . contWidth , this . containerParameters . contHeight ) ;
73+ for ( let i = 0 ; i < this . rows ; i ++ ) {
74+ for ( let j = 0 ; j < this . cols ; j ++ ) {
75+ if ( this . mazeMap [ i ] [ j ] === 1 ) {
76+ fill ( 0 , 0 , 0 ) ;
77+ } else {
78+ fill ( 150 , 150 , 150 ) ;
79+ }
80+ square ( i * this . squareSize , j * this . squareSize , this . squareSize ) ;
81+ }
82+ }
83+ this . displayCircle ( ) ;
84+ pop ( ) ;
85+ }
86+
87+ collisionDetection ( ) {
88+ // find immediate s
89+ let neighs = this . generateNeighborCoords ( ) ;
90+ neighs = Array . from ( neighs ) ;
91+
92+ neighs . forEach ( ( neigh ) => {
93+ let a = JSON . parse ( neigh ) ;
94+ if ( this . mazeMap [ a [ 0 ] ] [ a [ 1 ] ] === 1 ) { // if neighbor is wall
95+ if ( a [ 2 ] === "left" ) {
96+ this . circlePos . x = a [ 0 ] * this . squareSize + this . circleD / 2 ;
97+ this . circleVel . x = 0 ;
98+ this . circleAccel . x = 0 ;
99+
100+ } else if ( a [ 2 ] === "top" ) {
101+ this . circlePos . y = a [ 1 ] * this . squareSize + this . circleD / 2 ;
102+ this . circleVel . y = 0 ;
103+ this . circleAccel . y = 0 ;
104+
105+ } else if ( a [ 2 ] === "right" ) {
106+ this . circlePos . x = a [ 0 ] * this . squareSize - this . circleD / 2 ;
107+ this . circleVel . x = 0 ;
108+ this . circleAccel . x = 0 ;
109+
110+ } else {
111+ this . circlePos . y = a [ 1 ] * this . squareSize - this . circleD / 2 ;
112+ this . circleVel . y = 0 ;
113+ this . circleAccel . y = 0 ;
114+ }
115+
116+ fill ( 'red' ) ;
117+ square ( a [ 0 ] * this . squareSize , a [ 1 ] * this . squareSize , this . squareSize ) ;
118+ }
119+
120+ } )
121+
122+ console . log ( neighs ) ;
123+ }
124+
125+ generateNeighborCoords ( ) {
126+ let squares = new Set ( ) ;
127+ let coords = [ ]
128+
129+ // the four "vertexes"
130+ let circleTop = this . circlePos . y - this . circleD / 2 ;
131+ let circleBottom = this . circlePos . y + this . circleD / 2 ;
132+
133+ let circleLeft = this . circlePos . x - this . circleD / 2 ;
134+ let circleRight = this . circlePos . x + this . circleD / 2 ;
135+
136+ squares . add ( JSON . stringify ( [ Math . floor ( this . circlePos . x / this . squareSize ) , Math . floor ( this . circlePos . y / this . squareSize ) , "middle" ] ) ) ;
137+
138+ let leftNei = JSON . stringify ( [ Math . floor ( circleLeft / this . squareSize ) , Math . floor ( this . circlePos . y / this . squareSize ) , "left" ] ) ;
139+ let topNei = JSON . stringify ( [ Math . floor ( this . circlePos . x / this . squareSize ) , Math . floor ( circleTop / this . squareSize ) , "top" ] ) ;
140+ let rightNei = JSON . stringify ( [ Math . floor ( circleRight / this . squareSize ) , Math . floor ( this . circlePos . y / this . squareSize ) , "right" ] ) ;
141+ let bottomNei = JSON . stringify ( [ Math . floor ( this . circlePos . x / this . squareSize ) , Math . floor ( circleBottom / this . squareSize ) , "bottom" ] ) ;
142+
143+ // let potentialNeis = [leftNei, topNei, rightNei, bottomNei];
144+
145+ squares . add ( leftNei ) ;
146+ squares . add ( topNei ) ;
147+ squares . add ( rightNei ) ;
148+ squares . add ( bottomNei ) ;
149+
150+ return squares ;
151+ }
152+
153+ displayCircle ( ) {
154+ this . circleVel . add ( this . circleAccel ) ;
155+ this . circlePos . add ( this . circleVel ) ;
156+ if ( ( this . circlePos . x - this . squareSize / 2 >= ( this . cols - 1 ) * this . squareSize ) && ( this . circlePos . y - this . squareSize / 2 >= ( this . rows - 1 ) * this . squareSize ) ) {
157+ this . stopCircle ( ) ;
158+ this . done = true
159+ fill ( 'green' ) ;
160+ square ( ( this . rows - 1 ) * this . squareSize , ( this . cols - 1 ) * this . squareSize , this . squareSize ) ;
161+ }
162+ this . collisionDetection ( ) ;
163+ fill ( 50 , 50 , 50 ) ;
164+ circle ( this . circlePos . x , this . circlePos . y , this . circleD ) ;
165+ }
166+
167+ stopCircle ( ) {
168+ maze . circleVel . x = 0 ;
169+ maze . circleVel . y = 0 ;
170+
171+ maze . circleAccel . x = 0 ;
172+ maze . circleAccel . y = 0 ;
173+ }
174+ }
0 commit comments