-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
173 lines (133 loc) · 3.21 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
let bloques = [];
let blockSize = 0;
let blockSum= 0;
let balls = [];
let cantBalls = 1;
let ballSize = 15;
let ballPosA = 0;
let ballPosB = 0;
let ballSpeedMag = 2000;
let level = 1;
let columnas = 7;
let filas = 9;
let k = cantBalls * 10;
let pointA = 0;
let pointB = 0;
let pointC = 0;
let forceA = 0;
let forceB = 0;
let state = "waiting";
let xPos = null
let v = 10
function setup(){
cnv = createCanvas(420,580);
cnv.mousePressed(createPointA);
cnv.mouseReleased(createPointB);
frameRate(60)
//rectMode(CENTER)
blockSize = (window.width / columnas) - 1;
for (let i = 0; i < cantBalls; i++){
balls.push(new ball(window.width / 2, 540, ballSize,ballSpeedMag));
}
createBlocks(0.5)
v = floor(map(ballSpeedMag,1,2000,10,1));
}
function draw(){
background(150);
for (let i = 0; i < cantBalls; i++){
balls[i].update();
balls[i].draw();
//console.log("Loop")
}
for (let i = 0; i < bloques.length; i++){
if (bloques[i].health <= 0) {
bloques.splice(i,1);
break
}
if (bloques[i].i == 8){
state = "GAME OVER"
console.log(state)
}
bloques[i].draw();
}
//Le da acceleracion a las pelotas con varios frames de delay
if (state == "shooting"){
if (k % v == 0 && k < cantBalls * v){
balls[k / v].applyForce(forceA,forceB);
console.log("applied force")
k++
} else if (k < cantBalls * v){
k++
} else if (k == cantBalls * v){
k = (cantBalls + 1 ) * v
}
}
// Check when first ball touches ground
for (let i = 0; i < cantBalls; i++){
balls[i].check()
}
//Collision and bounce code
let moving = 0
//Determina el estado del juego
for (let i = 0; i < balls.length; i++){
if (balls[i].state == "moving"){
moving += 1
}
}
if (moving < balls.length && state == "waiting" && moving != 0){
state = "shooting"
} else if (moving == balls.length) {
state = "moving"
} else if (moving == 0 && state != "waiting" && state != "GAME OVER") {
state = "finished"
nuevoNivel()
}
//Dibuja la linea de disparo
if (mouseIsPressed && state == "waiting"){
pointB = createVector(mouseX,mouseY);
pointC = p5.Vector.sub(pointA,pointB)
ballPosA = balls[0].pos
ballPosB = p5.Vector.add(balls[0].pos,pointC)
line(ballPosA.x,ballPosA.y,ballPosB.x,ballPosB.y)
}
}
//Se encarga de disparar las pelotas
function shoot(dir){
k = 0;
forceA = dir.x
forceB = dir.y
state = "shooting"
}
//Crea el primer punto para disparar las bolas
function createPointA(){
if (state == "waiting"){
pointA = createVector(mouseX,mouseY);
pointB = 0;
}
}
//Determina el segundo punto para disparar y llama a shoot()
function createPointB(){
if (state == "waiting"){
shoot(pointC);
}
}
function createBlocks(prob){
for (let j = 0; j < columnas; j++){ //crear fila
let r = Math.random(0,1)
if (r < prob){
bloques.push(new bloque(1,j,blockSize,level))
}
}
}
function nuevoNivel(){
state = "waiting"
level += 1
for (let h = 0; h < bloques.length; h++){
bloques[h].i += 1
}
createBlocks(0.6)
balls.push(new ball(xPos,(height - ballSize), ballSize,ballSpeedMag));
cantBalls += 1
xPos = null
console.log("NEW LEVEL")
}