301 Days

A year of gamedev experiments.

Day 12 - More Sweeping Up

| Comments

Trim away a little bit of technical debt, or at least make things look a little better.


What are those NPCs doing there?

When we change the top Z layer, we’re only enabling/disabling map cells, nothing else. Hence the NPCs (and portals) floating in space.

All we need is a little structural reorg for the portals, and this:

Managers/MapManager.csGitLab
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
public void UpdateZ(float z) {
    Renderer currRend;
    foreach (Vector3 v in cell_transforms.Keys) {
        currRend = cell_transforms[v].GetComponent<Renderer>();
        currRend.enabled = (v.z <= z);
    }
    foreach (Vector3 v in cellsContainingNpcs.Keys) {
        foreach (int npcId in cellsContainingNpcs[v]) {
            currRend = npcTransforms[npcId].GetComponent<Renderer>();
            currRend.enabled = (v.z <= z);
        }
    }
    foreach (Vector3 v in portalTransforms.Keys) {
        currRend = portalTransforms[v].GetComponent<Renderer>();
        currRend.enabled = (v.z <= z);
    }
    zTop = z;
}

Done.


Get rid of the big list - Act One

This started as a quick hack so I could mess with the cell materials directly in the Unity editor, and quickly entrenched itself. Time to pull it out by the roots.

We should translate the display string directly into a material name, but a lot of those strings have special characters that don’t play nice with the file system. So we’ll fall back on ASCII codes:

Managers/MapManager.csGitLab
318
319
320
321
String StringToAsciiHex(String s) {
    byte[] ASCIIValues = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, (Encoding.Unicode).GetBytes(s));
    return BitConverter.ToString(ASCIIValues);
}

…and then we can replace the weird parallel array lookup with:

Managers/MapManager.csGitLab
100
101
102
103
104
105
106
107
108
109
110
111
if (!cellMaterials.ContainsKey(cells[v].DisplayGraphic)) {
    String materialName = StringToAsciiHex(cells[v].DisplayGraphic);
    Material cellMaterial = Resources.Load("Materials/Cells/" + materialName, typeof(Material)) as Material;
    if (cellMaterial == null) {
        print("Couldn't find material " + materialName + " for display graphic \"" + cells[v].DisplayGraphic + "\"!");
        Debug.Break();
    } else {
        cellMaterials[cells[v].DisplayGraphic] = cellMaterial;
    }
}
rend.material = cellMaterials[cells[v].DisplayGraphic];
rend.enabled = true;

We maintain a dictionary of cell materials so we don’t have to call Resources.Load for every cell. Now all we need is the materials: So now we can have: …with no actual change in functionality, and it seems a bit faster, too!


Next time

We’ll tackle this thing that keeps getting worse:

Managers/MapManager.csGitLab
174
175
176
177
178
179
180
181
182
183
184
185
186
    UpdateCellContents();
    cellsContainingNpcs.Clear();
    UpdateNpcs("Phong");
    UpdateNpcs("Rocky.T.Orc");
    UpdateNpcs("Ydnac");
    UpdateNpcs("boar");
    UpdateNpcs("bear");
    UpdateNpcs("crocodile");
    UpdateNpcs("orc");
    UpdateNpcs("wolf");
    UpdateNpcs("centaur");
    UpdateNpcs("griffin");
    UpdateNpcCohab();

Day 12 code - client

Comments