* case 'splash': noise(.12,.12,0,3000); break; case 'glitch': tone(180,.05,'square',.1,200); tone(320,.05,'square',.1,-200,.05); break; case 'boom': tone(90,.6,'sawtooth',.25,-60); noise(.5,.25,0,300); break; } } /* ---------- Temas musicales (composiciones originales) ---------- */ const NI={C:0,D:2,E:4,F:5,G:7,A:9,B:11}; const midiOf=t=>{const m=/^([A-G])([#b]?)(\d)$/.exec(t); return 12*(+m[3]+1)+NI[m[1]]+(m[2]==='#'?1:m[2]==='b'?-1:0)}; const mf=m=>440*Math.pow(2,(m-69)/12); function parseTrack(str){ const toks=str.trim().split(/\s+/), out=new Array(toks.length).fill(null); let last=-1; toks.forEach((t,i)=>{ if(t==='-') last=-1; else if(t==='='){ if(last>=0) out[last].d++; } else { out[i]={f:mf(midiOf(t)),d:1}; last=i; } }); return out; } function buildBass(roots,pat){ const out=[]; roots.forEach(r=>{ const m=midiOf(r); let last=-1; for(let j=0;j<8;j++){ const ch=pat[j]; let ev=null; if(ch==='R') ev={f:mf(m),d:1}; else if(ch==='F') ev={f:mf(m+7),d:1}; else if(ch==='O') ev={f:mf(m+12),d:1}; if(ch==='=' && last>=0){ out[last].d++; out.push(null); continue; } out.push(ev); last=ev?out.length-1:-1; } }); return out; } function buildArp(chords,pat){ const out=[]; chords.forEach(ch=>{ for(let j=0;j<8;j++){ const idx=pat[j]; out.push(idx==null?null:{f:mf(midiOf(ch[idx%ch.length])+(idx>=ch.length?12:0)),d:1}); } }); return out; } const THEME_DEF={ menu:{bpm:100,lw:'square',lv:.07,lead:'E5 = G5 = C6 = G5 - A5 = G5 = E5 = C5 - F5 = A5 = C6 = A5 - G5 = B5 = D6 = B5 -', chords:[['C4','E4','G4'],['A3','C4','E4'],['F3','A3','C4'],['G3','B3','D4']],roots:['C3','A2','F2','G2'],bp:'R-F-R-F-',ap:[0,1,2,1,0,1,2,1],aw:'triangle',av:.08,dr:'- - - - - - - -'}, w1:{bpm:96,lw:'square',lv:.075,lead:'A4 = D5 F5 A5 = G5 F5 E5 = A4 C#5 E5 = D5 C#5 D5 = F5 = A5 = D6 - C6 = G5 E5 G5 = E5 G5 A5 = C6 = A5 = F5 G5 E5 = G5 = E5 = D5 C5 D5 = A5 = F5 = D5 - E5 = A5 G5 F5 E5 D5 C#5', chords:[['D4','F4','A4'],['A3','C#4','E4'],['D4','F4','A4'],['C4','E4','G4'],['F4','A4','C5'],['C4','E4','G4'],['D4','F4','A4'],['A3','C#4','E4']], roots:['D3','A2','D3','C3','F3','C3','D3','A2'],bp:'R-F-R-F-',ap:[0,1,2,1,0,1,2,1],aw:'triangle',av:.07,dr:'k - h - k - h h'}, w2:{bpm:116,lw:'square',lv:.07,lead:'B5 = D6 = G5 = B5 - A5 = F#5 = D5 = F#5 A5 G5 = B5 = E6 = D6 B5 E6 = C6 = G5 = E5 G5 D6 = B5 G5 B5 = D6 G6 F#6 = D6 = A5 = D6 - E6 = G5 C6 E6 = D6 C6 C6 = A5 F#5 A5 = D5 -', chords:[['G4','B4','D5'],['D4','F#4','A4'],['E4','G4','B4'],['C4','E4','G4'],['G4','B4','D5'],['D4','F#4','A4'],['C4','E4','G4'],['D4','F#4','A4']], roots:['G3','D3','E3','C3','G3','D3','C3','D3'],bp:'R--FR--F',ap:[0,2,1,2,0,2,1,2],aw:'sine',av:.1,dr:'- - h - - - h -'}, w3:{bpm:126,lw:'square',lv:.065,lead:'A5 - C6 A5 F5 - A5 - G5 - E5 G5 C6 - G5 - A5 - C6 A5 F5 - C6 - D6 - Bb5 D6 F6 - D6 - C6 - A5 C6 F6 - C6 - B5 - G5 B5 D6 - B5 - A5 = F5 A5 C6 = A5 F5 G5 = E5 G5 C6 - - -', chords:[['F4','A4','C5'],['C4','E4','G4'],['F4','A4','C5'],['Bb3','D4','F4'],['F4','A4','C5'],['C4','E4','G4'],['F4','A4','C5'],['C4','E4','G4']], roots:['F3','C3','F3','Bb2','F3','C3','F3','C3'],bp:'R-F-R-F-',ap:[null,1,null,2,null,1,null,2],aw:'square',av:.045,dr:'k - s - k - s -'}, w4:{bpm:78,lw:'sine',lv:.16,lead:'E5 = = = C5 = = - C5 = = = A4 = = - D5 = = = F5 = = - G#4 = B4 = E5 = = -', chords:[['A3','C4','E4'],['F3','A3','C4'],['D4','F4','A4'],['E3','G#3','B3']],roots:['A2','F2','D3','E2'],bp:'R=======',ap:[0,1,2,1,2,1,0,1],aw:'triangle',av:.07,dr:'k - - - - - k -'}, boss:{bpm:148,lw:'sawtooth',lv:.06,lead:'E5 E5 G5 E5 B5 A5 G5 E5 G5 G5 E5 G5 C6 B5 G5 E5 A5 A5 F#5 A5 D6 C#6 A5 F#5 B5 B5 D#6 B5 F#6 E6 D#6 B5', chords:[['E4','G4','B4'],['C4','E4','G4'],['D4','F#4','A4'],['B3','D#4','F#4']],roots:['E3','C3','D3','B2'],bp:'RRFRRFRR',ap:[0,1,2,1,0,1,2,1],aw:'square',av:.035,dr:'k h s h k h s h'}, mini:{bpm:138,lw:'square',lv:.07,lead:'E5 G5 C6 G5 E5 G5 C6 - D5 G5 B5 G5 D5 G5 B5 - E5 A5 C6 A5 E5 A5 C6 - F5 A5 C6 A5 F5 A5 C6 A5', chords:[['C4','E4','G4'],['G3','B3','D4'],['A3','C4','E4'],['F3','A3','C4']],roots:['C3','G2','A2','F2'],bp:'R-R-F-R-',ap:[0,2,1,2,0,2,1,2],aw:'triangle',av:.07,dr:'k h s h k k s h'}, final:{bpm:84,lw:'triangle',lv:.14,lead:'G5 = = C6 = = E6 - D6 = = B5 = = G5 - C6 = = E6 = = A5 - A5 = = C6 = = F6 - E6 = D6 C6 = G5 = - B5 = D6 = G6 = = - A5 C6 F6 = E6 = D6 - C6 = = = = = = -', chords:[['C4','E4','G4'],['G3','B3','D4'],['A3','C4','E4'],['F3','A3','C4'],['C4','E4','G4'],['G3','B3','D4'],['F3','A3','C4'],['C4','E4','G4']], roots:['C3','G2','A2','F2','C3','G2','F2','C3'],bp:'R-F-O-F-',ap:[0,1,2,1,2,1,0,1],aw:'sine',av:.1,dr:'k - - - s - - -'} }; const THEMES={}; function buildThemes(){ for(const k in THEME_DEF){ const d=THEME_DEF[k]; THEMES[k]={bpm:d.bpm,tr:[ {ev:parseTrack(d.lead),wave:d.lw,vol:d.lv,len:.92}, {ev:buildArp(d.chords,d.ap),wave:d.aw,vol:d.av,len:.8}, {ev:buildBass(d.roots,d.bp),wave:'triangle',vol:.16,len:.9}], dr:d.dr.split(' ')}; } } const Music={cur:null,want:null,step:0,next:0,timer:0, play(name,force){ this.want=name; if(!Aud.ctx||(this.cur===name&&!force)) return; clearInterval(this.timer); const th=THEMES[name]; if(!th){this.cur=null;return} this.cur=name; this.step=0; this.next=Aud.ctx.currentTime+0.1; this.timer=setInterval(()=>this.tick(),60); }, stop(){clearInterval(this.timer);this.cur=null;this.want=null}, tick(){ const c=Aud.ctx, th=THEMES[this.cur]; if(!c||!th) return; const dur=60/th.bpm/2; if(this.next {if(tx<0||tx>=L.w)return(ty>=0&&ty =ROWS)return 0; return L.tiles[ty*L.w+tx]}; const setT=(L,tx,ty,v)=>{if(tx>=0&&tx =0&&ty clamp(Math.floor(px/(L.w*T/3)),0,2); function addItem(L,k,x,y,o){const it={k,x,y,taken:false,ph:Math.random()*6,pop:0,hidden:false};if(o)Object.assign(it,o);L.items.push(it);return it} function docRow(L,x0,x1,y,n,o){for(let i=0;i {const x0=x;for(let i=0;i .34){flat(12,'vault');vaultDone=true;continue} if(!towerDone&&prog>.6){if(h<11)h=11;flat(14,'tower');towerDone=true;continue} if(rl<.34) flat(5+(r()*7|0)); else if(rl<.54&&last.k!=='gap'&&last.x1-last.x0>=3){ const g=2+(r()*(W.maxGap-1)|0); segs.push({x0:x,x1:x+g,h,k:'gap'}); x+=g; flat(3+(r()*3|0)); } else if(rl<.68){const d=1+(r()*2|0); if(h-d>=7)h-=d; flat(4+(r()*4|0),'step')} else if(rl<.80){const d=1+(r()*2|0); if(h+d<=12)h+=d; flat(4+(r()*4|0),'step')} else if(rl<.90){const d=1+(r()*2|0); if(h-d>=7){h-=d;flat(2,'step');flat(2+(r()*3|0));h+=d;flat(3,'step')}else flat(6)} else flat(6+(r()*6|0)); } flat(w-x,'end'); L.top=top; for(let i=0;i =0) for(let y=top[i];y tx*T+T/2; const propTypes=['farol','farol','banco','cajas']; /* --- plan de cajas con potenciadores --- */ const plan=[{k:'sello',min:.42},{k:'resta',min:.58},{k:'sello',min:.78},{k:'resta',min:.86}]; const brick=(x0,x1,row,boxAt,content)=>{ for(let i=x0;i { if(plan.length&&prog>=plan[0].min) return plan.shift().k; const q=r(); return q<.5?'docs':q<.66?'lupa':q<.8?'digi':q<.9?'sello':'docs'; }; let startBox=false; segs.forEach(s=>{ const len=s.x1-s.x0, prog=s.x0/w, h0=s.h, gy=h0*T; if(s.k==='gap'){ const n=s.x1-s.x0+2; docArc(L,(s.x0-.5)*T,(s.x1+.5)*T,(s.h-1)*T-8,n+1,70+ (r()*30|0)); return; } if(s.k==='start'){ brick(5,8,h0-3,6,'lupa'); docRow(L,3*T,5*T,gy-20,3); docRow(L,8.5*T,9.5*T,gy-20,2); return; } if(s.k==='vault'||s.k==='tower'||s.k==='end') return; /* decoración normal de tramo */ if(len>=4&&r()<.6) L.props.push({t:propTypes[(r()*propTypes.length)|0],x:(s.x0+1+r()*(len-2))*T,gy,v:(r()*3)|0}); const needBox=plan.length&&prog>=plan[0].min&&len>=6; const roll=needBox?.5:r(); if(roll<.34){ // fila de documentos sobre el suelo docRow(L,(s.x0+1)*T,(s.x1-1)*T,gy-20,Math.min(len-1,3+(r()*3|0))); }else if(roll<.70&&len>=6){ // plataforma de bloques con caja const pw=3+(r()*Math.min(3,len-5)|0), px=s.x0+1+(r()*(len-pw-1)|0), row=h0-3; const bx=px+(r()*pw|0); brick(px,px+pw,row,bx,pickBox(prog)); docRow(L,px*T+16,(px+pw)*T-16,row*T-20,pw); if(r()<.5) docRow(L,(s.x0+1)*T,(px-0.2)*T,gy-20,Math.max(0,Math.min(3,px-s.x0-1)||0)||1); }else if(len>=8){ // doble nivel const pw=4, px=s.x0+1+(r()*(len-pw-3)|0), row=h0-3; brick(px,px+pw,row,px+1,pickBox(prog)); for(let i=0;i<3;i++) setT(L,px+i+1,h0-6,5); L.plats.push({x0:px+1,x1:px+4,row:h0-6}); docRow(L,(px+1)*T+16,(px+4)*T-16,(h0-6)*T-20,3); docRow(L,px*T+16,(px+pw)*T-16,row*T-20,pw); }else docRow(L,(s.x0+1)*T,(s.x1-1)*T,gy-20,2); /* enemigos */ if(prog>.09&&len>=5&&(s.k==='flat'||s.k==='step')&&r() { if(t==='error404')return prog>.4; if(t==='tranvia')return len>=11; if(len<6&&(t==='carretilla'||t==='estatua'||t==='pendulo'))return false; return true;}); const t=cand[(r()*cand.length)|0], b0=(s.x0+.3)*T, b1=(s.x1-.3)*T, mid=(b0+b1)/2; let px=mid; if(t==='barril'){ // una barrilera al final del tramo lanza barriles rodando L.enemies.push(mkEnemy('barrilera',b1-26,gy,b0,b1,r)); return; } L.enemies.push(mkEnemy(t,px,gy,b0,b1,r)); if(len>=10&&r()<.3&&t!=='tranvia'&&t!=='fuente'&&t!=='pendulo'){const t2=cand[(r()*cand.length)|0]; if(t2!=='barril'&&t2!=='tranvia'&&t2!=='fuente'&&t2!=='pendulo'&&t2!=='carretilla')L.enemies.push(mkEnemy(t2,mid+ (r()<.5?-1:1)*60,gy,b0,b1,r))} } }); /* --- documentos especiales por el recorrido --- */ const world=DB.filter(d=>d.w===wi); const elig=segs.filter(s=>(s.k==='flat'||s.k==='step')&&(s.x1-s.x0)>=6&&s.x0>14&&s.x0 d.spot==='path'); pathDocs.forEach((d,i)=>{ const s=elig[Math.min(elig.length-1,Math.floor((i+.5)*elig.length/pathDocs.length))]; if(!s)return; const mid=((s.x0+s.x1)/2)*T; addItem(L,'doc',mid,s.h*T-3.5*T,{id:d.id}); }); /* --- documentos ocultos (solo con la Lupa) sobre plataformas --- */ const plats=L.plats.filter(p=>p.x0>16); const hid=world.find(d=>d.spot==='hidden'); plats.forEach((p,i)=>{ if(i%2===0){ const yy=p.row*T-62; for(let k=0;k<3;k++) addItem(L,'perg',((p.x0+p.x1)/2+(k-1)*0.9)*T,yy-(k===1?14:0),{hidden:true}); } }); if(hid&&plats.length){const p=plats[Math.floor(plats.length*.55)]||plats[0]; addItem(L,'doc',((p.x0+p.x1)/2)*T,p.row*T-78,{id:hid.id,hidden:true})} /* --- cámara digital (bóveda con muros digitales) --- */ const vs=segs.find(s=>s.k==='vault'); if(vs){ const x0=vs.x0,h1=vs.h,row=h1-3; for(const bx of [x0,x0+2])setT(L,bx,row,2); setT(L,x0+1,row,3);L.boxes[row*w+x0+1]='digi'; for(let i=x0+3;i<=x0+10;i++)setT(L,i,row,2); for(const wx of [x0+3,x0+10]){setT(L,wx,h1-2,6);setT(L,wx,h1-1,6)} L.plats.push({x0:x0+3,x1:x0+11,row}); addItem(L,'digi',cx(x0+4),h1*T-22); for(let k=0;k<3;k++)addItem(L,'perg',cx(x0+5+k),h1*T-22); const vd=world.find(d=>d.spot==='vault'); if(vd) addItem(L,'doc',cx(x0+8)+10,h1*T-24,{id:vd.id}); for(let k=0;k<4;k++)addItem(L,'perg',cx(x0+4+k),row*T-20); L.props.push({t:'vaultsign',x:(x0+3)*T,gy:h1*T,v:0}); } /* --- torre secreta con puerta oculta a la Sala Memoria de Madrid --- */ const ts=segs.find(s=>s.k==='tower'); if(ts){ const x0=ts.x0,h1=ts.h, spans=[[x0+1,x0+4,h1-3],[x0+5,x0+8,h1-6],[x0+9,x0+12,h1-9]]; spans.forEach(sp=>{for(let i=sp[0];i s.k==='flat'&&s.x0>w*.5&&(s.x1-s.x0)>=5)||segs[segs.length-2]; L.cp={x:(cs.x0+2)*T+T/2,gy:cs.h*T,on:false}; const gx=w-7; L.goal={x:gx*T,y:top[gx]*T-96,w:64,h:96}; L.spawn={x:2*T,gy:top[2]*T}; return L; } /* Sala secreta: SALA MEMORIA DE MADRID, repleta de documentos */ function genRoom(wi){ const w=38, L=newLevel('room',wi,w), r=mulberry(555+wi*31); for(let x=0;x {for(let i=sp[0];i { const n=4; docRow(L,sp[0]*T+20,sp[1]*T-20,sp[2]*T-22,n); if(i>0&&i<5) docArc(L,(sp[0]-1)*T,sp[0]*T+8,(sp[2]+1)*T,3,34); }); docRow(L,22*T,36*T,12*T-20,9); for(let i=0;i<5;i++)addItem(L,'perg',(17+i*.7)*T,3*T-70-(i%2)*14); const ld=DB.find(d=>d.w===wi&&d.spot==='room'); if(ld) addItem(L,'doc',18.5*T+16,3*T-24,{id:ld.id}); L.doors.push({x:1*T,y:12*T-64,w:T,h:64,exit:true}); L.doors.push({x:(w-3)*T,y:12*T-64,w:T,h:64,exit:true}); L.spawn={x:2*T+8,gy:12*T}; return L; } /* Arenas del minijuego (Digitalización masiva) y del jefe */ function genArena(kind){ const w=25, L=newLevel(kind==='mini'?'mini':'boss',kind==='mini'?0:3,w); for(let x=0;x {for(let i=s[0];i {const c=mkCanvas(T*S,T*S),g=c.getContext('2d');g.scale(S,S);fn(g);return c}; const pal=[ {top:'#e2bd82',topl:'#f3d9a6',fill:'#a87b4a',fl:'#8c6238',mort:'#6e4a28',brick:'#cfa564',bl:'#e6c286',bd:'#8d6a38',plank:'#6a4222',pl:'#8a5a30'}, {top:'#5fb04a',topl:'#8ad06a',fill:'#8a6a44',fl:'#765a38',mort:'#5a4428',brick:'#2f8f3f',bl:'#4fb55a',bd:'#1f6a2c',plank:'#d9d2bd',pl:'#f2ecd8'}, {top:'#9b9ba8',topl:'#b8b8c4',fill:'#6d6d7a',fl:'#5a5a68',mort:'#40404c',brick:'#efe3c2',bl:'#fff6dc',bd:'#a8996a',plank:'#3f4250',pl:'#5a5e72'}, {top:'#8062b4',topl:'#a58ae0',fill:'#3f2f68',fl:'#33265a',mort:'#241a44',brick:'#b6a6e6',bl:'#d8ccf6',bd:'#6a5aa0',plank:'#7f62c0',pl:'#a58ae0'}][wi]; ts.fill=mk(g=>{g.fillStyle=pal.fill;g.fillRect(0,0,T,T);g.strokeStyle=pal.mort;g.lineWidth=1.5; g.strokeRect(.5,.5,T-1,T-1);g.beginPath();g.moveTo(0,16);g.lineTo(T,16);g.moveTo(16,0);g.lineTo(16,16);g.moveTo(8,16);g.lineTo(8,T);g.moveTo(24,16);g.lineTo(24,T);g.stroke(); g.fillStyle=pal.fl;g.fillRect(3,3,8,3);g.fillRect(19,21,7,3); if(wi===3){g.fillStyle='rgba(120,230,255,.35)';g.fillRect(2,15,T-4,1.5)}}); ts.top=mk(g=>{g.drawImage(ts.fill,0,0,T,T); g.fillStyle=pal.top;g.fillRect(0,0,T,10);g.fillStyle=pal.topl;g.fillRect(0,0,T,3); g.strokeStyle=pal.mort;g.lineWidth=1;g.beginPath();g.moveTo(0,10.5);g.lineTo(T,10.5); if(wi===1){g.stroke();g.fillStyle='#4a9a3a';for(let i=0;i {g.fillStyle=pal.brick;g.fillRect(0,0,T,T); if(wi===1){g.fillStyle=pal.bl;for(let i=0;i<12;i++)g.fillRect((i*13)%28+1,(i*9)%28+1,3,3);g.strokeStyle=pal.bd;g.lineWidth=2;g.strokeRect(1,1,T-2,T-2)} else if(wi===2){g.fillStyle='#fff9e8';for(let i=0;i<5;i++){g.fillRect(4,5+i*5,T-8,1.6)}g.fillStyle='#222';g.fillRect(5,3,T-10,3);g.strokeStyle='#8a6a3a';g.lineWidth=1.5;g.beginPath();g.moveTo(0,T/2);g.lineTo(T,T/2);g.stroke();g.strokeStyle=pal.bd;g.lineWidth=2;g.strokeRect(1,1,T-2,T-2)} else if(wi===3){g.globalAlpha=.85;g.fillStyle=pal.bl;g.fillRect(2,2,T-4,6);g.globalAlpha=1;g.strokeStyle=pal.bd;g.lineWidth=2;g.strokeRect(1,1,T-2,T-2);g.beginPath();g.moveTo(1,16);g.lineTo(T-1,16);g.stroke()} else{g.strokeStyle=pal.bd;g.lineWidth=2;g.strokeRect(1,1,T-2,T-2);g.beginPath();g.moveTo(1,16);g.lineTo(T-1,16);g.moveTo(16,1);g.lineTo(16,16);g.moveTo(8,16);g.lineTo(8,T-1);g.moveTo(24,16);g.lineTo(24,T-1);g.stroke();g.fillStyle=pal.bl;g.fillRect(3,3,10,3)} if(wi===0||wi===3){g.fillStyle=pal.bl;g.fillRect(3,3,T-6,3)}}); ts.box=mk(g=>{g.fillStyle='#f4b72b';g.fillRect(0,0,T,T);g.fillStyle='#ffe07a';g.fillRect(2,2,T-4,4); g.strokeStyle='#7a4a0c';g.lineWidth=2;g.strokeRect(1,1,T-2,T-2); g.fillStyle='#7a4a0c';[[4,4],[T-6,4],[4,T-6],[T-6,T-6]].forEach(p=>g.fillRect(p[0],p[1],2,2)); g.fillStyle='#7a2a0c';g.font='bold 20px Georgia,serif';g.textAlign='center';g.textBaseline='middle';g.fillText('?',T/2,T/2+2); if(wi===3){g.strokeStyle='rgba(120,240,255,.9)';g.lineWidth=1.5;g.strokeRect(2.5,2.5,T-5,T-5)}}); ts.used=mk(g=>{g.fillStyle='#8b6a45';g.fillRect(0,0,T,T);g.strokeStyle='#4a3420';g.lineWidth=2;g.strokeRect(1,1,T-2,T-2); g.fillStyle='#a07c52';g.fillRect(3,3,T-6,3);g.fillStyle='#4a3420';[[4,4],[T-6,4],[4,T-6],[T-6,T-6]].forEach(p=>g.fillRect(p[0],p[1],2,2))}); ts.plank=mk(g=>{g.fillStyle=pal.plank;g.fillRect(0,0,T,9);g.fillStyle=pal.pl;g.fillRect(0,0,T,3); g.fillStyle='rgba(0,0,0,.35)';g.fillRect(0,9,T,1.5); g.strokeStyle=wi===2?'#222':'#2a1a0c';g.lineWidth=2;g.beginPath();g.moveTo(3,9);g.lineTo(3,16);g.lineTo(9,9);g.moveTo(T-3,9);g.lineTo(T-3,16);g.lineTo(T-9,9);g.stroke(); if(wi===0){g.fillStyle='#caa24a';g.fillRect(2,3,2,2);g.fillRect(T-4,3,2,2)} if(wi===3){g.fillStyle='rgba(130,240,255,.7)';g.fillRect(0,0,T,1.5)}}); ts.digi=mk(g=>{g.fillStyle='rgba(20,60,90,.85)';g.fillRect(0,0,T,T);g.strokeStyle='#5ef0ff';g.lineWidth=2;g.strokeRect(1,1,T-2,T-2); g.fillStyle='rgba(94,240,255,.25)';for(let y=2;y {c.fillStyle=col;c.fillRect(x,y,w,h)}; const poly=(c,col,p)=>{c.fillStyle=col;c.beginPath();c.moveTo(p[0],p[1]);for(let i=2;i {c.fillStyle=col;c.beginPath();c.arc(x,y,r,0,TAU);c.fill()}; function wins(c,x,y,w,h,col,frame,lit,rnd){ const cols=Math.max(1,Math.floor((w-8)/24)), rows=Math.max(1,Math.floor((h-8)/40)), gx=w/cols, gy=h/rows; for(let j=0;j v+=Math.sin(TAU*k*x/W+ph[i])/(i+1));c.lineTo(x,base-amp*(.5+.5*v/1.5))} c.lineTo(W,H);c.closePath();c.fill(); } function tree(c,x,base,s,col,trunk){R(c,trunk||'#5a3d22',x-2*s,base-14*s,4*s,14*s);circ(c,col,x,base-26*s,14*s);circ(c,col,x-9*s,base-18*s,10*s);circ(c,col,x+9*s,base-18*s,10*s)} const FW=1024,FH=240,MW=1200,MH=330; function paintFar(c,wi){ const ph=[wi*1.3,wi*.7+1,wi*2.1]; if(wi===0){ridge(c,'#b7c8e8',FW,FH,150,95,[2,5],ph);ridge(c,'#9db2da',FW,FH,190,55,[3,7],ph); for(const x of [160,520,830]){R(c,'#8aa0cc',x,150,12,90);poly(c,'#7a90bc',[x-3,150,x+6,118,x+15,150])}} else if(wi===1){ridge(c,'#a7dba3',FW,FH,140,85,[2,4],ph);ridge(c,'#80c47c',FW,FH,185,50,[3,6],ph); for(let i=0;i<22;i++)circ(c,'#5fa85a',20+i*47,200+(i%3)*8,9+(i%4)*2)} else if(wi===2){ridge(c,'#d8cbb4',FW,FH,150,60,[2,6],ph);ridge(c,'#c3b49a',FW,FH,195,40,[3,8],ph); for(const x of [120,400,720,930]){R(c,'#a89880',x,90,14,150);circ(c,'rgba(255,255,255,.35)',x+7,80,16);circ(c,'rgba(255,255,255,.25)',x+20,62,20)} for(const x of [250,610]){R(c,'#b0a088',x,130,26,110);poly(c,'#9a8a72',[x-3,130,x+13,95,x+29,130])}} else{ridge(c,'#5a3f86',FW,FH,135,90,[2,5],ph);ridge(c,'#3f2c6a',FW,FH,185,55,[3,7],ph); for(const x of [90,330,600,880]){R(c,'#47337a',x,100,18,140);poly(c,'#47337a',[x-4,100,x+9,56,x+22,100]);R(c,'#ffb35a',x+6,130,6,10)}} } function paintMid(c,wi,zone){ const W=MW,H=MH,r=mulberry(300+wi*10+zone); if(wi===0){ if(zone===0){ // PLAZA MAYOR R(c,'#c4673f',0,76,W,H-76);R(c,'#8c3f27',0,76,W,6);R(c,'#4b4f5c',0,52,W,26); for(let x=10;x 0)balc(c,x-6,y+28,26,'#17171f')}} R(c,'#a85332',0,H-66,W,66);archRow(c,0,H-64,W,64,Math.floor(W/62),'#3a1f18'); for(const tx of [170,830]){R(c,'#b85a38',tx,60,66,H-60);R(c,'#8c3f27',tx,60,66,5);poly(c,'#41454f',[tx-7,60,tx+33,6,tx+73,60]);circ(c,'#e5c35a',tx+33,6,3);R(c,'#2c2a3e',tx+26,84,14,24);R(c,'#2c2a3e',tx+26,130,14,24)} }else if(zone===1){ // CALLE MAYOR let x=0;const cs=['#e7c487','#d68d68','#c9a46a','#e3b79c','#b98a6a','#d9b36a']; while(x 480&&x<640){x=640;continue}const bw=100+(r()*40|0),bh=190+(r()*70|0); R(c,cs[(r()*4)|0],x,H-bh,bw,bh);poly(c,'#59616f',[x,H-bh,x+12,H-bh-26,x+bw-12,H-bh-26,x+bw,H-bh]); wins(c,x+4,H-bh+12,bw-8,bh-62,'#2a3040','#6a5a42',.25,r);for(let b=0;b<3;b++)balc(c,x+6,H-bh+50+b*48,bw-12,'#1c1c24'); R(c,'#3a2a20',x,H-40,bw,40);x+=bw} R(c,'#cdb48a',480,60,160,H-60);poly(c,'#59616f',[472,60,560,10,648,60]); circ(c,'#f6f0dc',560,110,32);c.strokeStyle='#222';c.lineWidth=2;c.beginPath();c.arc(560,110,32,0,TAU);c.moveTo(560,110);c.lineTo(560,88);c.moveTo(560,110);c.lineTo(574,116);c.stroke(); R(c,'#2a2a30',552,10,2,-8);circ(c,'#c0392b',560,4,5); R(c,'#3a2a20',520,H-60,80,60);R(c,'#ffd87a',536,H-48,48,40); for(let i=0;i<6;i++){const px=60+i*200;R(c,'#222',px,H-110,3,110);circ(c,'rgba(255,220,120,.4)',px+1,H-114,12);circ(c,'#ffe08a',px+1,H-114,5)} c.strokeStyle='#222';c.lineWidth=1;c.beginPath();c.moveTo(0,H-120);c.quadraticCurveTo(W/2,H-80,W,H-120);c.stroke(); }else if(zone===1){ // ESTACIONES R(c,'#d2c4a6',0,150,W,H-150); c.strokeStyle='#2c3340';c.lineWidth=7;c.beginPath();c.moveTo(60,H);c.lineTo(60,170);c.arc(W/2,170,W/2-60,Math.PI,0);c.lineTo(W-60,H);c.stroke(); c.lineWidth=2;c.strokeStyle='#3d4655';for(let i=0;i<11;i++){c.beginPath();c.moveTo(W/2,170);c.lineTo(W/2+Math.cos(Math.PI+i*Math.PI/10)*(W/2-60),170+Math.sin(Math.PI+i*Math.PI/10)*(W/2-60));c.stroke()} R(c,'rgba(190,225,240,.5)',60,170,W-120,H-170); R(c,'#2c3340',0,H-20,W,20);for(let x=0;x {const c=mkCanvas(MW,MH);paintMid(c.getContext('2d'),wi,z);return c}); const r=mulberry(77+wi), clouds=[], stars=[]; for(let i=0;i<7;i++)clouds.push({x:r()*1200,y:20+r()*110,s:.6+r()*.9,v:.1+r()*.2}); for(let i=0;i<60;i++)stars.push({x:r()*VW,y:r()*260,a:r()}); BG[wi]={far,mid,clouds,stars}; } function drawClouds(c,bg,t,cam,col){ c.fillStyle=col; bg.clouds.forEach(k=>{const x=((k.x+t*k.v*10-cam*.08)%1300+1300)%1300-150;c.beginPath();c.ellipse(x,k.y,50*k.s,16*k.s,0,0,TAU);c.ellipse(x+28*k.s,k.y-8*k.s,34*k.s,14*k.s,0,0,TAU);c.ellipse(x-28*k.s,k.y-4*k.s,30*k.s,12*k.s,0,0,TAU);c.fill()}); } function drawBackground(c,L,cam,t){ const wi=L.wi, bg=BG[wi]; if(!bg){R(c,'#222',0,0,VW,VH);return} const g=c.createLinearGradient(0,0,0,VH*.85);g.addColorStop(0,SKY[wi][0]);g.addColorStop(1,SKY[wi][1]);c.fillStyle=g;c.fillRect(0,0,VW,VH); if(wi===3){bg.stars.forEach(s=>{c.globalAlpha=.4+.6*Math.abs(Math.sin(t*1.5+s.a*9));R(c,'#fff',s.x,s.y,1.6,1.6)});c.globalAlpha=1; circ(c,'rgba(255,240,210,.25)',610,100,70);circ(c,'#fff1d6',610,100,48);circ(c,'rgba(200,170,150,.5)',596,92,9);circ(c,'rgba(200,170,150,.5)',626,112,6)} else if(wi===0){circ(c,'rgba(255,240,170,.35)',640,86,58);circ(c,'#fff3b0',640,86,34)} else if(wi===1){circ(c,'rgba(255,255,220,.4)',160,76,50);circ(c,'#fffbd0',160,76,30)} else{circ(c,'rgba(255,245,220,.5)',560,100,52);circ(c,'rgba(255,250,235,.7)',560,100,30)} drawClouds(c,bg,t,cam,wi===3?'rgba(120,80,160,.35)':'rgba(255,255,255,.75)'); const fo=-((cam*.16)%FW); c.drawImage(bg.far,fo,372-FH-34);c.drawImage(bg.far,fo+FW,372-FH-34); if(fo+FW*2 {c.globalAlpha=a;c.drawImage(img,mo,my);c.drawImage(img,mo+MW,my);c.globalAlpha=1}; dm(bg.mid[z],1); if(z<2&&f>.88)dm(bg.mid[z+1],(f-.88)/.12); if(wi===3){const gg=c.createLinearGradient(0,0,0,VH);gg.addColorStop(0,'rgba(40,20,90,0)');gg.addColorStop(1,'rgba(240,120,70,.18)');c.fillStyle=gg;c.fillRect(0,0,VW,VH)} } /* ---------- Sala secreta: biblioteca encantada ---------- */ let ROOMBG=null; function makeRoomBg(){ const c=mkCanvas(1200,VH),g=c.getContext('2d'),r=mulberry(9); const gr=g.createLinearGradient(0,0,0,VH);gr.addColorStop(0,'#1a1f45');gr.addColorStop(1,'#2d1f4a');g.fillStyle=gr;g.fillRect(0,0,1200,VH); for(let x=0;x<1200;x+=150){ R(g,'#3a2616',x+10,60,130,330);R(g,'#241408',x+10,60,130,8); for(let row=0;row<7;row++){const y=74+row*44;R(g,'#241408',x+14,y+38,122,4); let bx=x+16;while(bx Math.round(((pa>>s)&255)*(1-t)+((pb>>s)&255)*t);return `rgb(${m(16)},${m(8)},${m(0)})`} /* ---------- Decorado en primer plano del mundo (farolas, bancos…) ---------- */ function drawProp(c,p,wi,t){ const x=p.x,y=p.gy; switch(p.t){ case 'farol':{ const glow=wi===3?'rgba(120,240,255,.45)':'rgba(255,215,120,.45)',core=wi===3?'#aef6ff':'#ffe6a0'; R(c,wi===2?'#2a2a32':'#26262e',x-2,y-84,4,84);R(c,'#26262e',x-8,y-6,16,6); if(wi===1){circ(c,glow,x,y-92,13);circ(c,core,x,y-92,7);R(c,'#26262e',x-6,y-84,12,3)} else{R(c,'#26262e',x-8,y-96,16,3);poly(c,'#26262e',[x-8,y-96,x+8,y-96,x+4,y-104,x-4,y-104]);circ(c,glow,x,y-88,14);R(c,core,x-5,y-94,10,12);R(c,'#26262e',x-5,y-82,10,2)} break} case 'banco':R(c,'#6a4a2a',x-20,y-16,40,5);R(c,'#6a4a2a',x-20,y-30,40,4);R(c,'#3a2a1a',x-18,y-11,3,11);R(c,'#3a2a1a',x+15,y-11,3,11);R(c,'#3a2a1a',x-18,y-30,3,16);R(c,'#3a2a1a',x+15,y-30,3,16);break; case 'cajas':R(c,'#9a6a34',x-14,y-22,28,22);R(c,'#6a4420',x-14,y-22,28,3);R(c,'#6a4420',x-1,y-22,2,22);R(c,'#b07a3a',x-26,y-14,14,14);R(c,'#6a4420',x-26,y-14,14,2);R(c,'#a67238',x+10,y-36,16,14);R(c,'#6a4420',x+10,y-36,16,2);break; case 'vaultsign':R(c,'#26262e',x-2,y-120,3,34);R(c,'rgba(20,60,90,.9)',x-30,y-138,70,22);c.strokeStyle='#5ef0ff';c.lineWidth=1.5;c.strokeRect(x-30,y-138,70,22); c.fillStyle='#9bfaff';c.font='bold 11px monospace';c.textAlign='center';c.fillText('DIGITAL',x+5,y-123);break; case 'torre':R(c,'#6a4422',x-2,y-60,3,60);R(c,'#8a5a30',x-26,y-78,52,24);c.strokeStyle='#3a2210';c.lineWidth=2;c.strokeRect(x-26,y-78,52,24); c.fillStyle='#3a1d08';c.font='bold 18px Georgia,serif';c.textAlign='center';c.fillText('↑ ?',x,y-60);break; } } function drawDoor(c,d,t,lupa,near){ const a=d.hidden?(lupa||near?1:.32+.12*Math.sin(t*3)):1; c.globalAlpha=a;const x=d.x,y=d.y; R(c,'#3a2210',x-3,y-4,T+6,d.h+4); c.fillStyle='#8a5a30';c.beginPath();c.moveTo(x,y+d.h);c.lineTo(x,y+16);c.arc(x+T/2,y+16,T/2,Math.PI,0);c.lineTo(x+T,y+d.h);c.fill(); c.strokeStyle='#4a2c12';c.lineWidth=2;c.beginPath();c.moveTo(x+T/2,y+2);c.lineTo(x+T/2,y+d.h);c.stroke(); circ(c,'#ffd84a',x+T/2-6,y+d.h/2+4,2.5);circ(c,'#ffd84a',x+T/2+6,y+d.h/2+4,2.5); if(d.hidden||d.exit){c.globalAlpha=a*(.5+.5*Math.sin(t*4));circ(c,'rgba(255,230,120,.6)',x+T/2,y+d.h/2,14);c.globalAlpha=1} c.globalAlpha=1; } function drawGoal(c,g,t){ const x=g.x,y=g.y; R(c,'#c9b48a',x-14,y+10,92,g.h-10);R(c,'#8f7a58',x-14,y+10,92,6); poly(c,'#6a4a3a',[x-20,y+10,x+32,y-26,x+84,y+10]); c.fillStyle='#2a1408';c.beginPath();c.moveTo(x+8,y+g.h);c.lineTo(x+8,y+46);c.arc(x+32,y+46,24,Math.PI,0);c.lineTo(x+56,y+g.h);c.fill(); c.globalAlpha=.55+.25*Math.sin(t*3);const gr=c.createRadialGradient(x+32,y+60,2,x+32,y+60,34);gr.addColorStop(0,'#fff6c0');gr.addColorStop(1,'rgba(255,200,80,0)');c.fillStyle=gr;c.fillRect(x-4,y+20,72,g.h);c.globalAlpha=1; R(c,'#f6e9c8',x-8,y-2,80,16);c.fillStyle='#3a1d08';c.font='bold 11px Georgia,serif';c.textAlign='center';c.fillText('BIBLIOTECA',x+32,y+10); R(c,'#4a3a2a',x+80,y-40,2,60);poly(c,'#c0392b',[x+82,y-40,x+104,y-32,x+82,y-24]); } function drawCheckpoint(c,cp,t){ const x=cp.x,y=cp.gy;R(c,'#4a3a2a',x-2,y-70,4,70); circ(c,cp.on?'#3ad06a':'#c0392b',x,y-74,9);circ(c,'rgba(255,255,255,.5)',x-3,y-77,3); R(c,cp.on?'#2f9a52':'#8a2a20',x-1,y-62,22+Math.sin(t*5)*2,12);c.fillStyle='#fff';c.font='bold 8px Georgia,serif';c.textAlign='left';c.fillText('SELLO',x+2,y-53); } /* ===================================================================== 9. SPRITES VECTORIALES: Marqués de Vadillo, documentos, potenciadores, enemigos ===================================================================== */ function drawMarques(c,x,y,o){ c.save();c.translate(x,y);c.scale(o.dir*.72,.72); const t=o.t,st=o.st,walk=st==='walk',air=st==='jump'||st==='fall',win=st==='win',dead=st==='dead'; const bob=walk?Math.abs(Math.sin(o.ph))*2.5:(st==='idle'?Math.sin(t*3)*.8:(win?Math.abs(Math.sin(t*9))*5:0)); const sw=walk?Math.sin(o.ph):0, by=-bob; if(dead)c.rotate(Math.sin(t*14)*.3); const leg=(a,back)=>{c.save();c.translate(0,-20);c.rotate(a);R(c,'#e9dcc0',-3.6,0,7.2,8);R(c,back?'#d8d8e0':'#fbfbfb',-3,8,6,11); c.fillStyle=back?'#1c120d':'#2a1a14';c.beginPath();c.ellipse(2.5,19.5,6.5,3.6,0,0,TAU);c.fill();R(c,'#e5b52a',3,17.5,3,3);c.restore()}; const arm=(a,cane,back)=>{c.save();c.translate(back?-4:6,-37+by);c.rotate(a);R(c,back?'#234aa8':'#2c5fd0',-3,0,6,12);R(c,'#fff',-3.6,11,7.2,3); circ(c,'#f7c9a0',0,16.5,3.3); if(cane){c.strokeStyle='#6b3f1c';c.lineWidth=2.8;c.lineCap='round';c.beginPath();c.moveTo(0,16);c.lineTo(5,40);c.stroke();circ(c,'#e8b832',0,15.5,3.2)}c.restore()}; // pierna trasera, brazo trasero leg(walk?-sw*.85:(air?-.5:(win?-.25:0)),true); arm(win?2.5+Math.sin(t*10)*.3:(air?1.7:(walk?sw*.9:.2)),false,true); // cola de la casaca const fl=air?3:0, tail=Math.sin(o.ph+1)*(walk?4:1)+(air?-3:0); poly(c,'#234aa8',[-9,-30+by,-17+tail,-13+by,-5,-16+by]); // casaca azul poly(c,'#2c5fd0',[-8,-39+by,8,-39+by,12+fl,-17+by,-12-fl,-17+by]); c.strokeStyle='#e8b832';c.lineWidth=2.4;c.beginPath();c.moveTo(6,-39+by);c.lineTo(10+fl,-17+by);c.moveTo(-12-fl,-17.5+by);c.lineTo(12+fl,-17.5+by);c.stroke(); [[4,-35],[5,-29],[6,-23]].forEach(p=>circ(c,'#f5cd44',p[0],p[1]+by,1.7)); poly(c,'#fff',[2,-41+by,11,-41+by,10,-35+by,8.5,-31+by,6,-35+by,4,-31+by]); // chorrera // pierna delantera leg(walk?sw*.85:(air?.95:(win?.25:0)),false); // cabeza: peluca blanca const hy=-49+by; circ(c,'#fff',-13,hy+9,4.5);poly(c,'#111',[-17,hy+5,-12,hy+8,-17,hy+12]); circ(c,'#fdfdfd',-2,hy-3,12.5); [[-10,hy+1,5.4],[-12,hy+7,5],[-10,hy+13,4.4]].forEach(p=>{circ(c,'#fdfdfd',p[0],p[1],p[2]);c.strokeStyle='#cdd0e0';c.lineWidth=1;c.beginPath();c.arc(p[0],p[1],p[2]*.6,0,5);c.stroke()}); circ(c,'#f7c9a0',1,hy,10.8); // cara circ(c,'rgba(240,120,120,.5)',5,hy+4,3); circ(c,'#f0a888',11.5,hy+1.5,3.4); // nariz if(dead){c.strokeStyle='#222';c.lineWidth=1.5;c.beginPath();c.moveTo(3,hy-4);c.lineTo(8,hy+1);c.moveTo(8,hy-4);c.lineTo(3,hy+1);c.stroke()} else{c.fillStyle='#1a1a22';c.beginPath();c.ellipse(5.5,hy-2.5,1.8,2.5,0,0,TAU);c.fill();c.beginPath();c.ellipse(-.5,hy-2.5,1.6,2.3,0,0,TAU);c.fill(); R(c,'#fff',5,hy-4.5,1.2,1.2);R(c,'#fff',-1,hy-4.5,1.2,1.2)} c.strokeStyle='#7a2a1a';c.lineWidth=1.5;c.beginPath(); if(win||st==='jump'){c.fillStyle='#7a2a1a';c.ellipse(6,hy+5,3.4,2.6,0,0,TAU);c.fill()}else if(dead){c.arc(6,hy+6,2,0,TAU);c.stroke()}else{c.arc(6,hy+3,3.8,.2,Math.PI-.2);c.stroke()} c.fillStyle='#eee';c.beginPath();c.ellipse(8.5,hy+1.8,3,1.2,.2,0,TAU);c.fill();c.beginPath();c.ellipse(2,hy+2.2,2.4,1.1,-.2,0,TAU);c.fill(); // bigote // sombrero de tres picos c.save();if(win){c.translate(0,-8-Math.abs(Math.sin(t*9))*3);c.rotate(-.35+Math.sin(t*9)*.1)} c.fillStyle='#16161e';c.beginPath();c.moveTo(-17,hy-6);c.quadraticCurveTo(-2,hy-27,17,hy-6);c.lineTo(12,hy-3);c.quadraticCurveTo(0,hy-11,-13,hy-3);c.closePath();c.fill(); c.strokeStyle='#e8b832';c.lineWidth=2;c.beginPath();c.moveTo(-17,hy-6);c.quadraticCurveTo(-2,hy-27,17,hy-6);c.stroke(); circ(c,'#d63a2a',15,hy-7.5,2.4);c.restore(); // brazo delantero con bastón arm(win?-2.6-Math.sin(t*10)*.25:(air?-1.9:(walk?-sw*.9:-.25)),true,false); c.restore(); } /* ---------- Iconos de documentos (también usados en la colección) ---------- */ function iconDoc(c,cat){ switch(cat){ case 'plano':c.rotate(-.08);R(c,'#efdcae',-13,-10,26,20);c.strokeStyle='#b89a5a';c.lineWidth=1;c.beginPath();c.moveTo(-4,-10);c.lineTo(-4,10);c.moveTo(5,-10);c.lineTo(5,10);c.stroke(); c.strokeStyle='#3d7bbd';c.lineWidth=2;c.beginPath();c.moveTo(-13,4);c.quadraticCurveTo(-4,-6,4,3);c.quadraticCurveTo(10,8,13,-2);c.stroke(); c.strokeStyle='#b3342a';c.lineWidth=2;c.beginPath();c.moveTo(6,-8);c.lineTo(10,-4);c.moveTo(10,-8);c.lineTo(6,-4);c.stroke();c.strokeStyle='#7a5a2a';c.lineWidth=1.2;c.strokeRect(-13,-10,26,20);break; case 'foto':R(c,'#6a4422',-11,-13,22,26);const g=c.createLinearGradient(0,-10,0,10);g.addColorStop(0,'#e6cfa4');g.addColorStop(1,'#a8824e');c.fillStyle=g;c.fillRect(-8,-10,16,20); circ(c,'#fff2cf',3,-5,3);poly(c,'#5a3a1c',[-8,10,-3,0,2,6,5,2,8,10]);c.strokeStyle='#c79a2a';c.lineWidth=1;c.strokeRect(-11,-13,22,26);break; case 'grabado':R(c,'#c79a2a',-12,-14,24,28);R(c,'#f3ead2',-9,-11,18,22);c.strokeStyle='#222';c.lineWidth=1; for(let i=-8;i<9;i+=3){c.beginPath();c.moveTo(i,-4);c.lineTo(i+3,6);c.stroke()}c.strokeRect(-6,-8,12,14);poly(c,'#222',[-7,-8,0,-12,7,-8]);break; case 'manus':R(c,'#6a3a1c',-13,-9,26,18);R(c,'#f6ebcc',-12,-8,11.5,16);R(c,'#f6ebcc',.5,-8,11.5,16);c.strokeStyle='#6a5a3a';c.lineWidth=1; for(let i=0;i<4;i++){c.beginPath();c.moveTo(-10,-5+i*4);c.lineTo(-3,-5+i*4);c.moveTo(3,-5+i*4);c.lineTo(10,-5+i*4);c.stroke()} R(c,'#b3342a',-1,-9,2,12);circ(c,'#b3342a',7,6,3);break; case 'cartel':R(c,'#f3ead2',-10,-14,20,28);R(c,'#c0392b',-8,-12,16,24);R(c,'#fff',-6,-9,12,3);R(c,'#fff',-6,-3,12,2);R(c,'#fff',-6,1,9,2);circ(c,'#ffd84a',0,8,3);break; case 'prensa':R(c,'#efe6cc',-13,-10,26,20);R(c,'#222',-11,-8,22,5);c.strokeStyle='#666';c.lineWidth=1; for(let i=0;i<3;i++){for(let j=0;j<2;j++){c.beginPath();c.moveTo(-11+j*12,-1+i*3.4);c.lineTo(-2+j*12,-1+i*3.4);c.stroke()}}break; default:circ(c,'#f6ebcc',0,0,9); } } function iconPerg(c){ c.rotate(-.3);R(c,'#f3e2b0',-10,-5,20,10);c.fillStyle='#d9bd78';c.beginPath();c.ellipse(-10,0,3.2,5,0,0,TAU);c.fill();c.beginPath();c.ellipse(10,0,3.2,5,0,0,TAU);c.fill(); R(c,'#b8402e',-2,-5.6,4,11.2);c.strokeStyle='#8a6a3a';c.lineWidth=1;c.beginPath();c.moveTo(-8,-1);c.lineTo(-4,-1);c.moveTo(4,1);c.lineTo(8,1);c.stroke(); } function spark(c,x,y,s,a){c.globalAlpha=a;c.fillStyle='#fff8c0';c.beginPath();c.moveTo(x,y-s);c.lineTo(x+s*.25,y-s*.25);c.lineTo(x+s,y);c.lineTo(x+s*.25,y+s*.25);c.lineTo(x,y+s);c.lineTo(x-s*.25,y+s*.25);c.lineTo(x-s,y);c.lineTo(x-s*.25,y-s*.25);c.closePath();c.fill();c.globalAlpha=1} function drawItem(c,it,t,lupa){ if(it.taken) return; if(it.hidden&&!lupa){ // destello muy tenue: pista de que "algo" hay c.globalAlpha=.16+.12*Math.sin(t*4+it.ph);circ(c,'#fff6c0',it.x,it.y,3);c.globalAlpha=1;return} const bob=Math.sin(t*3+it.ph)*3, popy=it.pop>0?-it.pop*2.2:0; c.save();c.translate(it.x,it.y+bob+popy); if(it.hidden){c.globalAlpha=.9;circ(c,'rgba(255,240,150,.35)',0,0,20+Math.sin(t*6)*2)} switch(it.k){ case 'perg':iconPerg(c);break; case 'doc':{const d=DBI[it.id]; if(d.r>=3){const gl=c.createRadialGradient(0,0,2,0,0,d.r===4?30:22);gl.addColorStop(0,d.r===4?'rgba(255,230,120,.85)':'rgba(200,150,255,.6)');gl.addColorStop(1,'rgba(255,255,255,0)');c.fillStyle=gl;c.fillRect(-32,-32,64,64)} if(d.r===4){c.save();c.rotate(t);c.strokeStyle='rgba(255,230,120,.8)';c.lineWidth=2;c.beginPath();for(let i=0;i<6;i++){const a=i*Math.PI/3;c.lineTo(Math.cos(a)*19,Math.sin(a)*19)}c.closePath();c.stroke();c.restore();c.scale(1.25,1.25)} iconDoc(c,d.cat); if(d.r>=2)for(let i=0;i<2;i++)spark(c,Math.cos(t*3+i*3)*16,Math.sin(t*3+i*3)*16,3+d.r,.8);break} case 'lupa':{c.strokeStyle='#6b3f1c';c.lineWidth=4;c.lineCap='round';c.beginPath();c.moveTo(5,5);c.lineTo(13,13);c.stroke(); circ(c,'#c79a2a',-2,-2,11);circ(c,'rgba(170,225,255,.9)',-2,-2,8);c.strokeStyle='#fff';c.lineWidth=2;c.beginPath();c.arc(-2,-2,5,3.6,4.8);c.stroke();break} case 'sello':{R(c,'#6b3f1c',-3,-14,6,10);circ(c,'#8a5a2a',0,-14,4);circ(c,'#8a1f1f',0,3,11);circ(c,'#c0392b',0,3,9); c.strokeStyle='#ffd0c0';c.lineWidth=1.4;c.beginPath();c.arc(0,3,6,0,TAU);c.stroke();R(c,'#ffd0c0',-1,-1,2,8);R(c,'#ffd0c0',-4,3,8,2); spark(c,10,-6,4,.7+.3*Math.sin(t*6));break} case 'digi':{c.fillStyle='rgba(94,240,255,.25)';c.beginPath();c.arc(0,0,17+Math.sin(t*5)*2,0,TAU);c.fill(); c.fillStyle='#0b3a5a';c.strokeStyle='#5ef0ff';c.lineWidth=2;c.beginPath();for(let i=0;i<6;i++){const a=i*Math.PI/3+Math.PI/6;c.lineTo(Math.cos(a)*12,Math.sin(a)*12)}c.closePath();c.fill();c.stroke(); c.fillStyle='#9bfaff';c.font='bold 11px monospace';c.textAlign='center';c.fillText('01',0,4);break} case 'resta':{c.fillStyle='#e8344a';c.beginPath();c.moveTo(0,10);c.bezierCurveTo(-18,-2,-10,-16,0,-6);c.bezierCurveTo(10,-16,18,-2,0,10);c.fill(); c.fillStyle='rgba(255,255,255,.55)';c.beginPath();c.ellipse(-5,-6,3,2,-.6,0,TAU);c.fill();R(c,'#fff',-1,-4,2,8);R(c,'#fff',-4,-1,8,2);spark(c,11,-9,4,.8);break} } c.restore(); } function itemName(k){return{lupa:'LUPA HISTÓRICA',sello:'SELLO REAL',digi:'DIGITALIZACIÓN',resta:'RESTAURACIÓN'}[k]} /* ---------- Enemigos temáticos (no violentos) ---------- */ function drawEnemy(c,e,t){ const cx=e.x+e.w/2, cy=e.y+e.h/2; if(e.dead){ // "ARCHIVADO": se aplasta y se sella if(e.t==='barril'||e.t==='gota'){return} c.save();c.globalAlpha=clamp(e.dead/20,0,1);c.translate(cx,e.y+e.h);c.scale(1.15,.25);c.fillStyle=e.t==='tinta'?'#3a3a52':'#e8dcb8'; c.beginPath();c.ellipse(0,-e.h/2,e.w/2,e.h/2,0,0,TAU);c.fill();c.restore();return} c.save(); switch(e.t){ case 'polilla':{const f=Math.abs(Math.sin(t*16+e.ph));c.translate(cx,cy);c.scale(e.dir,1); c.fillStyle='#e4d4ac';c.beginPath();c.ellipse(-4,-5,14,4+f*10,-.35,0,TAU);c.fill();c.beginPath();c.ellipse(4,-5,12,4+f*9,.35,0,TAU);c.fill(); c.fillStyle='rgba(120,90,50,.7)';circ(c,'rgba(120,90,50,.6)',-8,-6-f*4,2.5);circ(c,'rgba(120,90,50,.6)',6,-8-f*4,2); c.fillStyle='#7a5a3a';c.beginPath();c.ellipse(-2,2,6.5,8,.2,0,TAU);c.fill();circ(c,'#8a6a46',7,0,5);circ(c,'#ff4a3a',9,-1,1.6); c.strokeStyle='#5a3a1a';c.lineWidth=1.3;c.beginPath();c.moveTo(9,-4);c.quadraticCurveTo(14,-12,18,-10);c.moveTo(6,-5);c.quadraticCurveTo(8,-13,13,-14);c.stroke(); R(c,'#fff6d0',-8,8,8,5);break} case 'tinta':{c.translate(cx,e.y+e.h);const w=Math.sin(t*5+e.ph)*1.5;c.fillStyle='#1d1b2e'; c.beginPath();c.moveTo(-e.w/2,0);c.bezierCurveTo(-e.w/2,-e.h-4+w,-6,-e.h-8,0,-e.h-3);c.bezierCurveTo(8,-e.h-9+w,e.w/2,-e.h,e.w/2,0);c.closePath();c.fill(); circ(c,'#1d1b2e',-e.w/2-3,-2,4);circ(c,'#1d1b2e',e.w/2+2,-1,3);circ(c,'#fff',-5,-e.h/2-1,4);circ(c,'#fff',6,-e.h/2-1,4);circ(c,'#111',-4+e.dir,-e.h/2-1,2);circ(c,'#111',7+e.dir,-e.h/2-1,2); c.fillStyle='rgba(255,255,255,.35)';c.beginPath();c.ellipse(-8,-e.h+1,5,2,-.3,0,TAU);c.fill();break} case 'expediente':{c.translate(cx,e.y+e.h);const wob=Math.sin(t*9+e.ph)*(e.vx?1:.3); c.strokeStyle='#3a2a1a';c.lineWidth=2.4;c.lineCap='round';const lg=Math.sin(t*12+e.ph)*4*(e.vx?1:0);c.beginPath();c.moveTo(-5,-6);c.lineTo(-5+lg,0);c.moveTo(5,-6);c.lineTo(5-lg,0);c.stroke(); for(let i=0;i<4;i++){c.save();c.translate(0,-18-i*3);c.rotate((i-1.5)*.1+wob*.03);R(c,i%2?'#f3e6c0':'#e8d9a8',-13,-8,26,12);c.strokeStyle='rgba(60,40,20,.5)';c.lineWidth=1;c.beginPath();c.moveTo(-9,-4);c.lineTo(8,-4);c.stroke();c.restore()} R(c,'#c0392b',-13,-24,26,3);circ(c,'#fff',-5,-27,3.4);circ(c,'#fff',5,-27,3.4);circ(c,'#111',-5+e.dir*1.2,-27,1.6);circ(c,'#111',5+e.dir*1.2,-27,1.6); for(let i=0;i<2;i++){R(c,'#fff6d6',9+i*3,-36+Math.sin(t*6+i)*3,6,4)}break} case 'error404':{c.translate(cx,e.y+e.h);const sq=e.vy?.8:1+Math.sin(t*8)*.04,glt=Math.sin(t*37+e.ph)>.82;c.scale(1/sq,sq); if(glt)c.translate((Math.random()-.5)*6,0); c.strokeStyle='#4af';c.lineWidth=2;c.beginPath();c.moveTo(0,-30);c.lineTo(0,-38);c.stroke();circ(c,'#f4a',0,-39,2.6); rr(c,-15,-30,30,26,5);c.fillStyle='#071a2c';c.fill();c.strokeStyle='#5ef0ff';c.lineWidth=2;c.stroke(); c.fillStyle='#5ef0ff';c.font='bold 13px monospace';c.textAlign='center';c.fillText('404',0,-12); c.fillStyle='#fff';c.fillRect(-9,-26,5,5);c.fillRect(4,-26,5,5);c.fillStyle='#071a2c';c.fillRect(-8+e.dir,-25,2,3);c.fillRect(5+e.dir,-25,2,3); c.strokeStyle='#5ef0ff';c.lineWidth=2;c.beginPath();c.moveTo(-7,-3);c.lineTo(-7,2);c.moveTo(7,-3);c.lineTo(7,2);c.stroke(); if(glt){c.fillStyle='rgba(255,60,200,.6)';c.fillRect(-15,-24,30,4);c.fillStyle='rgba(60,255,200,.6)';c.fillRect(-12,-14,30,3)}break} case 'carretilla':{c.translate(cx,e.y+e.h);c.fillStyle='#8a5a2c';c.beginPath();c.moveTo(-22,-22);c.lineTo(20,-22);c.lineTo(15,-8);c.lineTo(-17,-8);c.closePath();c.fill(); R(c,'#6a4420',-22,-24,42,3);R(c,'#a67238',-12,-34,16,12);R(c,'#6a4420',-12,-34,16,2); c.strokeStyle='#5a3a1a';c.lineWidth=3;c.beginPath();c.moveTo(-20,-16);c.lineTo(-34,-30);c.stroke(); c.save();c.translate(0,-8);c.rotate(e.x/7);circ(c,'#4a2e14',0,0,9);circ(c,'#c9a35a',0,0,6);c.strokeStyle='#4a2e14';c.lineWidth=1.5;for(let i=0;i<4;i++){c.rotate(Math.PI/4);c.beginPath();c.moveTo(-6,0);c.lineTo(6,0);c.stroke()}c.restore();break} case 'barril':{c.translate(cx,cy);c.rotate(e.x/10*(e.vx?1:0));rr(c,-12,-13,24,26,7);c.fillStyle='#9a6a34';c.fill();c.strokeStyle='#4a3014';c.lineWidth=2;c.stroke(); R(c,'#4a3014',-12,-8,24,3);R(c,'#4a3014',-12,5,24,3);R(c,'#c79a52',-5,-13,3,26);break} case 'barrilera':{c.translate(cx,e.y+e.h);const bar=(x,y)=>{rr(c,x-11,y-22,22,22,6);c.fillStyle='#9a6a34';c.fill();c.strokeStyle='#4a3014';c.lineWidth=1.8;c.stroke();R(c,'#4a3014',x-11,y-15,22,2.5);R(c,'#4a3014',x-11,y-8,22,2.5)}; bar(-12,0);bar(12,0);bar(0,-22);if(e.act&&Math.sin(t*10)>0){c.fillStyle='rgba(255,255,255,.5)';c.fillRect(-18,-30,3,3)}break} case 'gato':{c.translate(cx,e.y+e.h);c.scale(e.dir,1);const lp=e.vy?1:0,tl=Math.sin(t*6+e.ph)*.5; c.fillStyle='#22222a';c.beginPath();c.ellipse(-2,-10-lp*2,12,8,lp?-.3:0,0,TAU);c.fill();circ(c,'#22222a',10,-15-lp*2,6.5); poly(c,'#22222a',[6,-19,8,-27,11,-20]);poly(c,'#22222a',[11,-20,14,-27,15,-18]); c.strokeStyle='#22222a';c.lineWidth=3.2;c.lineCap='round';c.beginPath();c.moveTo(-12,-12);c.quadraticCurveTo(-22,-20+tl*8,-18,-30+tl*6);c.stroke(); c.beginPath();c.moveTo(-6,-4);c.lineTo(-6,0);c.moveTo(4,-4);c.lineTo(4,0);c.stroke(); circ(c,'#ffd84a',12,-16-lp*2,1.9);circ(c,'#111',12.3,-16-lp*2,.9);R(c,'#f0a0a0',15,-14,1.5,1.5);break} case 'fuente':{c.translate(cx,e.y+e.h);rr(c,-17,-12,34,12,4);c.fillStyle='#cfd6dc';c.fill();R(c,'#aab4bc',-17,-12,34,3);R(c,'#4aa8e0',-14,-14,28,3);R(c,'#aab4bc',-3,-22,6,10); if(e.jet>0){const h=e.jet*108;const g=c.createLinearGradient(0,-h-14,0,-14);g.addColorStop(0,'rgba(200,240,255,.95)');g.addColorStop(1,'rgba(80,170,255,.9)');c.fillStyle=g;c.fillRect(-6,-h-14,12,h);circ(c,'rgba(220,245,255,.9)',0,-h-14,10)} else if(e.warn){for(let i=0;i<5;i++)circ(c,'rgba(200,240,255,.8)',Math.sin(t*9+i*1.7)*10,-16-((t*40+i*13)%26),2.4)}break} case 'estatua':{c.translate(cx,e.y+e.h);c.scale(e.dir,1);const sway=Math.sin(t*3+e.ph)*(e.vx?3:.5); R(c,'#aeb4ba',-14,-10,28,10);R(c,'#c9ced3',-14,-10,28,3); c.save();c.translate(0,-10);c.rotate(sway*.02);c.fillStyle='#d6dade';c.beginPath();c.moveTo(-9,0);c.lineTo(-7,-26);c.lineTo(7,-26);c.lineTo(9,0);c.closePath();c.fill(); c.strokeStyle='#d6dade';c.lineWidth=5;c.lineCap='round';c.beginPath();c.moveTo(6,-24);c.lineTo(15,-34+sway);c.stroke(); circ(c,'#d6dade',0,-32,6.5);poly(c,'#b4bac0',[-9,-36,0,-44,9,-36]);circ(c,'#8a9096',3,-32,1.1);c.restore();break} case 'tranvia':{c.translate(cx,e.y+e.h);c.scale(e.dir,1);R(c,'#b3342a',-54,-36,108,28);R(c,'#f0e2c0',-54,-42,108,8);R(c,'#7a1f18',-54,-12,108,4); for(let i=0;i<5;i++)R(c,'#ffe9a0',-48+i*20,-32,14,14);circ(c,'#ffe9a0',54,-20,5); c.strokeStyle='#222';c.lineWidth=2;c.beginPath();c.moveTo(-10,-42);c.lineTo(-18,-54);c.lineTo(8,-54);c.stroke(); circ(c,'#222',-34,-6,7);circ(c,'#222',34,-6,7);circ(c,'#777',-34,-6,3);circ(c,'#777',34,-6,3); if(Math.sin(t*20)>.6){circ(c,'rgba(255,255,160,.9)',-13,-55,3)}break} case 'humo':{c.translate(cx,cy);const p=Math.sin(t*3+e.ph);c.fillStyle='rgba(95,95,110,.92)'; circ(c,'rgba(95,95,110,.92)',-10,3,13);circ(c,'rgba(115,115,130,.92)',2,-4+p*2,15);circ(c,'rgba(100,100,116,.92)',14,4,12);circ(c,'rgba(125,125,140,.9)',-2,5,12); circ(c,'#ffb040',-2,-3,2.6);circ(c,'#ffb040',8,-3,2.6);break} case 'gota':{c.translate(cx,cy);c.fillStyle='#1d1b2e';c.beginPath();c.moveTo(0,-10);c.quadraticCurveTo(8,2,0,10);c.quadraticCurveTo(-8,2,0,-10);c.fill();R(c,'rgba(255,255,255,.5)',-2,-3,2,4);break} case 'aviso':{c.translate(cx,cy);c.globalAlpha=.6+.4*Math.sin(t*20);c.fillStyle='#ff5a4a';c.font='bold 18px Georgia,serif';c.textAlign='center';c.fillText('!',0,6);break} case 'pendulo':{ c.strokeStyle='#4a3a2a';c.lineWidth=2.4;c.beginPath();c.moveTo(e.ax,e.ay);c.lineTo(e.px,e.py);c.stroke(); R(c,'#5a4632',e.ax-18,e.ay-16,36,18);circ(c,'#f4ecd0',e.ax,e.ay-7,6.5);c.strokeStyle='#222';c.lineWidth=1.2;c.beginPath();c.moveTo(e.ax,e.ay-7);c.lineTo(e.ax,e.ay-12);c.moveTo(e.ax,e.ay-7);c.lineTo(e.ax+4,e.ay-6);c.stroke(); circ(c,'#9a7a2a',e.px,e.py,14);circ(c,'#e5c35a',e.px,e.py,11);circ(c,'#fff2a8',e.px-3,e.py-3,3.5);break} } c.restore(); } /* ---------- EL OLVIDO: nube oscura de documentos perdidos ---------- */ function drawOlvido(c,t,mem,cx,cy,dying){ const k=1-mem*.45, rad=150*k; c.save();c.translate(cx,cy+Math.sin(t*1.2)*6); const dark=mix3('#0a0314','#7a5ab0',mem), mid=mix3('#1d0e35','#b89ae0',mem); const gr=c.createRadialGradient(0,0,10,0,0,rad*1.5);gr.addColorStop(0,mid);gr.addColorStop(.55,dark);gr.addColorStop(1,'rgba(10,3,20,0)'); c.fillStyle=gr;c.globalAlpha=dying?Math.max(0,1-dying):1;c.beginPath();c.arc(0,0,rad*1.5,0,TAU);c.fill(); for(let i=0;i<9;i++){const a=t*.35+i*TAU/9;circ(c,dark,Math.cos(a)*rad*.7,Math.sin(a*1.3)*rad*.35,rad*.42+Math.sin(t+i)*8)} const r=mulberry(11); for(let i=0;i<70;i++){const a=t*(.3+r()*.5)+r()*TAU,d=rad*(.3+r()*1.1)*(dying?1+dying*2:1);c.save();c.translate(Math.cos(a)*d,Math.sin(a)*d*.55);c.rotate(a*2+i); c.fillStyle=i%5===0?`rgba(255,226,150,${.5+mem*.4})`:`rgba(230,220,200,${.25+mem*.5})`;c.fillRect(-9,-6,18,12);c.fillStyle='rgba(60,40,20,.4)';c.fillRect(-6,-3,12,1.4);c.fillRect(-6,0,9,1.4);c.restore()} const eyeA=(1-mem)*.9+.1;c.globalAlpha=(dying?Math.max(0,1-dying):1)*eyeA; for(const sx of [-1,1]){c.fillStyle='#fff4c0';c.beginPath();c.ellipse(sx*34,-8,14,8+Math.sin(t*2)*2,sx*.25,0,TAU);c.fill();c.fillStyle='#2a0a3a';c.beginPath();c.arc(sx*34,-8,5,0,TAU);c.fill()} c.fillStyle='rgba(5,0,10,.9)';c.beginPath();c.ellipse(0,34,30-mem*10,10+Math.sin(t*3)*3,0,0,TAU);c.fill(); c.restore(); } /* ---------- Biblioteca Digital restaurada (pantalla final) ---------- */ function drawFinalScene(c,t){ const g=c.createLinearGradient(0,0,0,VH);g.addColorStop(0,'#1a2a6a');g.addColorStop(.6,'#5a7ac0');g.addColorStop(1,'#f6d89a');c.fillStyle=g;c.fillRect(0,0,VW,VH); // rayos de luz c.globalAlpha=.12;for(let i=0;i<7;i++){c.fillStyle='#fff6c0';c.beginPath();c.moveTo(400,-20);c.lineTo(100+i*100-40,VH);c.lineTo(100+i*100+40,VH);c.closePath();c.fill()}c.globalAlpha=1; // nave principal con arcos y estanterías R(c,'#3a2a4a',0,60,VW,VH-60); for(let i=0;i<9;i++){const x=i*100;R(c,'#5a4a6a',x,60,12,VH-120); c.fillStyle='#2a1e3c';c.beginPath();c.moveTo(x+12,VH-60);c.lineTo(x+12,150);c.arc(x+56,150,44,Math.PI,0);c.lineTo(x+100,VH-60);c.fill(); for(let row=0;row<6;row++){const y=118+row*32;if(y>VH-70)break;let bx=x+16;while(bx maxw&&cur){lines.push(cur);cur=x}else cur=t} if(cur)lines.push(cur);return lines; } const ovl=(a,b)=>a.x b.x&&a.y b.y; const iconOf=k=>({k,x:0,y:0,ph:0,taken:false,hidden:false,pop:0}); const G={ L:null,main:null,kind:'world',wi:0,mode:'play',p:null,cam:0,fr:0,run:null,snap:null, lupa:0,sello:0,digi:0,hp:2,dOn:false, parts:[],floats:[],popQ:[],pop:null,banner:null,zone:-1,flash:0,shake:0,bumps:[],doorCd:0, lv:null,ip:null,winT:0,mi:null,boss:null, /* ---------- inicio de nivel ---------- */ begin(kind,wi,run){ this.run=run; this.snap=Object.assign({},run); this.kind=kind; this.wi=wi; let L; if(kind==='world'){L=genWorld(wi); if(!BG[wi]) makeBg(wi)} else if(kind==='mini'){L=genArena('mini'); L.ts=3} else {L=genArena('boss'); L.ts=3} this.main=null; this.L=L; this.mode='play'; this.fr=0; this.lupa=this.sello=this.digi=0; this.hp=2; this.parts=[]; this.floats=[]; this.popQ=[]; this.pop=null; this.zone=-1; this.flash=0; this.shake=0; this.bumps=[]; this.winT=0; this.doorCd=0; this.lv={docs:[],pergs:0,start:run.time,score0:run.score,bonus:0}; this.spawnPlayer(L.spawn.x,L.spawn.gy); this.cam=this.camTarget(); this.mi=null; this.boss=null; if(kind==='mini') this.mi={time:1800,items:[],streak:0,streakT:0,mult:1,caught:0,dbl:0,spawnT:0,inkFlash:0,best:0}; if(kind==='boss') this.boss={phase:0,got:0,need:6,mem:0,dying:0,inkT:60,mothT:200,bugT:300,items:[],cats:['plano','manus','foto'],done:false}; const nm=kind==='world'?WORLDS[wi].name:kind==='mini'?'Digitalización masiva':'El Olvido'; const sub=kind==='world'?WORLDS[wi].note:kind==='mini'?'¡Atrapa documentos antes de que acabe el tiempo!':'Recupera los documentos para devolver la memoria'; this.banner={t:0,a:nm,b:sub}; Music.play(kind==='world'?WORLDS[wi].mus:kind==='mini'?'mini':'boss'); }, restart(){const r=Object.assign({},this.snap);r.lives=3;this.begin(this.kind,this.wi,r)}, spawnPlayer(x,gy){ this.p={x:x-11,y:gy-44,w:22,h:44,vx:0,vy:0,dir:1,ground:false,coy:0,jb:0,air2:false,inv:0,ph:0,st:'idle',dead:0,pb:gy,stuck:false,nocut:false,wasG:false}; }, camTarget(){const p=this.p,L=this.L;return clamp(p.x+p.w/2-VW/2+p.dir*50,0,Math.max(0,L.w*T-VW))}, /* ---------- utilidades ---------- */ addScore(n,x,y,col){this.run.score=Math.max(0,this.run.score+n); if(x!=null)this.floats.push({x,y,s:(n>0?'+':'')+n,l:55,c:col||'#fff3b0'})}, burst(x,y,n,cols,sp,g){ for(let i=0;i =0;i--){const q=this.parts[i];q.x+=q.vx;q.y+=q.vy;q.vy+=q.g;q.l--;if(q.l<=0)this.parts.splice(i,1)} for(let i=this.floats.length-1;i>=0;i--){const f=this.floats[i];f.y-=.7;f.l--;if(f.l<=0)this.floats.splice(i,1)} for(let i=this.bumps.length-1;i>=0;i--){this.bumps[i].t--;if(this.bumps[i].t<=0)this.bumps.splice(i,1)} if(!this.pop&&this.popQ.length) this.pop={...this.popQ.shift(),t:0}; if(this.pop&&++this.pop.t>180) this.pop=null; if(this.banner&&++this.banner.t>200) this.banner=null; if(this.flash>0)this.flash--; if(this.shake>0)this.shake--; if(this.doorCd>0)this.doorCd--; clearEdges(); }, stepWorld(ip){ const p=this.p,L=this.L; if(this.mode==='win'){ this.dOn=false; p.vx*=.9; p.vy=Math.min(p.vy+GRAV,12); this.moveX(p.vx); this.moveY(p.vy); p.st=p.ground?'win':'fall'; p.ph+=.1; this.camFollow(); return } if(this.lupa>0)this.lupa--; if(this.sello>0)this.sello--; if(this.digi>0)this.digi--; this.dOn=this.digi>0||p.stuck; p.stuck=this.digi<=0&&this.ovl6(); this.dOn=this.digi>0||p.stuck; this.updatePlayer(ip); for(const it of L.items){ if(it.taken)continue; if(it.pop>0)it.pop--; if(it.ty!=null){it.y+=(it.ty-it.y)*.3;if(Math.abs(it.ty-it.y)<.5){it.y=it.ty;it.ty=null}} } if(!p.dead) this.checkItems(); if(this.kind==='world'||this.kind==='room'||L.kind==='world'||L.kind==='room'){ this.updateEnemies(); this.checkMisc(); } if(this.mi) this.updateMini(); if(this.boss) this.updateBoss(); this.camFollow(); }, camFollow(){const t=this.camTarget();this.cam+=(t-this.cam)*.12; if(Math.abs(t-this.cam)<.3)this.cam=t}, /* ---------- jugador ---------- */ updatePlayer(ip){ const p=this.p,L=this.L; if(p.dead){p.dead--;p.vy=Math.min(p.vy+GRAV*.8,12);p.y+=p.vy;p.st='dead';p.ph+=.2;if(p.dead<=0)this.afterDeath();return} p.pb=p.y+p.h; if(p.inv>0)p.inv--; // puertas if(this.doorCd<=0&&(ip.up||ip.jp)){ for(const d of L.doors){ if(ovl(p,{x:d.x-4,y:d.y,w:d.w+8,h:d.h})){this.enterDoor(d);return} } } const max=ip.run?RUN:WALK, acc=p.ground?(ip.run?.42:.34):.24, dir=(ip.r?1:0)-(ip.l?1:0); if(dir){ p.dir=dir; p.vx+=dir*acc; if(Math.sign(p.vx)!==dir&&p.ground)p.vx+=dir*acc*1.4 } else p.vx*=p.ground?.8:.97; if(Math.abs(p.vx)<.05)p.vx=0; if(Math.abs(p.vx)>max)p.vx=Math.sign(p.vx)*Math.max(max,Math.abs(p.vx)-.35); // salto: coyote, buffer, altura variable, doble salto opcional if(p.ground){p.coy=6;p.air2=false}else if(p.coy>0)p.coy--; if(ip.jp)p.jb=6;else if(p.jb>0)p.jb--; if(p.jb>0&&p.coy>0){p.vy=JV;p.jb=0;p.coy=0;p.ground=false;p.nocut=false;sfx('jump');this.burst(p.x+p.w/2,p.y+p.h,5,['#e8dcc0','#c9b48a'],1.4,.05)} else if(ip.jp&&!p.ground&&p.coy<=0&&save.dbl&&!p.air2){p.vy=-10.4;p.air2=true;p.jb=0;p.nocut=false;sfx('jump2');this.burst(p.x+p.w/2,p.y+p.h,8,['#fff','#cdb8ff','#ffe89a'],2,.02)} if(!ip.jh&&p.vy<-4.5&&!p.nocut)p.vy=-4.5; if(p.vy>0)p.nocut=false; p.vy=Math.min(p.vy+GRAV,12); this.moveX(p.vx); this.moveY(p.vy); if(p.ground&&!p.wasG&&p.vy>=0)this.burst(p.x+p.w/2,p.y+p.h,3,['#e8dcc0'],1,.03); p.wasG=p.ground; if(p.y>ROWS*T+40&&!p.dead) this.die(); // animación if(!p.ground)p.st=p.vy<0?'jump':'fall'; else if(Math.abs(p.vx)>.3){p.st='walk';p.ph+=Math.abs(p.vx)*.11} else p.st='idle'; }, moveX(dx){ const p=this.p,L=this.L; if(!dx)return; p.x+=dx; const x0=Math.floor(p.x/T),x1=Math.floor((p.x+p.w-.01)/T),y0=Math.floor(p.y/T),y1=Math.floor((p.y+p.h-.01)/T); for(let ty=y0;ty<=y1;ty++)for(let tx=x0;tx<=x1;tx++){ if(this.solid(tileAt(L,tx,ty))){ p.x=dx>0?tx*T-p.w:(tx+1)*T; p.vx=0; return } } }, moveY(dy){ const p=this.p,L=this.L,pb=p.y+p.h; p.y+=dy; p.ground=false; const x0=Math.floor(p.x/T),x1=Math.floor((p.x+p.w-.01)/T); if(dy>=0){ const bot=p.y+p.h,ty=Math.floor(bot/T); for(let tx=x0;tx<=x1;tx++){ const t=tileAt(L,tx,ty); if(this.solid(t)||(t===5&&pb<=ty*T+.5)){p.y=ty*T-p.h;p.vy=0;p.ground=true;return} } }else{ const ty=Math.floor(p.y/T); let hit=-1,best=1e9; for(let tx=x0;tx<=x1;tx++){ if(this.solid(tileAt(L,tx,ty))){const d=Math.abs(tx*T+T/2-(p.x+p.w/2)); if(d =0){ p.y=(ty+1)*T;p.vy=0; this.bumpTile(hit,ty) } } }, bumpTile(tx,ty){ const L=this.L,t=tileAt(L,tx,ty); this.bumps.push({tx,ty,t:8}); if(t===3){ const k=ty*L.w+tx,c=L.boxes[k]; setT(L,tx,ty,4); sfx('box'); const cx=tx*T+T/2; if(c==='docs'){ for(let i=0;i<3;i++){this.addScore(10,cx+(i-1)*16,ty*T-8-i*8);this.lv.pergs++;this.run.pergs++} sfx('doc'); this.burst(cx,ty*T,12,['#ffe89a','#fff','#f4b72b'],3,.2) } else { const it=addItem(L,c,cx,ty*T+16,{}); it.ty=ty*T-22; this.burst(cx,ty*T,8,['#ffe89a','#fff'],2.4,.15) } } else if(t===2||t===4){ sfx('tick'); this.burst(tx*T+T/2,ty*T+T,4,['#cfa564','#8d6a38'],1.6,.2) } }, die(){ const p=this.p; if(p.dead)return; p.dead=75;p.vy=-9;p.vx=0;this.run.lives--;this.lupa=this.sello=this.digi=0;sfx('die');this.shake=10; }, afterDeath(){ if(this.run.lives<=0){this.mode='over';Music.stop();sfx('over');onGameOver();return} if(this.main){const d=this.main.door;this.L=this.main.L;this.main=null} const L=this.L; let x=L.spawn.x,gy=L.spawn.gy; if(L.cp&&L.cp.on){x=L.cp.x;gy=L.cp.gy} this.spawnPlayer(x,gy); this.p.inv=110; this.hp=2; this.cam=this.camTarget(); this.flash=18; this.restoreMusic(); }, restoreMusic(){const k=this.L.kind; Music.play(k==='world'?WORLDS[this.L.wi].mus:k==='room'?'menu':k==='mini'?'mini':'boss')}, hurt(from){ const p=this.p; if(p.inv>0||p.dead||this.sello>0)return; sfx('hit'); this.hp--; p.inv=120; this.shake=8; const pc=p.x+p.w/2; p.vx=(pc>from?1:-1)*3.2; p.vy=-6; p.ground=false; if(this.hp<=0)this.die(); }, enterDoor(d){ const p=this.p; sfx('door'); this.flash=14; this.doorCd=40; if(d.room!=null&&this.L.rooms[d.room]){ this.main={L:this.L,door:d}; this.L=this.L.rooms[d.room]; this.spawnPlayer(this.L.spawn.x,this.L.spawn.gy); this.cam=this.camTarget(); this.banner={t:0,a:'Sala Memoria de Madrid',b:'Un tesoro documental, a salvo del olvido'}; Music.play('menu'); this.zone=-1; }else if(d.exit&&this.main){ const m=this.main; this.L=m.L; this.main=null; p.x=m.door.x+T/2-p.w/2; p.y=m.door.y+m.door.h-p.h; p.vx=p.vy=0; this.cam=this.camTarget(); this.restoreMusic(); } }, /* ---------- objetos ---------- */ checkItems(){ const p=this.p,px=p.x+p.w/2,py=p.y+p.h/2; for(const it of this.L.items){ if(it.taken)continue; if(it.hidden&&this.lupa<=0)continue; const dx=it.x-px,dy=it.y-py; if(dx*dx+dy*dy<(it.k==='doc'?30:24)*(it.k==='doc'?30:24)) this.pickup(it); } }, pickup(it){ it.taken=true; const x=it.x,y=it.y; switch(it.k){ case 'perg':this.addScore(10,x,y);this.lv.pergs++;this.run.pergs++;sfx('doc');this.burst(x,y,5,['#ffe89a','#fff'],2,.05);break; case 'doc':{ const d=DBI[it.id],isNew=!save.got[d.id]; this.addScore(RAR[d.r].p,x,y,RCOL[d.r]); save.got[d.id]=true; persist(); if(!this.lv.docs.includes(d.id))this.lv.docs.push(d.id); sfx(d.r>=4?'legend':'rare'); this.popQ.push({d,isNew}); this.burst(x,y,d.r*8,['#ffe89a','#fff',RCOL[d.r]],3.5,.1); if(d.r>=4)this.flash=10; break} case 'lupa':this.lupa=600;sfx('power');this.floats.push({x,y,s:'LUPA HISTÓRICA',l:70,c:'#ffe89a'});break; case 'sello':this.sello=480;sfx('power');this.floats.push({x,y,s:'SELLO REAL',l:70,c:'#ff9a8a'});break; case 'digi':this.digi=360;sfx('power');this.floats.push({x,y,s:'DIGITALIZACIÓN',l:70,c:'#7ff6ff'});break; case 'resta':this.run.lives=Math.min(9,this.run.lives+1);this.hp=2;sfx('life');this.floats.push({x,y,s:'+1 VIDA',l:70,c:'#ff8aa0'});this.burst(x,y,14,['#ff6a8a','#fff'],3,.05);break; } }, checkMisc(){ const p=this.p,L=this.L,pc=p.x+p.w/2; if(L.kind==='world'){ const z=zoneOf(L,pc); if(z!==this.zone){this.zone=z; if(this.fr>30)this.banner={t:0,a:L.zones[z],b:WORLDS[L.wi].name}; if(this.fr<=30)this.zone=z} if(L.cp&&!L.cp.on&&Math.abs(pc-L.cp.x)<26&&p.y+p.h>L.cp.gy-80&&p.y e.b1){e.x=e.b1-e.w;e.dir=-1}}, updateEnemies(){ const L=this.L,p=this.p; if(p.dead&&false)return; for(let i=L.enemies.length-1;i>=0;i--){ const e=L.enemies[i]; if(e.rm){L.enemies.splice(i,1);continue} if(e.dead){e.dead--;if(e.dead<=0)e.rm=true;continue} if(!e.fly&&e.t!=='gota'&&Math.abs(e.x+e.w/2-this.cam-VW/2)>780)continue; this.stepEnemy(e,p); if(!p.dead&&this.mode==='play')this.touchEnemy(e,p); } }, stepEnemy(e,p){ const t=this.fr/60,pc=p.x+p.w/2,ec=e.x+e.w/2; switch(e.t){ case 'polilla': if(e.fly){e.x+=e.vx;e.y=e.by+Math.sin(t*3+e.ph)*26;e.dir=e.vx>0?1:-1;if(e.x<-90||e.x>VW+90)e.rm=true;break} this.patrol(e,1.1);e.y=e.by+Math.sin(t*2+e.ph)*20;break; case 'tinta':this.patrol(e,.6);break; case 'expediente':{ const d=pc-ec; if(Math.abs(d)<170&&Math.abs((p.y+p.h)-(e.y+e.h))<50){e.dir=d>0?1:-1;this.patrol(e,1.7)}else this.patrol(e,.7);break} case 'error404': if(e.fly){e.x+=e.vx;e.dir=e.vx>0?1:-1;if(e.x<-90||e.x>VW+90)e.rm=true;break} this.patrol(e,.9);e.timer++; if(e.timer>110&&e.y>=e.gy-e.h-.1){e.vy=-8.5;e.timer=0;if(Math.abs(pc-ec)<260){e.dir=pc>ec?1:-1;sfx('glitch')}} e.vy+=.5;e.y+=e.vy;if(e.y>=e.gy-e.h){e.y=e.gy-e.h;e.vy=0}break; case 'carretilla':this.patrol(e,2.2);break; case 'barrilera': e.timer++;e.act=e.timer%170>135; if(e.timer%170===0){const m=mkEnemy('barril',e.x-6,e.gy,e.b0,e.b1);m.vx=-2.2;m.dir=-1;L_push(this.L,m)}break; case 'barril':e.x+=e.vx;if(e.x+e.w 70&&e.y>=e.gy-e.h-.1){e.vy=-8;e.timer=0;e.dir=pc>ec?1:-1} e.vy+=.5;e.y+=e.vy;if(e.y>=e.gy-e.h){e.y=e.gy-e.h;e.vy=0}break; case 'fuente':{ e.timer=(e.timer+1)%260;const q=e.timer;e.warn=q>110&&q<170; e.jet=(q>=170&&q<235)?Math.min(1,(q-170)/8,(235-q)/8):0;break} case 'estatua':{ const looking=(p.dir*(ec-pc)>0)&&Math.abs(ec-pc)<420; if(!looking)this.patrol(e,1.1);else e.vx=0;break} case 'pendulo':{ if(e.ax==null){e.ax=e.home;e.ay=e.gy-175} const a=Math.sin(this.fr/60*1.7+e.ph)*.95,Ln=130; e.px=e.ax+Math.sin(a)*Ln;e.py=e.ay+Math.cos(a)*Ln;e.x=e.px-e.w/2;e.y=e.py-e.h/2;break} case 'tranvia':this.patrol(e,2);break; case 'humo':e.x+=e.dir*.6;if(e.x e.b1){e.x=e.b1-e.w;e.dir=-1}e.y=e.by+Math.sin(t*1.5+e.ph)*10;break; case 'gota': e.vy=Math.min(e.vy+.07,4.6);e.y+=e.vy; if(e.y+e.h>=12*T-2){e.rm=true;sfx('splash');this.burst(e.x+e.w/2,12*T,6,['#1d1b2e','#3a3a52'],2.4,.2)}break; } }, touchEnemy(e,p){ let hx=e.x,hy=e.y,hw=e.w,hh=e.h; if(e.t==='fuente'){ if(e.jet<.3)return; hw=14;hx=e.x+e.w/2-7;hh=e.jet*108+14;hy=e.gy-hh } else if(e.t==='pendulo'){hx+=3;hy+=3;hw-=6;hh-=6} else {hx+=4;hy+=4;hw-=8;hh-=6} if(!ovl(p,{x:hx,y:hy,w:hw,h:hh}))return; const mode=STOMP[e.t], stomp=p.vy>.3&&p.pb<=e.y+14; if(mode&&stomp){ p.vy=this.ip&&this.ip.jh?-11:-8; p.nocut=true; p.ground=false; p.air2=false; if(mode==='kill')this.archive(e); else {sfx('stomp');this.burst(p.x+11,p.y+p.h,5,['#fff','#ffe89a'],2,.05)} return; } if(this.sello>0){ if(mode==='kill'||e.t==='barril'||e.t==='gota')this.archive(e); return } this.hurt(e.x+e.w/2); }, archive(e){ if(e.t==='gota'||e.t==='barril'){e.dead=1;return} e.dead=20; sfx('stomp'); this.burst(e.x+e.w/2,e.y+e.h/2,10,['#f3ead2','#e8d9a8','#c0392b'],3,.12); this.floats.push({x:e.x+e.w/2,y:e.y-6,s:'ARCHIVADO',l:45,c:'#c8f0ff'}); }, /* ---------- minijuego: Digitalización masiva ---------- */ updateMini(){ const m=this.mi,p=this.p; if(this.mode!=='play')return; m.time--; m.spawnT--; if(m.dbl>0)m.dbl--; if(m.inkFlash>0)m.inkFlash--; if(m.streakT>0){m.streakT--;if(m.streakT<=0)m.streak=0} const prog=1-m.time/1800; if(m.spawnT<=0&&m.time>60){ m.spawnT=Math.max(9,22-prog*12|0); const r=Math.random(),x=24+Math.random()*(VW-48); const k=r<.62?'perg':r<.82?'rare':r<.88?'ink':r<.93?'digi':r<.97?'gold':'ink'; m.items.push({k,x,y:-24,vy:1.6+Math.random()*1.4+prog*1.2,rot:Math.random()*6,cat:['plano','foto','grabado','manus','cartel','prensa'][(Math.random()*6)|0]}); } const px=p.x+p.w/2,py=p.y+p.h/2; for(let i=m.items.length-1;i>=0;i--){ const it=m.items[i]; it.y+=it.vy; it.rot+=.05; const dx=it.x-px,dy=it.y-py; if(dx*dx+dy*dy<36*36){ m.items.splice(i,1); const dm=m.dbl>0?2:1; if(it.k==='ink'){m.streak=0;m.inkFlash=20;this.addScore(-25,it.x,it.y,'#ff8a8a');sfx('hit');this.burst(it.x,it.y,8,['#1d1b2e','#3a3a52'],2.5,.2);continue} if(it.k==='digi'){m.dbl=300;sfx('power');this.floats.push({x:it.x,y:it.y,s:'¡PUNTOS x2!',l:70,c:'#7ff6ff'});continue} m.streak++; m.streakT=150; m.mult=m.streak>=20?4:m.streak>=10?3:m.streak>=5?2:1; m.caught++; m.best=Math.max(m.best,m.streak); const base=it.k==='perg'?10:it.k==='rare'?50:100, pts=base*m.mult*dm; this.addScore(pts,it.x,it.y,it.k==='gold'?'#ffe89a':'#fff3b0'); this.lv.pergs++; this.run.pergs++; sfx(it.k==='perg'?'doc':'rare'); this.burst(it.x,it.y,6,['#ffe89a','#7ff6ff'],2.4,.05); } else if(it.y>12*T+10) m.items.splice(i,1); } if(m.time<=0){ const n=m.caught; m.bonusN=n>=45?500:n>=30?250:n>=15?100:0; this.finish(m.bonusN); } }, /* ---------- jefe final: El Olvido ---------- */ updateBoss(){ const b=this.boss,p=this.p,L=this.L,t=this.fr/60; if(this.mode!=='play'&&this.mode!=='win')return; if(this.mode==='win')return; if(b.dying){ b.dying++; if(b.dying%12===0){sfx('boom');this.shake=8;this.burst(400,110,16,['#e8dcc0','#ffe89a','#fff'],5,.05)} if(b.dying===150){ // recompensa del jefe const d=DBI.barrios,isNew=!save.got[d.id]; save.got[d.id]=true;persist();this.lv.docs.push(d.id);this.addScore(RAR[4].p,400,200,RCOL[4]); this.popQ.push({d,isNew}); sfx('legend'); this.flash=14 } if(b.dying===230) this.finish(1000); return; } // aparecen documentos clave const spots=[[4*T,9*T-24],[21*T,9*T-24],[12.5*T,6*T-24],[400,12*T-22],[150,12*T-22],[650,12*T-22],[10*T,6*T-24],[15*T,6*T-24]]; b.items=b.items.filter(i=>!i.taken); if(b.items.length<3&&this.fr%40===0){ const s=spots[(Math.random()*spots.length)|0]; if(!b.items.some(i=>Math.abs(i.x-s[0])<40&&Math.abs(i.y-s[1])<40)) b.items.push({k:'bdoc',x:s[0],y:s[1],cat:b.cats[b.phase],taken:false,ph:Math.random()*6}); } const px=p.x+p.w/2,py=p.y+p.h/2; for(const it of b.items){const dx=it.x-px,dy=it.y-py; if(dx*dx+dy*dy<30*30&&!p.dead){it.taken=true;b.got++;this.addScore(50,it.x,it.y,'#ffe89a');this.lv.pergs++;this.run.pergs++;sfx('rare');this.burst(it.x,it.y,12,['#ffe89a','#fff'],3,.05); if(b.got>=b.need){ b.got=0;b.phase++;b.mem=b.phase/3;sfx('power');this.flash=12;this.shake=10; L.enemies.length=0; for(const q of b.items)q.taken=true; this.floats.push({x:400,y:200,s:b.phase<3?'¡EL OLVIDO RETROCEDE!':'¡MEMORIA RECUPERADA!',l:110,c:'#cfe8ff'}); if(b.phase>=3){b.dying=1;b.mem=1;this.sello=0} } }} if(b.dying||p.dead)return; // ataques const cnt=t_=>L.enemies.filter(e=>e.t===t_).length; b.inkT--; if(b.inkT<=0){ b.inkT=[105,85,66][b.phase]; if(cnt('gota')<7){const g=mkEnemy('gota',60+Math.random()*680,60,0,VW);g.y=70;g.vy=.6;L.enemies.push(g)} } if(b.phase>=1){ b.mothT--; if(b.mothT<=0){ b.mothT=250; const from=Math.random()<.5, m=mkEnemy('polilla',from?-40:VW+40,0,0,VW); m.fly=true;m.vx=from?1.7:-1.7;m.by=170+Math.random()*130;m.y=m.by;L.enemies.push(m)} } if(b.phase>=2){ b.bugT--; if(b.bugT<=0){ b.bugT=340; const from=Math.random()<.5, m=mkEnemy('error404',from?-40:VW+40,12*T,0,VW); m.fly=true;m.vx=from?1.5:-1.5;L.enemies.push(m)} } this.updateEnemies(); } }; function L_push(L,e){L.enemies.push(e)} /* ===================================================================== 11. DIBUJO DE LA ESCENA Y DEL HUD ===================================================================== */ G.draw=function(c){ const L=this.L,p=this.p,t=this.fr/60,cam=Math.round(this.cam),lupa=this.lupa>0; c.save(); if(this.shake>0)c.translate((Math.random()-.5)*this.shake*.8,(Math.random()-.5)*this.shake*.8); // fondo if(L.kind==='world')drawBackground(c,L,cam,t); else if(L.kind==='room')drawRoomBG(c,cam,t); else if(L.kind==='mini')drawMiniBG(c,t); else{drawBossBG(c,t,this.boss.mem);drawOlvido(c,t,this.boss.mem,400,105,this.boss.dying?Math.min(1,this.boss.dying/130):0)} c.save();c.translate(-cam,0); // decorado if(L.kind==='world'){c.fillStyle=['#2b1d14','#1f2a1c','#23232c','#150f2a'][L.wi];c.fillRect(cam,374,VW,VH-374);for(const pr of L.props){if(pr.x>cam-120&&pr.x 0?L.tiles[(ty-1)*L.w+tx]:0;img=(up===1||up===2||up===3||up===4)?ts.fill:ts.top} else img=v===2?ts.brick:v===3?ts.box:v===4?ts.used:v===5?ts.plank:ts.digi; if(v===6){c.globalAlpha=this.dOn?.25:.75+.2*Math.sin(t*4+ty)} if(this.bumps.length)for(const bp of this.bumps)if(bp.tx===tx&&bp.ty===ty)oy=-Math.sin(bp.t/8*Math.PI)*7; c.drawImage(img,tx*T,ty*T+oy,T,T); if(v===6)c.globalAlpha=1; } // puertas, meta, checkpoint const pcx=p.x+p.w/2; for(const d of L.doors)drawDoor(c,d,t,lupa,Math.abs(d.x+T/2-pcx)<90&&Math.abs(d.y+d.h-p.y-p.h)<60); if(L.goal)drawGoal(c,L.goal,t); if(L.cp)drawCheckpoint(c,L.cp,t); // objetos for(const it of L.items){ if(it.x>cam-40&&it.x cam-130&&e.x 0&&!p.dead&&(this.fr>>2)%2)){ const pcx2=p.x+p.w/2,fy=p.y+p.h; if(this.sello>0&&(this.sello>120||(this.fr>>2)%2)){ c.globalAlpha=.35+.15*Math.sin(t*10);circ(c,'#ffd24a',pcx2,p.y+22,36);c.globalAlpha=1; c.strokeStyle='rgba(255,236,150,.9)';c.lineWidth=2;c.beginPath();c.arc(pcx2,p.y+22,32,t*3,t*3+4.4);c.stroke(); } if(lupa){c.strokeStyle='rgba(255,230,140,.55)';c.lineWidth=3;c.beginPath();c.arc(pcx2,p.y+22,64+Math.sin(t*5)*3,0,TAU);c.stroke()} if(this.dOn){c.globalAlpha=.62+.2*Math.sin(t*30); c.fillStyle='rgba(94,240,255,.18)';c.fillRect(p.x-8,p.y-6,p.w+16,p.h+10)} drawMarques(c,pcx2,fy,{dir:p.dir,t,st:this.mode==='win'?'win':p.st,ph:p.ph}); if(this.dOn){c.globalAlpha=1;c.strokeStyle='rgba(94,240,255,.8)';c.lineWidth=1.5;for(let i=0;i<5;i++){const yy=p.y+((t*60+i*10)%p.h);c.beginPath();c.moveTo(p.x-6,yy);c.lineTo(p.x+p.w+6,yy);c.stroke()}} c.globalAlpha=1; } // indicador de puerta if(this.mode==='play')for(const d of L.doors){if(Math.abs(d.x+T/2-pcx)<44&&Math.abs(d.y+d.h-p.y-p.h)<20){txt(c,'↑',d.x+T/2,d.y-8+Math.sin(t*6)*2,18,'#fff3b0','center')}} // partículas for(const q of this.parts){c.globalAlpha=Math.min(1,q.l/20);c.fillStyle=q.c;c.fillRect(q.x-q.s/2,q.y-q.s/2,q.s,q.s)} c.globalAlpha=1; for(const f of this.floats){c.globalAlpha=Math.min(1,f.l/20);txt(c,f.s,f.x,f.y,f.s.length>6?13:15,f.c,'center')} c.globalAlpha=1; c.restore(); // cámara // viñeta de Lupa: oscurece el entorno con un halo if(lupa){const g=c.createRadialGradient(p.x+p.w/2-cam,p.y+22,80,p.x+p.w/2-cam,p.y+22,420);g.addColorStop(0,'rgba(40,20,0,0)');g.addColorStop(1,'rgba(40,20,0,.28)');c.fillStyle=g;c.fillRect(0,0,VW,VH)} c.restore(); this.drawHud(c); if(this.flash>0){c.globalAlpha=this.flash/22;c.fillStyle='#fff';c.fillRect(0,0,VW,VH);c.globalAlpha=1} }; G.heart=function(c,x,y,s,col){c.fillStyle=col;c.beginPath();c.moveTo(x,y+s*.9);c.bezierCurveTo(x-s*1.5,y-s*.1,x-s*.7,y-s*1.1,x,y-s*.35);c.bezierCurveTo(x+s*.7,y-s*1.1,x+s*1.5,y-s*.1,x,y+s*.9);c.fill()}; G.drawHud=function(c){ const run=this.run,t=this.fr/60; // panel superior izquierdo c.fillStyle='rgba(20,10,40,.6)';rr(c,8,8,236,52,10);c.fill();c.strokeStyle='rgba(255,226,150,.5)';c.lineWidth=1.5;c.stroke(); for(let i=0;i 6)txt(c,'+'+(run.lives-6),26+6*20-4,27,12,'#fff','left'); for(let i=0;i<2;i++){c.fillStyle=i =2)txt(c,'Racha '+m.streak+' ×'+m.mult,400,120,16,'#ffe89a','center'); if(m.dbl>0)txt(c,'¡PUNTOS ×2! '+Math.ceil(m.dbl/60)+'s',400,142,14,'#7ff6ff','center'); if(m.inkFlash>0){c.globalAlpha=m.inkFlash/40;c.fillStyle='#1d1b2e';c.fillRect(0,0,VW,VH);c.globalAlpha=1} } // jefe if(this.boss){const b=this.boss; if(!b.dying){ const names=['Planos','Manuscritos','Fotografías'],ph=Math.min(2,b.phase); txt(c,'EL OLVIDO · Fase '+(ph+1)+'/3',400,VH-34,14,'#cfe8ff','center'); c.fillStyle='rgba(0,0,0,.55)';rr(c,250,VH-26,300,12,6);c.fill(); c.fillStyle='#ffd870';rr(c,252,VH-24,296*(b.got/b.need),8,4);c.fill(); txt(c,'Recupera '+names[ph]+': '+b.got+'/'+b.need,400,VH-6,12,'#fff','center'); } } // aviso / título de nivel if(this.banner){const b=this.banner,a=b.t<20?b.t/20:b.t>170?(200-b.t)/30:1;c.globalAlpha=clamp(a,0,1); c.fillStyle='rgba(15,8,30,.62)';rr(c,170,118,460,72,14);c.fill();c.strokeStyle='rgba(255,226,150,.7)';c.lineWidth=2;c.stroke(); txt(c,b.a,400,152,26,'#ffe89a','center');txt(c,b.b,400,178,14,'#fff','center');c.globalAlpha=1} this.drawPopup(c); if(this.mode==='win'&&this.kind!=='mini'||this.mode==='win'){ const a=clamp(this.winT/25,0,1);c.globalAlpha=a; txt(c,this.kind==='mini'?'¡TIEMPO!':this.kind==='boss'?'¡MEMORIA RESTAURADA!':'¡NIVEL COMPLETADO!',400,230,34,'#ffe89a','center');c.globalAlpha=1} }; G.drawPopup=function(c){ const pp=this.pop; if(!pp)return; const d=pp.d,tt=pp.t,k=tt<14?tt/14:tt>166?(180-tt)/14:1,e=1-Math.pow(1-clamp(k,0,1),3); const w=470,h=116,x=(VW-w)/2,y=lerp(-h-6,176,e); c.save(); const g=c.createLinearGradient(0,y,0,y+h);g.addColorStop(0,'#fff4d6');g.addColorStop(1,'#ecd7a2'); rr(c,x,y,w,h,12);c.fillStyle=g;c.fill();c.lineWidth=4;c.strokeStyle=RCOL[d.r];c.stroke(); c.fillStyle='#3a2410';rr(c,x+10,y+12,78,78,8);c.fill(); c.save();c.translate(x+49,y+51);c.scale(2.3,2.3);iconDoc(c,d.cat);c.restore(); c.textAlign='left';c.textBaseline='alphabetic'; c.font='bold 16px Georgia,serif';c.fillStyle='#7b1e2b';c.fillText(d.name,x+98,y+26,w-110); c.font='italic 12px Georgia,serif';c.fillStyle='#5a3a12'; c.fillText(d.date+' · '+RAR[d.r].n+(pp.isNew?' · ¡Nueva ficha!':' · Ya en tu colección'),x+98,y+44,w-110); c.font='12px Georgia,serif';c.fillStyle='#2a1608'; const lines=wrapText(c,d.txt,w-112); for(let i=0;i W*1.05; document.body.classList.toggle('portrait',portrait); let sw=Math.min(W,H*16/9); if(portrait)sw=W; const sh=sw*9/16; stage.style.width=sw+'px'; stage.style.height=sh+'px'; stage.style.fontSize=(sw/800*14)+'px'; SC=Math.min(2,window.devicePixelRatio||1)*sw/VW; SC=Math.min(SC,1600/VW); const cw=Math.round(VW*SC),ch=Math.round(VH*SC); if(cv.width!==cw||cv.height!==ch){cv.width=cw;cv.height=ch} $('ui').style.fontSize=clamp(Math.min(W/50,H/33),9,22)+'px'; } window.addEventListener('resize',fit);window.addEventListener('orientationchange',()=>setTimeout(fit,200)); /* ---------- pantallas ---------- */ function show(id){ document.querySelectorAll('.screen').forEach(s=>s.classList.remove('on')); if(id){const s=$(id);s.classList.add('on');s.scrollTop=0;const b=s.querySelector('.btn:not([disabled])');if(b&&!document.body.classList.contains('touch'))b.focus()} } function fadeTo(fn){const f=$('fade');f.classList.add('on');setTimeout(()=>{fn();setTimeout(()=>f.classList.remove('on'),60)},360)} function refreshToggles(){ const s=Aud.mute?'NO':'SÍ'; $('bSnd').textContent='Sonido: '+s;$('bPauseSnd').textContent='Sonido: '+s;$('hSnd').style.opacity=Aud.mute?.45:1; $('bDbl').textContent='Doble salto: '+(save.dbl?'SÍ':'NO'); $('bCont').disabled=!save.run; } function togglePause(onlyPause){ if(state==='play'){state='pause';show('sPause');refreshToggles();persistRun()} else if(state==='pause'&&!onlyPause){state='play';show(null);document.activeElement&&document.activeElement.blur()} } function persistRun(){ if(!G.run||state==='menu')return; save.run=Object.assign({},G.snap,{lives:Math.max(1,G.run.lives),step:G.run.step});persist(); } const fsReq=()=>{const el=document.documentElement; if(document.fullscreenElement||document.webkitFullscreenElement){(document.exitFullscreen||document.webkitExitFullscreen).call(document)} else{const f=el.requestFullscreen||el.webkitRequestFullscreen;if(f)try{f.call(el)}catch(e){}}}; if(!(document.documentElement.requestFullscreen||document.documentElement.webkitRequestFullscreen)){$('hFull').style.display='none';$('bFull').style.display='none'} /* ---------- flujo ---------- */ function toMenu(){ Music.play('menu');state='menu';document.body.classList.remove('playing');refreshToggles();show('sMenu'); } function startStep(i){ fadeTo(()=>{ state='play';document.body.classList.add('playing');show(null); const s=SEQ[i];G.run.step=i;G.begin(s.k,s.wi||0,G.run); lastAuto=performance.now();persistRun(); }); } function newGame(){ G.run={score:0,lives:3,time:0,pergs:0,step:0};save.run=null; startStep(0); } function continueGame(){ const r=Object.assign({score:0,lives:3,time:0,pergs:0,step:0},save.run); if(r.lives<=0)r.lives=3; if(r.step>=SEQ.length)r.step=0; G.run=r;startStep(r.step); } function onLevelEnd(res){ const run=G.run,step=run.step,last=step>=SEQ.length-1; save.best=Math.max(save.best||0,run.score); if(last){save.done=true;save.run=null;persist();return showFinal(res)} save.run=Object.assign({},run,{step:step+1});persist(); state='end';document.body.classList.remove('playing'); const world=res.kind==='world', mini=res.kind==='mini'; $('eKick').textContent=world?'¡Nivel completado!':'¡Digitalización completada!'; $('eTitle').textContent=world?WORLDS[res.wi].name:'Digitalización masiva'; const rows=[['Puntos del nivel',res.gain],['Bonificación',res.bonus],['Pergaminos y documentos',res.pergs], ['Fichas históricas',res.docs.length],['Tiempo',fmtTime(res.time)],['Colección',collectedCount()+'/'+DB.length+' ('+collectedPct()+'%)'],['Puntuación total',run.score]]; $('eStats').innerHTML=rows.map(r=>` ${r[0]} ${r[1]} `).join(''); $('eNote').textContent=world?(res.docs.length?'Nuevas fichas: '+res.docs.map(id=>DBI[id].name).join(' · '):'Rejuega el nivel para descubrir los documentos ocultos con la Lupa.') :'Documentos digitalizados: '+res.caught+(res.bonus?' — ¡bonificación de archivo!':'. Cuantos más atrapes seguidos, mayor la racha.'); $('bNext').textContent=SEQ[step+1].k==='boss'?'Hacia El Olvido':'Siguiente'; show('sEnd'); } function onGameOver(){state='over';document.body.classList.remove('playing');persistRun();setTimeout(()=>{if(state==='over')show('sOver')},500)} function showFinal(res){ state='final';finalT=0;document.body.classList.remove('playing'); Music.play('final');sfx('win'); const run=G.run; $('fStats').innerHTML=[['Puntuación',run.score],['Tiempo total',fmtTime(run.time)],['Documentos recuperados',run.pergs+collectedCount()], ['Fichas históricas',collectedCount()+'/'+DB.length],['Colección completada',collectedPct()+'%']].map(r=>`
${r[0]}: ${r[1]}
`).join(''); show('sFinal'); } /* ---------- colección ---------- */ let collFrom='sMenu',collTab='all'; function showCollection(from){ collFrom=from;state=state==='final'?'final':'coll'; renderColl();show('sColl'); } function renderColl(){ $('cHead').textContent='Fichas desbloqueadas: '+collectedCount()+' de '+DB.length+' ('+collectedPct()+'%)'; const tabs=[{id:'all',name:'Todo'}].concat(CATS); $('cTabs').innerHTML=tabs.map(t=>` `).join(''); $('cTabs').querySelectorAll('.tab').forEach(b=>b.onclick=()=>{collTab=b.dataset.t;sfx('select');renderColl()}); const list=DB.filter(d=>collTab==='all'||d.cat===collTab); const grid=$('cGrid');grid.innerHTML=''; if(!list.length){grid.innerHTML='

No hay fichas en esta categoría.

';return} list.forEach(d=>{ const got=!!save.got[d.id],card=document.createElement('div');card.className='card'+(got?'':' lock'); const cn=document.createElement('canvas');cn.width=cn.height=96;const g=cn.getContext('2d');g.translate(48,48); if(got){g.scale(2.6,2.6);d.r===4&&(g.fillStyle='rgba(255,220,100,.35)',g.beginPath(),g.arc(0,0,16,0,TAU),g.fill());iconDoc(g,d.cat)} else{g.fillStyle='#9d8a63';g.font='bold 54px Georgia,serif';g.textAlign='center';g.textBaseline='middle';g.fillText('?',0,4)} const cat=CATS.find(c=>c.id===d.cat).name.split(' ')[0]; const body=document.createElement('div'); body.innerHTML=got?` ${d.name} ${d.date} · ${RAR[d.r].n} · ${cat}

${d.txt}

` :` ??? ${RAR[d.r].n} · ${cat} · ${d.w<4?WNAME[d.w]:'Jefe final'}

${SPOT_HINT[d.spot]}

`; card.appendChild(cn);card.appendChild(body);grid.appendChild(card); }); } /* ---------- eventos de botones ---------- */ const clk=(id,fn)=>$(id).addEventListener('click',e=>{unlockAudio();sfx('select');fn(e)}); clk('bStart',()=>{toMenu()}); clk('bPlay',()=>newGame()); clk('bCont',()=>{if(save.run)continueGame()}); clk('bColl',()=>showCollection('sMenu')); clk('bCred',()=>{state='coll';show('sCred')}); clk('bDbl',()=>{save.dbl=!save.dbl;persist();refreshToggles()}); clk('bSnd',()=>toggleMute()); clk('bFull',()=>fsReq()); clk('hFull',()=>fsReq()); clk('bCollBack',()=>{if(collFrom==='sFinal'){state='final';show('sFinal')}else toMenu()}); clk('bCredBack',()=>toMenu()); clk('bResume',()=>togglePause()); clk('bRestart',()=>{state='play';show(null);fadeTo(()=>{document.body.classList.add('playing');G.restart()})}); clk('bPauseSnd',()=>toggleMute()); clk('bQuit',()=>{persistRun();toMenu()}); clk('bNext',()=>startStep(G.run.step+1)); clk('bRetry',()=>{startStepRetry()}); clk('bOverMenu',()=>{toMenu()}); clk('bFinalColl',()=>showCollection('sFinal')); clk('bFinalMenu',()=>{save.run=null;persist();toMenu()}); clk('hSnd',()=>toggleMute()); $('hPause').addEventListener('click',()=>{unlockAudio();togglePause()}); function startStepRetry(){fadeTo(()=>{state='play';document.body.classList.add('playing');show(null);G.restart()})} document.addEventListener('visibilitychange',()=>{if(document.hidden){if(state==='play')togglePause(true);else persistRun();if(Aud.ctx&&Aud.ctx.state==='running')Aud.ctx.suspend()}else if(Aud.ctx&&state!=='pause')Aud.ctx.resume()}); window.addEventListener('pagehide',()=>{persistRun()}); document.addEventListener('contextmenu',e=>e.preventDefault()); /* ---------- escenas de menú y final ---------- */ function drawMenuScene(c,t){ const wi=Math.floor(t/14)%4; if(!BG[wi]){R(c,'#241848',0,0,VW,VH);return} const cam=t*60, L={wi,w:150}; drawBackground(c,L,cam,t); const ts=TILESETS[wi]; if(!ts)return; const off=-(cam%T); for(let i=0;i<27;i++){c.drawImage(ts.top,off+i*T,352,T,T);for(let j=1;j<3;j++)c.drawImage(ts.fill,off+i*T,352+j*T,T,T)} if(state==='menu'||state==='load')drawMarques(c,250,352,{dir:1,t,st:'walk',ph:t*8}); c.fillStyle='rgba(10,5,25,.25)';c.fillRect(0,0,VW,VH); } function drawFinal(c,t){ drawFinalScene(c,t); const x=120,y=VH-62;drawMarques(c,x,y+3,{dir:1,t,st:'win',ph:t*8}); for(let i=0;i<46;i++){const px=(i*97+Math.sin(t*.7+i)*40)%VW,py=((t*50+i*61)%(VH+30))-15;c.save();c.translate(px,py);c.rotate(t*2+i);c.fillStyle=['#ffd84a','#e8344a','#4aa8e0','#3ad06a','#fff'][i%5];c.fillRect(-3,-2,6,4);c.restore()} } /* ---------- bucle principal a 60 Hz fijos ---------- */ let acc=0,last=0,booted=false; function frame(ts){ requestAnimationFrame(frame); const dt=Math.min(100,ts-(last||ts));last=ts;acc+=dt; let n=0; while(acc>=1000/60&&n<5){acc-=1000/60;n++; if(state==='play')G.update(); else if(state==='menu'||state==='coll'||state==='load')menuT+=1/60; else if(state==='final')finalT+=1/60; } if(n===5)acc=0; ctx.setTransform(SC,0,0,SC,0,0);ctx.imageSmoothingEnabled=true; if(state==='play'||state==='pause'||state==='end'||state==='over'){if(G.L)G.draw(ctx)} else if(state==='final')drawFinal(ctx,finalT); else drawMenuScene(ctx,menuT); if(state==='play'&&performance.now()-lastAuto>10000){lastAuto=performance.now();persistRun()} } /* ---------- carga inicial ---------- */ (function boot(){ fit();refreshToggles(); const steps=[]; for(let wi=0;wi<4;wi++){steps.push(()=>makeBg(wi));steps.push(()=>{TILESETS[wi]=makeTileSet(wi)})} steps.push(()=>makeRoomBg()); let i=0,tip=0; $('loadTip').textContent=TIPS[0]; const tipTimer=setInterval(()=>{tip=(tip+1)%TIPS.length;$('loadTip').textContent=TIPS[tip]},2600); (function run(){ if(i