Commit b8f78f0a authored by Joe Libipl's avatar Joe Libipl

First pass at moving NPCs to CellLoc coordinates.

parent c1c17d48
......@@ -23,9 +23,10 @@ public class GUIManager : MonoBehaviour {
mainCamera.ReturnToHome();
}
if (Input.GetKeyDown (KeyCode.N)) {
Vector3 randomNpcCoords = npcManager.locateRandomNpc();
mainCamera.lookAtNpc(randomNpcCoords);
mapManager.UpdateZ(randomNpcCoords.z + 1f);
CellLoc randomNpcCell = npcManager.locateRandomNpc();
Vector3 randomNpcPos = CLUtils.CellLocToVector3(randomNpcCell, 1);
mainCamera.lookAtNpc(randomNpcPos);
mapManager.UpdateZ(randomNpcPos.z + 1f);
}
}
......
......@@ -84,7 +84,7 @@ public class MapManager : MonoBehaviour {
num_cells++;
if (cellNeedsTransform[c]) {
// Instantiate this one
Vector3 position = CLUtils.CellLocToVector3(new CellLoc(cells[c].x, cells[c].y, cells[c].z));
Vector3 position = CLUtils.CellLocToVector3(new CellLoc(cells[c].x, cells[c].y, cells[c].z), 0);
Transform tempts = Instantiate(prefab,
position,
......@@ -142,8 +142,8 @@ public class MapManager : MonoBehaviour {
}
}
// 5. Update the position, scale, and texture scale.
Vector3 firstCellPosition = CLUtils.CellLocToVector3(c);
Vector3 lastCellPosition = CLUtils.CellLocToVector3(new CellLoc(c.x + (bestXSize - 1), c.y + (bestYSize - 1), c.z));
Vector3 firstCellPosition = CLUtils.CellLocToVector3(c, 0);
Vector3 lastCellPosition = CLUtils.CellLocToVector3(new CellLoc(c.x + (bestXSize - 1), c.y + (bestYSize - 1), c.z), 0);
tempts.position = (firstCellPosition + lastCellPosition) / 2f;
// tempts.position += new Vector3(-0.5f * (bestXSize - 1), -0.5f * (bestYSize - 1), 0f);
tempts.localScale += new Vector3((float) (bestXSize - 1), (float) (bestYSize - 1), 0f);
......@@ -152,7 +152,7 @@ public class MapManager : MonoBehaviour {
rend.material.mainTextureScale = new Vector2((-1f) * tempts.localScale.x, (-1f) * tempts.localScale.y);
}
if (cells[c].isMapPortal) {
Vector3 position = CLUtils.CellLocToVector3(new CellLoc(cells[c].x, cells[c].y, cells[c].z));
Vector3 position = CLUtils.CellLocToVector3(new CellLoc(cells[c].x, cells[c].y, cells[c].z), 0);
position.z += 0.5f;
Transform tempts = Instantiate(portalPrefab,
position,
......
......@@ -22,8 +22,8 @@ public class NpcManager : MonoBehaviour {
System.Random myRandom = new System.Random();
private Dictionary<int,NpcScript> npcScripts;
private Dictionary<int, Vector3> npcLocations;
private Dictionary<Vector3,List<int>> cellsContainingNpcs = new Dictionary<Vector3,List<int>>();
private Dictionary<int,CellLoc> npcLocations;
private Dictionary<CellLoc,List<int>> cellsContainingNpcs = new Dictionary<CellLoc,List<int>>();
public int NumNpcScripts {
get { return this.npcScripts.Count; }
......@@ -49,7 +49,7 @@ public class NpcManager : MonoBehaviour {
void Start () {
npcScripts = new Dictionary<int,NpcScript>();
npcLocations = new Dictionary<int, Vector3>();
npcLocations = new Dictionary<int,CellLoc>();
nextDBUpdate = Time.time + dbUpdateDelta;
}
......@@ -99,12 +99,12 @@ public class NpcManager : MonoBehaviour {
}
} else {
if (!activeGameRounds.Contains(npc.lastActiveRound)) activeGameRounds.Add(npc.lastActiveRound);
Vector3 cell = new Vector3(npc.X, npc.Y, npc.Z);
CellLoc cell = new CellLoc(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);
Vector3 position = CLUtils.CellLocToVector3(cell, 1);
if (!npcScripts.ContainsKey(npc.worldNpcID)) {
// Try to find an inactive one to re-use
......@@ -132,9 +132,10 @@ public class NpcManager : MonoBehaviour {
tempNpc.toBeSeen = (position.z <= mapManager.zTop + 1);
tempNpc.gameObject.SetActive(true);
npcScripts[npc.worldNpcID] = tempNpc;
}
npcScripts[npc.worldNpcID].newPosition = position;
npcScripts[npc.worldNpcID].lastActiveRound = npc.lastActiveRound;
}
npcScripts[npc.worldNpcID].cell = cell;
npcScripts[npc.worldNpcID].newPosition = position;
npcScripts[npc.worldNpcID].lastActiveRound = npc.lastActiveRound;
npcScripts[npc.worldNpcID].UpdateMaterial(maxGameRound);
npcScripts[npc.worldNpcID].Hits = npc.Hits;
npcScripts[npc.worldNpcID].HitsFull = npc.HitsFull;
......@@ -158,28 +159,24 @@ public class NpcManager : MonoBehaviour {
}
private void UpdateNpcCohab() {
foreach (Vector3 cell in cellsContainingNpcs.Keys) {
foreach (CellLoc 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));
int row = 0, column = 0;
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;
column += 1;
if (column >= dimension) { column = 0; row += 1; }
short dimension = (short) Math.Ceiling(Math.Sqrt(population));
short idx = 0;
foreach (int npcId in cellsContainingNpcs[cell]) {
npcScripts[npcId].newScale = new Vector3(1.0f/dimension, 1.0f/dimension, npcScripts[npcId].transform.localScale.z);
npcScripts[npcId].cell.idx = idx;
npcScripts[npcId].cell.dim = dimension;
npcScripts[npcId].newPosition = CLUtils.CellLocToVector3(npcScripts[npcId].cell, 1);
idx += 1;
}
}
}
public Vector3 locateRandomNpc() {
if (cellsContainingNpcs.Count == 0) return Vector3.zero;
List<Vector3> cells = new List<Vector3>(cellsContainingNpcs.Keys);
public CellLoc locateRandomNpc() {
if (cellsContainingNpcs.Count == 0) return new CellLoc(0,0,0);
List<CellLoc> cells = new List<CellLoc>(cellsContainingNpcs.Keys);
return cells[myRandom.Next(cells.Count)];
}
......@@ -191,9 +188,9 @@ public class NpcManager : MonoBehaviour {
}
public void UpdateZ(float z) {
foreach (Vector3 v in cellsContainingNpcs.Keys) {
foreach (int npcId in cellsContainingNpcs[v]) {
npcScripts[npcId].toBeSeen = (v.z <= z);
foreach (CellLoc c in cellsContainingNpcs.Keys) {
foreach (int npcId in cellsContainingNpcs[c]) {
npcScripts[npcId].toBeSeen = (c.z <= z);
}
}
}
......
using UnityEngine;
using System.Collections;
public class NpcScript : MonoBehaviour {
public class NpcScript : MonoBehaviour {
public CellLoc cell;
public Vector3 newPosition, newScale;
Vector3 oldPosition, oldScale;
float timeSinceStable;
......
......@@ -260,8 +260,8 @@ Transform:
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1529516578}
m_LocalRotation: {x: 0, y: 1, z: 0, w: -4.37113883e-08}
m_LocalPosition: {x: -40, y: -10, z: 14}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 40, y: -10, z: -8}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 55464512}
......
......@@ -5,32 +5,45 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
[System.Serializable]
public struct CellLoc {
public short x;
public short y;
public int z;
public short idx; // index of this space in the cell
public short idx; // index of this space in the cell (zero-based)
public short 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;
this.idx = 0; 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;
this.idx = 0; this.dim = 1;
}
}
public class CLUtils {
public static Vector3 CellLocToVector3(CellLoc c) {
public static Vector3 CellLocToVector3(CellLoc c, int layer) {
// note: layer 0 for ground, 1 for characters, etc
if (c.dim == 1) { // the simple case
return new Vector3((1f) * c.x,
(-1f) * c.y,
(-0.1f) * c.z);
(-0.1f) * (c.z + layer));
} else {
return Vector3.zero;
Vector3 position;
int xPos = c.idx % c.dim;
int yPos = c.idx / c.dim;
position.x = c.x - 0.5f; // left side
position.x += (1f / (c.dim * 2f)); // first position
position.x += xPos * (1f / c.dim);
position.y = c.y - 0.5f; // top side
position.y += (1f / (c.dim * 2f)); // first position
position.y += yPos * (1f / c.dim);
position.y *= -1f;
position.z = (-0.1f) * (c.z + layer);
return position;
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment