But why? Reading in a map file is good enough, right?
No, because the map changes. Doors are opened and closed, trees are burnt down, walls are blown up, etc.
But by now we should certainly know how to keep track of stuff.
Server side
As usual, we shove things into the database as they change, queueing them up to do one big update per round…
voidUpdate(){if(updateComplete&&Time.time>nextDBUpdate){print("Time to read from the DB! "+Time.time);nextDBUpdate=Time.time+dbUpdateDelta;updateComplete=false;intdoorsOpen=0;foreach(KeyValuePair<CellLoc,CellLite>pairincells){if(pair.Value.displayGraphic=="/ "||pair.Value.displayGraphic=="\\ "){doorsOpen+=1;}}print("Open doors: "+doorsOpen);UpdateCellContents();}}
Let this run for a bit, and…
$ grep "Open doors:" Editor.log
Open doors: 0
Open doors: 113
Open doors: 116
Open doors: 115
So we’re able to read the DB and update the display graphic. But we need to make that useful…
***
Visualizer Side pt 2
Time to upgrade the cell objects in the scene. CellScript is a slightly modified simpler version of NpcScript that we’ll attach to the cell prefab.
publicclassCellScript:MonoBehaviour{publicCellLoccellLocation;publicVector3newPosition,newScale;Vector3oldPosition,oldScale;floattimeSinceStable;publicbooltoBeSeen;// independent of local concerns, does the UI want to see me?publicstringdisplayString;privateMaterialcurrMaterial;publicMaterialCurrMaterial{get{returncurrMaterial;}set{if(currMaterial!=value&&myRend!=null){currMaterial=value;myRend.material=currMaterial;myRend.material.mainTextureScale=newVector2(-1f,-1f);oldScale=transform.localScale=Vector3.zero;}}}privateRenderermyRend;publicMapManagermapManager;voidAwake(){Reset();myRend=GetComponent<Renderer>();}publicvoidReset(){oldPosition=newPosition=transform.position;oldScale=newScale=transform.localScale;timeSinceStable=0f;toBeSeen=true;}voidUpdate(){timeSinceStable+=Time.deltaTime;if(transform.position!=newPosition){transform.position=Vector3.Slerp(oldPosition,newPosition,timeSinceStable);if(timeSinceStable>=1f)transform.position=newPosition;}if(transform.localScale!=newScale){transform.localScale=Vector3.Slerp(oldScale,newScale,timeSinceStable);if(timeSinceStable>=1f)transform.localScale=newScale;}if(transform.localScale==newScale&&transform.position==newPosition){timeSinceStable=0f;oldPosition=transform.position;oldScale=transform.localScale;}// disappear if desiredmyRend.enabled=toBeSeen;}}
This required a few changes to MapManager. Disabling the chunking for now, and keeping a dictionary of CellScript objects instead of transforms, we instantiate a little differently:
voidUpdate(){if(updateComplete&&Time.time>nextDBUpdate){print("Time to read from the DB! "+Time.time);nextDBUpdate=Time.time+dbUpdateDelta;updateComplete=false;intdoorsOpen=0;foreach(KeyValuePair<CellLoc,CellLite>pairincells){if(!cellMaterials.ContainsKey(pair.Value.displayGraphic)){StringmaterialName=StringToAsciiHex(pair.Value.displayGraphic);MaterialcellMaterial=Resources.Load("Materials/Cells/"+materialName,typeof(Material))asMaterial;if(cellMaterial==null){print("Couldn't find material "+materialName+" for display graphic \""+pair.Value.displayGraphic+"\"!");Debug.Break();}else{cellMaterials[pair.Value.displayGraphic]=cellMaterial;}}cell_objects[pair.Key].CurrMaterial=cellMaterials[pair.Value.displayGraphic];if(pair.Value.displayGraphic=="/ "||pair.Value.displayGraphic=="\\ "){doorsOpen+=1;}}print("Open doors: "+doorsOpen);UpdateCellContents();}}
With that and a few other tweaks, we see the results pretty quickly.
Doors are being opened by NPCs, and the trees change because of the randomness in their placement when the server loads the map.