-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1158 lines (1048 loc) · 43.3 KB
/
Copy pathscript.js
File metadata and controls
1158 lines (1048 loc) · 43.3 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ─────────────────────────────────────────────
// PAC-MAN | games/pacman/script.js
// ─────────────────────────────────────────────
// ── Valores de celda ──────────────────────────
// 0 = pasillo libre
// 1 = muro
// 2 = punto normal
// 3 = power pellet
// 4 = interior del corral (fantasmas pueden pasar, Pac NO)
// 5 = puerta del corral (solo fantasmas en modo ojos pueden pasar)
// ── Mapa base nivel 1 ────────────────────────
const BASE_MAP = [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1],
[1,3,1,1,2,1,1,1,2,1,1,1,2,1,1,1,2,1,1,3,1],
[1,2,1,1,2,1,1,1,2,1,1,1,2,1,1,1,2,1,1,2,1],
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1],
[1,2,1,1,2,1,2,1,1,1,1,1,1,1,2,1,2,1,1,2,1],
[1,2,2,2,2,1,2,2,2,2,1,2,2,2,2,1,2,2,2,2,1],
[1,1,1,1,2,1,1,1,2,2,2,2,2,1,1,1,2,1,1,1,1],
[1,1,1,1,2,1,2,2,2,2,2,2,2,2,2,1,2,1,1,1,1],
[1,1,1,1,2,1,2,1,1,5,5,5,1,1,2,1,2,1,1,1,1],
[0,0,0,0,2,2,2,1,4,4,4,4,4,1,2,2,2,0,0,0,0],
[1,1,1,1,2,1,2,1,1,1,1,1,1,1,2,1,2,1,1,1,1],
[1,1,1,1,2,1,2,2,2,2,2,2,2,2,2,1,2,1,1,1,1],
[1,1,1,1,2,1,2,1,1,1,1,1,1,1,2,1,2,1,1,1,1],
[1,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1],
[1,2,1,1,2,1,1,1,2,1,1,1,2,1,1,1,2,1,1,2,1],
[1,3,2,1,2,2,2,2,2,2,0,2,2,2,2,2,2,1,2,3,1],
[1,1,2,1,2,1,2,1,1,1,1,1,1,1,2,1,2,1,2,1,1],
[1,2,2,2,2,1,2,2,2,2,1,2,2,2,2,1,2,2,2,2,1],
[1,2,1,1,1,1,1,1,2,1,1,1,2,1,1,1,1,1,1,2,1],
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
];
const ROWS = BASE_MAP.length; // 22
const COLS = BASE_MAP[0].length; // 21
// Filas de túnel horizontal (donde col 0 y col 20 no son muros)
const TUNNEL_ROWS = [10];
// Posición de inicio de Pac-Man
const PAC_START_R = 16, PAC_START_C = 10;
// Posición de salida del corral
const GHOST_EXIT_R = 7, GHOST_EXIT_C = 10;
// Posición interna por defecto (dentro del corral)
const GHOST_HOME_R = 9, GHOST_HOME_C = 10;
// ── Resolución lógica ────────────────────────
const LW = 462; // 21 * 22
const LH = 580; // 22*22 + UI
const CELL = 22;
const MAP_Y = 60;
// ── Colores ──────────────────────────────────
const C_BG = '#0d0d1a';
const C_WALL = '#1a4fff';
const C_WALLEDGE = '#4a7fff';
const C_DOOR = '#ffb8ff'; // puerta del corral (valor 5)
const C_DOT = '#ffdd99';
const C_POWER = '#ffaa00';
const C_PAC = '#ffe000';
const C_TEXT = '#ffffff';
const C_SCORE = '#ffdd99';
const C_GHOST = ['#ff4444','#ffb8ff','#00b8ff','#ffb852'];
const C_FRIGHTEN = '#2222cc';
// ── Direcciones ──────────────────────────────
const DIR = {
UP: { dx: 0, dy: -1 },
DOWN: { dx: 0, dy: 1 },
LEFT: { dx: -1, dy: 0 },
RIGHT: { dx: 1, dy: 0 },
};
const DIR_KEYS = ['UP','DOWN','LEFT','RIGHT'];
// ── Fases scatter / chase ────────────────────
const PHASE_TIMES = [7, 20, 7, 20, 5, 20, 5, 99999];
// ── Utilidades ───────────────────────────────
function cellX(col) { return col * CELL + CELL / 2; }
function cellY(row) { return MAP_Y + row * CELL + CELL / 2; }
/**
* Normaliza columna para túneles: wrappea 0↔COLS-1 en filas de túnel.
* Devuelve la columna ya dentro de [0, COLS-1].
*/
function wrapCol(c) { return ((c % COLS) + COLS) % COLS; }
/**
* ¿Es un muro para Pac-Man?
* Pac NO puede pisar: 1 (muro), 4 (interior corral), 5 (puerta corral).
* Puede pisar: 0, 2, 3.
* Fuera de límites de fila → muro. Fuera de columna → túnel (no muro).
*/
function isWallPac(map, r, c) {
if (r < 0 || r >= ROWS) return true;
// Túnel horizontal: columnas fuera de rango son pasables en filas de túnel
if (c < 0 || c >= COLS) return !TUNNEL_ROWS.includes(r);
const v = map[r][c];
return v === 1 || v === 4 || v === 5;
}
/**
* ¿Es un muro para un fantasma normal (no en modo ojos)?
* Fantasma NO puede pisar: 1 (muro), 5 (puerta - solo entra al corral en modo ojos).
* Puede pisar: 0, 2, 3, 4.
*/
function isWallGhost(map, r, c) {
if (r < 0 || r >= ROWS) return true;
if (c < 0 || c >= COLS) return !TUNNEL_ROWS.includes(r);
const v = map[r][c];
return v === 1 || v === 5;
}
/**
* ¿Es un muro para fantasma en modo OJOS (volviendo al corral)?
* Solo bloquea muros sólidos (1). Puede pasar puertas (5) y corral (4).
*/
function isWallEyes(map, r, c) {
if (r < 0 || r >= ROWS) return true;
if (c < 0 || c >= COLS) return !TUNNEL_ROWS.includes(r);
return map[r][c] === 1;
}
// ── Botones de menú ──────────────────────────
// ── Generador procedural de niveles ──────────
/**
* Genera un nuevo mapa basándose en el nivel actual.
* Estrategia: empezar con el mapa base y agregar variantes
* estructurales que mantienen simetría izquierda-derecha.
*
* Para nivel 1 devuelve el mapa base.
* Para niveles superiores añade bloques de muro extra,
* reduce power pellets y cambia la disposición de las paredes internas.
*/
function generateMap(level) {
// Siempre clonar profundo el mapa base
const map = BASE_MAP.map(r => [...r]);
if (level === 1) return map;
// Semilla determinista por nivel para reproducibilidad
let seed = level * 1337;
function rand() {
seed = (seed * 16807 + 0) % 2147483647;
return (seed - 1) / 2147483646;
}
function randInt(min, max) { return Math.floor(rand() * (max - min + 1)) + min; }
// Flood-fill desde Pac-Man: ¿todos los puntos (2 y 3) son accesibles?
function _allDotsReachable(m) {
const visited = Array.from({length: ROWS}, () => Array(COLS).fill(false));
const queue = [[PAC_START_R, PAC_START_C]];
visited[PAC_START_R][PAC_START_C] = true;
let head = 0;
while (head < queue.length) {
const [r, c] = queue[head++];
for (const [dr, dc] of [[-1,0],[1,0],[0,-1],[0,1]]) {
const nr = r + dr;
const nc = wrapCol(c + dc);
if (nr < 0 || nr >= ROWS) continue;
if (visited[nr][nc]) continue;
const v = m[nr][nc];
if (v === 1 || v === 4 || v === 5) continue;
visited[nr][nc] = true;
queue.push([nr, nc]);
}
}
for (let r = 0; r < ROWS; r++)
for (let c = 0; c < COLS; c++)
if ((m[r][c] === 2 || m[r][c] === 3) && !visited[r][c])
return false;
return true;
}
// ── Zonas editables: solo filas 1-6 y 14-20, cols 1-9 (se espeja a 11-19) ──
// No tocar: fila 0, 21 (bordes), filas 7-13 (zona corral), col 0, 20 (bordes)
const EDIT_ROWS_TOP = [1, 2, 3, 4, 5, 6];
const EDIT_ROWS_BOTTOM = [14, 15, 16, 17, 18, 19, 20];
const EDIT_COLS = [1, 2, 3, 4, 5, 6, 7, 8, 9]; // se espeja en 11..19
// Candidatos para nuevos muros (solo pasillo 2 puede convertirse en muro)
// Añadir entre 2 y (level-1)*2 bloques extra, máx 8
const extraWalls = Math.min((level - 1) * 2, 8);
let attempts = 0;
let placed = 0;
while (placed < extraWalls && attempts < 200) {
attempts++;
// Elegir una celda aleatoria en zona editable
const rowPool = rand() < 0.5 ? EDIT_ROWS_TOP : EDIT_ROWS_BOTTOM;
const r = rowPool[randInt(0, rowPool.length - 1)];
const c = EDIT_COLS[randInt(0, EDIT_COLS.length - 1)];
const cm = COLS - 1 - c; // columna espejada
// Solo convertir pasillos (2) en muros - no tocar power pellets (3) ni ya-muros
if (map[r][c] !== 2) continue;
if (c === cm) continue; // columna central, no espejar
// Verificar que el corredor no quede totalmente bloqueado
const neighbors = [
[r-1,c],[r+1,c],[r,c-1],[r,c+1],
];
const freeNeighbors = neighbors.filter(([nr,nc]) =>
nc >= 0 && nc < COLS && nr >= 0 && nr < ROWS &&
map[nr][nc] !== 1
).length;
if (freeNeighbors < 2) continue; // deja mínimo 2 vecinos libres
// Colocar paredes temporalmente
map[r][c] = 1;
map[r][cm] = 1;
// Verificar que todos los puntos queden accesibles (flood-fill desde Pac)
if (!_allDotsReachable(map)) {
map[r][c] = 2; // revertir
map[r][cm] = 2;
continue;
}
placed++;
}
// ── Para niveles altos quitar algunas power pellets (mínimo 2) ──
if (level >= 4) {
// Ubicaciones de power pellets en el mapa base
const powerPositions = [];
for (let r = 0; r < ROWS; r++)
for (let c = 0; c < COLS; c++)
if (map[r][c] === 3) powerPositions.push([r, c]);
// Quitar 1 power pellet cada 2 niveles, máximo dejar 2
const toRemove = Math.min(Math.floor((level - 3) / 2), powerPositions.length - 2);
for (let i = 0; i < toRemove; i++) {
const idx = randInt(0, powerPositions.length - 1);
const [pr, pc] = powerPositions.splice(idx, 1)[0];
map[pr][pc] = 2; // se convierte en punto normal
}
}
return map;
}
// ── Clase Fantasma ────────────────────────────
class Ghost {
constructor(startR, startC, colorIdx, scatterTarget) {
this.startR = startR;
this.startC = startC;
this.colorIdx = colorIdx;
this.scatterTarget = scatterTarget; // [row, col] esquina de scatter
this.baseSpeed = 5;
this.reset();
}
reset() {
this.r = this.startR;
this.c = this.startC;
this.dir = DIR.LEFT;
this.frightened = false;
this.frightenTimer = 0;
this.blinkPhase = false;
this.eaten = false; // modo ojos
this.px = cellX(this.c);
this.py = cellY(this.r);
this._targetPx = this.px;
this._targetPy = this.py;
this._moving = false;
this.releaseDelay = 0;
this.released = false;
this._houseAnim = 0;
this._eyeVisited = null;
this._eyePath = null;
this._eyePathIdx = 0;
this._eyeFallback = false;
}
get speed() {
if (this.eaten) return this.baseSpeed * 2.0;
if (this.frightened) return this.baseSpeed * 0.5;
return this.baseSpeed;
}
frighten(dur) {
// No afecta a fantasmas que aún no salieron del corral ni a los que están en modo ojos
if (!this.released || this.eaten) return;
this.frightened = true;
this.frightenTimer = dur;
this.blinkPhase = false;
}
update(dt, map, pacR, pacC, pacDir, blinky, scatterMode, level) {
// ── Dentro del corral esperando ──
if (!this.released) {
this.releaseDelay -= dt;
this._houseAnim += dt;
if (this.releaseDelay <= 0) {
this.released = true;
// Teletransportar al punto de salida del corral
this.r = GHOST_EXIT_R;
this.c = GHOST_EXIT_C;
this.px = cellX(this.c);
this.py = cellY(this.r);
this._targetPx = this.px;
this._targetPy = this.py;
this._moving = false;
this.dir = DIR.LEFT;
}
return;
}
// ── Gestión del timer de miedo ──
if (this.frightened) {
this.frightenTimer -= dt;
this.blinkPhase = this.frightenTimer < 2 && Math.floor(this.frightenTimer * 4) % 2 === 0;
if (this.frightenTimer <= 0) {
this.frightened = false;
this.blinkPhase = false;
}
}
// ── Movimiento celda a celda ──
if (!this._moving) {
this._chooseDir(map, pacR, pacC, pacDir, blinky, scatterMode, level);
const nr = this.r + this.dir.dy;
const nc = wrapCol(this.c + this.dir.dx);
const wallFn = this.eaten ? isWallEyes : isWallGhost;
if (!wallFn(map, nr, nc)) {
this.r = nr;
this.c = nc;
this._targetPx = cellX(this.c);
this._targetPy = cellY(this.r);
this._moving = true;
// Si llegó al corral en modo ojos → resetear dentro del corral
if (this.eaten && this.r === this.startR && this.c === this.startC) {
this.eaten = false;
this.released = false;
this.releaseDelay = 4;
this._moving = false;
this.px = cellX(this.c);
this.py = cellY(this.r);
this._eyeVisited = null;
this._eyePath = null;
this._eyePathIdx = 0;
this._eyeFallback = false;
}
}
}
// ── Interpolación suave en píxeles ──
if (this._moving) {
const sp = this.speed * CELL * dt;
const dx = this._targetPx - this.px;
const dy = this._targetPy - this.py;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist <= sp) {
this.px = this._targetPx;
this.py = this._targetPy;
this._moving = false;
} else {
this.px += (dx / dist) * sp;
this.py += (dy / dist) * sp;
}
}
// ── FIX TÚNEL: teletransportar px cuando la celda lógica wrapeó ──
// Si la columna lógica es 0 pero el px está cerca del lado derecho (o viceversa)
// significa que se hizo wrap → snapear px al nuevo lado
if (TUNNEL_ROWS.includes(this.r)) {
const expectedX = cellX(this.c);
if (Math.abs(this.px - expectedX) > CELL * 5) {
this.px = expectedX;
this._targetPx = expectedX;
}
}
}
_chooseDir(map, pacR, pacC, pacDir, blinky, scatterMode, level) {
const wallFn = this.eaten ? isWallEyes : isWallGhost;
// ── Modo ojos: ir al corral ──
if (this.eaten) {
// Inicializar tracking al entrar en modo ojos
if (this._eyeVisited === null) {
this._eyeVisited = {};
this._eyePath = null;
this._eyePathIdx = 0;
this._eyeFallback = false;
}
// Detección de ciclo (solo en modo greedy)
if (!this._eyeFallback) {
const key = `${this.r},${this.c}`;
this._eyeVisited[key] = (this._eyeVisited[key] || 0) + 1;
if (this._eyeVisited[key] >= 3) {
this._eyeFallback = true;
}
}
// Fallback BFS: ruta óptima al corral
if (this._eyeFallback) {
if (!this._eyePath || this._eyePathIdx >= this._eyePath.length) {
this._eyePath = this._bfsPath(map, this.r, this.c, this.startR, this.startC);
this._eyePathIdx = 0;
}
if (this._eyePath && this._eyePathIdx < this._eyePath.length) {
const [nr, nc] = this._eyePath[this._eyePathIdx];
for (const k of DIR_KEYS) {
const d = DIR[k];
if (this.r + d.dy === nr && wrapCol(this.c + d.dx) === nc) {
this.dir = d;
this._eyePathIdx++;
break;
}
}
return;
}
// Si BFS falló, usar greedy como último recurso
}
// Greedy por defecto
this._moveTowards(map, this.startR, this.startC, wallFn);
return;
}
// ── Modo miedo ──
if (this.frightened) {
// Blinky huye directo a su esquina (sup-der) en vez de aleatorio
if (this.colorIdx === 0) {
this._moveTowards(map, this.scatterTarget[0], this.scatterTarget[1], wallFn);
return;
}
// Resto: aleatorio sin reversa (salvo dead-end)
let opts = DIR_KEYS.filter(k => {
const d = DIR[k];
if (d.dx === -this.dir.dx && d.dy === -this.dir.dy) return false;
return !wallFn(map, this.r + d.dy, wrapCol(this.c + d.dx));
});
if (opts.length === 0) {
opts = DIR_KEYS.filter(k => {
const d = DIR[k];
return !wallFn(map, this.r + d.dy, wrapCol(this.c + d.dx));
});
}
if (opts.length > 0) this.dir = DIR[opts[Math.floor(Math.random() * opts.length)]];
return;
}
// ── Modo scatter: ir a esquina ──
if (scatterMode) {
this._moveTowards(map, this.scatterTarget[0], this.scatterTarget[1], wallFn);
return;
}
// ── Modo chase: lógica individual ──
let tr, tc;
switch (this.colorIdx) {
case 0: // Blinky: directo a Pac
tr = pacR; tc = pacC;
break;
case 1: // Pinky: 4 casillas adelante de Pac
tr = pacR + pacDir.dy * 4;
tc = pacC + pacDir.dx * 4;
// En niveles altos el cálculo se vuelve impredecible por cambios del mapa
if (level >= 5) {
const jitter = Math.min(Math.floor(level / 2), 4);
tr += Math.floor(Math.random() * (jitter * 2 + 1)) - jitter;
tc += Math.floor(Math.random() * (jitter * 2 + 1)) - jitter;
}
break;
case 2: { // Inky: vector Blinky→pivote duplicado
const pivR = pacR + pacDir.dy * 2;
const pivC = pacC + pacDir.dx * 2;
tr = pivR + (pivR - blinky.r);
tc = pivC + (pivC - blinky.c);
break;
}
case 3: // Clyde: persigue si lejos, scatter si cerca
if (Math.hypot(this.r - pacR, this.c - pacC) > 8) { tr = pacR; tc = pacC; }
else { tr = this.scatterTarget[0]; tc = this.scatterTarget[1]; }
break;
default:
tr = pacR; tc = pacC;
}
this._moveTowards(map, tr, tc, wallFn);
}
/** Elige la dirección que minimiza distancia euclidiana a (tr,tc). Sin reversa, salvo dead-end. */
_moveTowards(map, tr, tc, wallFn) {
let best = null, bestDist = Infinity;
for (const k of DIR_KEYS) {
const d = DIR[k];
if (d.dx === -this.dir.dx && d.dy === -this.dir.dy) continue;
const nr = this.r + d.dy;
const nc = wrapCol(this.c + d.dx);
if (wallFn(map, nr, nc)) continue;
const dist = (nr - tr) ** 2 + (nc - tc) ** 2;
if (dist < bestDist) { bestDist = dist; best = d; }
}
// Si no encontró dirección (callejón sin salida), permitir reversa
if (!best) {
for (const k of DIR_KEYS) {
const d = DIR[k];
const nr = this.r + d.dy;
const nc = wrapCol(this.c + d.dx);
if (wallFn(map, nr, nc)) continue;
const dist = (nr - tr) ** 2 + (nc - tc) ** 2;
if (dist < bestDist) { bestDist = dist; best = d; }
}
}
if (best) this.dir = best;
}
/** BFS shortest path (excluye celda origen). Usa isWallEyes como walkable. */
_bfsPath(map, fromR, fromC, toR, toC) {
const visited = Array.from({length: ROWS}, () => Array(COLS).fill(false));
const prev = Array.from({length: ROWS}, () => Array(COLS).fill(null));
const q = [[fromR, fromC]];
visited[fromR][fromC] = true;
let head = 0;
while (head < q.length) {
const [r, c] = q[head++];
if (r === toR && c === toC) {
const path = [];
let cur = [r, c];
while (cur[0] !== fromR || cur[1] !== fromC) {
path.unshift(cur);
cur = prev[cur[0]][cur[1]];
}
return path;
}
for (const [dr, dc] of [[-1,0],[1,0],[0,-1],[0,1]]) {
const nr = r + dr;
const nc = wrapCol(c + dc);
if (nr < 0 || nr >= ROWS) continue;
if (visited[nr][nc]) continue;
if (isWallEyes(map, nr, nc)) continue;
visited[nr][nc] = true;
prev[nr][nc] = [r, c];
q.push([nr, nc]);
}
}
return null;
}
draw(ctx) {
if (!this.released) {
const bx = cellX(this.startC);
const by = cellY(this.startR) + Math.sin(this._houseAnim * 3) * 3;
this._drawBody(ctx, bx, by, C_GHOST[this.colorIdx], false);
return;
}
if (this.eaten) {
this._drawEyes(ctx, this.px, this.py);
return;
}
const color = this.frightened
? (this.blinkPhase ? '#ddddff' : C_FRIGHTEN)
: C_GHOST[this.colorIdx];
this._drawBody(ctx, this.px, this.py, color, this.frightened);
}
_drawBody(ctx, bx, by, color, frightened) {
const r = CELL * 0.46;
ctx.save();
// Cuerpo con faldones curvos
ctx.beginPath();
ctx.arc(bx, by - r * 0.05, r, Math.PI, 0, false);
const baseY = by + r * 0.75;
const left = bx - r, right = bx + r;
const w3 = (right - left) / 3;
ctx.lineTo(right, baseY);
ctx.quadraticCurveTo(right - w3 * 0.5, baseY + r * 0.35, right - w3, baseY);
ctx.quadraticCurveTo(right - w3 * 1.5, baseY + r * 0.35, right - w3 * 2, baseY);
ctx.quadraticCurveTo(right - w3 * 2.5, baseY + r * 0.35, left, baseY);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
if (!frightened) {
this._drawEyes(ctx, bx, by);
} else {
// Cara asustada
ctx.fillStyle = '#aaaaff';
ctx.beginPath(); ctx.arc(bx - r * 0.3, by - r * 0.1, r * 0.15, 0, Math.PI * 2); ctx.fill();
ctx.beginPath(); ctx.arc(bx + r * 0.3, by - r * 0.1, r * 0.15, 0, Math.PI * 2); ctx.fill();
ctx.strokeStyle = '#aaaaff';
ctx.lineWidth = 1.5;
ctx.lineJoin = 'round';
ctx.beginPath();
const mx0 = bx - r * 0.38, my0 = by + r * 0.22;
ctx.moveTo(mx0, my0);
for (let i = 1; i <= 5; i++) {
ctx.lineTo(mx0 + (r * 0.76 / 5) * i, my0 + (i % 2 === 0 ? 0 : r * 0.18));
}
ctx.stroke();
}
ctx.restore();
}
_drawEyes(ctx, bx, by) {
const r = CELL * 0.46;
const eyeR = r * 0.26;
const pupilR = r * 0.13;
const offX = r * 0.3;
const eyeY = by - r * 0.15;
const pdx = this.dir.dx * pupilR * 0.7;
const pdy = this.dir.dy * pupilR * 0.7;
ctx.fillStyle = '#ffffff';
ctx.beginPath(); ctx.arc(bx - offX, eyeY, eyeR, 0, Math.PI * 2); ctx.fill();
ctx.beginPath(); ctx.arc(bx + offX, eyeY, eyeR, 0, Math.PI * 2); ctx.fill();
ctx.fillStyle = '#0055ff';
ctx.beginPath(); ctx.arc(bx - offX + pdx, eyeY + pdy, pupilR, 0, Math.PI * 2); ctx.fill();
ctx.beginPath(); ctx.arc(bx + offX + pdx, eyeY + pdy, pupilR, 0, Math.PI * 2); ctx.fill();
}
}
// ── Juego principal ───────────────────────────
const game = {
state: 'menu',
map: null,
score: 0,
hiScore: 0,
lives: 3,
level: 1,
dotsLeft: 0,
pac: {
r: PAC_START_R, c: PAC_START_C,
px: 0, py: 0,
dir: DIR.LEFT, nextDir: DIR.LEFT,
speed: 7,
_moving: false, _targetPx: 0, _targetPy: 0,
mouthAngle: 0, mouthOpen: true, mouthSpeed: 8,
dead: false, deadTimer: 0, deadAnim: 0,
},
ghosts: [],
powerTimer: 0,
POWER_DUR: 8,
ghostEatCombo: 0,
phaseIdx: 0,
phaseTimer: PHASE_TIMES[0],
scatterMode: true,
_btns: {},
_hover: '',
_touchStart: null,
// ── Init ──
init() {
this.hiScore = parseInt(localStorage.getItem('pacman_hi') || '0');
Audio.synth('dot', 'square', 440, 0.05, 0.15);
Audio.synth('power', 'square', 220, 0.4, 0.25);
Audio.synth('eat', 'square', 600, 0.12, 0.3);
Audio.synth('dead', 'sine', 180, 0.6, 0.3, 80);
Audio.synth('start', 'square', 330, 0.3, 0.2);
Audio.synth('win', 'square', 660, 0.3, 0.2);
this._setupBtns();
},
_setupBtns() {
const cx = LW / 2;
const bw = 200;
const bh = 48;
this._btns = {
play: { x: cx - bw / 2, y: 340 - bh / 2, w: bw, h: bh, label: '▶ JUGAR', accent: '#ffe000' },
};
},
// ── Iniciar partida ──
_startGame() {
this.score = 0;
this.lives = 3;
this.level = 1;
this._loadLevel();
this.state = 'playing';
Audio.play('start');
},
_loadLevel() {
// Generar mapa para este nivel
this.map = generateMap(this.level);
// Contar puntos
this.dotsLeft = 0;
for (let r = 0; r < ROWS; r++)
for (let c = 0; c < COLS; c++)
if (this.map[r][c] === 2 || this.map[r][c] === 3) this.dotsLeft++;
// Pac-Man
const p = this.pac;
p.r = PAC_START_R; p.c = PAC_START_C;
p.px = cellX(p.c); p.py = cellY(p.r);
p._targetPx = p.px; p._targetPy = p.py;
p._moving = false;
p.dir = DIR.LEFT; p.nextDir = DIR.LEFT;
p.dead = false; p.deadTimer = 0; p.deadAnim = 0;
p.speed = 7 + (this.level - 1) * 0.4;
// Fantasmas - posiciones internas del corral
// Blinky empieza fuera (ya liberado), los demás dentro
this.ghosts = [
new Ghost(GHOST_HOME_R, GHOST_HOME_C, 0, [0, COLS-1]), // Blinky sup-der
new Ghost(GHOST_HOME_R, GHOST_HOME_C - 1, 1, [0, 0 ]), // Pinky sup-izq
new Ghost(GHOST_HOME_R, GHOST_HOME_C, 2, [ROWS-1, 0 ]), // Inky inf-izq
new Ghost(GHOST_HOME_R, GHOST_HOME_C + 1, 3, [ROWS-1, COLS-1]), // Clyde inf-der
];
// Blinky sale inmediatamente fuera del corral
this.ghosts[0].released = true;
this.ghosts[0].r = GHOST_EXIT_R;
this.ghosts[0].c = GHOST_EXIT_C;
this.ghosts[0].px = cellX(GHOST_EXIT_C);
this.ghosts[0].py = cellY(GHOST_EXIT_R);
// Delays del resto
this.ghosts[1].releaseDelay = 3;
this.ghosts[2].releaseDelay = 6;
this.ghosts[3].releaseDelay = 9;
// Velocidad base por nivel
const gs = 4.5 + (this.level - 1) * 0.35;
this.ghosts.forEach(g => { g.baseSpeed = gs; });
// Fases
this.phaseIdx = 0;
this.phaseTimer = PHASE_TIMES[0];
this.scatterMode = true;
this.powerTimer = 0;
this.ghostEatCombo = 0;
},
_respawn() {
const p = this.pac;
p.r = PAC_START_R; p.c = PAC_START_C;
p.px = cellX(p.c); p.py = cellY(p.r);
p._targetPx = p.px; p._targetPy = p.py;
p._moving = false;
p.dir = DIR.LEFT; p.nextDir = DIR.LEFT;
p.dead = false; p.deadTimer = 0; p.deadAnim = 0;
this.ghosts.forEach(g => g.reset());
this.ghosts[0].released = true;
this.ghosts[0].r = GHOST_EXIT_R; this.ghosts[0].c = GHOST_EXIT_C;
this.ghosts[0].px = cellX(GHOST_EXIT_C); this.ghosts[0].py = cellY(GHOST_EXIT_R);
this.ghosts[1].releaseDelay = 3;
this.ghosts[2].releaseDelay = 6;
this.ghosts[3].releaseDelay = 9;
const gs = 4.5 + (this.level - 1) * 0.35;
this.ghosts.forEach(g => { g.baseSpeed = gs; });
this.powerTimer = 0;
this.ghostEatCombo = 0;
this.phaseIdx = 0;
this.phaseTimer = PHASE_TIMES[0];
this.scatterMode = true;
},
// ── Update ──
update(dt) {
const m = Input.getMouse();
const gm = Engine.toGame(m.x, m.y);
const touch = Input.getTouch(0);
const gt = touch ? Engine.toGame(touch.x, touch.y) : null;
const gx = gt ? gt.x : gm.x;
const gy = gt ? gt.y : gm.y;
const clicked = Input.isMousePressed() || Input.isTouchStarted();
// ── Estados de menú ──
if (this.state === 'menu') {
this._hover = '';
for (const [key, btn] of Object.entries(this._btns)) {
if (UICanvas.hitTest(gx, gy, btn)) this._hover = key;
}
if (clicked && UICanvas.hitTest(gx, gy, this._btns.play)) this._startGame();
return;
}
if (this.state === 'gameover' || this.state === 'win') {
if (clicked) { this.state = 'menu'; this._setupBtns(); }
return;
}
if (this.state !== 'playing') return;
const p = this.pac;
// ── Animación de muerte ──
if (p.dead) {
p.deadTimer -= dt;
p.deadAnim += dt * 3;
if (p.deadTimer <= 0) {
this.lives--;
if (this.lives <= 0) {
this.state = 'gameover';
if (this.score > this.hiScore) {
this.hiScore = this.score;
localStorage.setItem('pacman_hi', this.hiScore);
}
} else {
this._respawn();
}
}
return;
}
// ── Input teclado ──
if (Input.isDown('ArrowUp') || Input.isDown('KeyW')) p.nextDir = DIR.UP;
if (Input.isDown('ArrowDown') || Input.isDown('KeyS')) p.nextDir = DIR.DOWN;
if (Input.isDown('ArrowLeft') || Input.isDown('KeyA')) p.nextDir = DIR.LEFT;
if (Input.isDown('ArrowRight') || Input.isDown('KeyD')) p.nextDir = DIR.RIGHT;
// ── Input táctil (swipe) ──
if (Input.isTouchStarted()) this._touchStart = gt;
if (touch && this._touchStart) {
const sdx = (gt?.x || 0) - this._touchStart.x;
const sdy = (gt?.y || 0) - this._touchStart.y;
if (Math.abs(sdx) > 20 || Math.abs(sdy) > 20) {
p.nextDir = Math.abs(sdx) > Math.abs(sdy)
? (sdx > 0 ? DIR.RIGHT : DIR.LEFT)
: (sdy > 0 ? DIR.DOWN : DIR.UP);
this._touchStart = null;
}
}
if (!touch) this._touchStart = null;
// ── Movimiento Pac-Man ──
if (!p._moving) {
// Intentar girar
const nd = p.nextDir;
const tnr = p.r + nd.dy;
const tnc = wrapCol(p.c + nd.dx);
if (!isWallPac(this.map, tnr, tnc)) p.dir = nd;
// Avanzar en dirección actual
const cr = p.r + p.dir.dy;
const cc = wrapCol(p.c + p.dir.dx);
if (!isWallPac(this.map, cr, cc)) {
const oldC = p.c;
p.r = cr; p.c = cc;
p._targetPx = cellX(p.c);
p._targetPy = cellY(p.r);
p._moving = true;
// ── FIX TÚNEL Pac-Man: si la columna wrapeó, teletransportar px ──
if (TUNNEL_ROWS.includes(p.r) && Math.abs(p.c - oldC) > 1) {
p.px = cellX(p.c);
p.py = cellY(p.r);
}
}
}
if (p._moving) {
const sp = p.speed * CELL * dt;
const dx = p._targetPx - p.px;
const dy = p._targetPy - p.py;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist <= sp) {
p.px = p._targetPx;
p.py = p._targetPy;
p._moving = false;
this._eatCell();
} else {
p.px += (dx / dist) * sp;
p.py += (dy / dist) * sp;
}
}
// Animar boca
p.mouthAngle += dt * p.mouthSpeed * (p.mouthOpen ? 1 : -1);
if (p.mouthAngle > 0.9) { p.mouthOpen = false; p.mouthAngle = 0.9; }
if (p.mouthAngle < 0.0) { p.mouthOpen = true; p.mouthAngle = 0.0; }
// ── Power timer ──
if (this.powerTimer > 0) {
this.powerTimer -= dt;
if (this.powerTimer <= 0) {
this.powerTimer = 0;
this.ghostEatCombo = 0;
this.ghosts.forEach(g => { g.frightened = false; g.blinkPhase = false; });
}
}
// ── Fases scatter / chase (pausa durante powerup) ──
if (this.powerTimer <= 0) {
this.phaseTimer -= dt;
if (this.phaseTimer <= 0) {
this.phaseIdx = Math.min(this.phaseIdx + 1, PHASE_TIMES.length - 1);
this.scatterMode = (this.phaseIdx % 2 === 0);
this.phaseTimer = PHASE_TIMES[this.phaseIdx] ?? 99999;
}
}
// ── Actualizar fantasmas ──
const blinky = this.ghosts[0];
this.ghosts.forEach(g =>
g.update(dt, this.map, p.r, p.c, p.dir, blinky, this.scatterMode, this.level)
);
// ── Colisiones Pac vs fantasmas ──
for (const g of this.ghosts) {
if (!g.released || g.eaten) continue;
if (Math.hypot(p.px - g.px, p.py - g.py) < CELL * 0.72) {
if (g.frightened) {
this.ghostEatCombo++;
const pts = [200, 400, 800, 1600][Math.min(this.ghostEatCombo - 1, 3)];
this.score += pts;
g.frightened = false;
g.blinkPhase = false;
g.eaten = true;
g._moving = false;
Audio.play('eat');
} else {
p.dead = true;
p.deadTimer = 1.8;
Audio.play('dead');
break;
}
}
}
// ── Siguiente nivel ──
if (this.dotsLeft <= 0) {
this.level++;
Audio.play('win');
this._loadLevel();
}
},
_eatCell() {
const p = this.pac;
const cell = this.map[p.r][p.c];
if (cell === 2) {
this.map[p.r][p.c] = 0;
this.score += 10;
this.dotsLeft--;
Audio.play('dot');
} else if (cell === 3) {
this.map[p.r][p.c] = 0;
this.score += 50;
this.dotsLeft--;
this.powerTimer = this.POWER_DUR;
this.ghostEatCombo = 0;
// Activar modo miedo en todos los fantasmas liberados
this.ghosts.forEach(g => g.frighten(this.POWER_DUR));
Audio.play('power');
}
if (this.score > this.hiScore) {
this.hiScore = this.score;
localStorage.setItem('pacman_hi', this.hiScore);
}
},
// ── Render ──
render(ctx) {
ctx.fillStyle = C_BG;
ctx.fillRect(0, 0, LW, LH);
if (this.state === 'menu') {
this._renderMenu(ctx);
} else {
this._renderGame(ctx);
if (this.state === 'gameover') this._renderOverlay(ctx, 'GAME OVER', '#ff4444');
else if (this.state === 'win') this._renderOverlay(ctx, '¡GANASTE!', '#ffe000');
}
},
_renderMenu(ctx) {
const cx = LW / 2;
ctx.save();
ctx.font = "bold 52px 'Courier New', monospace";
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.fillStyle = '#ffaa00'; ctx.fillText('PAC-MAN', cx + 2, 152);
ctx.fillStyle = C_PAC; ctx.fillText('PAC-MAN', cx, 150);
ctx.restore();
// Pac-Man animado
ctx.save();
const t = performance.now() / 1000;
const mA = Math.abs(Math.sin(t * 4)) * 0.8;
ctx.fillStyle = C_PAC;
ctx.beginPath();
ctx.moveTo(cx, 240);
ctx.arc(cx, 240, 28, mA, Math.PI * 2 - mA);
ctx.closePath(); ctx.fill();
ctx.restore();
Engine.text(`HI-SCORE: ${this.hiScore}`, cx, 290, '#ffaa00', 16);
const btn = this._btns.play;
UICanvas.drawButton(ctx, btn.label, btn.x, btn.y, btn.w, btn.h, btn.accent, this._hover === 'play');
ctx.save();
ctx.font = "13px 'Courier New', monospace";
ctx.fillStyle = '#888'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.fillText('WASD / ← ↑ ↓ → / Swipe', cx, 400);