We’re handling a number of different NPCs now, but the scaling and positioning code only knows about NPCs of the same type, leading to a problem:
Different types of NPCs on the same cell just overlap.
Not too difficult to deal with, it turns out. Just split out the cohabitation handling and maintain the cellsContainingNpcs dictionary at the MapManager level:
voidUpdate(){if(Time.time>nextDBUpdate){print("Time to read from the DB! "+Time.time);nextDBUpdate=Time.time+dbUpdateDelta;activeGameRounds.Clear();UpdateCellContents();cellsContainingNpcs.Clear();UpdateNpcs("Phong");UpdateNpcs("Rocky.T.Orc");UpdateNpcs("Ydnac");UpdateNpcs("boar");UpdateNpcs("crocodile");UpdateNpcs("orc");UpdateNpcs("wolf");UpdateNpcCohab();}}voidUpdateNpcs(stringnpcName){MaterialliveNpc=Resources.Load("Materials/"+npcName,typeof(Material))asMaterial;MaterialexNpc=Resources.Load("Materials/ex"+npcName,typeof(Material))asMaterial;List<NPC>npcs=DragonsSpine.DAL.DBNPC.GetNpcs(npcName);print("I found "+npcs.Count+" NPCs called "+npcName);foreach(NPCnpcinnpcs){if(npc.lastActiveRound>maxGameRound)maxGameRound=npc.lastActiveRound;}foreach(NPCnpcinnpcs){if(!activeGameRounds.Contains(npc.lastActiveRound))activeGameRounds.Add(npc.lastActiveRound);Vector3cell=newVector3(npc.X,npc.Y,npc.Z);npcLocations[npc.worldNpcID]=cell;if(!cellsContainingNpcs.ContainsKey(cell))cellsContainingNpcs[cell]=newList<int>();cellsContainingNpcs[cell].Add(npc.worldNpcID);Vector3position=newVector3((-1f)*npc.X,(-1f)*npc.Y,(0.1f*npc.Z)+0.2f);if(npcTransforms.ContainsKey(npc.worldNpcID)){npcTransforms[npc.worldNpcID].position=position;}else{Transformtempts=Instantiate(npcPrefab,position,Quaternion.identity)asTransform;npcTransforms[npc.worldNpcID]=tempts;}Rendererrend=npcTransforms[npc.worldNpcID].GetComponent<Renderer>();if(npc.lastActiveRound>=maxGameRound-1)// one round leeway in case we catch the DB in mid-updaterend.material=liveNpc;elserend.material=exNpc;}}privatevoidUpdateNpcCohab(){foreach(Vector3cellincellsContainingNpcs.Keys){intpopulation=cellsContainingNpcs[cell].Count;print("Cell "+cell+" has an NPC population of "+population);intdimension=(int)Math.Ceiling(Math.Sqrt(population));print("We can fit those in a "+dimension+"x"+dimension+" grid.");introw=0,column=0;foreach(intnpcIdincellsContainingNpcs[cell]){print("NPC "+row+","+column+" is "+npcId);npcTransforms[npcId].localScale=newVector3(1.0f/dimension,1.0f/dimension,npcTransforms[npcId].localScale.z);Vector3position=newVector3((-1f)*cell.x,(-1f)*cell.y,(0.1f*cell.z)+0.2f);// start at the center of the cellposition+=newVector3(0.5f,0.5f,0f);// move to cornernpcTransforms[npcId].position=position-newVector3(column*(1.0f/dimension),row*(1.0f/dimension),0f);npcTransforms[npcId].position-=newVector3((1.0f/dimension)/2f,(1.0f/dimension)/2f,0f);column+=1;if(column>=dimension){column=0;row+=1;}}}}
Not a very major code change, but it should get the job done.
Before making that admittedly simple fix, I spent a lot of time redoing the map visuals, and probably not for the last time. The look now resembles what you’d get from
ANSI control codes if you had a really nice computer in 1985.