WebGL map: keep the pictures' keys, the waves and the clouds out of the garbage
A frame joined a key for every city, label, quiet dot and stamp it drew, built a closure to paint each even when the picture was already there, concatenated a prefix onto every key, made an array per city per copy of the map for its place on screen, and rebuilt the waves' and the clouds' arrays. Keys are now kept per city (packed into one number for the yes/no parts), per label text and look, per stamp and scale, and joined again only when the picture changes; pictures already painted are found before any closure is made; the card's and the 2D map's pictures are two maps, so keys need no prefix; the cities' places and the POP lights are computed in place; the waves and the clouds are written into arrays kept from frame to frame. Sampled heap profile, TTL save, RTX 4060 Ti: allocations under drawFrame 68 -> 37 MB/s at x1 and 62 -> 43 MB/s during a pan/zoom sweep; under glBegin 5.4 -> 0.5 MB/s.
This commit is contained in:
parent
3482142ae5
commit
87b153db6f
8 changed files with 153 additions and 37 deletions
|
|
@ -74,13 +74,17 @@ function glLand(gl,ks){const G=glLandGeo(gl,lodFar(),coastLevel(cam.z));
|
|||
const WAVE_COL=glColor('rgba(255,255,255,.45)');
|
||||
function glWaves(gl,now,ks){const A=GLX.wa||(GLX.wa=new Float32Array(WAVES.length*8*LINE_STRIDE)),ph0=now/900;let m=0;
|
||||
for(const [x,y,ph] of WAVES){const o=Math.sin(ph0+ph*6)*3;
|
||||
for(const [x0,cx,cy,x1] of [[x-9+o,x-4.5+o,y-4,x+o],[x+o,x+4.5+o,y+4,x+9+o]]){let px=x0,py=y;
|
||||
// Two curves: up from x-9 to x, then down to x+9 (no array literals per wave: they were garbage every frame).
|
||||
for(let c=0;c<2;c++){const x0=x+o+(c?0:-9),cx=x0+4.5,cy=y+(c?4:-4),x1=x0+9;let px=x0,py=y;
|
||||
for(let j=1;j<=4;j++){const t=j/4,u=1-t,qx=u*u*x0+2*u*t*cx+t*t*x1,qy=u*u*y+2*u*t*cy+t*t*y;glSegAt(A,m++,px,py,qx,qy,0,2,0,0,WAVE_COL[3],WAVE_COL);px=qx;py=qy;}}}
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER,GLX.wv.b);gl.bufferData(gl.ARRAY_BUFFER,A.subarray(0,m*LINE_STRIDE),gl.DYNAMIC_DRAW);glLines(gl,GLX.wv.v,m,ks);}
|
||||
// The clouds: their puffs are stencilled as one shape, then filled once, so overlapping puffs do not stack.
|
||||
function glClouds(gl,now,ks){const A=[];for(const k of ks)for(const c of CLOUDS){const x=((c.x+now/1000*c.v)%(W+200))-100+k*W,y=c.y;
|
||||
A.push(x,y,16*c.s,0, x+18*c.s,y-8*c.s,20*c.s,0, x+40*c.s,y,15*c.s,0);}
|
||||
if(!A.length)return;const F=new Float32Array(A),P=GLX.circ;gl.bindBuffer(gl.ARRAY_BUFFER,GLX.cl.b);gl.bufferData(gl.ARRAY_BUFFER,F,gl.DYNAMIC_DRAW);
|
||||
function glClouds(gl,now,ks){const need=ks.length*CLOUDS.length*12;if(!need)return;
|
||||
// Written into one array kept from frame to frame, grown when more copies of the map show.
|
||||
if(!GLX.ca||GLX.ca.length<need)GLX.ca=new Float32Array(need);const A=GLX.ca;let n=0;
|
||||
for(const k of ks)for(const c of CLOUDS){const x=((c.x+now/1000*c.v)%(W+200))-100+k*W,y=c.y,s=c.s;
|
||||
A[n++]=x;A[n++]=y;A[n++]=16*s;A[n++]=0;A[n++]=x+18*s;A[n++]=y-8*s;A[n++]=20*s;A[n++]=0;A[n++]=x+40*s;A[n++]=y;A[n++]=15*s;A[n++]=0;}
|
||||
const F=A.subarray(0,n),P=GLX.circ;gl.bindBuffer(gl.ARRAY_BUFFER,GLX.cl.b);gl.bufferData(gl.ARRAY_BUFFER,F,gl.DYNAMIC_DRAW);
|
||||
gl.enable(gl.STENCIL_TEST);gl.stencilFunc(gl.ALWAYS,1,255);gl.stencilOp(gl.KEEP,gl.KEEP,gl.REPLACE);gl.colorMask(false,false,false,false);
|
||||
gl.useProgram(P.p);gl.bindVertexArray(GLX.cl.v);gl.uniform2f(P.u.uView,CW,CH);gl.uniform3f(P.u.uCam,cam.x,cam.y,cam.z);gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,F.length/4);
|
||||
gl.colorMask(true,true,true,true);gl.stencilFunc(gl.EQUAL,1,255);gl.stencilOp(gl.KEEP,gl.KEEP,gl.KEEP);glFill(gl,CLOUD_COL);gl.disable(gl.STENCIL_TEST);}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@
|
|||
as instanced quads, with the small round things (lights, steam, glows) as discs, in one draw per frame after the
|
||||
tubes. Without WebGL the same calls draw the pictures with the 2D canvas, so the map's code does not care which. */
|
||||
const SPR_MAX=1400,SPR_STRIDE=13;
|
||||
const SPRS=new Map();
|
||||
// The card's pictures (SPRS) and the 2D map's (SPRC), by key; both at the screen's pixel density SPR_DPR.
|
||||
const SPRS=new Map(),SPRC=new Map();let SPR_DPR=0;
|
||||
const GLS={prog:null,vao:null,buf:null,tex:null,size:4096,shx:0,shy:0,shh:0,frame:false,data:new Float32Array(SPR_STRIDE*2048),n:0};
|
||||
/* One record is 13 floats: x,y,w,h (CSS px) | u0,v0,u1,v1 (atlas) | r,g,b,a | kind. A picture (kind 0) takes its
|
||||
colours from the atlas and a from the record; a disc (kind 1) is centred on x,y with radius w, in colour r,g,b,a. */
|
||||
|
|
@ -25,7 +26,7 @@ function glsInit(gl){GLS.prog=glProg(gl,SPR_VS,SPR_FS);const {v,b}=glVao(gl,[4,4
|
|||
for(const k of [gl.TEXTURE_WRAP_S,gl.TEXTURE_WRAP_T])gl.texParameteri(gl.TEXTURE_2D,k,gl.CLAMP_TO_EDGE);
|
||||
GLS.shx=GLS.shy=GLS.shh=0;}
|
||||
// Forgets every picture and starts the atlas again, after the pictures already queued are drawn (they point into it).
|
||||
function spritesReset(){glsDrawNow();SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;}
|
||||
function spritesReset(){glsDrawNow();SPRS.clear();SPRC.clear();GLS.shx=GLS.shy=GLS.shh=0;}
|
||||
// The card given up: its pictures go (they have no canvas to be drawn from in 2D) and the atlas is forgotten.
|
||||
function spritesDrop(){GLS.n=0;SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;}
|
||||
/* The card's pictures are painted on one canvas, reused, and each goes into the atlas as soon as it is painted: a
|
||||
|
|
@ -35,10 +36,14 @@ const GLS_PAD={cv:null,g:null};
|
|||
function glsPad(pw,ph){const P=GLS_PAD;if(!P.cv){P.cv=document.createElement('canvas');P.cv.width=P.cv.height=256;}
|
||||
if(P.cv.width<pw||P.cv.height<ph){P.cv.width=Math.max(P.cv.width,Math.ceil(pw/64)*64);P.cv.height=Math.max(P.cv.height,Math.ceil(ph/64)*64);P.g=null;}
|
||||
if(!P.g)P.g=P.cv.getContext('2d',{willReadFrequently:true});return P;}
|
||||
// Where this frame's pictures are kept: the card's or the 2D map's. A new pixel density starts both again.
|
||||
function sprMap(){if(DPR!==SPR_DPR){SPR_DPR=DPR;spritesReset();}return GLS.frame&&!!GLX.gl&&!!GLS.prog?SPRS:SPRC;}
|
||||
// The picture already painted under key, if any: callers ask first, so a picture drawn every frame costs no closure.
|
||||
const spriteHas=key=>sprMap().get(key);
|
||||
// A picture of w×h CSS px with its anchor at (ax,ay): painted once by paint(g) (the 2D canvas, at the screen's pixel
|
||||
// density, which is part of the key), and kept in SPRS. The card's pictures and the 2D map's are kept apart ('g', 'c').
|
||||
function spriteEntry(key,w,h,ax,ay,paint){const gpu=GLS.frame&&!!GLX.gl&&!!GLS.prog;key=(gpu?'g':'c')+DPR+'|'+key;let e=SPRS.get(key);if(e)return e;
|
||||
if(SPRS.size>=SPR_MAX)spritesReset();
|
||||
// density), and kept by key in SPRS (the card's) or SPRC (the 2D map's).
|
||||
function spriteEntry(key,w,h,ax,ay,paint){const M=sprMap(),gpu=M===SPRS;let e=M.get(key);if(e)return e;
|
||||
if(SPRS.size+SPRC.size>=SPR_MAX)spritesReset();
|
||||
const pw=Math.ceil(w*DPR),ph=Math.ceil(h*DPR),saved=ctx;
|
||||
e={cv:null,pw,ph,w,h,ax,ay,up:false,u0:0,v0:0,u1:1,v1:1};
|
||||
if(gpu&&pw<=GLS.size&&ph<=GLS.size){const P=glsPad(pw,ph),g=P.g;
|
||||
|
|
@ -48,7 +53,7 @@ function spriteEntry(key,w,h,ax,ay,paint){const gpu=GLS.frame&&!!GLX.gl&&!!GLS.p
|
|||
try{if(!glsPut(GLX.gl,e,P.cv))glsPut(GLX.gl,e,P.cv);}catch(x){throw glErr(x);}
|
||||
}else{const cv=document.createElement('canvas');cv.width=pw;cv.height=ph;const g=cv.getContext('2d');ctx=g;
|
||||
try{g.setTransform(DPR,0,0,DPR,0,0);paint(g);}finally{ctx=saved;}e.cv=cv;}
|
||||
SPRS.set(key,e);return e;}
|
||||
M.set(key,e);return e;}
|
||||
// Puts a picture with its own canvas into the atlas (once), shelf by shelf. A full atlas starts again; this picture then waits a frame.
|
||||
function glsEnsure(e){if(e.up)return true;const gl=GLX.gl;if(!gl||!GLS.prog||!e.cv)return false;
|
||||
try{return glsPut(gl,e,e.cv);}catch(x){throw glErr(x);}}
|
||||
|
|
@ -85,8 +90,11 @@ const sprQ=k=>Math.max(1/CSPR_STEP,Math.round(k*ZH.f*CSPR_STEP)/CSPR_STEP);
|
|||
// (k in steps of 1/24, as the cities; zoomed: k grows with the zoom, and is held while it moves, sprQ()), then drawn with
|
||||
// the anchor at (x,y). Things that bob or pulse are drawn as these at a moving place, so the overlay above the card
|
||||
// stays untouched (repainting it costs a whole screen of pixels).
|
||||
// The full key of a stamp at scale kq, kept per key: it was built again for every stamp on every frame.
|
||||
const STK=new Map();
|
||||
function stamp(key,w,h,ax,ay,k,x,y,paint,zoomed){const kq=zoomed?sprQ(k):Math.max(1/CSPR_STEP,Math.round(k*CSPR_STEP)/CSPR_STEP),r=k/kq;
|
||||
const e=spriteEntry(key+'|'+kq,Math.ceil(w*kq),Math.ceil(h*kq),ax*kq,ay*kq,g=>{g.translate(ax*kq,ay*kq);g.scale(kq,kq);paint(g);});
|
||||
let m=STK.get(key);if(!m||m.kq!==kq){if(STK.size>4000)STK.clear();STK.set(key,m={kq,k:key+'|'+kq});}
|
||||
const e=spriteHas(m.k)||spriteEntry(m.k,Math.ceil(w*kq),Math.ceil(h*kq),ax*kq,ay*kq,g=>{g.translate(ax*kq,ay*kq);g.scale(kq,kq);paint(g);});
|
||||
blit(e,x-e.ax*r,y-e.ay*r,e.w*r,e.h*r);}
|
||||
// Text with an outline, centred on (x,y), as a picture placed on whole device pixels; tw is its width, px its size.
|
||||
function textStamp(t,font,px,tw,fill,stroke,lw,x,y){const W=Math.ceil(tw+lw+4),H=Math.ceil(px*1.5+lw+2);
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ function drawFrame(now){
|
|||
// cities
|
||||
UI.feed=null;if(UI.sel&&UI.sel.type==='link'){const sl=S.links.find(x=>x.id===UI.sel.id);const rec=sl&&R.ef[edgeKey(sl)];if(rec){UI.feed=new Set(Object.keys(rec.eg).map(k=>R.egDesc[k].city));UI.dest=new Set(Object.keys(rec.dst));}}
|
||||
// Place labels: your POPs and the selected city first, then others; a label that would overlap moves above its city or hides.
|
||||
const vis=[];for(const C of CITIES){if(C.off&&!cityOn(C.id))continue;if(pzHide(C.id))continue;for(const k of ks){const [sx,sy]=toS(C.x,C.y,k);if(sx<-60||sx>CW+60||sy<-60||sy>CH+60)continue;vis.push({C,sx,sy});}}
|
||||
const vis=[];for(const C of CITIES){const sy=(C.y-cam.y)*cam.z+CH/2;if(sy<-60||sy>CH+60)continue;if(C.off&&!cityOn(C.id))continue;if(pzHide(C.id))continue;for(const k of ks){const sx=(C.x+k*W-cam.x)*cam.z+CW/2;if(sx<-60||sx>CW+60)continue;vis.push({C,sx,sy});}}
|
||||
const prio=v=>(UI.sel&&UI.sel.type==='city'&&UI.sel.id===v.C.id?0:S.pops[v.C.id]?1:2);vis.sort((a,b)=>prio(a)-prio(b)||b.C.P-a.C.P);
|
||||
const rects=new RGrid(),labs=new RGrid(),hit=(r)=>rects.hits(r);
|
||||
const farL=lodFar();
|
||||
|
|
@ -116,7 +116,7 @@ function drawFrame(now){
|
|||
if(ly===null&&prio(v)===1)for(const y of [below,above]){const r={x:v.sx-tw/2,y:y-10,w:tw,h:21};if(!labs.hits(r)){ly=y;rects.push(r);labs.push(r);break;}}
|
||||
if(ly===null&&(prio(v)===0||(prio(v)===1&&(v.C.id===S.home||UI.hover===v.C.id)))){ly=below;const r={x:v.sx-tw/2,y:below-10,w:tw,h:21};rects.push(r);labs.push(r);}
|
||||
v.ly=ly;}
|
||||
if(pzOn()){for(const v of vis.slice().reverse())pzDrawCity(v,csc,now);pzDrawMarks(ks,now);}else for(const v of vis.slice().reverse())drawCity(v.C,v.sx,v.sy,csc,now,v.ly,v.dem);
|
||||
if(pzOn()){for(let i=vis.length-1;i>=0;i--)pzDrawCity(vis[i],csc,now);pzDrawMarks(ks,now);}else for(let i=vis.length-1;i>=0;i--){const v=vis[i];drawCity(v.C,v.sx,v.sy,csc,now,v.ly,v.dem);}
|
||||
// cable ships at sea (88-cable-ships.js), over the city labels so a ship leaving port stays in sight
|
||||
drawShips(ks,now);
|
||||
// Architect runs: what the tubes between two cities are (one trench, or separate paths), above the cities.
|
||||
|
|
|
|||
|
|
@ -1,11 +1,15 @@
|
|||
// Label widths by text (city labels by id, link labels and far-view labels by text): measuring text every frame is slow.
|
||||
const LBLW={},LINKW=new Map(),LBLF=new Map(),CM={off:new Set(),big:new Set(),inc:new Set(),far:false};
|
||||
const LBLW={},LINKW=new Map(),LBLF=new Map(),FKEY=new Map(),QKEY=new Map(),CM={off:new Set(),big:new Set(),inc:new Set(),far:false};
|
||||
try{document.fonts&&document.fonts.ready.then(()=>{for(const k in LBLW)delete LBLW[k];LINKW.clear();LBLF.clear();FRAME.length=0;spritesReset();});}catch(e){}
|
||||
// A label on a pill: its text and look never change, so it is painted once (spriteEntry) and drawn as a picture. The
|
||||
// centre is x, y; the text sits at y+ty. The pill's box is w×h with corners r; the picture also takes the outline.
|
||||
// Keys are kept by text (PILLK), one per look: joining them for every label on every frame was most of the map's garbage.
|
||||
const PILLK=new Map();
|
||||
function pill(t,font,w,h,r,fill,ink,stroke,lw,x,y,ty){
|
||||
const pad=lw+2,W=Math.ceil(w+pad*2),H=Math.ceil(h+pad*2),key=['pill',t,font,w,h,r,fill,ink,stroke,lw,ty].join('|');
|
||||
const e=spriteEntry(key,W,H,W/2,H/2,g=>{g.translate(W/2,H/2);g.fillStyle=fill;g.strokeStyle=stroke;g.lineWidth=lw;
|
||||
const pad=lw+2,W=Math.ceil(w+pad*2),H=Math.ceil(h+pad*2);let L=PILLK.get(t),m=null;
|
||||
if(L)for(let i=0;i<L.length;i++){const q=L[i];if(q.font===font&&q.w===w&&q.h===h&&q.r===r&&q.fill===fill&&q.ink===ink&&q.stroke===stroke&&q.lw===lw&&q.ty===ty){m=q;break;}}
|
||||
if(!m){if(!L){if(PILLK.size>4000)PILLK.clear();PILLK.set(t,L=[]);}L.push(m={font,w,h,r,fill,ink,stroke,lw,ty,k:['pill',t,font,w,h,r,fill,ink,stroke,lw,ty].join('|')});}
|
||||
const e=spriteHas(m.k)||spriteEntry(m.k,W,H,W/2,H/2,g=>{g.translate(W/2,H/2);g.fillStyle=fill;g.strokeStyle=stroke;g.lineWidth=lw;
|
||||
rr(-w/2,-h/2,w,h,r);g.fill();g.stroke();g.fillStyle=ink;g.font=font;g.textAlign='center';g.textBaseline='middle';g.fillText(t,0,ty);});
|
||||
// Whole device pixels, so the text stays as sharp as before.
|
||||
blit(e,Math.round((x-e.w/2)*DPR)/DPR,Math.round((y-e.h/2)*DPR)/DPR,e.w,e.h);}
|
||||
|
|
@ -13,8 +17,10 @@ function pill(t,font,w,h,r,fill,ink,stroke,lw,x,y,ty){
|
|||
function drawCityFar(C,sx,sy,sc,now,lyOverride){
|
||||
const p=S.pops[C.id],down=p&&p.down>S.day,sel=UI.sel&&UI.sel.type==='city'&&UI.sel.id===C.id,hov=UI.hover===C.id||(UI.snap&&UI.snap.id===C.id);
|
||||
if(p&&!down&&R.cityDem[C.id]>0&&sfOf(R,C.id)<0.9)dot(sx,sy,12,'rgba(255,77,94,1)',.35+.25*Math.sin(now/200));
|
||||
const t=cTier(C.id),ph=sel?Math.floor(now/40)%8:-1,key=['far',p?1:0,down?1:0,p&&p.site2?1:0,p?(p.routers||1)>=2?1:0:0,t,hov?1:0,hasIxAt(C.id)?1:0,CM.off.has(C.id)?1:0,CM.inc.has(C.id)?1:0,ph].join('|');
|
||||
stamp(key,36,36,18,18,1,sx,sy,()=>cityFarStatic(C,p,down,sel,hov,ph));
|
||||
const t=cTier(C.id),ph=sel?Math.floor(now/40)%8:-1,f=(p?1:0)|(down?2:0)|(p&&p.site2?4:0)|(p&&(p.routers||1)>=2?8:0)|(hov?16:0)|(hasIxAt(C.id)?32:0)|(CM.off.has(C.id)?64:0)|(CM.inc.has(C.id)?128:0);
|
||||
// The key, kept per city and built again only when the picture changes.
|
||||
let m=FKEY.get(C.id);if(!m||m.f!==f||m.t!==t||m.ph!==ph)FKEY.set(C.id,m={f,t,ph,k:'far|'+t+'|'+f+'|'+ph});
|
||||
stamp(m.k,36,36,18,18,1,sx,sy,()=>cityFarStatic(C,p,down,sel,hov,ph));
|
||||
if(lyOverride!==null&&(p||sel||hov)){ctx.font=`800 ${p?12:11}px "Baloo 2", sans-serif`;const t=C.name,wk=C.id+(p?1:0);let tw=LBLF.get(wk);if(tw===undefined)LBLF.set(wk,tw=ctx.measureText(t).width+12);const ly=lyOverride==null?sy+15:lyOverride;
|
||||
pill(t,ctx.font,tw,16,8,p?'#FF4F9A':'#FFFFFF',p?'#FFFFFF':'#1E2640','#1E2640',1.6,sx,ly,1);}
|
||||
}
|
||||
|
|
@ -79,18 +85,25 @@ function cityStatic(C,p,t,cs,k,ph,st){const down=st.down;
|
|||
if(st.inc){ctx.fillStyle='#FF4D5E';ctx.beginPath();ctx.arc(-20,-h+2,7,0,7);ctx.fill();ctx.lineWidth=2;ctx.stroke();ctx.fillStyle='#fff';ctx.font='900 11px Nunito, sans-serif';ctx.textAlign='center';ctx.textBaseline='middle';ctx.fillText('!',-20,-h+3);}
|
||||
}
|
||||
// The picture of a city in a given state, at the quantised scale kq: painted once and kept until it is not needed.
|
||||
function citySprite(C,p,t,cs,kq,ph,st){
|
||||
const key=['city',C.id,t,kq,st.down?1:0,p?1:0,p&&p.site2?1:0,p&&p.site2&&siteDown(C.id,'b')?1:0,p&&p.ringcut>S.day?1:0,p?(p.routers||1):0,p&&p.gen?1:0,p?(p.rgen||0):0,C.au?1:0,
|
||||
st.ixDraw?1:0,st.ixHot?1:0,ixName(C.id)||'',S.myIx[C.id]?1:0,hasIxAt(C.id)?1:0,!p&&ixQuiet(C.id)?1:0,st.feed?1:0,st.sel?ph.sel:-1,ph.dd,st.inc?1:0].join('|');
|
||||
// Its key is kept per city (CKEY) and joined again only when one of its parts changes: the yes/no parts are bits of f.
|
||||
const CKEY=new Map();
|
||||
function citySprite(C,p,t,cs,kq,ph,st){const id=C.id,
|
||||
f=(st.down?1:0)|(p?2:0)|(p&&p.site2?4:0)|(p&&p.site2&&siteDown(id,'b')?8:0)|(p&&p.ringcut>S.day?16:0)|(p&&p.gen?32:0)|(C.au?64:0)|(st.ixDraw?128:0)|
|
||||
(st.ixHot?256:0)|(S.myIx[id]?512:0)|(hasIxAt(id)?1024:0)|(!p&&ixQuiet(id)?2048:0)|(st.feed?4096:0)|(st.inc?8192:0),
|
||||
ro=p?(p.routers||1):0,rg=p?(p.rgen||0):0,ix=ixName(id)||'',sp=st.sel?ph.sel:-1,dd=ph.dd;
|
||||
let m=CKEY.get(id);
|
||||
if(!m||m.t!==t||m.kq!==kq||m.f!==f||m.ro!==ro||m.rg!==rg||m.ix!==ix||m.sp!==sp||m.dd!==dd)CKEY.set(id,m={t,kq,f,ro,rg,ix,sp,dd,k:['city',id,t,kq,f,ro,rg,ix,sp,dd].join('|')});
|
||||
const e=spriteHas(m.k);if(e)return e;
|
||||
const W=Math.ceil(92*kq),H=Math.ceil(84*kq),ax=45*kq,ay=46*kq;
|
||||
return spriteEntry(key,W,H,ax,ay,g=>{g.translate(ax,ay);cityStatic(C,p,t,cs,kq,ph,st);});}
|
||||
return spriteEntry(m.k,W,H,ax,ay,g=>{g.translate(ax,ay);cityStatic(C,p,t,cs,kq,ph,st);});}
|
||||
// The parts that move, in the city's frame at the true scale k.
|
||||
function cityMoving(C,p,t,cs,k,sx,sy,now,h,st,hot){const down=st.down,K=k*cs;
|
||||
if(p){// The POP's own frame is shifted 2 down, and mirrored Down Under (C.au): mp() takes a point of that frame to the city's.
|
||||
const fl=!!C.au,mp=(x,y)=>fl?[-x,-y-2]:[x,y+2],sf=sfOf(R,C.id),at=(x,y)=>{const q=mp(x,y);return [sx+q[0]*K,sy+q[1]*K];};
|
||||
if(!down&&(R.nodeLoad[C.id]||0)>=(R.nodeCap[C.id]||1e12)*0.95){for(let i=0;i<3;i++){const tt=(now/700+i/3)%1,[x,y]=at(6+i*3,-12-tt*18);dot(x,y,(3+tt*4)*K,'rgba(90,98,130,1)',0.55*(1-tt));}}
|
||||
// A point (x,y) of that frame is at sx+(fl?-x:x)*K, sy+(fl?-y-2:y+2)*K (written out: no array per light per frame).
|
||||
const fl=!!C.au,sf=sfOf(R,C.id);
|
||||
if(!down&&(R.nodeLoad[C.id]||0)>=(R.nodeCap[C.id]||1e12)*0.95){for(let i=0;i<3;i++){const tt=(now/700+i/3)%1,x=6+i*3,y=-12-tt*18;dot(sx+(fl?-x:x)*K,sy+(fl?-y-2:y+2)*K,(3+tt*4)*K,'rgba(90,98,130,1)',0.55*(1-tt));}}
|
||||
// The four lights: a disc each, lit or not.
|
||||
for(let i=0;i<4;i++){const lit=down?false:(((now/(160+i*37))|0)+i)%3!==0,[x,y]=at(-10+i*6.6,1),col=down?'#3B4260':lit?(sf<0.9?'#FF4D5E':'#7CFFB2'):'#2A3150';dot(x,y,2.1*K,col,1);}
|
||||
for(let i=0;i<4;i++){const lit=down?false:(((now/(160+i*37))|0)+i)%3!==0,x=-10+i*6.6,col=down?'#3B4260':lit?(sf<0.9?'#FF4D5E':'#7CFFB2'):'#2A3150';dot(sx+(fl?-x:x)*K,sy+(fl?-3:3)*K,2.1*K,col,1);}
|
||||
}
|
||||
// A flashing IX: two pictures, one per colour.
|
||||
if(st.ixDraw&&st.ixHot){const on=Math.sin(now/180)>0,ix=p?19:SKY_W[t]/2+4;stamp('ixhot'+(on?1:0),22,22,11,11,K,sx+ix*K,sy+(2-h)*K,()=>{ctx.translate(-ix,h-2);ctx.strokeStyle='#1E2640';cityIX(C,p,t,h,true,on?1:-1);},true);}
|
||||
|
|
@ -106,7 +119,8 @@ function drawCity(C,sx,sy,sc,now,lyOverride,dem){
|
|||
const p=S.pops[C.id],down=p&&p.down>S.day,sel=UI.sel&&UI.sel.type==='city'&&UI.sel.id===C.id,hov=UI.hover===C.id||(UI.snap&&UI.snap.id===C.id);
|
||||
// No room for its skyline here: a quiet dot, a little bigger for bigger cities, until you zoom in (cityRoom).
|
||||
if(dem&&!p&&!sel&&!hov){const r=(2.6+cTier(C.id)*0.35)*sc,rq=Math.round(r*4)/4,fill=hasIxAt(C.id)?'#FFD23F':'#FFFFFF',S2=Math.ceil(rq*2+4);
|
||||
const e=spriteEntry(['quiet',rq,fill].join('|'),S2,S2,S2/2,S2/2,g=>{g.translate(S2/2,S2/2);g.fillStyle=fill;g.strokeStyle='#1E2640';g.lineWidth=1.6;g.beginPath();g.arc(0,0,rq,0,7);g.fill();g.stroke();});
|
||||
const qk=rq*8+(fill==='#FFFFFF'?0:1);let key=QKEY.get(qk);if(!key)QKEY.set(qk,key=['quiet',rq,fill].join('|'));
|
||||
const e=spriteHas(key)||spriteEntry(key,S2,S2,S2/2,S2/2,g=>{g.translate(S2/2,S2/2);g.fillStyle=fill;g.strokeStyle='#1E2640';g.lineWidth=1.6;g.beginPath();g.arc(0,0,rq,0,7);g.fill();g.stroke();});
|
||||
blit(e,sx-e.ax,sy+2*sc-e.ay,e.w,e.h);return;}
|
||||
const t=cTier(C.id),cs=p?1:CSC[t],k=sc*(hov?1.15:1),kq=sprQ(k);
|
||||
const ixn=ixName(C.id),ixDraw=!!ixn&&(ixShown(C.id,p)||(hot&&hot.ix)||sel||hov),ixHot=!!(ixDraw&&hot&&hot.ix);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue