301 Days

A year of gamedev experiments.

Day 9 - a Little Tidier

| Comments

Get rid of that pesky NPC overlap…


“We seek peaceful coexistence.”

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:

Not too difficult to deal with, it turns out. Just split out the cohabitation handling and maintain the cellsContainingNpcs dictionary at the MapManager level:

Managers/MapManager.csGitLab
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
void Update () {
    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();
    }
}

void UpdateNpcs(string npcName) {
    Material liveNpc = Resources.Load("Materials/" + npcName, typeof(Material)) as Material;
    Material exNpc = Resources.Load("Materials/ex" + npcName, typeof(Material)) as Material;
    List<NPC> npcs = DragonsSpine.DAL.DBNPC.GetNpcs(npcName);
    print("I found " + npcs.Count + " NPCs called " + npcName);
    foreach (NPC npc in npcs) {
        if (npc.lastActiveRound > maxGameRound) maxGameRound = npc.lastActiveRound;
    }
    foreach (NPC npc in npcs) {
        if (!activeGameRounds.Contains(npc.lastActiveRound)) activeGameRounds.Add(npc.lastActiveRound);
        Vector3 cell = new Vector3(npc.X, npc.Y, npc.Z);
        npcLocations[npc.worldNpcID] = cell;
        if (!cellsContainingNpcs.ContainsKey(cell))
            cellsContainingNpcs[cell] = new List<int>();
        cellsContainingNpcs[cell].Add(npc.worldNpcID);
        Vector3 position = new Vector3((-1f) * npc.X, (-1f) * npc.Y, (0.1f * npc.Z) + 0.2f);
        if (npcTransforms.ContainsKey(npc.worldNpcID)) {
            npcTransforms[npc.worldNpcID].position = position;
        } else {
            Transform tempts = Instantiate(npcPrefab,
                                           position,
                                           Quaternion.identity) as Transform;
            npcTransforms[npc.worldNpcID] = tempts;
        }
        Renderer rend = npcTransforms[npc.worldNpcID].GetComponent<Renderer>();
        if (npc.lastActiveRound >= maxGameRound-1) // one round leeway in case we catch the DB in mid-update
            rend.material = liveNpc;
        else
            rend.material = exNpc;
    }
}

private void UpdateNpcCohab() {
    foreach (Vector3 cell in cellsContainingNpcs.Keys) {
        int population = cellsContainingNpcs[cell].Count;
        print("Cell " + cell + " has an NPC population of " + population);
        int dimension = (int) Math.Ceiling(Math.Sqrt(population));
        print("We can fit those in a " + dimension + "x" + dimension + " grid.");
        int row = 0, column = 0;
        foreach (int npcId in cellsContainingNpcs[cell]) {
            print("NPC " + row + "," + column + " is " + npcId);
            npcTransforms[npcId].localScale = new Vector3(1.0f/dimension, 1.0f/dimension, npcTransforms[npcId].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
            npcTransforms[npcId].position = position - new Vector3(column * (1.0f/dimension), row * (1.0f/dimension), 0f);
            npcTransforms[npcId].position -= new Vector3((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.


ANSI Visuals

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.


Day 9 code - client

Comments