-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
108 lines (90 loc) · 2.22 KB
/
sketch.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
var world;
var pathPoints = []
var pause = false
function keyPressed(){
if( key == 'q' ){
pause = !pause
}
}
function newPathFind( world ){
pPoints = new Pathfind(
pathPoints[0].x,
pathPoints[0].y,
pathPoints[1].x,
pathPoints[1].y,
10,
world
)
}
function addNewPathPoint(){
let newPoint = { x: mouseX, y: mouseY }
if( !world.collision( newPoint ) ){
pathPoints.push( newPoint )
}
}
function mousePressed(){
if( !world ){ return }
switch( pathPoints.length ){
case 2:
pathPoints = []
case 0:
addNewPathPoint()
break
case 1:
addNewPathPoint()
console.log( pathPoints )
newPathFind( world )
}
}
var seedInput;
function newWorld(){
world = new World( seedInput.value() )
pPoints = undefined
}
var pPoints;
function setup() {
createCanvas(400, 400, P2D)
seedInput = createInput()
seedInput.parent('container')
seedInput.attribute( 'placeholder', 'Seed')
seedBtn = createButton('Create New Map')
seedBtn.mousePressed( newWorld )
seedBtn.parent('container')
}
function draw() {
background(30)
//frameRate(4)
if( world ){
world.show()
let color = world.collision( {x:mouseX, y:mouseY} ) ? 'red' : 'white'
fill( color )
circle( mouseX, mouseY, 5 )
fill( 255 )
text( floor(mouseX)+', '+floor(mouseY), 10, 20 )
if( pPoints ){
if( !pause && !pPoints.ready ){
pPoints.getNextPoints()
}
if ( pPoints.path.length == 0 && pPoints.ready ){
pPoints.getPath().forEach( p => {
circle( p.x, p.y, 5 )
} )
}
if( pPoints.path.length ){
pPoints.showPath()
} else {
pPoints.showPoints()
}
}
drawPathPoints()
}
}
function drawPathPoints(){
pathPoints.forEach( (p, i) =>{
push()
let hue = i*160 + 10
fill( color('hsl(' + hue + ', 54%, 55%)') )
circle( p.x, p.y, 5 )
pop()
} )
}