mirror of
https://git.j0bot.ch/admin_j0bot/ploxion-planete-bowl.git
synced 2026-11-29 06:55:12 +00:00
319 lines
16 KiB
PHP
319 lines
16 KiB
PHP
{{-- PL0XION planete-bowl 🪐 — le défouloir de RS-7 (« ils me saoulent, on va faire un ploxion pour eux »).
|
||
Un BOL (canvas 2D) plein de petites planètes qui rebondissent : gravité constante vers le centre du bol +
|
||
collisions élastiques par impulsion + rebonds sur la paroi. On glisse une planète pour la LANCER, 🌀 secoue
|
||
tout, on ajoute une planète en tapant un NOM (« pour eux ») → couleur/taille/anneau DÉTERMINISTES depuis le
|
||
nom (hash FNV). Les noms sont persistés (localStorage) et ré-largables via des chips. 100% canvas, 0 dépendance,
|
||
requestAnimationFrame propre (annulé à l'unmount, physique à pas fixe = indépendante du framerate). Thème SOMBRE
|
||
hardcodé. window.jOSApps['planete-bowl']. --}}
|
||
<style>
|
||
.pbw{display:flex;flex-direction:column;height:100%;min-height:0;background:#070c16;color:#eaf3ff;
|
||
font-family:ui-sans-serif,system-ui,-apple-system,"Segoe UI",sans-serif;font-size:13px}
|
||
.pbw *{box-sizing:border-box}
|
||
.pbw-bar{flex:0 0 auto;display:flex;flex-wrap:wrap;align-items:center;gap:7px;padding:9px 11px;
|
||
background:#0e1a30;border-bottom:1px solid rgba(63,224,208,.16)}
|
||
.pbw-logo{font-weight:800;letter-spacing:.3px;color:#3fe0d0;white-space:nowrap}
|
||
.pbw-count{font-family:ui-monospace,"SFMono-Regular",Menlo,monospace;font-size:.72rem;color:#9fc7d8;
|
||
background:#081426;border:1px solid rgba(120,150,170,.28);border-radius:999px;padding:2px 9px;white-space:nowrap}
|
||
.pbw-in{flex:1 1 130px;min-width:0;background:#050e1c;color:#eaf3ff;border:1px solid rgba(120,150,170,.34);
|
||
border-radius:8px;padding:6px 9px;font:inherit;font-size:.82rem}
|
||
.pbw-in::placeholder{color:rgba(160,190,205,.5)}
|
||
.pbw-in:focus{outline:none;border-color:#3fe0d0;box-shadow:0 0 0 2px rgba(63,224,208,.16)}
|
||
.pbw-b{cursor:pointer;padding:6px 11px;border-radius:8px;border:1px solid rgba(120,150,170,.34);
|
||
background:#0a1526;color:#dbe9ff;font:inherit;font-size:.8rem;white-space:nowrap;transition:.12s}
|
||
.pbw-b:hover{background:#12233c;border-color:rgba(63,224,208,.5)}
|
||
.pbw-b:active{transform:translateY(1px)}
|
||
.pbw-b.pri{border-color:#3fe0d0;color:#eafffb;font-weight:700;background:rgba(63,224,208,.12)}
|
||
.pbw-b.pri:hover{background:rgba(63,224,208,.22)}
|
||
.pbw-stage{flex:1 1 auto;position:relative;min-height:0;overflow:hidden;background:#04070e}
|
||
.pbw-cv{position:absolute;inset:0;width:100%;height:100%;display:block;touch-action:none;cursor:default}
|
||
.pbw-hint{position:absolute;left:10px;bottom:8px;font-size:.66rem;color:rgba(150,180,200,.55);
|
||
pointer-events:none;text-shadow:0 1px 3px #000;line-height:1.4}
|
||
.pbw-chips{flex:0 0 auto;display:flex;gap:6px;align-items:center;padding:8px 10px;overflow-x:auto;
|
||
background:#0b1424;border-top:1px solid rgba(63,224,208,.14);scrollbar-width:thin}
|
||
.pbw-chips::-webkit-scrollbar{height:6px}
|
||
.pbw-chips::-webkit-scrollbar-thumb{background:rgba(63,224,208,.3);border-radius:99px}
|
||
.pbw-chip{display:inline-flex;align-items:center;gap:5px;cursor:pointer;white-space:nowrap;
|
||
background:#0f2036;border:1px solid rgba(120,150,170,.3);border-radius:999px;padding:3px 5px 3px 10px;
|
||
font-size:.74rem;color:#dbe9ff;transition:.12s}
|
||
.pbw-chip:hover{border-color:#3fe0d0;background:#14294a}
|
||
.pbw-chip b{font-weight:600}
|
||
.pbw-chip i{font-style:normal;display:inline-flex;align-items:center;justify-content:center;width:16px;height:16px;
|
||
border-radius:50%;background:rgba(255,120,120,.14);color:#ff9d9d;font-size:.8rem;line-height:1}
|
||
.pbw-chip i:hover{background:rgba(255,90,90,.32);color:#fff}
|
||
.pbw-empty{font-size:.72rem;color:rgba(150,180,200,.5);padding:2px 4px;white-space:nowrap}
|
||
</style>
|
||
<script>
|
||
(function(){
|
||
'use strict';
|
||
|
||
function esc(s){ return String(s).replace(/[&<>"']/g,function(c){
|
||
return {'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]; }); }
|
||
|
||
// hash déterministe (FNV-1a) → couleur / taille / anneau depuis un nom
|
||
function hashStr(s){ var h=2166136261>>>0; s=String(s);
|
||
for(var i=0;i<s.length;i++){ h^=s.charCodeAt(i); h=Math.imul(h,16777619)>>>0; }
|
||
return h>>>0; }
|
||
|
||
var LIVE=null; // posé au mount : { planets }
|
||
function describe(){
|
||
var planets=LIVE?LIVE.planets():[];
|
||
return { v:1, id:'planete-bowl', title:'Planète bowl', icon:'🪐', items:[
|
||
{ p:1, kind:'stat', label:'planètes en jeu', value:planets.length }
|
||
] };
|
||
}
|
||
function mount(container, ctx){
|
||
var LS='px_planete_bowl_names';
|
||
var MAXP=140, MAXV=15, MAXTHROW=24;
|
||
|
||
container.innerHTML=
|
||
'<div class="pbw">'+
|
||
'<div class="pbw-bar">'+
|
||
'<span class="pbw-logo">🪐 Planète bowl</span>'+
|
||
'<span class="pbw-count" data-count>0</span>'+
|
||
'<input class="pbw-in" data-in maxlength="24" placeholder="un nom… (pour eux)">'+
|
||
'<button class="pbw-b pri" data-add>+ ajouter</button>'+
|
||
'<button class="pbw-b" data-shake>🌀 secouer</button>'+
|
||
'<button class="pbw-b" data-clear>🗑 vider</button>'+
|
||
'</div>'+
|
||
'<div class="pbw-stage" data-stage>'+
|
||
'<canvas class="pbw-cv" data-cv></canvas>'+
|
||
'<div class="pbw-hint">glisse une planète pour la lancer · 🌀 pour tout agiter · clique un nom pour le relâcher</div>'+
|
||
'</div>'+
|
||
'<div class="pbw-chips" data-chips></div>'+
|
||
'</div>';
|
||
|
||
var root = container.querySelector('.pbw');
|
||
var stage = root.querySelector('[data-stage]');
|
||
var cv = root.querySelector('[data-cv]');
|
||
var inEl = root.querySelector('[data-in]');
|
||
var countEl= root.querySelector('[data-count]');
|
||
var chipsEl= root.querySelector('[data-chips]');
|
||
var g2 = cv.getContext('2d');
|
||
|
||
var W=300,H=300,cx=150,cy=150,R=130,gAccel=0.05;
|
||
var planets=[];
|
||
var dragIndex=-1, hoverIndex=-1, lastPt=null, dragPointerId=null;
|
||
var disposed=false, raf=0;
|
||
var TAU=Math.PI*2;
|
||
|
||
// ---------- persistance ----------
|
||
var saved=loadNames();
|
||
if(!saved){ saved=['Lune','Mars','Vénus','Terre','Jupiter','Saturne']; saveNames(); }
|
||
function loadNames(){ try{ var r=localStorage.getItem(LS); if(r){ var a=JSON.parse(r);
|
||
if(Array.isArray(a)) return a.filter(function(x){return typeof x==='string';}).slice(0,MAXP); } }catch(e){} return null; }
|
||
function saveNames(){ try{ localStorage.setItem(LS, JSON.stringify(saved.slice(0,MAXP))); }catch(e){} }
|
||
|
||
// ---------- création d'une planète ----------
|
||
function spawn(name, atX, atY){
|
||
if(planets.length>=MAXP) return null;
|
||
var h=hashStr(name||('a'+Date.now()+Math.random()));
|
||
var r=8+(h%15); // 8..22 px
|
||
var x,y;
|
||
if(atX==null){ var ang=Math.random()*TAU, rr=Math.max(2,R-r-2);
|
||
x=cx+Math.cos(ang)*rr; y=cy+Math.sin(ang)*rr; }
|
||
else { x=atX; y=atY; }
|
||
var p={ name:name||'', x:x, y:y, vx:0, vy:0, r:r,
|
||
hue:(h>>>3)%360, ring:((h>>>16)&3)===0, invM:1/(r*r) };
|
||
var dx=cx-x, dy=cy-y, d=Math.hypot(dx,dy)||1; p.vx=dx/d*2; p.vy=dy/d*2; // petit élan vers le centre
|
||
planets.push(p); return p;
|
||
}
|
||
|
||
function clampInside(p){ var dx=p.x-cx, dy=p.y-cy, d=Math.hypot(dx,dy), lim=Math.max(1,R-p.r);
|
||
if(d>lim && d>0.001){ var nx=dx/d, ny=dy/d; p.x=cx+nx*lim; p.y=cy+ny*lim; } }
|
||
|
||
function pick(x,y){ for(var i=planets.length-1;i>=0;i--){ var p=planets[i];
|
||
var dx=x-p.x, dy=y-p.y; if(dx*dx+dy*dy<=(p.r+3)*(p.r+3)) return i; } return -1; }
|
||
|
||
function updateCount(){ countEl.textContent=planets.length+' 🪐'; }
|
||
|
||
// ---------- PHYSIQUE (pas fixe) ----------
|
||
function resolve(a,b,ia,ib){
|
||
var dx=b.x-a.x, dy=b.y-a.y, dist=Math.hypot(dx,dy), min=a.r+b.r;
|
||
if(dist>=min || dist<=0.0001) return;
|
||
var nx=dx/dist, ny=dy/dist, overlap=min-dist;
|
||
var ima=(ia===dragIndex)?0:a.invM, imb=(ib===dragIndex)?0:b.invM, tot=ima+imb;
|
||
if(tot<=0) return;
|
||
a.x-=nx*overlap*(ima/tot); a.y-=ny*overlap*(ima/tot);
|
||
b.x+=nx*overlap*(imb/tot); b.y+=ny*overlap*(imb/tot);
|
||
var rvx=b.vx-a.vx, rvy=b.vy-a.vy, vn=rvx*nx+rvy*ny;
|
||
if(vn>0) return;
|
||
var e=0.92, jimp=-(1+e)*vn/tot, ix=jimp*nx, iy=jimp*ny;
|
||
a.vx-=ix*ima; a.vy-=iy*ima; b.vx+=ix*imb; b.vy+=iy*imb;
|
||
}
|
||
|
||
function physics(){
|
||
var n=planets.length, i, j, p;
|
||
for(i=0;i<n;i++){ if(i===dragIndex) continue; p=planets[i];
|
||
var dx=cx-p.x, dy=cy-p.y, d=Math.hypot(dx,dy);
|
||
if(d>0.001){ p.vx+=gAccel*dx/d; p.vy+=gAccel*dy/d; }
|
||
p.vx*=0.994; p.vy*=0.994;
|
||
var sp=Math.hypot(p.vx,p.vy); if(sp>MAXV){ p.vx=p.vx/sp*MAXV; p.vy=p.vy/sp*MAXV; }
|
||
}
|
||
for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ resolve(planets[i],planets[j],i,j); } }
|
||
for(i=0;i<n;i++){ if(i===dragIndex) continue; p=planets[i];
|
||
p.x+=p.vx; p.y+=p.vy;
|
||
var wx=p.x-cx, wy=p.y-cy, wd=Math.hypot(wx,wy), lim=R-p.r;
|
||
if(wd>lim && wd>0.001){ var nx=wx/wd, ny=wy/wd; p.x=cx+nx*lim; p.y=cy+ny*lim;
|
||
var vn=p.vx*nx+p.vy*ny; if(vn>0){ p.vx-=1.7*vn*nx; p.vy-=1.7*vn*ny; } }
|
||
}
|
||
}
|
||
|
||
// ---------- RENDU ----------
|
||
function roundRect(x,y,w,h,rr){ g2.beginPath();
|
||
g2.moveTo(x+rr,y); g2.arcTo(x+w,y,x+w,y+h,rr); g2.arcTo(x+w,y+h,x,y+h,rr);
|
||
g2.arcTo(x,y+h,x,y,rr); g2.arcTo(x,y,x+w,y,rr); g2.closePath(); }
|
||
|
||
function drawPlanet(p){
|
||
if(p.ring){ g2.save(); g2.translate(p.x,p.y); g2.rotate(-0.5); g2.scale(1,0.34);
|
||
g2.beginPath(); g2.arc(0,0,p.r*1.75,0,TAU); g2.lineWidth=Math.max(2,p.r*0.34);
|
||
g2.strokeStyle='hsla('+p.hue+',62%,62%,0.5)'; g2.stroke(); g2.restore(); }
|
||
var gr=g2.createRadialGradient(p.x-p.r*0.35, p.y-p.r*0.4, p.r*0.12, p.x, p.y, p.r);
|
||
gr.addColorStop(0,'hsl('+p.hue+',78%,80%)');
|
||
gr.addColorStop(0.5,'hsl('+p.hue+',66%,54%)');
|
||
gr.addColorStop(1,'hsl('+p.hue+',60%,25%)');
|
||
g2.beginPath(); g2.arc(p.x,p.y,p.r,0,TAU); g2.fillStyle=gr; g2.fill();
|
||
g2.lineWidth=1; g2.strokeStyle='hsla('+p.hue+',60%,88%,0.28)'; g2.stroke();
|
||
}
|
||
|
||
function drawLabel(p){
|
||
if(!p || !p.name) return;
|
||
g2.font='600 12px ui-sans-serif,system-ui,sans-serif';
|
||
var t=p.name, tw=g2.measureText(t).width, pad=7, bw=tw+pad*2, bh=20;
|
||
var bx=p.x-bw/2, by=p.y-p.r-bh-6;
|
||
if(by<2) by=p.y+p.r+6;
|
||
if(bx<2) bx=2; if(bx+bw>W-2) bx=W-2-bw;
|
||
g2.fillStyle='rgba(7,12,22,0.9)'; roundRect(bx,by,bw,bh,7); g2.fill();
|
||
g2.lineWidth=1; g2.strokeStyle='rgba(63,224,208,0.55)'; roundRect(bx,by,bw,bh,7); g2.stroke();
|
||
g2.fillStyle='#eaf3ff'; g2.textAlign='center'; g2.textBaseline='middle';
|
||
g2.fillText(t, bx+bw/2, by+bh/2);
|
||
g2.textAlign='left'; g2.textBaseline='alphabetic';
|
||
}
|
||
|
||
function render(){
|
||
g2.setTransform(dpr,0,0,dpr,0,0);
|
||
g2.fillStyle='#070c16'; g2.fillRect(0,0,W,H);
|
||
var bowl=g2.createRadialGradient(cx,cy,R*0.08,cx,cy,R);
|
||
bowl.addColorStop(0,'#0d1a34'); bowl.addColorStop(0.75,'#0a1424'); bowl.addColorStop(1,'#05080f');
|
||
g2.beginPath(); g2.arc(cx,cy,R,0,TAU); g2.fillStyle=bowl; g2.fill();
|
||
g2.lineWidth=2; g2.strokeStyle='rgba(63,224,208,0.28)'; g2.stroke();
|
||
g2.lineWidth=1; g2.beginPath(); g2.arc(cx,cy,Math.max(1,R-4),0,TAU);
|
||
g2.strokeStyle='rgba(63,224,208,0.08)'; g2.stroke();
|
||
for(var i=0;i<planets.length;i++) drawPlanet(planets[i]);
|
||
if(hoverIndex>=0 && hoverIndex<planets.length) drawLabel(planets[hoverIndex]);
|
||
}
|
||
|
||
// ---------- boucle (pas fixe 16ms → indépendante du framerate) ----------
|
||
var acc=0, last=(typeof performance!=='undefined'?performance.now():Date.now());
|
||
function frame(now){
|
||
if(disposed) return;
|
||
var el=now-last; last=now; if(el<0) el=0; acc+=el; if(acc>120) acc=120;
|
||
var steps=0;
|
||
while(acc>=16 && steps<8){ physics(); acc-=16; steps++; }
|
||
render();
|
||
raf=requestAnimationFrame(frame);
|
||
}
|
||
|
||
// ---------- resize / DPR ----------
|
||
var dpr=Math.min((window.devicePixelRatio||1),2);
|
||
function resize(){
|
||
var r=stage.getBoundingClientRect();
|
||
var w=Math.max(80,Math.floor(r.width||300)), h=Math.max(80,Math.floor(r.height||300));
|
||
dpr=Math.min((window.devicePixelRatio||1),2);
|
||
cv.width=Math.round(w*dpr); cv.height=Math.round(h*dpr);
|
||
cv.style.width=w+'px'; cv.style.height=h+'px';
|
||
W=w; H=h; cx=w/2; cy=h/2; R=Math.min(w,h)/2-14; if(R<30) R=30;
|
||
gAccel=0.085*(R/250);
|
||
for(var i=0;i<planets.length;i++) clampInside(planets[i]);
|
||
}
|
||
|
||
// ---------- pointer (glisser = lancer) ----------
|
||
function relPos(e){ var r=cv.getBoundingClientRect(); return { x:e.clientX-r.left, y:e.clientY-r.top }; }
|
||
function onDown(e){ var pt=relPos(e); var idx=pick(pt.x,pt.y);
|
||
if(idx>=0){ dragIndex=idx; dragPointerId=e.pointerId; lastPt=pt;
|
||
planets[idx].vx=0; planets[idx].vy=0; cv.style.cursor='grabbing';
|
||
try{ cv.setPointerCapture(e.pointerId); }catch(_){} e.preventDefault(); } }
|
||
function onMove(e){ var pt=relPos(e);
|
||
if(dragIndex>=0){ var p=planets[dragIndex]; if(!p){ dragIndex=-1; return; }
|
||
var ndx=pt.x-lastPt.x, ndy=pt.y-lastPt.y;
|
||
p.x=pt.x; p.y=pt.y; clampInside(p);
|
||
p.vx=Math.max(-MAXTHROW,Math.min(MAXTHROW,ndx));
|
||
p.vy=Math.max(-MAXTHROW,Math.min(MAXTHROW,ndy));
|
||
lastPt=pt; e.preventDefault();
|
||
} else { hoverIndex=pick(pt.x,pt.y); cv.style.cursor=hoverIndex>=0?'grab':'default'; } }
|
||
function onUp(e){ if(dragIndex>=0){ dragIndex=-1; cv.style.cursor='default';
|
||
try{ cv.releasePointerCapture(e.pointerId); }catch(_){} } }
|
||
function onLeave(){ if(dragIndex<0) hoverIndex=-1; }
|
||
|
||
cv.addEventListener('pointerdown', onDown);
|
||
cv.addEventListener('pointermove', onMove);
|
||
cv.addEventListener('pointerup', onUp);
|
||
cv.addEventListener('pointercancel', onUp);
|
||
cv.addEventListener('pointerleave', onLeave);
|
||
|
||
// ---------- contrôles ----------
|
||
function add(){ var v=inEl.value.trim(); inEl.value='';
|
||
if(v){ saved.push(v); saveNames(); spawn(v); renderChips(); }
|
||
else { spawn(''); }
|
||
updateCount();
|
||
try{ if(window.jOSGame && window.jOSGame.event) window.jOSGame.event('spawn'); }catch(_){} }
|
||
function shake(){ for(var i=0;i<planets.length;i++){ if(i===dragIndex) continue;
|
||
planets[i].vx+=(Math.random()-0.5)*13; planets[i].vy+=(Math.random()-0.5)*13; } }
|
||
function clearBowl(){ planets.length=0; dragIndex=-1; hoverIndex=-1; updateCount(); }
|
||
|
||
root.querySelector('[data-add]').addEventListener('click', add);
|
||
root.querySelector('[data-shake]').addEventListener('click', shake);
|
||
root.querySelector('[data-clear]').addEventListener('click', clearBowl);
|
||
inEl.addEventListener('keydown', function(e){ if(e.key==='Enter'){ e.preventDefault(); add(); } });
|
||
|
||
// ---------- chips (noms sauvegardés) ----------
|
||
function renderChips(){
|
||
if(!saved.length){ chipsEl.innerHTML='<span class="pbw-empty">aucun nom sauvegardé — tape un nom ci-dessus</span>'; return; }
|
||
var html='';
|
||
for(var i=0;i<saved.length;i++){
|
||
html+='<span class="pbw-chip" data-ci="'+i+'" title="relâcher dans le bol"><b>'+esc(saved[i])+'</b><i data-rm="'+i+'" title="retirer">×</i></span>';
|
||
}
|
||
chipsEl.innerHTML=html;
|
||
}
|
||
chipsEl.addEventListener('click', function(e){
|
||
var rm=e.target.closest ? e.target.closest('[data-rm]') : null;
|
||
if(rm){ var ri=parseInt(rm.getAttribute('data-rm'),10);
|
||
if(!isNaN(ri) && ri>=0 && ri<saved.length){ saved.splice(ri,1); saveNames(); renderChips(); } return; }
|
||
var chip=e.target.closest ? e.target.closest('[data-ci]') : null;
|
||
if(chip){ var ci=parseInt(chip.getAttribute('data-ci'),10);
|
||
if(!isNaN(ci) && saved[ci]!=null){ spawn(saved[ci]); updateCount(); } }
|
||
});
|
||
|
||
// ---------- boot ----------
|
||
resize();
|
||
saved.forEach(function(nm){ spawn(nm); });
|
||
updateCount();
|
||
renderChips();
|
||
|
||
var ro=null;
|
||
try{ ro=new ResizeObserver(function(){ if(disposed) return; resize(); }); ro.observe(stage); }
|
||
catch(_){ window.addEventListener('resize', resize); }
|
||
|
||
raf=requestAnimationFrame(frame);
|
||
try{ if(window.jOSGame && window.jOSGame.bion) window.jOSGame.bion('planete-bowl'); }catch(_){}
|
||
|
||
LIVE={ planets:function(){return planets;} };
|
||
return { unmount:function(){
|
||
LIVE=null;
|
||
disposed=true;
|
||
if(raf) cancelAnimationFrame(raf);
|
||
try{ if(ro) ro.disconnect(); }catch(_){}
|
||
try{ window.removeEventListener('resize', resize); }catch(_){}
|
||
try{
|
||
cv.removeEventListener('pointerdown', onDown); cv.removeEventListener('pointermove', onMove);
|
||
cv.removeEventListener('pointerup', onUp); cv.removeEventListener('pointercancel', onUp);
|
||
cv.removeEventListener('pointerleave', onLeave);
|
||
}catch(_){}
|
||
container.innerHTML='';
|
||
} };
|
||
}
|
||
|
||
window.jOSApps=window.jOSApps||{};
|
||
window.jOSApps['planete-bowl']={ manifest:{ id:'planete-bowl', name:'Planète bowl', icon:'🪐' }, mount:mount, describe:describe };
|
||
})();
|
||
</script>
|