Skip to main content
  1. Posts/

Day 32 - PC

OldDays ste-reez-muvi drag-spin-exp Unity csharp
Table of Contents

Finally bring some PCs in.

I’m a PC and I’m way cool.
I’m a PC and I’m way cool.


Coding
#

Adding PCs
#

Starting out simple, we just make a LivePC table that’s essentially the same as the LiveNPC table, and start pushing regular PC updates to it. Then we augment the NPCLite class to include an isPC flag, and let the NpcManager handle it.

Assets/Managers/NpcManager.cs
116
117
118
try {
  result = DragonsSpine.DAL.DBNPC.GetAllNpcsAndPcs(mapNum);
} catch (Exception e) {
Assets/DragonsSpine/DAL/DBNPC.cs
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
public static List<NPCLite> GetAllNpcsAndPcs(int mapNum) {
  List<NPCLite> npclist = new List<NPCLite>();

  using (SqlConnection tempConnection = DataAccess.GetSQLConnection())
  using (SqlStoredProcedure sp = new SqlStoredProcedure("prApp_LiveNPC_by_MapID", tempConnection)) {
    sp.AddParameter("@facet", SqlDbType.Int, 4, ParameterDirection.Input, 0);
    sp.AddParameter("@map", SqlDbType.Int, 4, ParameterDirection.Input, mapNum);
    using (DataTable dtNPCs = sp.ExecuteDataTable()) {
      foreach (DataRow dr in dtNPCs.Rows) {
        NPCLite npc = new NPCLite();
        npc.worldNpcID = Convert.ToInt32(dr["uniqueId"]);
        npc.lastActiveRound = Convert.ToInt32(dr["lastActiveRound"]);
        npc.Name = dr["name"].ToString();
        npc.X = Convert.ToInt16(dr["xCord"]);
        npc.Y = Convert.ToInt16(dr["yCord"]);
        npc.Z = Convert.ToInt16(dr["zCord"]);

        npc.HitsFull = Convert.ToInt32(dr["fullHits"]);
        npc.Hits = Convert.ToInt32(dr["hits"]);

        npc.hasMostHated = !(dr.IsNull(dtNPCs.Columns["mostHatedId"]));
        if (npc.hasMostHated) {
          npc.mostHatedId = Convert.ToInt32(dr["mostHatedId"]);
        }
        npclist.Add(npc);
      }
    }
  }

  using (SqlConnection tempConnection = DataAccess.GetSQLConnection())
  using (SqlStoredProcedure sp = new SqlStoredProcedure("prApp_LivePC_by_MapID", tempConnection)) {
    sp.AddParameter("@facet", SqlDbType.Int, 4, ParameterDirection.Input, 0);
    sp.AddParameter("@map", SqlDbType.Int, 4, ParameterDirection.Input, mapNum);
    using (DataTable dtNPCs = sp.ExecuteDataTable()) {
      foreach (DataRow dr in dtNPCs.Rows) {
        NPCLite npc = new NPCLite();
        npc.worldNpcID = Convert.ToInt32(dr["uniqueId"]);
        npc.lastActiveRound = Convert.ToInt32(dr["lastActiveRound"]);
        npc.Name = dr["name"].ToString();
        npc.X = Convert.ToInt16(dr["xCord"]);
        npc.Y = Convert.ToInt16(dr["yCord"]);
        npc.Z = Convert.ToInt16(dr["zCord"]);

        npc.HitsFull = Convert.ToInt32(dr["fullHits"]);
        npc.Hits = Convert.ToInt32(dr["hits"]);

        npc.isPC = true;
        npclist.Add(npc);
      }
    }
  }
  
  return npclist;
}

Ugly, yes, but it’ll get us started.

We add a Material for our favorite test account, Testttt, and we’re in business:

Hi there!
Hi there!

Follow cam
#

We want to be able to have the camera follow the NPC or PC that we’ve selected. We’ll expand the “look at a random NPC” code for now:

Assets/Managers/GUIManager.cs
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void Update () {
  if (followingCharId != 0) {
    CellLoc newFollowingCL = npcManager.locateNpc(followingCharId);
    if (!newFollowingCL.Equals(followingCL)) {
      followingV3 = CLUtils.CellLocToVector3(newFollowingCL, 1);
      mainCamera.lookAtNpc(followingV3);
      if (!newFollowingCL.z.Equals(followingCL.z)) {
        mapManager.UpdateZ(newFollowingCL.z);
      }
      followingCL = newFollowingCL;
    }
  }
  if (Input.GetKeyDown(KeyCode.N)) {
    followingCharId = npcManager.getRandomNpc();
  }
  if (Input.GetKeyDown(KeyCode.P)) {
    followingCharId = npcManager.getRandomPc();
  }
  if (Input.GetKeyDown(KeyCode.F)) {
    followingCharId = 0;
  }

So now N will choose a random NPC to follow, P will choose a random PC to follow, and F will stop following. Let’s see how well it works on Testttt.

Even between Z layers!
Even between Z layers!

More to come
More to come

Day 32 code - visualizer

Day 32 code - server