Back to the app
The code
Cat Island by Claude Sonnet 5.5
Exactly as the AI wrote it. Shown as text, so nothing runs on this page.
index.html
works · auto-checked (Checked by an automated pass. Not hand-played yet.)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Cat Island</title>
<style>
*{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
html,body{margin:0;height:100%;overflow:hidden;background:#4d9ae6;touch-action:none;user-select:none;-webkit-user-select:none;-webkit-touch-callout:none;font-family:ui-monospace,Menlo,Consolas,"Courier New",monospace;color:#fff}
#c{position:fixed;left:0;top:0;width:100%;height:100%;display:block;image-rendering:crisp-edges;image-rendering:pixelated;touch-action:none;cursor:grab}
.px{background:#1d1b3a;border:3px solid #0d0c20;box-shadow:inset 0 0 0 2px #4a4690,0 4px 0 #0d0c20;text-shadow:2px 2px 0 #0d0c20}
#top{position:fixed;left:12px;top:12px;right:12px;display:flex;flex-direction:column;gap:10px;align-items:flex-start;pointer-events:none}
#title{padding:8px 12px;font-weight:900;letter-spacing:2px;font-size:16px}
#hint{padding:7px 10px;font-size:12px;line-height:1.4;max-width:min(92vw,330px);transition:opacity 1.2s}
#follow{pointer-events:auto;padding:6px 6px 6px 12px;display:none;align-items:center;gap:10px;font-size:13px;max-width:92vw}
#follow b{color:#ffd84a}
#demo{display:none;padding:7px 12px;font-size:13px;color:#ffd84a}
button{font:inherit;color:#fff;cursor:pointer;border:3px solid #0d0c20;background:#3a3680;box-shadow:inset 0 3px 0 #6a66c8,0 4px 0 #0d0c20;padding:0 14px;min-height:56px;min-width:92px;display:flex;align-items:center;justify-content:center;gap:8px;font-weight:900;font-size:16px;letter-spacing:1px;text-shadow:2px 2px 0 #0d0c20;touch-action:manipulation}
button:active{transform:translateY(3px);box-shadow:inset 0 3px 0 #6a66c8,0 1px 0 #0d0c20}
button svg{width:24px;height:24px;fill:currentColor;flex:none}
#stop{min-height:44px;min-width:70px;background:#b83a3a;box-shadow:inset 0 3px 0 #ff7a7a,0 4px 0 #0d0c20;font-size:13px}
#bar{position:fixed;left:0;right:0;bottom:max(14px,env(safe-area-inset-bottom));display:flex;justify-content:center;gap:10px;padding:0 10px;pointer-events:none}
#bar button{pointer-events:auto}
#bar button.on{color:#2a1a00;text-shadow:none}
#bar button[data-m=day].on{background:#ffd84a;box-shadow:inset 0 3px 0 #fff3a0,0 4px 0 #0d0c20}
#bar button[data-m=sunset].on{background:#ff8a4a;box-shadow:inset 0 3px 0 #ffc08a,0 4px 0 #0d0c20}
#bar button[data-m=night].on{background:#9a9aff;box-shadow:inset 0 3px 0 #d0d0ff,0 4px 0 #0d0c20}
@media(max-width:420px){#bar button{min-width:0;flex:1;padding:0 6px;font-size:14px}#bar{gap:6px}}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="top">
<div id="title" class="px">CAT ISLAND</div>
<div id="hint" class="px">Drag to spin the island. Pinch or scroll to zoom. Tap a cat to follow it.</div>
<div id="follow" class="px"><span id="ftxt"></span><button id="stop">STOP</button></div>
<div id="demo" class="px">DEMO - touch anywhere to play</div>
</div>
<div id="bar">
<button data-m="day" class="on"><svg viewBox="0 0 8 8" shape-rendering="crispEdges"><path d="M3 0h2v1H3zM3 7h2v1H3zM0 3h1v2H0zM7 3h1v2H7zM2 2h4v4H2z"/></svg>Day</button>
<button data-m="sunset"><svg viewBox="0 0 8 8" shape-rendering="crispEdges"><path d="M3 1h2v1H3zM3 3h2v1H3zM2 4h4v1H2zM1 5h6v1H1zM0 7h8v1H0z"/></svg>Sunset</button>
<button data-m="night"><svg viewBox="0 0 8 8" shape-rendering="crispEdges"><path d="M3 0h3v1H3zM2 1h2v1H2zM1 2h2v4H1zM2 6h2v1H2zM3 7h3v1H3zM5 5h2v2H5z"/></svg>Night</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
(function(){
'use strict';
function $(s){return document.querySelector(s);}
function fail(m){document.body.insertAdjacentHTML('beforeend','<div class="px" style="position:fixed;left:50%;top:40%;transform:translate(-50%,-50%);padding:18px;max-width:80vw">'+m+'</div>');}
if(typeof THREE==='undefined'){fail('Could not load three.js.');return;}
const T=THREE;
const clamp=(v,a,b)=>Math.max(a,Math.min(b,v));
const rr=(a,b)=>a+(b-a)*Math.random();
const pick=a=>a[Math.floor(Math.random()*a.length)];
let seed=20261122;
const rnd=()=>{seed=(seed*16807)%2147483647;return (seed-1)/2147483646;};
const hash=(i,j)=>{const s=Math.sin(i*127.1+j*311.7)*43758.5453;return s-Math.floor(s);};
const shade=(c,f)=>new T.Color(c).multiplyScalar(f);
// ---------- renderer (low resolution = chunky pixels) ----------
const canvas=$('#c');
let renderer;
try{renderer=new T.WebGLRenderer({canvas:canvas,antialias:false,powerPreference:'high-performance'});}
catch(e){fail('WebGL is not available in this browser.');return;}
renderer.setPixelRatio(1);
const scene=new T.Scene();
const camera=new T.PerspectiveCamera(36,1,0.5,1200);
scene.add(camera);
let PIX=3;
function fitDist(){const a=innerWidth/Math.max(1,innerHeight);return Math.max(38,33/a);}
function resize(){
const w=Math.max(2,innerWidth),h=Math.max(2,innerHeight);
PIX=Math.max(2,Math.round(Math.min(w,h)/260));
renderer.setSize(Math.max(2,Math.floor(w/PIX)),Math.max(2,Math.floor(h/PIX)),false);
camera.aspect=w/h;camera.updateProjectionMatrix();
sunSpr.position.set(0.55*97*camera.aspect,63,-300);moonSpr.position.set(-0.55*97*camera.aspect,63,-300);
if(!zoomed)cam.distT=fitDist();
}
// ---------- helpers ----------
const mats={};
const lam=c=>mats[c]||(mats[c]=new T.MeshLambertMaterial({color:c}));
const UB=new T.BoxGeometry(1,1,1);
function part(p,x,y,z,w,h,d,c){const m=new T.Mesh(UB,lam(c));m.position.set(x,y,z);m.scale.set(w,h,d);p.add(m);return m;}
function ctex(w,h,draw,nearest){const cv=document.createElement('canvas');cv.width=w;cv.height=h;draw(cv.getContext('2d'),w,h);const t=new T.CanvasTexture(cv);if(nearest!==false){t.magFilter=T.NearestFilter;t.minFilter=T.NearestFilter;}return t;}
const FACES=[
[[1,0,0],[[.5,-.5,.5],[.5,-.5,-.5],[.5,.5,-.5],[.5,.5,.5]]],
[[-1,0,0],[[-.5,-.5,-.5],[-.5,-.5,.5],[-.5,.5,.5],[-.5,.5,-.5]]],
[[0,1,0],[[-.5,.5,.5],[.5,.5,.5],[.5,.5,-.5],[-.5,.5,-.5]]],
[[0,-1,0],[[-.5,-.5,-.5],[.5,-.5,-.5],[.5,-.5,.5],[-.5,-.5,.5]]],
[[0,0,1],[[-.5,-.5,.5],[.5,-.5,.5],[.5,.5,.5],[-.5,.5,.5]]],
[[0,0,-1],[[.5,-.5,-.5],[-.5,-.5,-.5],[-.5,.5,-.5],[.5,.5,-.5]]]
];
class VB{
constructor(){this.p=[];this.n=[];this.c=[];this.i=[];this.v=0;}
box(x,y,z,w,h,d,col,skip,top){
const c=new T.Color(col),ct=top!==undefined?new T.Color(top):c;skip=skip||0;
for(let f=0;f<6;f++){
if((skip>>f)&1)continue;
const n=FACES[f][0],q=FACES[f][1],cc=f===2?ct:c;
for(let k=0;k<4;k++){
this.p.push(x+q[k][0]*w,y+q[k][1]*h,z+q[k][2]*d);
this.n.push(n[0],n[1],n[2]);
this.c.push(cc.r,cc.g,cc.b);
}
const b=this.v;this.i.push(b,b+1,b+2,b,b+2,b+3);this.v+=4;
}
return this;
}
mesh(mat){
const g=new T.BufferGeometry();
g.setAttribute('position',new T.Float32BufferAttribute(this.p,3));
g.setAttribute('normal',new T.Float32BufferAttribute(this.n,3));
g.setAttribute('color',new T.Float32BufferAttribute(this.c,3));
g.setIndex(this.i);g.computeBoundingSphere();
return new T.Mesh(g,mat);
}
}
// ---------- lights and sky ----------
const hemi=new T.HemisphereLight(0xffffff,0x888888,0.7);scene.add(hemi);
const sunL=new T.DirectionalLight(0xffffff,0.8);scene.add(sunL);
const skyMat=new T.ShaderMaterial({side:T.BackSide,depthWrite:false,
uniforms:{cLow:{value:new T.Color()},cHor:{value:new T.Color()},cTop:{value:new T.Color()}},
vertexShader:'varying vec3 vP;void main(){vP=normalize(position);gl_Position=projectionMatrix*modelViewMatrix*vec4(position,1.0);}',
fragmentShader:'uniform vec3 cLow;uniform vec3 cHor;uniform vec3 cTop;varying vec3 vP;void main(){float y=vP.y;float s=y<0.0?smoothstep(-0.85,0.0,y)*0.5:0.5+smoothstep(0.0,0.7,y)*0.5;s=floor(s*12.0)/12.0;vec3 c=s<0.5?mix(cLow,cHor,s*2.0):mix(cHor,cTop,(s-0.5)*2.0);gl_FragColor=vec4(c,1.0);}'});
const sky=new T.Mesh(new T.SphereGeometry(500,16,12),skyMat);sky.renderOrder=-100;sky.frustumCulled=false;scene.add(sky);
const starPos=[];for(let i=0;i<260;i++){const u=Math.random()*2-1,a=Math.random()*6.283,s=Math.sqrt(1-u*u);starPos.push(Math.cos(a)*s*420,u*420,Math.sin(a)*s*420);}
const starG=new T.BufferGeometry();starG.setAttribute('position',new T.Float32BufferAttribute(starPos,3));
const stars=new T.Points(starG,new T.PointsMaterial({color:0xffffff,size:3,sizeAttenuation:false,transparent:true,opacity:0,depthWrite:false}));stars.frustumCulled=false;scene.add(stars);
function discTex(col,rim,craters){return ctex(16,16,(x)=>{for(let j=0;j<16;j++)for(let i=0;i<16;i++){const d=Math.hypot(i-7.5,j-7.5);if(d<7.6){x.fillStyle=d>6.3?rim:col;x.fillRect(i,j,1,1);}}if(craters){x.fillStyle='rgba(120,130,170,.55)';x.fillRect(5,4,2,2);x.fillRect(9,8,3,2);x.fillRect(4,10,2,1);}});}
const sunSpr=new T.Sprite(new T.SpriteMaterial({map:discTex('#fff0a0','#ffb84a'),depthWrite:false,transparent:true,fog:false}));
const moonSpr=new T.Sprite(new T.SpriteMaterial({map:discTex('#f0f2ff','#b8c0f0',true),depthWrite:false,transparent:true,fog:false}));
camera.add(sunSpr);camera.add(moonSpr);
const ENV={
day:{low:'#4d9ae6',hor:'#bfe6ff',top:'#3a80dc',sky:'#e4f2ff',gnd:'#a8b890',hi:0.62,sun:'#fff0d0',si:0.72,sd:[0.5,1,0.6],cloud:'#ffffff',win:'#6fa8d8',bulb:'#e8d8a0',glow:0,star:0,sunA:1,sunY:63,sunS:34,sunC:'#ffffff'},
sunset:{low:'#6a3f8e',hor:'#ffa86e',top:'#4a3a8a',sky:'#ffc4a0',gnd:'#7a6070',hi:0.5,sun:'#ff9a55',si:0.85,sd:[0.95,0.32,0.35],cloud:'#ffa89a',win:'#ffb060',bulb:'#ffc070',glow:0.45,star:0.15,sunA:1,sunY:28,sunS:52,sunC:'#ff9a6a'},
night:{low:'#060a22',hor:'#1b2766',top:'#03051a',sky:'#8a9ce8',gnd:'#252d55',hi:0.42,sun:'#9fb3ff',si:0.34,sd:[-0.4,0.9,0.5],cloud:'#4a5890',win:'#ffe07a',bulb:'#ffd040',glow:1,star:1,sunA:0,sunY:63,sunS:34,sunC:'#ffffff'}
};
const col=c=>new T.Color(c);
function mkE(e){return {low:col(e.low),hor:col(e.hor),top:col(e.top),sky:col(e.sky),gnd:col(e.gnd),sun:col(e.sun),cloud:col(e.cloud),win:col(e.win),bulb:col(e.bulb),sunC:col(e.sunC),sd:new T.Vector3(e.sd[0],e.sd[1],e.sd[2]).normalize(),hi:e.hi,si:e.si,glow:e.glow,star:e.star,sunA:e.sunA,sunY:e.sunY,sunS:e.sunS};}
const E=mkE(ENV.day),TG={day:mkE(ENV.day),sunset:mkE(ENV.sunset),night:mkE(ENV.night)};
let mode='day';
// ---------- static world ----------
const S=new VB(),G=new VB(),L=new VB();
const rects=[],circs=[],roofs=[],glows=[],balls=[],spots=[];
const POND={x:-5,z:5,a:2.4,b:3.1},BR={x:-5,z:5,h:3.5};
const inPond=(x,z)=>Math.pow((x-POND.x)/POND.a,2)+Math.pow((z-POND.z)/POND.b,2)<1;
const bh=x=>{const u=(x-BR.x)/BR.h;return 0.16+0.52*(1-u*u);};
const edgeR=t=>13.2+0.9*Math.sin(3*t+1)+0.6*Math.sin(5*t+2)+0.35*Math.sin(8*t+0.5);
function isPath(i,j){
if(Math.hypot(i,j+0.5)<3.2)return true;
if(j===-4&&i>=-10&&i<=-1)return true;
if(j===0&&i>=3&&i<=6)return true;
if(i===0&&j>=3&&j<=6)return true;
if(j===5&&(i<=-8||i>=-2)&&i>=-11&&i<=-1)return true;
return false;
}
const OFF=16,D=[],P=[];
let pMin=[99,99],pMax=[-99,-99];
for(let i=-16;i<=16;i++){D[i+OFF]=[];P[i+OFF]=[];for(let j=-16;j<=16;j++){
const r=Math.hypot(i,j),e=edgeR(Math.atan2(j,i));
const d=r<e?2+Math.floor((e-r)*0.72+hash(i,j)*1.6):0;
D[i+OFF][j+OFF]=d;const pp=d>0&&inPond(i,j);P[i+OFF][j+OFF]=pp;
if(pp){pMin=[Math.min(pMin[0],i),Math.min(pMin[1],j)];pMax=[Math.max(pMax[0],i),Math.max(pMax[1],j)];}
}}
function solid(i,k,j){if(k<0||i<-16||i>16||j<-16||j>16)return false;if(k>=D[i+OFF][j+OFF])return false;if(P[i+OFF][j+OFF]&&k<2)return false;return true;}
const GR=['#5ec24a','#66cb52','#58b845','#6ed05a'],CB=['#bdb094','#aa9e86','#cabda3'];
for(let i=-16;i<=16;i++)for(let j=-16;j<=16;j++){
const d=D[i+OFF][j+OFF];if(!d)continue;
for(let k=0;k<d;k++){
if(!solid(i,k,j))continue;
let sk=0;
if(solid(i+1,k,j))sk|=1;if(solid(i-1,k,j))sk|=2;if(solid(i,k-1,j))sk|=4;
if(solid(i,k+1,j))sk|=8;if(solid(i,k,j+1))sk|=16;if(solid(i,k,j-1))sk|=32;
if(sk===63)continue;
const h=hash(i+k*7,j-k*3);let c,top;
if(P[i+OFF][j+OFF]&&k===2){c='#c9b27a';top='#e4d29a';}
else if(k===0){top=isPath(i,j)?CB[Math.floor(h*3)]:GR[Math.floor(h*4)];c=shade('#8a6038',0.9+0.2*h);}
else if(k<3)c=shade('#7c5733',0.88+0.24*h);
else c=shade(k%2?'#8a8a96':'#7a7a88',0.85+0.3*h);
S.box(i,-k-0.5,j,1,1,1,c,sk,top);
}
}
// houses
function win(x,y,z,face,sx,sy,side){
if(side){S.box(x+face*0.02,y,z,0.05,sy+0.14,sx+0.14,'#f2ead8');G.box(x+face*0.045,y,z,0.04,sy,sx,'#ffffff');}
else{S.box(x,y,z+face*0.02,sx+0.14,sy+0.14,0.05,'#f2ead8');G.box(x,y,z+face*0.045,sx,sy,0.04,'#ffffff');}
}
function tiles(cx,y,cz,w,d,hh,a,b){const n=Math.max(2,Math.round(d/0.4));for(let r=0;r<n;r++)S.box(cx,y,cz-d/2+(r+0.5)*d/n,w,hh,d/n,r%2?a:b);}
function house(cx,cz,w,d,h,face,wall,ra,rb,big){
const fz=cz+face*d/2,wy=0.4+(h-0.4)*0.6;
S.box(cx,0.2,cz,w+0.16,0.4,d+0.16,'#8e8e9a');
S.box(cx,(h+0.4)/2,cz,w,h-0.4,d,wall);
for(const sx of[-1,1])for(const sz of[-1,1])S.box(cx+sx*w/2,(h+0.4)/2,cz+sz*d/2,0.18,h-0.4,0.18,'#6a4222');
S.box(cx,0.95,fz+face*0.03,0.62,1.1,0.08,'#6a3d1f');S.box(cx+0.18,0.95,fz+face*0.08,0.07,0.07,0.04,'#ffd24a');S.box(cx,0.1,fz+face*0.35,0.9,0.2,0.5,'#9a9aa6');
const wxs=big?[-1.5,-0.8,0.8,1.5]:[-0.85,0.85];
for(const x of wxs){
win(cx+x,wy,fz,face,0.5,0.55,false);
S.box(cx+x,wy-0.4,fz+face*0.12,0.7,0.12,0.2,'#8a5a34');
for(let f=-1;f<=1;f++)S.box(cx+x+f*0.2,wy-0.3,fz+face*0.12,0.1,0.14,0.1,pick(['#ff6f91','#ffd24a','#ffffff']));
}
for(const x of(big?[-1.2,0,1.2]:[-0.65,0.65]))win(cx+x,wy,cz-face*d/2,-face,0.5,0.55,false);
for(const sx of[-1,1])win(cx+sx*w/2,wy,cz,sx,0.5,0.55,true);
S.box(cx,h+0.15,cz,w+0.6,0.3,d+0.6,ra);
tiles(cx,h+0.45,cz,w-0.2,d-0.2,0.3,ra,rb);
S.box(cx+w/2-0.55,h+1.1,cz-face*(d/2-0.55),0.42,1.0,0.42,'#a8574a');S.box(cx+w/2-0.55,h+1.62,cz-face*(d/2-0.55),0.52,0.1,0.52,'#6d6d78');
const gx=cx,gz=fz+face*1.1;
if(big){roofs.push({x:cx-1.4,y:h+0.6,z:cz+face*0.3,gx:gx-1.2,gz:gz,occ:null},{x:cx+1.4,y:h+0.6,z:cz+face*0.3,gx:gx+1.2,gz:gz,occ:null});}
else roofs.push({x:cx-w*0.1,y:h+0.6,z:cz+face*0.15,gx:gx,gz:gz,occ:null});
rects.push([cx-w/2-0.45,cx+w/2+0.45,cz-d/2-0.45,cz+d/2+0.45]);
}
house(-9,-1,2.6,2.6,2.6,1,'#f0d9a8','#c8553d','#b04632');
house(-0.4,8.4,2.8,2.4,2.8,-1,'#bfd8f0','#5a7fc0','#4a6cae');
house(5.4,7.4,2.6,2.4,2.4,-1,'#f4c6c6','#7a5aa8','#6a4a98');
house(2.2,4.2,2.6,2.4,2.4,-1,'#d6ecc0','#c8973d','#b5832f');
house(4.2,-7,4.4,3.2,3,1,'#f2e6c8','#3d8a7a','#2f7566',true);
// town hall tower + flag
S.box(4.2,4.4,-7.9,1.3,1.8,1.3,'#e8dcc0');S.box(4.2,5.4,-7.9,1.5,0.2,1.5,'#3d8a7a');S.box(4.2,5.7,-7.9,1.0,0.4,1.0,'#2f7566');S.box(4.2,6.1,-7.9,0.5,0.4,0.5,'#2f7566');
S.box(4.2,6.8,-7.9,0.08,1.0,0.08,'#6a4222');S.box(4.5,7.1,-7.9,0.6,0.35,0.05,'#e04a4a');S.box(4.5,7.1,-7.88,0.2,0.2,0.05,'#ffd24a');
G.box(4.2,4.5,-7.22,0.5,0.5,0.04,'#ffffff');S.box(4.2,4.5,-7.25,0.64,0.64,0.04,'#f2ead8');
// fish market
function fish(x,y,z,c){S.box(x,y,z,0.3,0.1,0.14,c);S.box(x+0.19,y,z,0.09,0.15,0.05,shade(c,0.8));S.box(x-0.1,y+0.03,z+0.07,0.03,0.03,0.02,'#111');}
function stall(cx,cz,c1,c2){
const W=2.6;
S.box(cx,1.0,cz-0.95,W,2.0,0.12,'#7a5230');
S.box(cx,0.45,cz+0.3,W-0.2,0.9,0.7,'#9b6a3c');S.box(cx,0.95,cz+0.3,W,0.1,0.86,'#c99a62');
for(const sx of[-1,1]){S.box(cx+sx*(W/2-0.08),0.95,cz+1.15,0.14,1.9,0.14,'#6a4222');S.box(cx+sx*(W/2-0.08),1.15,cz-0.95,0.14,2.3,0.14,'#6a4222');
L.box(cx+sx*(W/2-0.08),1.5,cz+1.3,0.2,0.26,0.2,'#ffffff');glows.push([cx+sx*(W/2-0.08),1.5,cz+1.3]);}
for(let s=0;s<5;s++){const y=2.35-s*0.16,z=cz-0.7+s*0.5;for(let j=0;j<7;j++)S.box(cx-W/2+(j+0.5)*W/7,y,z,W/7,0.12,0.5,j%2?c2:c1);}
for(let j=0;j<7;j++){const h=j%2?0.26:0.36;S.box(cx-W/2+(j+0.5)*W/7,1.65-h/2,cz+1.56,W/7,h,0.08,j%2?c2:c1);}
S.box(cx,1.08,cz+0.3,1.1,0.16,0.62,'#cfe6f2');
const fc=['#b9d3e6','#5d8fc4','#e0604a','#f2cf5b','#c9d8e2'];
for(let k=0;k<6;k++)fish(cx-0.4+(k%3)*0.4,1.19,cz+0.12+Math.floor(k/3)*0.3,fc[k%5]);
fish(cx+0.95,1.04,cz+0.3,'#8fb7d6');fish(cx-0.95,1.04,cz+0.45,'#e0604a');
S.box(cx-0.8,0.4,cz-0.5,0.55,0.8,0.55,'#8a5a34');S.box(cx-0.8,0.25,cz-0.5,0.6,0.08,0.6,'#4a4a55');S.box(cx+0.8,0.3,cz-0.5,0.6,0.6,0.6,'#a67c4a');
S.box(cx,2.75,cz-0.95,1.5,0.5,0.1,'#2e5f9e');fish(cx,2.75,cz-0.88,'#ffffff');
for(let k=0;k<3;k++){S.box(cx-0.7+k*0.7,1.45,cz+1.5,0.08,0.22,0.05,'#b9d3e6');}
rects.push([cx-1.6,cx+1.6,cz-1.3,cz+1.0]);
spots.push({x:cx,z:cz+1.85});
}
stall(-7.4,-6,'#e04a4a','#ffffff');stall(-4.2,-6,'#2f78d0','#ffffff');stall(-1,-6,'#f2b630','#ffffff');
// pond, bridge
let water;
{
const wx=pMax[0]-pMin[0]+1,wz=pMax[1]-pMin[1]+1;
const wt=ctex(16,16,(x,w,h)=>{x.fillStyle='#3b9be0';x.fillRect(0,0,w,h);x.fillStyle='#6cc0f5';for(let k=0;k<9;k++)x.fillRect(Math.floor(Math.random()*13),Math.floor(Math.random()*16),3,1);x.fillStyle='#2b86cc';for(let k=0;k<6;k++)x.fillRect(Math.floor(Math.random()*13),Math.floor(Math.random()*16),2,1);});
wt.wrapS=wt.wrapT=T.RepeatWrapping;wt.repeat.set(wx/2,wz/2);
water=new T.Mesh(new T.PlaneGeometry(wx,wz),new T.MeshLambertMaterial({map:wt,emissive:0x0a2a50}));
water.rotation.x=-Math.PI/2;water.position.set((pMin[0]+pMax[0])/2,-0.4,(pMin[1]+pMax[1])/2);scene.add(water);water.userData.t=wt;
for(const lp of[[-5,4],[-6,6.5],[-4,6],[-5.5,3.2]]){S.box(lp[0],-0.37,lp[1],0.55,0.06,0.55,'#3f9b3a');S.box(lp[0]+0.1,-0.3,lp[1]+0.05,0.16,0.1,0.16,pick(['#ff8fc4','#ffffff']));}
for(let k=0;k<14;k++){const x=BR.x-BR.h+0.25+k*0.5,h=bh(x);
S.box(x,h-0.06,BR.z,0.5,0.12,1.6,k%2?'#a8743f':'#946334');
for(const sz of[-1,1]){S.box(x,h-0.2,BR.z+sz*0.7,0.5,0.16,0.14,'#6a4222');S.box(x,h+0.5,BR.z+sz*0.75,0.5,0.08,0.1,'#6a4222');if(k%2===0)S.box(x,h+0.22,BR.z+sz*0.75,0.14,0.5,0.14,'#6a4222');}}
for(const sx of[-2,2])for(const sz of[-1,1])S.box(BR.x+sx,-0.75,BR.z+sz*0.7,0.22,2.5,0.22,'#5a3a1c');
}
// trees, lamps, bench, flowers
function tree(x,z,pink,sc){
sc=sc||1;const g=pink?['#f4a6c8','#f08ab4','#f9c4da']:['#3f9b3a','#4cae44','#5fc455'];
S.box(x,0.9*sc,z,0.5,1.8*sc,0.5,'#7a5230');
S.box(x,2.2*sc,z,2.0*sc,1.0,2.0*sc,g[0]);S.box(x,3.0*sc,z,1.5*sc,0.9,1.5*sc,g[1]);S.box(x,3.7*sc,z,0.9*sc,0.6,0.9*sc,g[2]);
circs.push([x,z,0.9*sc]);
}
tree(10.4,-3.2,true);tree(6.2,5.2,false);tree(9.4,4.8,true,1.1);tree(0.5,-9.2,false,1.1);tree(-3.2,-9,true);tree(-10.2,3.6,false);tree(8.6,-5,false,0.9);
function lamp(x,z){
S.box(x,0.1,z,0.34,0.2,0.34,'#4a4a55');S.box(x,1.0,z,0.12,1.8,0.12,'#33333c');L.box(x,2.1,z,0.34,0.4,0.34,'#ffffff');S.box(x,2.38,z,0.46,0.1,0.46,'#33333c');
glows.push([x,2.1,z]);circs.push([x,z,0.35]);
}
[[2.8,-2.8],[-2.8,1.8],[-3.4,-3.3],[5.8,0.6],[-8.4,6.3],[-1.6,3.8],[2.6,2.4],[10.2,2.2]].forEach(l=>lamp(l[0],l[1]));
S.box(7.6,0.5,3.9,1.6,0.12,0.5,'#8a5a34');S.box(7.6,0.85,3.7,1.6,0.5,0.1,'#8a5a34');for(const sx of[-0.7,0.7])S.box(7.6+sx,0.25,3.9,0.12,0.5,0.4,'#4a3018');
rects.push([6.7,8.5,3.4,4.4]);
// yarn
const yarnDef=[[7.2,-1.4,1.5,'#ff7aa8'],[9,2.6,1.15,'#3fc8c0'],[6.4,2.2,0.95,'#ffd24a'],[10.2,-0.2,1.3,'#9a6cf0']];
yarnDef.forEach(y=>{
const t=ctex(16,16,(x,w,h)=>{const b=new T.Color(y[3]);x.fillStyle=y[3];x.fillRect(0,0,w,h);for(let j=0;j<h;j++)for(let i=0;i<w;i++){if((i+j)%5===0){x.fillStyle='#'+b.clone().multiplyScalar(0.72).getHexString();x.fillRect(i,j,1,1);}else if((i-j+32)%7===0){x.fillStyle='#'+b.clone().lerp(new T.Color(1,1,1),0.4).getHexString();x.fillRect(i,j,1,1);}}});
t.wrapS=t.wrapT=T.RepeatWrapping;t.repeat.set(2,1);
const m=new T.Mesh(new T.SphereGeometry(y[2],10,8),new T.MeshPhongMaterial({map:t,flatShading:true,shininess:4,specular:0x222222}));
m.position.set(y[0],y[2]-0.05,y[1]);scene.add(m);balls.push({x:y[0],z:y[1],r:y[2],m:m,wob:0,y:y[2]-0.05});circs.push([y[0],y[1],y[2]+0.35]);
const a=rr(0,6.28);for(let k=0;k<10;k++){const d=y[2]+0.15+k*0.22,an=a+Math.sin(k*0.7)*0.5;S.box(y[0]+Math.cos(an)*d,0.07,y[1]+Math.sin(an)*d,0.13,0.13,0.13,y[3]);}
});
for(let n=0;n<70;n++){
const a=rnd()*6.283,r=Math.sqrt(rnd())*(n<45?4.5:10.5),x=(n<45?8:0)+Math.cos(a)*r,z=(n<45?1.5:0)+Math.sin(a)*r;
if(!free(x,z)||isPath(Math.round(x),Math.round(z)))continue;
S.box(x,0.12,z,0.1,0.24,0.1,'#3f9b3a');S.box(x,0.3,z,0.2,0.14,0.2,['#ff6f91','#ffd24a','#ffffff','#b48cff','#ff9a3c'][Math.floor(rnd()*5)]);
}
const solidMat=new T.MeshLambertMaterial({vertexColors:true});
const glassMat=new T.MeshBasicMaterial({vertexColors:true}),bulbMat=new T.MeshBasicMaterial({vertexColors:true});
scene.add(S.mesh(solidMat));scene.add(G.mesh(glassMat));scene.add(L.mesh(bulbMat));
function free(x,z){
if(Math.hypot(x,z)>edgeR(Math.atan2(z,x))-1.0)return false;
if(Math.pow((x-POND.x)/(POND.a+0.9),2)+Math.pow((z-POND.z)/(POND.b+0.9),2)<1){if(!(Math.abs(z-BR.z)<0.55&&Math.abs(x-BR.x)<BR.h))return false;}
for(const r of rects)if(x>r[0]&&x<r[1]&&z>r[2]&&z<r[3])return false;
for(const c of circs)if(Math.hypot(x-c[0],z-c[1])<c[2])return false;
return true;
}
function gy(x,z){return(Math.abs(z-BR.z)<0.8&&Math.abs(x-BR.x)<BR.h)?bh(x)+0.08:0;}
function randFree(){for(let k=0;k<40;k++){const a=Math.random()*6.283,r=Math.sqrt(Math.random())*10;const x=Math.cos(a)*r,z=Math.sin(a)*r;if(free(x,z))return{x:x,z:z};}return{x:0,z:0};}
// glow sprites + point lights
const glowTex=ctex(32,32,(x,w,h)=>{const g=x.createRadialGradient(16,16,0,16,16,16);g.addColorStop(0,'rgba(255,255,255,1)');g.addColorStop(0.35,'rgba(255,255,255,.45)');g.addColorStop(1,'rgba(255,255,255,0)');x.fillStyle=g;x.fillRect(0,0,w,h);},false);
const glowSprites=glows.map(p=>{const s=new T.Sprite(new T.SpriteMaterial({map:glowTex,color:0xffc060,blending:T.AdditiveBlending,transparent:true,depthWrite:false,opacity:0}));s.position.set(p[0],p[1],p[2]);s.scale.set(3.4,3.4,1);scene.add(s);return s;});
const plights=[[-4.2,2.4,-3.2],[7,2.5,1],[-5,2.2,3.6],[0,2.4,0.5]].map(p=>{const l=new T.PointLight(0xffb060,0,12,1.2);l.position.set(p[0],p[1],p[2]);scene.add(l);return l;});
// clouds
const cloudMat=new T.MeshLambertMaterial({color:0xffffff,emissive:0x101828});
const clouds=[];
for(let n=0;n<20;n++){
const g=new T.Group(),cnt=5+Math.floor(Math.random()*5),w=rr(3,6);
for(let k=0;k<cnt;k++){const m=new T.Mesh(UB,cloudMat);const bw=rr(2,w),bh2=rr(1,2);m.scale.set(bw,bh2,rr(1.6,3.2));m.position.set(rr(-w,w),(k%3)*0.45,rr(-1.6,1.6));g.add(m);}
const a=Math.random()*6.283,r=n<6?rr(17,26):rr(12,62);
g.position.set(Math.cos(a)*r,n<6?rr(-9,-4):rr(-30,-13),Math.sin(a)*r);g.userData.v=rr(0.5,1.5);scene.add(g);clouds.push(g);
}
// butterflies and fireflies
const bflies=[];
['#ffe45c','#ffffff','#ff8fc4','#7fd0ff','#c08cff','#ffe45c','#ffffff'].forEach(c=>{
const g=new T.Group(),wl=new T.Group(),wrr=new T.Group();part(g,0,0,0,0.05,0.05,0.22,'#222');part(g,0,0,0.12,0.04,0.04,0.04,'#222');
wl.position.set(-0.03,0,0);wrr.position.set(0.03,0,0);part(wl,-0.11,0,0,0.22,0.02,0.18,c);part(wrr,0.11,0,0,0.22,0.02,0.18,c);g.add(wl,wrr);scene.add(g);
const a=rr(0,6.28);bflies.push({g:g,wl:wl,wr:wrr,x:8+Math.cos(a)*2,z:1+Math.sin(a)*2,y:1.6,tx:8,tz:1,ty:1.6,ph:Math.random()*6,flee:0,h:0});
});
const ffPos=new Float32Array(30*3),ffs=[];for(let i=0;i<30;i++)ffs.push({x:rr(-9,11),z:rr(-3,8),y:rr(0.6,2.6),p:Math.random()*6});
const ffG=new T.BufferGeometry();ffG.setAttribute('position',new T.BufferAttribute(ffPos,3));
const fireflies=new T.Points(ffG,new T.PointsMaterial({color:0xfff2a0,size:3,sizeAttenuation:false,transparent:true,opacity:0,depthWrite:false,blending:T.AdditiveBlending}));fireflies.frustumCulled=false;scene.add(fireflies);
// ---------- cats ----------
const CATS=[
{n:'Mayor Marmalade',c:'#f28c28',s:'#c8651a',m:'#fff1d6',e:'#2b8a3e',mayor:1,sc:1.35,st:1},
{n:'Pickles',c:'#8b919a',s:'#5f656e',m:'#d9dde2',e:'#e8d44d',st:1},
{n:'Salem',c:'#2a2a33',m:'#4a4a55',e:'#f2d03b'},
{n:'Mochi',c:'#f4f4f6',m:'#ffffff',e:'#3b82c4'},
{n:'Tofu',c:'#f1ede4',m:'#ffffff',e:'#2f2f3a',pt:[[-0.08,0.61,0.12,0.26,0.03,0.3,'#7a5230'],[0.1,0.61,-0.2,0.24,0.03,0.26,'#26262e']]},
{n:'Sushi',c:'#26262e',m:'#ffffff',e:'#78d070',pt:[[0,0.4,0.41,0.3,0.26,0.03,'#ffffff']]},
{n:'Miso',c:'#efe3cf',l:'#6b4a3a',t:'#6b4a3a',ear:'#6b4a3a',m:'#6b4a3a',e:'#4aa0e8'},
{n:'Luna',c:'#6f86a8',m:'#9db0cc',e:'#e6a23c'},
{n:'Noodle',c:'#b8a6c9',m:'#e4dbee',e:'#3f8a5a'},
{n:'Biscuit',c:'#9a6b3f',s:'#5b3a1f',m:'#d8b98a',e:'#e8d44d',st:1},
{n:'Bean',c:'#4a4e58',l:'#f2f2f2',m:'#f2f2f2',e:'#e8d44d'},
{n:'Clover',c:'#f6f6f2',m:'#ffffff',e:'#2f2f3a',pt:[[0.05,0.61,0.0,0.3,0.03,0.34,'#2a2a33'],[-0.24,0.45,-0.15,0.03,0.2,0.3,'#2a2a33']]}
];
const shadowGeo=new T.PlaneGeometry(0.9,1.2),shadowMat=new T.MeshBasicMaterial({color:0x000000,transparent:true,opacity:0.28,depthWrite:false});
const zTex=ctex(32,32,(x)=>{x.font='bold 26px monospace';x.textAlign='center';x.textBaseline='middle';x.lineWidth=5;x.strokeStyle='#2a3b8a';x.strokeText('Z',16,17);x.fillStyle='#ffffff';x.fillText('Z',16,17);});
const cats=CATS.map((sp,idx)=>{
const root=new T.Group(),tor=new T.Group();root.add(tor);
part(tor,0,0.43,0,0.46,0.34,0.8,sp.c);
if(sp.st)for(let k=-1;k<=1;k++)part(tor,0,0.61,k*0.22,0.3,0.03,0.08,sp.s);
(sp.pt||[]).forEach(p=>part(tor,p[0],p[1],p[2],p[3],p[4],p[5],p[6]));
const head=new T.Group();head.position.set(0,0.58,0.4);tor.add(head);
part(head,0,0.08,0.1,0.44,0.36,0.36,sp.c);
for(const sx of[-1,1]){part(head,sx*0.13,0.3,0.06,0.12,0.14,0.08,sp.ear||sp.c);part(head,sx*0.13,0.29,0.105,0.06,0.08,0.02,'#f4a3b5');part(head,sx*0.11,0.12,0.285,0.07,0.09,0.03,sp.e);part(head,sx*0.11,0.12,0.3,0.03,0.07,0.01,'#111');if(sp.st)part(head,sx*0.14,0.265,0.1,0.08,0.02,0.2,sp.s);}
part(head,0,0.0,0.28,0.22,0.1,0.02,sp.m);part(head,0,0.04,0.29,0.08,0.05,0.03,'#f08aa0');
if(sp.mayor){part(head,0,0.27,0.06,0.24,0.03,0.24,'#1b1b24');part(head,0,0.37,0.06,0.15,0.19,0.15,'#1b1b24');part(head,0,0.31,0.06,0.16,0.04,0.16,'#c0392b');
part(tor,0,0.42,0.405,0.1,0.1,0.02,'#ffd24a');part(tor,0,0.5,0.405,0.07,0.07,0.02,'#c0392b');}
const fg=new T.Group();fg.position.set(0,-0.04,0.3);head.add(fg);part(fg,0,0,0,0.34,0.09,0.08,'#9bc3e0');part(fg,0.21,0,0,0.1,0.15,0.05,'#6f9bc0');part(fg,-0.1,0.03,0.04,0.03,0.03,0.02,'#111');fg.visible=false;
const legs=[[-0.15,0.28],[0.15,0.28],[-0.15,-0.28],[0.15,-0.28]].map(p=>{const g=new T.Group();g.position.set(p[0],0.27,p[1]);tor.add(g);part(g,0,-0.135,0,0.13,0.27,0.13,sp.l||sp.c);return g;});
const t1=new T.Group(),t2=new T.Group(),t3=new T.Group();t1.position.set(0,0.5,-0.4);tor.add(t1);t2.position.set(0,0,-0.24);t1.add(t2);t3.position.set(0,0,-0.22);t2.add(t3);
part(t1,0,0,-0.12,0.1,0.1,0.26,sp.t||sp.c);part(t2,0,0,-0.11,0.09,0.09,0.24,sp.t||sp.c);part(t3,0,0,-0.1,0.08,0.08,0.22,sp.s&&!sp.t?sp.s:(sp.t||sp.c));
const sh=new T.Mesh(shadowGeo,shadowMat);sh.rotation.x=-Math.PI/2;sh.position.y=0.03;root.add(sh);
const sc=sp.sc||1.25;root.scale.setScalar(sc);scene.add(root);
const zs=[0,1].map(()=>{const s=new T.Sprite(new T.SpriteMaterial({map:zTex,transparent:true,depthWrite:false,opacity:0}));s.scale.set(0.7,0.7,1);scene.add(s);return s;});
return {sp:sp,name:sp.n,mayor:!!sp.mayor,root:root,tor:tor,head:head,legs:legs,t1:t1,t2:t2,t3:t3,fg:fg,sh:sh,zs:zs,sc:sc,x:0,y:0,z:0,h:rr(0,6),plan:[],pose:'idle',sl:0,stt:0,si:0,wk:0,jm:0,sw:0,ph:Math.random()*6,say:'strolling around',onRoof:false,hopT:0,pcd:0,fishT:0};
});
// ---------- cat behaviour ----------
const wait=(t,a,say,face,ball)=>({k:'wait',t:t,a:a,say:say,face:face,ball:ball});
const go=(x,z,sp)=>({k:'go',x:x,z:z,sp:sp||1.7});
const doit=fn=>({k:'do',fn:fn});
const jump=(x,y,z,t,hg)=>({k:'jump',x:x,y:y,z:z,t:t||1,hg:hg||1.3});
function nearestFree(x,z){for(let r=0;r<4;r+=0.5)for(let a=0;a<6.3;a+=0.8){const nx=x+Math.cos(a)*r,nz=z+Math.sin(a)*r;if(free(nx,nz))return{x:nx,z:nz};}return randFree();}
function roofPlan(c,r){
r.occ=c;
return [go(r.gx,r.gz),jump(r.x,r.y,r.z,1.1,1.6),doit(k=>{k.onRoof=true;}),wait(rr(16,26),'sleep','napping on a roof in the sun',rr(0,6)),wait(2.2,'stretch','stretching on the roof'),jump(r.gx,gy(r.gx,r.gz),r.gz,1,1.2),doit(k=>{k.onRoof=false;r.occ=null;})];
}
function planCat(c){
const day=mode!=='night';
const opts=[['w',3],['shop',day?3:0.3],['chase',day?2.6:0],['str',1.2],['nap',day?0.8:3],['roof',day?2.6:0.6],['yarn',1.4],['br',1.2]];
let tot=0;opts.forEach(o=>tot+=o[1]);let q=Math.random()*tot,ch='w';for(const o of opts){q-=o[1];if(q<=0){ch=o[0];break;}}
if(ch==='shop'){const s=pick(spots);c.plan=[go(s.x,s.z),wait(2.6,'idle','shopping for fish',Math.PI),doit(k=>{k.fishT=14;k.say='carrying a fresh fish';}),go(rr(-2,2),rr(-1,2)),wait(4,'sit','enjoying the fish'),doit(k=>{k.fishT=0;})];}
else if(ch==='chase'){const b=pick(bflies);c.plan=[doit(k=>{k.say='chasing a butterfly';}),{k:'chase',t:9,b:b},wait(1.8,'sit','catching its breath')];}
else if(ch==='str'){const p=randFree();c.plan=[go(p.x,p.z),wait(3,'stretch','stretching')];}
else if(ch==='nap'){const p=nearestFree(rr(5,10),rr(-3,5));c.plan=[go(p.x,p.z),wait(rr(10,18),'sleep','curled up asleep',rr(0,6)),wait(2,'stretch','stretching')];}
else if(ch==='roof'){const fr=roofs.filter(r=>!r.occ);if(fr.length)c.plan=roofPlan(c,pick(fr));else c.plan=[wait(1,'sit')];}
else if(ch==='yarn'){const b=pick(balls),a=rr(0,6.28),p=nearestFree(b.x+Math.cos(a)*(b.r+0.9),b.z+Math.sin(a)*(b.r+0.9));
c.plan=[go(p.x,p.z),wait(5,'yarn','playing with a ball of yarn',Math.atan2(b.x-p.x,b.z-p.z),b),wait(1.5,'sit')];}
else if(ch==='br'){const w=Math.random()<0.5,a=BR.x-BR.h-0.6,e=BR.x+BR.h+0.6;c.plan=[go(w?a:e,BR.z),go(BR.x,BR.z,1.4),wait(3.5,'sit','watching the pond',Math.PI),go(w?e:a,BR.z)];}
else{const p=randFree(),p2=randFree();c.plan=[go(p.x,p.z),wait(rr(1,3),pick(['idle','sit']),'strolling around'),go(p2.x,p2.z),wait(rr(1,2.5),'idle')];}
}
function turn(c,a,k){let d=a-c.h;d=Math.atan2(Math.sin(d),Math.cos(d));c.h+=d*Math.min(1,k);}
function moveTo(c,tx,tz,step){
const ang=Math.atan2(tx-c.x,tz-c.z);
for(const o of[0,0.5,-0.5,1,-1,1.6,-1.6]){const a=ang+o,nx=c.x+Math.sin(a)*step,nz=c.z+Math.cos(a)*step;if(free(nx,nz)){c.x=nx;c.z=nz;c.want=a;return true;}}
return false;
}
function stepCat(c,dt){
let s=c.plan[0];if(!s){planCat(c);s=c.plan[0];if(!s)return;}
c.pose='idle';
if(s.k==='do'){s.fn(c);c.plan.shift();}
else if(s.k==='wait'){
c.pose=s.a||'idle';if(s.say)c.say=s.say;if(s.face!==undefined)c.want=s.face;
if(s.a==='yarn'&&s.ball){s.tt=(s.tt||0)+dt;c.sw=Math.max(0,Math.sin(s.tt*5));if(c.sw>0.95&&!s.hit){s.ball.wob=1;s.hit=1;}if(c.sw<0.2)s.hit=0;}
s.t-=dt;if(s.t<=0){c.plan.shift();c.sw=0;}
}
else if(s.k==='go'){
c.pose='walk';s.life=(s.life||0)+dt;
const d=Math.hypot(s.x-c.x,s.z-c.z);
if(d<0.25||s.life>25)c.plan.shift();
else if(!moveTo(c,s.x,s.z,s.sp*dt)){s.stuck=(s.stuck||0)+dt;if(s.stuck>1.2)c.plan.shift();}
c.run=s.sp;
}
else if(s.k==='chase'){
const b=s.b;s.t-=dt;c.pose='walk';c.run=3;
const d=Math.hypot(b.x-c.x,b.z-c.z);
if(c.hopT>0){c.hopT-=dt;c.pose='jump';moveTo(c,b.x,b.z,1.8*dt);}
else if(d<1.4&&(c.pcd-=dt)<=0){c.hopT=0.55;c.pcd=1.3;b.flee=1.2;}
else if(d>0.4)moveTo(c,b.x,b.z,3*dt);
if(s.t<=0)c.plan.shift();
}
else if(s.k==='jump'){
if(s.p===undefined){s.p=0;s.sx=c.x;s.sy=c.y;s.sz=c.z;c.want=Math.atan2(s.x-c.x,s.z-c.z);}
s.p+=dt/s.t;const u=Math.min(1,s.p);c.pose='jump';
c.x=s.sx+(s.x-s.sx)*u;c.z=s.sz+(s.z-s.sz)*u;c.y=s.sy+(s.y-s.sy)*u+Math.sin(Math.PI*u)*s.hg;c.jy=1;
if(u>=1){c.y=s.y;c.plan.shift();}
}
if(s.k!=='jump'){c.jy=0;if(!c.onRoof)c.y+=(gy(c.x,c.z)-c.y)*Math.min(1,dt*12);}
}
function poseCat(c,dt,time){
const k=1-Math.exp(-dt*7),p=c.pose;
const walking=p==='walk',run=c.run||1.7;
c.sl+=((p==='sleep'?1:0)-c.sl)*k;c.stt+=((p==='stretch'?1:0)-c.stt)*k;c.si+=((p==='sit'?1:0)-c.si)*k;
c.wk+=((walking?1:0)-c.wk)*k;c.jm+=((p==='jump'?1:0)-c.jm)*k;
c.ph+=dt*(walking?run*5:1.5);
turn(c,c.want===undefined?c.h:c.want,dt*(walking?10:6));
const hop=c.hopT>0?Math.sin(Math.PI*(1-c.hopT/0.55))*0.8:0;
const bob=walking?Math.abs(Math.sin(c.ph))*0.05:0;
const sl=c.sl,st=c.stt,si=c.si,sw=c.sw;
c.root.position.set(c.x,c.y+hop,c.z);c.root.rotation.y=c.h;
c.sh.position.y=0.03-hop/c.sc;
const tor=c.tor;
tor.position.y=bob-0.2*sl+0.1*st+0.2*si-0.06*sw;
tor.scale.y=1-0.25*sl+Math.sin(time*1.6+c.ph)*0.015*sl;
tor.rotation.x=0.38*st-0.62*si+0.18*sw;
c.head.rotation.x=-0.38*st+0.55*si+0.6*sl-0.1*sw;c.head.rotation.y=0.7*sl+Math.sin(time*0.7+c.ph)*0.15*(1-c.wk)*(1-sl);
c.head.position.z=0.4-0.1*sl;
const sw1=Math.sin(c.ph)*0.8*c.wk,j=c.jm;
const lr=[(sw1-j*0.9)-0.9*st-sw*1.4,(-sw1-j*0.9)-0.9*st,(-sw1+j*0.9)-0.1*si-1.2*si,(sw1+j*0.9)-1.2*si];
c.legs.forEach((l,i)=>{l.rotation.x=lr[i]+(i<2?0.55*si:0);l.visible=sl<0.85;});
const tw=Math.sin(time*2.2+c.ph)*0.25;
c.t1.rotation.x=(0.9+0.4*st)*(1-sl)+0.1*sl;c.t1.rotation.y=tw*(1-sl)+1.2*sl;c.t2.rotation.x=-0.5*(1-sl);c.t2.rotation.y=0.8*sl+tw*0.4*(1-sl);c.t3.rotation.x=-0.5*(1-sl);c.t3.rotation.y=0.8*sl;
c.fg.visible=c.fishT>0;if(c.fishT>0)c.fishT-=dt;
c.zs.forEach((z,i)=>{const on=sl>0.85,u=((time*0.35+i*0.5)%1);z.material.opacity=on?Math.sin(Math.PI*u)*0.95:0;z.position.set(c.x+0.3+u*0.5,c.y+1.3+u*1.2,c.z);const s=0.55+u*0.5;z.scale.set(s,s,1);});
}
// butterflies / fireflies
function updBfly(b,dt,time){
const d=Math.hypot(b.tx-b.x,b.tz-b.z);
if(d<0.5||b.flee>0){
if(b.flee>0&&b.flee>1.1){const a=Math.random()*6.28;b.tx=clamp(b.x+Math.cos(a)*3,-9,11);b.tz=clamp(b.z+Math.sin(a)*3,-8,9);b.ty=rr(1.4,2.8);}
else if(d<0.5){if(Math.random()<0.65){b.tx=8+rr(-4,4);b.tz=1.5+rr(-4,4);}else{b.tx=rr(-9,10);b.tz=rr(-5,8);}b.ty=rr(1.2,2.8);}
}
const sp=b.flee>0?3:1.2;b.flee=Math.max(0,b.flee-dt);
const a=Math.atan2(b.tx-b.x,b.tz-b.z);b.x+=Math.sin(a)*sp*dt;b.z+=Math.cos(a)*sp*dt;b.y+=(b.ty-b.y)*dt;
b.g.position.set(b.x+Math.sin(time*2+b.ph)*0.15,b.y+Math.sin(time*3+b.ph)*0.15,b.z);b.g.rotation.y=a;
const f=0.2+Math.abs(Math.sin(time*16+b.ph))*1.0;b.wl.rotation.z=-f;b.wr.rotation.z=f;
}
// ---------- camera, input ----------
const cam={az:0.55,el:0.62,dist:60,distT:60,tx:0,ty:-1.5,tz:0,vAz:0,vEl:0};
let zoomed=false,follow=null,dragging=false,demo=false;
const ptrs=new Map();let moved=0,downT=0,lastPinch=0;
function endDemo(){if(!demo)return;demo=false;$('#demo').style.display='none';}
function setMode(m){
mode=m;document.querySelectorAll('#bar button').forEach(b=>b.classList.toggle('on',b.dataset.m===m));
}
document.querySelectorAll('#bar button').forEach(b=>b.addEventListener('click',()=>setMode(b.dataset.m)));
function setFollow(c){follow=c;const f=$('#follow');if(c){f.style.display='flex';cam.distT=Math.min(cam.distT,12);zoomed=true;$('#hint').style.opacity=0;updFollowTxt(true);}else f.style.display='none';}
let ftT=0;
function updFollowTxt(force){if(!follow)return;const n=performance.now();if(!force&&n-ftT<300)return;ftT=n;$('#ftxt').innerHTML='Following <b>'+follow.name+(follow.mayor?' (the mayor)':'')+'</b>: '+follow.say;}
$('#stop').addEventListener('click',()=>setFollow(null));
window.addEventListener('pointerdown',endDemo,true);
window.addEventListener('wheel',endDemo,true);
function pinchD(){const a=[...ptrs.values()];return Math.hypot(a[0].x-a[1].x,a[0].y-a[1].y)||1;}
canvas.addEventListener('pointerdown',e=>{
try{canvas.setPointerCapture(e.pointerId);}catch(err){}
ptrs.set(e.pointerId,{x:e.clientX,y:e.clientY});
if(ptrs.size===1){dragging=true;moved=0;downT=performance.now();cam.vAz=0;cam.vEl=0;}
else if(ptrs.size===2){lastPinch=pinchD();moved=99;}
});
canvas.addEventListener('pointermove',e=>{
const p=ptrs.get(e.pointerId);if(!p)return;
const dx=e.clientX-p.x,dy=e.clientY-p.y;p.x=e.clientX;p.y=e.clientY;
if(ptrs.size===1){moved+=Math.abs(dx)+Math.abs(dy);cam.az-=dx*0.0075;cam.el=clamp(cam.el+dy*0.005,0.1,1.45);cam.vAz=-dx*0.0075;cam.vEl=dy*0.005*0.3;}
else if(ptrs.size===2){const d=pinchD();cam.distT=clamp(cam.distT*lastPinch/d,6,fitDist()*1.9);lastPinch=d;zoomed=true;}
});
function up(e){
if(!ptrs.has(e.pointerId))return;ptrs.delete(e.pointerId);
if(ptrs.size===0){dragging=false;if(moved<10&&performance.now()-downT<600&&e.type==='pointerup')tap(e.clientX,e.clientY);}
else if(ptrs.size===1){lastPinch=0;}
}
canvas.addEventListener('pointerup',up);canvas.addEventListener('pointercancel',up);
canvas.addEventListener('wheel',e=>{e.preventDefault();const k=e.deltaMode===1?16:1;cam.distT=clamp(cam.distT*Math.exp(e.deltaY*k*0.0012),6,fitDist()*1.9);zoomed=true;},{passive:false});
canvas.addEventListener('contextmenu',e=>e.preventDefault());
const _v=new T.Vector3();
function tap(px,py){
let best=null,bd=1e9;const thr=Math.max(36,1.2/(0.65*cam.dist)*innerHeight);
cats.forEach(c=>{_v.set(c.x,c.y+0.6*c.sc,c.z).project(camera);if(_v.z>1)return;const sx=(_v.x*0.5+0.5)*innerWidth,sy=(-_v.y*0.5+0.5)*innerHeight,d=Math.hypot(sx-px,sy-py);if(d<thr&&d<bd){bd=d;best=c;}});
if(best)setFollow(best);else if(follow)setFollow(null);
}
// follow marker
const mk=new T.Group();const mkM=new T.MeshBasicMaterial({color:0xffd84a});
[[0,0,0.5],[0,0.15,0.34],[0,0.3,0.18]].forEach(a=>{const m=new T.Mesh(UB,mkM);m.position.set(0,a[1],0);m.scale.set(a[2],0.16,a[2]);mk.add(m);});mk.visible=false;scene.add(mk);
// ---------- demo script ----------
const demoEv=[
[0,()=>{setMode('day');setFollow(null);cam.el=0.62;cam.distT=fitDist();dem.el=0.62;}],
[3,()=>{cam.distT=fitDist()*0.62;dem.el=0.5;}],
[6,()=>{setMode('sunset');}],
[9,()=>{setMode('night');cam.distT=fitDist()*0.5;dem.el=0.36;}],
[12.5,()=>{setFollow(cats[0]);dem.el=0.4;}],
[16.5,()=>{setFollow(null);setMode('day');cam.distT=fitDist();dem.el=0.62;}]
];
const dem={t:0,el:0.62,prev:0,fired:[]};
// ---------- main loop ----------
let last=performance.now()/1000,time=0;
const Z=new T.Vector3();
function frame(now){
requestAnimationFrame(frame);
now/=1000;const dt=Math.min(0.05,Math.max(0,now-last));last=now;time+=dt;
// demo
if(demo){
dem.t+=dt;const ph=dem.t%20;if(ph<dem.prev)dem.fired=[];dem.prev=ph;
demoEv.forEach((e,i)=>{if(!dem.fired[i]&&ph>=e[0]){dem.fired[i]=1;e[1]();}});
cam.az+=dt*0.2;cam.el+=(dem.el-cam.el)*Math.min(1,dt*1.5);
}
// environment
const tg=TG[mode],ke=1-Math.exp(-dt*2.4);
['low','hor','top','sky','gnd','sun','cloud','win','bulb','sunC'].forEach(k=>E[k].lerp(tg[k],ke));
E.sd.lerp(tg.sd,ke);['hi','si','glow','star','sunA','sunY','sunS'].forEach(k=>{E[k]+=(tg[k]-E[k])*ke;});
skyMat.uniforms.cLow.value.copy(E.low);skyMat.uniforms.cHor.value.copy(E.hor);skyMat.uniforms.cTop.value.copy(E.top);
hemi.color.copy(E.sky);hemi.groundColor.copy(E.gnd);hemi.intensity=E.hi;
sunL.color.copy(E.sun);sunL.intensity=E.si;sunL.position.copy(E.sd).multiplyScalar(100);
glassMat.color.copy(E.win);bulbMat.color.copy(E.bulb);cloudMat.color.copy(E.cloud);
stars.material.opacity=E.star;stars.position.copy(camera.position);sky.position.copy(camera.position);
const gl=E.glow;glowSprites.forEach((s,i)=>{s.material.opacity=gl*(0.75+0.25*Math.sin(time*3+i*1.7));});
plights.forEach((l,i)=>{l.intensity=gl*(1.15+0.1*Math.sin(time*2.5+i));});
sunSpr.material.opacity=E.sunA;sunSpr.material.color.copy(E.sunC);sunSpr.position.y=E.sunY;sunSpr.scale.set(E.sunS,E.sunS,1);
moonSpr.material.opacity=E.star>0.6?(E.star-0.6)/0.4:0;moonSpr.scale.set(30,30,1);
fireflies.material.opacity=Math.max(0,E.star-0.5)*1.6;
const nightish=E.star>0.5;bflies.forEach(b=>b.g.visible=!nightish);
// water, clouds
water.userData.t.offset.x+=dt*0.025;water.userData.t.offset.y+=dt*0.012;
clouds.forEach(g=>{g.position.x+=g.userData.v*dt;if(g.position.x>75)g.position.x=-75;});
// butterflies, fireflies
bflies.forEach(b=>updBfly(b,dt,time));
for(let i=0;i<30;i++){const f=ffs[i];f.p+=dt;ffPos[i*3]=f.x+Math.sin(f.p*0.6+i)*1.2;ffPos[i*3+1]=f.y+Math.sin(f.p*0.9+i*2)*0.5;ffPos[i*3+2]=f.z+Math.cos(f.p*0.5+i)*1.2;}
ffG.attributes.position.needsUpdate=true;
// yarn
balls.forEach((b,i)=>{b.wob=Math.max(0,b.wob-dt*1.5);b.m.rotation.y+=dt*0.1;b.m.rotation.z=Math.sin(time*40)*0.12*b.wob;b.m.position.y=b.y+Math.abs(Math.sin(b.wob*3))*0.12*b.wob;});
// cats
cats.forEach(c=>{c.run=1.7;stepCat(c,dt);});
for(let i=0;i<cats.length;i++)for(let j=i+1;j<cats.length;j++){
const a=cats[i],b=cats[j];if(a.onRoof||b.onRoof||a.jy||b.jy)continue;
const dx=b.x-a.x,dz=b.z-a.z,d=Math.hypot(dx,dz);
if(d<0.85&&d>0.001){const p=(0.85-d)*0.5*Math.min(1,dt*5),ux=dx/d*p,uz=dz/d*p;if(free(b.x+ux,b.z+uz)){b.x+=ux;b.z+=uz;}if(free(a.x-ux,a.z-uz)){a.x-=ux;a.z-=uz;}}
}
cats.forEach(c=>poseCat(c,dt,time));
// camera
let tx=0,ty=-1.5,tz=0;
if(follow){tx=follow.x;ty=follow.y+0.7;tz=follow.z;}
const kc=1-Math.exp(-dt*(follow?5:4));cam.tx+=(tx-cam.tx)*kc;cam.ty+=(ty-cam.ty)*kc;cam.tz+=(tz-cam.tz)*kc;
cam.dist+=(cam.distT-cam.dist)*(1-Math.exp(-dt*6));
if(!dragging){cam.az+=cam.vAz;cam.el=clamp(cam.el+cam.vEl,0.1,1.45);cam.vAz*=0.92;cam.vEl*=0.9;}
const ce=Math.cos(cam.el);
camera.position.set(cam.tx+cam.dist*Math.sin(cam.az)*ce,cam.ty+cam.dist*Math.sin(cam.el),cam.tz+cam.dist*Math.cos(cam.az)*ce);
camera.lookAt(cam.tx,cam.ty,cam.tz);
if(follow){mk.visible=true;mk.position.set(follow.x,follow.y+2.2*follow.sc/1.25+Math.sin(time*5)*0.12,follow.z);updFollowTxt(false);}else mk.visible=false;
renderer.render(scene,camera);
}
// ---------- start ----------
// initial cat setups
function place(c,x,z){c.x=x;c.z=z;c.y=gy(x,z);c.h=rr(0,6.28);c.want=c.h;c.root.position.set(x,c.y,z);}
function onRoof(c,r,t){r.occ=c;c.onRoof=true;c.x=r.x;c.z=r.z;c.y=r.y;c.h=rr(0,6.28);c.want=c.h;c.plan=[wait(t,'sleep','napping on a roof in the sun',c.h),wait(2.2,'stretch','stretching on the roof'),jump(r.gx,0,r.gz,1,1.2),doit(k=>{k.onRoof=false;r.occ=null;})];c.pose='sleep';c.sl=1;}
place(cats[0],-3.2,-2.6);cats[0].plan=[go(spots[1].x,spots[1].z),wait(2.6,'idle','shopping for fish',Math.PI),doit(k=>{k.fishT=14;k.say='carrying a fresh fish';}),go(0.5,0.5),wait(4,'sit','enjoying the fish')];
place(cats[1],6.2,0.2);cats[1].plan=[{k:'chase',t:10,b:bflies[0]},wait(2,'sit')];
place(cats[2],-8.2,1.8);onRoof(cats[2],roofs[0],25);
place(cats[3],-1,10);onRoof(cats[3],roofs[1],30);
place(cats[4],1.5,-1.5);cats[4].plan=[wait(3.5,'stretch','stretching')];
place(cats[5],-3,2);cats[5].plan=[go(3,-2),wait(2,'idle')];
place(cats[6],-6.6,-3.0);cats[6].plan=[go(spots[0].x,spots[0].z),wait(2.6,'idle','shopping for fish',Math.PI),doit(k=>{k.fishT=14;}),go(-5,-1),wait(4,'sit','enjoying the fish')];
place(cats[7],7.4,-3.0);cats[7].plan=[go(10.2,-1.8),wait(8,'yarn','playing with a ball of yarn',Math.atan2(-0.2+1.8,0),balls[3])];
place(cats[8],8.6,4.0);cats[8].plan=[wait(14,'sleep','curled up asleep',0.5),wait(2,'stretch')];cats[8].pose='sleep';cats[8].sl=1;
place(cats[9],-9.4,5);cats[9].plan=[go(BR.x,BR.z,1.4),wait(3.5,'sit','watching the pond',Math.PI),go(-1.5,BR.z)];
place(cats[10],9,0.5);cats[10].plan=[{k:'chase',t:12,b:bflies[2]}];
place(cats[11],2.4,-3.6);onRoof(cats[11],roofs[4],12);place(cats[11],cats[11].x,cats[11].z);cats[11].y=roofs[4].y;
cats.forEach(c=>{c.root.position.set(c.x,c.y,c.z);});
resize();addEventListener('resize',resize);
cam.dist=cam.distT=fitDist();
setTimeout(()=>{$('#hint').style.opacity=0;},14000);
if(/#demo$/.test(location.href)){demo=true;$('#demo').style.display='block';$('#hint').style.opacity=0;}
addEventListener('hashchange',()=>{if(/#demo$/.test(location.href)&&!demo){demo=true;dem.t=0;dem.fired=[];$('#demo').style.display='block';}});
requestAnimationFrame(frame);
})();
</script>
</body>
</html>
