Continuing what we started, bringing the NPCs into the new CellLoc reality.
First Impression
Yesterday we moved the map to the new CellLoc coordinates, leaving the NPCs floating around looking foolish. To fix that, we’ll need to expand the CellLoc struct’s capabilities.
[System.Serializable]publicstructCellLoc{publicshortx;publicshorty;publicintz;publicshortidx;// index of this space in the cell (zero-based)publicshortdim;// number of subcells on a row, and also number of rowspublicCellLoc(shortx,shorty,intz){this.x=x;this.y=y;this.z=z;this.idx=0;this.dim=1;}publicCellLoc(intx,inty,intz){this.x=(short)x;this.y=(short)y;this.z=z;this.idx=0;this.dim=1;}}publicclassCLUtils{publicstaticVector3CellLocToVector3(CellLocc,intlayer){// note: layer 0 for ground, 1 for characters, etcif(c.dim==1){// the simple casereturnnewVector3((1f)*c.x,(-1f)*c.y,(-0.1f)*(c.z+layer));}else{Vector3position;intxPos=c.idx%c.dim;intyPos=c.idx/c.dim;position.x=c.x-0.5f;// left sideposition.x+=(1f/(c.dim*2f));// first positionposition.x+=xPos*(1f/c.dim);position.y=c.y-0.5f;// top sideposition.y+=(1f/(c.dim*2f));// first positionposition.y+=yPos*(1f/c.dim);position.y*=-1f;position.z=(-0.1f)*(c.z+layer);returnposition;}}}
Changing the index to zero-based cleaned up the math a bit, although it’s still definitely subject to improvement. We also take a layer in the conversion method to tweak the z-value.
That [System.Serializable] line is to allow a CellLoc to show up in the Unity editor, just like a Vector3 would:
As with the map, there were many changes to get from Vector3 to CellLoc. Notably, UpdateNpcCohab is much simplified:
privatevoidUpdateNpcCohab(){foreach(CellLoccellincellsContainingNpcs.Keys){intpopulation=cellsContainingNpcs[cell].Count;// print("Cell " + cell + " has an NPC population of " + population);shortdimension=(short)Math.Ceiling(Math.Sqrt(population));shortidx=0;foreach(intnpcIdincellsContainingNpcs[cell]){npcScripts[npcId].newScale=newVector3(1.0f/dimension,1.0f/dimension,npcScripts[npcId].transform.localScale.z);npcScripts[npcId].cell.idx=idx;npcScripts[npcId].cell.dim=dimension;npcScripts[npcId].newPosition=CLUtils.CellLocToVector3(npcScripts[npcId].cell,1);idx+=1;}}}
We’re still updating newPosition along with the cellular location, for now. How do we look?
Sorry for the short blog today. Trying to keep in good health, despite the weather and coworkers trying to make me sick, by getting a little extra rest.