foreach (int npcId in cellsContainingNpcs[cell]) {
Vector3 scale = new Vector3(1.0f/dimension, 1.0f/dimension, npcScripts[npcId].transform.localScale.z);
Vector3 position = new Vector3((-1f) * cell.x, (-1f) * cell.y, (0.1f * cell.z) + 0.2f); // start at the center of the cell
position += new Vector3(0.5f, 0.5f, 0f); // move to corner
position -= new Vector3(column * (1.0f/dimension), row * (1.0f/dimension), 0f);
position -= new Vector3((1.0f/dimension)/2f, (1.0f/dimension)/2f, 0f);
npcScripts[npcId].newPosition = position;
npcScripts[npcId].newScale = scale;
The natural coordinates of a cell are discrete values, but we’re using a Vector3 (three floating point values) everywhere. Expensive. Plus, we’re making the conversion from the
server coordinate system to Unity’s left-hand Cartesian coordinate system in multiple places.
From screen-coordinates-plus-altitude to Cartesian southpaw.
publicstructCellLoc {
publicshort x;
publicshort y;
publicint z;
publicshort idx; // index of this space in the cell
publicshort dim; // number of subcells on a row, and also number of rows
public CellLoc(short x, short y, int z) {
this.x = x; this.y = y; this.z = z;
this.idx = this.dim = 1;
}
public CellLoc(int x, int y, int z) {
this.x = (short) x; this.y = (short) y; this.z = z;
this.idx = this.dim = 1;
}
}
publicclassCLUtils {
publicstatic Vector3 CellLocToVector3(CellLoc c) {
if (c.dim == 1) { // the simple case
returnnew Vector3((1f) * c.x,
(-1f) * c.y,
(-0.1f) * c.z);
} else {
return Vector3.zero;
}
}
}
Not handling the multiple-entities-in-a-cell case yet, but this structure is sufficient for the map:
if (cellNeedsTransform[c]) {
// Instantiate this one
Vector3 position = CLUtils.CellLocToVector3(new CellLoc(cells[c].x, cells[c].y, cells[c].z));
Transform tempts = Instantiate(prefab,
position,
Quaternion.identity) as Transform;
We store and index on CellLocs, and only convert to Vector3 when we need to. We still need to push CellLoc into the CellLite class, as you can see above.
We’ve also switched conversions, from negating x and y to negating y and z. It seems more natural this way, but it does make our textures upside down and backwards, ergo:
BTW, tried some stuff with Octopress 3 and decided not to make that move yet. I’ve also seen a lot of lockups in Unity while using Visual Studio for debugging. Oh well.