Commit c3d0abdd authored by Joe Libipl's avatar Joe Libipl

Finished (for now) converting NPCs to CellLoc.

parent b8f78f0a
...@@ -12,3 +12,4 @@ ...@@ -12,3 +12,4 @@
sanctuary sanctuary
ste-reez-muvi.v12.suo
...@@ -92,8 +92,8 @@ public class CameraControlScript : MonoBehaviour { ...@@ -92,8 +92,8 @@ public class CameraControlScript : MonoBehaviour {
angle -= 360F; angle -= 360F;
} }
public void lookAtNpc(Vector3 npcCoords) { public void lookAtNpc(Vector3 npcPos) {
transform.position = new Vector3((-1f) * npcCoords.x, (-1f) * npcCoords.y, (0.1f * npcCoords.z) + 15f); transform.position = new Vector3(npcPos.x, npcPos.y, npcPos.z - 15f);
transform.LookAt(new Vector3((-1f) * npcCoords.x, (-1f) * npcCoords.y, npcCoords.z)); transform.LookAt(npcPos);
} }
} }
...@@ -21,7 +21,7 @@ namespace DragonsSpine.DAL ...@@ -21,7 +21,7 @@ namespace DragonsSpine.DAL
public static SqlConnection GetSQLConnection() public static SqlConnection GetSQLConnection()
{ {
// string sConnStr = "User ID='db1_readonly';Password='IMakeMaps';Initial Catalog='DSpineBackup';Data Source='AQUALUNG\\SQLEXPRESS';Connect Timeout=15"; // string sConnStr = "User ID='db1_readonly';Password='IMakeMaps';Initial Catalog='DSpineBackup';Data Source='AQUALUNG\\SQLEXPRESS';Connect Timeout=15";
string sConnStr = "User ID='db1_readonly';Password='IMakeMaps';Initial Catalog='dragonsspine';Data Source='localhost\\MYTESTSQL';Connect Timeout=15"; string sConnStr = "User ID='db1_readonly';Password='IMakeMaps';Initial Catalog='dragonsspine';Data Source='localhost\\MYTESTSQL';Connect Timeout=30";
Utils.Log(sConnStr, Utils.LogType.SystemFailure); Utils.Log(sConnStr, Utils.LogType.SystemFailure);
try try
{ {
......
Assets/Images/arrow_red.png

9.88 KB | W: | H:

Assets/Images/arrow_red.png

9.76 KB | W: | H:

Assets/Images/arrow_red.png
Assets/Images/arrow_red.png
Assets/Images/arrow_red.png
Assets/Images/arrow_red.png
  • 2-up
  • Swipe
  • Onion skin
using UnityEngine; using UnityEngine;
using System.Text; using System.Text;
public class GUIManager : MonoBehaviour { public class GUIManager : MonoBehaviour {
private static GUIManager instance; private static GUIManager instance;
public MapManager mapManager; public MapManager mapManager;
public NpcManager npcManager; public NpcManager npcManager;
public CameraControlScript mainCamera; public CameraControlScript mainCamera;
void Start () { void Start () {
instance = this; instance = this;
} }
void Update () { void Update () {
if (Input.GetKeyDown (KeyCode.Z)) { if (Input.GetKeyDown (KeyCode.Z)) {
mapManager.CycleZ(false); mapManager.CycleZ(false);
} }
if (Input.GetKeyDown (KeyCode.A)) { if (Input.GetKeyDown (KeyCode.A)) {
mapManager.CycleZ(true); mapManager.CycleZ(true);
} }
if (Input.GetKeyDown (KeyCode.Space)) { if (Input.GetKeyDown (KeyCode.Space)) {
mainCamera.ReturnToHome(); mainCamera.ReturnToHome();
} }
if (Input.GetKeyDown (KeyCode.N)) { if (Input.GetKeyDown (KeyCode.N)) {
CellLoc randomNpcCell = npcManager.locateRandomNpc(); CellLoc randomNpcCell = npcManager.locateRandomNpc();
Vector3 randomNpcPos = CLUtils.CellLocToVector3(randomNpcCell, 1); Vector3 randomNpcPos = CLUtils.CellLocToVector3(randomNpcCell, 1);
mainCamera.lookAtNpc(randomNpcPos); mainCamera.lookAtNpc(randomNpcPos);
mapManager.UpdateZ(randomNpcPos.z + 1f); mapManager.UpdateZ(randomNpcCell.z);
} }
} }
void OnGUI(){ void OnGUI(){
GUI.Box (new Rect (0,0,100,50), "# of cells\n" + mapManager.num_cells); GUI.Box (new Rect (0,0,100,50), "# of cells\n" + mapManager.num_cells);
GUI.Box (new Rect (100,0,100,50), "NPC Instances\n" + npcManager.NumNpcScripts); GUI.Box (new Rect (100,0,100,50), "NPC Instances\n" + npcManager.NumNpcScripts);
GUI.Box (new Rect (200,0,100,50), "Active NPC Insts\n" + npcManager.NumActiveNpcScripts); GUI.Box (new Rect (200,0,100,50), "Active NPC Insts\n" + npcManager.NumActiveNpcScripts);
GUI.Box (new Rect (300,0,100,50), "NPCs in DB\n" + npcManager.NumNpcsInDb); GUI.Box (new Rect (300,0,100,50), "NPCs in DB\n" + npcManager.NumNpcsInDb);
GUI.Box (new Rect (Screen.width - 100,0,100,50), "# of blocks\n" + mapManager.num_blocks); GUI.Box (new Rect (Screen.width - 100,0,100,50), "# of blocks\n" + mapManager.num_blocks);
GUI.Box (new Rect (0,Screen.height - 50,100,50), "Z top\n" + mapManager.zTop); GUI.Box (new Rect (0,Screen.height - 50,100,50), "Z top\n" + mapManager.zTop);
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
foreach (int gameRound in npcManager.activeGameRounds) foreach (int gameRound in npcManager.activeGameRounds)
builder.Append(gameRound).Append(" "); builder.Append(gameRound).Append(" ");
GUI.Box (new Rect (Screen.width - 100,Screen.height - 50,100,50), "Game Round\n" + builder.ToString()); GUI.Box (new Rect (Screen.width - 100,Screen.height - 50,100,50), "Game Round\n" + builder.ToString());
} }
} }
using UnityEngine; using UnityEngine;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using DragonsSpine; using DragonsSpine;
using System; using System;
using System.Data; using System.Data;
using System.Data.SqlClient; using System.Data.SqlClient;
using System.Text; using System.Text;
public class MapManager : MonoBehaviour { public class MapManager : MonoBehaviour {
public NpcManager npcManager; public NpcManager npcManager;
public int num_cells = 0; public int num_cells = 0;
public int num_blocks = 0; public int num_blocks = 0;
public float zTop = 0; public float zTop = 0;
public Transform prefab; public Transform prefab;
public Transform portalPrefab; public Transform portalPrefab;
public Map ourMap; public Map ourMap;
public int[] zPlanes; // Array of elevations of Z planes, as copied from Map public int[] zPlanes; // Array of elevations of Z planes, as copied from Map
public int currentZTopIdx; // Index into zPlanes for highest elevation currently visible public int currentZTopIdx; // Index into zPlanes for highest elevation currently visible
private Dictionary<CellLoc, Transform> cell_transforms; private Dictionary<CellLoc, Transform> cell_transforms;
public float nextDBUpdate; public float nextDBUpdate;
public float dbUpdateDelta = 5f; public float dbUpdateDelta = 5f;
private List<string> cellKeyList; private List<string> cellKeyList;
private Dictionary<CellLoc, CellLite> cells; private Dictionary<CellLoc, CellLite> cells;
private List<CellLoc> cellsKeys; private List<CellLoc> cellsKeys;
private Dictionary<CellLoc,Transform> portalTransforms; private Dictionary<CellLoc,Transform> portalTransforms;
System.Random myRandom = new System.Random(); System.Random myRandom = new System.Random();
void Awake() { void Awake() {
cells = new Dictionary<CellLoc, CellLite>(); cells = new Dictionary<CellLoc, CellLite>();
} }
void Start () { void Start () {
nextDBUpdate = Time.time + dbUpdateDelta; nextDBUpdate = Time.time + dbUpdateDelta;
ourMap = new Map(); ourMap = new Map();
ourMap.Name = "Island of Kesmai"; ourMap.Name = "Island of Kesmai";
if (LoadMapCells("Assets/IoKesmai.txt")) { if (LoadMapCells("Assets/IoKesmai.txt")) {
print ("Loaded map. " + ourMap.cells.Count + " cells loaded."); print ("Loaded map. " + ourMap.cells.Count + " cells loaded.");
} else { } else {
print ("Failed to load map!"); print ("Failed to load map!");
Application.Quit(); Application.Quit();
} }
currentZTopIdx = zPlanes.Length - 1; currentZTopIdx = zPlanes.Length - 1;
portalTransforms = new Dictionary<CellLoc,Transform>(); portalTransforms = new Dictionary<CellLoc,Transform>();
Dictionary<CellLoc, bool> cellNeedsTransform = new Dictionary<CellLoc, bool>(); Dictionary<CellLoc, bool> cellNeedsTransform = new Dictionary<CellLoc, bool>();
foreach (string k in cellKeyList) { foreach (string k in cellKeyList) {
CellLoc mapPosition = new CellLoc(ourMap.cells[k].X, ourMap.cells[k].Y, ourMap.cells[k].Z); CellLoc mapPosition = new CellLoc(ourMap.cells[k].X, ourMap.cells[k].Y, ourMap.cells[k].Z);
cellNeedsTransform[mapPosition] = true; // Every cell needs a transform by default cellNeedsTransform[mapPosition] = true; // Every cell needs a transform by default
} }
// Make a list of the cells sorted by coordinates: // Make a list of the cells sorted by coordinates:
cellsKeys = new List<CellLoc>(cells.Keys); cellsKeys = new List<CellLoc>(cells.Keys);
cellsKeys.Sort( cellsKeys.Sort(
delegate(CellLoc p1, CellLoc p2) { delegate(CellLoc p1, CellLoc p2) {
int compareV = p1.z.CompareTo(p2.z); int compareV = p1.z.CompareTo(p2.z);
if (compareV == 0) { if (compareV == 0) {
compareV = p1.y.CompareTo(p2.y); compareV = p1.y.CompareTo(p2.y);
if (compareV == 0) { if (compareV == 0) {
compareV = p1.x.CompareTo(p2.x); compareV = p1.x.CompareTo(p2.x);
} }
} }
return compareV; return compareV;
} }
); );
// Look through and only create the transforms we need // Look through and only create the transforms we need
cell_transforms = new Dictionary<CellLoc, Transform>(); cell_transforms = new Dictionary<CellLoc, Transform>();
var cellMaterials = new Dictionary<String,Material>(); var cellMaterials = new Dictionary<String,Material>();
foreach (CellLoc c in cellsKeys) { foreach (CellLoc c in cellsKeys) {
num_cells++; num_cells++;
if (cellNeedsTransform[c]) { if (cellNeedsTransform[c]) {
// Instantiate this one // Instantiate this one
Vector3 position = CLUtils.CellLocToVector3(new CellLoc(cells[c].x, cells[c].y, cells[c].z), 0); Vector3 position = CLUtils.CellLocToVector3(new CellLoc(cells[c].x, cells[c].y, cells[c].z), 0);
Transform tempts = Instantiate(prefab, Transform tempts = Instantiate(prefab,
position, position,
Quaternion.identity) as Transform; Quaternion.identity) as Transform;
cell_transforms[c] = tempts; cell_transforms[c] = tempts;
Renderer rend = tempts.GetComponent<Renderer>(); Renderer rend = tempts.GetComponent<Renderer>();
rend.enabled = false; rend.enabled = false;
if (!cellMaterials.ContainsKey(cells[c].displayGraphic)) { if (!cellMaterials.ContainsKey(cells[c].displayGraphic)) {
String materialName = StringToAsciiHex(cells[c].displayGraphic); String materialName = StringToAsciiHex(cells[c].displayGraphic);
Material cellMaterial = Resources.Load("Materials/Cells/" + materialName, typeof(Material)) as Material; Material cellMaterial = Resources.Load("Materials/Cells/" + materialName, typeof(Material)) as Material;
if (cellMaterial == null) { if (cellMaterial == null) {
print("Couldn't find material " + materialName + " for display graphic \"" + cells[c].displayGraphic + "\"!"); print("Couldn't find material " + materialName + " for display graphic \"" + cells[c].displayGraphic + "\"!");
Debug.Break(); Debug.Break();
} else { } else {
cellMaterials[cells[c].displayGraphic] = cellMaterial; cellMaterials[cells[c].displayGraphic] = cellMaterial;
} }
} }
rend.material = cellMaterials[cells[c].displayGraphic]; rend.material = cellMaterials[cells[c].displayGraphic];
rend.enabled = true; rend.enabled = true;
// Now check to find the best chunk: // Now check to find the best chunk:
short bestXSize = 1, bestYSize = 1, currXSize = 1, currYSize = 1; short bestXSize = 1, bestYSize = 1, currXSize = 1, currYSize = 1;
// 1. Find the maximum X size we could possible be // 1. Find the maximum X size we could possible be
CellLoc nextCell = c; nextCell.x += 1; CellLoc nextCell = c; nextCell.x += 1;
while (cells.ContainsKey(nextCell) && cellNeedsTransform[nextCell] && cells[nextCell].displayGraphic == cells[c].displayGraphic) { while (cells.ContainsKey(nextCell) && cellNeedsTransform[nextCell] && cells[nextCell].displayGraphic == cells[c].displayGraphic) {
currXSize++; currXSize++;
nextCell.x += 1; nextCell.x += 1;
} }
// 2. Work down from the maximum possible X, evaluating the max possible Y at each X size // 2. Work down from the maximum possible X, evaluating the max possible Y at each X size
while (currXSize >= 1) { while (currXSize >= 1) {
bool nextYGood = true; bool nextYGood = true;
while (nextYGood) { while (nextYGood) {
for (short xOffset = 0; xOffset < currXSize; xOffset++) { for (short xOffset = 0; xOffset < currXSize; xOffset++) {
nextCell = c; nextCell.x += xOffset; nextCell.y += currYSize; nextCell = c; nextCell.x += xOffset; nextCell.y += currYSize;
if (!(cells.ContainsKey(nextCell) && cellNeedsTransform[nextCell] && cells[nextCell].displayGraphic == cells[c].displayGraphic)) { if (!(cells.ContainsKey(nextCell) && cellNeedsTransform[nextCell] && cells[nextCell].displayGraphic == cells[c].displayGraphic)) {
nextYGood = false; nextYGood = false;
} }
} }
if (nextYGood) currYSize++; if (nextYGood) currYSize++;
} }
// 3. Keep track of the highest X*Y we've found so far // 3. Keep track of the highest X*Y we've found so far
if (currXSize * currYSize > bestXSize * bestYSize) { if (currXSize * currYSize > bestXSize * bestYSize) {
bestXSize = currXSize; bestYSize = currYSize; bestXSize = currXSize; bestYSize = currYSize;
} }
currXSize--; currXSize--;
} }
// Uncomment me for no grouping: // Uncomment me for no grouping:
// bestXSize = 1; bestYSize = 1; // bestXSize = 1; bestYSize = 1;
// print("Best size for cell " + v + " is " + bestXSize + "x, " + bestYSize + "y."); // print("Best size for cell " + v + " is " + bestXSize + "x, " + bestYSize + "y.");
// 4. Mark each cell we're absorbing // 4. Mark each cell we're absorbing
for (short yOffset = 0; yOffset < bestYSize; yOffset++) { for (short yOffset = 0; yOffset < bestYSize; yOffset++) {
for (short xOffset = 0; xOffset < bestXSize; xOffset++) { for (short xOffset = 0; xOffset < bestXSize; xOffset++) {
if (xOffset == 0 && yOffset == 0) continue; if (xOffset == 0 && yOffset == 0) continue;
cellNeedsTransform[new CellLoc(c.x + xOffset, c.y + yOffset, c.z)] = false; cellNeedsTransform[new CellLoc(c.x + xOffset, c.y + yOffset, c.z)] = false;
} }
} }
// 5. Update the position, scale, and texture scale. // 5. Update the position, scale, and texture scale.
Vector3 firstCellPosition = CLUtils.CellLocToVector3(c, 0); Vector3 firstCellPosition = CLUtils.CellLocToVector3(c, 0);
Vector3 lastCellPosition = CLUtils.CellLocToVector3(new CellLoc(c.x + (bestXSize - 1), c.y + (bestYSize - 1), c.z), 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 = (firstCellPosition + lastCellPosition) / 2f;
// tempts.position += new Vector3(-0.5f * (bestXSize - 1), -0.5f * (bestYSize - 1), 0f); // tempts.position += new Vector3(-0.5f * (bestXSize - 1), -0.5f * (bestYSize - 1), 0f);
tempts.localScale += new Vector3((float) (bestXSize - 1), (float) (bestYSize - 1), 0f); tempts.localScale += new Vector3((float) (bestXSize - 1), (float) (bestYSize - 1), 0f);
// Uncomment for borders between the blocks: // Uncomment for borders between the blocks:
// tempts.localScale -= new Vector3(0.1f, 0.1f, 0f); // tempts.localScale -= new Vector3(0.1f, 0.1f, 0f);
rend.material.mainTextureScale = new Vector2((-1f) * tempts.localScale.x, (-1f) * tempts.localScale.y); rend.material.mainTextureScale = new Vector2((-1f) * tempts.localScale.x, (-1f) * tempts.localScale.y);
} }
if (cells[c].isMapPortal) { if (cells[c].isMapPortal) {
Vector3 position = CLUtils.CellLocToVector3(new CellLoc(cells[c].x, cells[c].y, cells[c].z), 0); Vector3 position = CLUtils.CellLocToVector3(new CellLoc(cells[c].x, cells[c].y, cells[c].z), 0);
position.z += 0.5f; position.z += 0.5f;
Transform tempts = Instantiate(portalPrefab, Transform tempts = Instantiate(portalPrefab,
position, position,
Quaternion.identity) as Transform; Quaternion.identity) as Transform;
portalTransforms[c] = tempts; portalTransforms[c] = tempts;
} }
} }
num_blocks = cell_transforms.Count; num_blocks = cell_transforms.Count;
print("Instantiated " + num_cells + " cells in " + cell_transforms.Count + " transforms."); print("Instantiated " + num_cells + " cells in " + cell_transforms.Count + " transforms.");
enabled = true; enabled = true;
} }
void Update () { void Update () {
if (Time.time > nextDBUpdate) { if (Time.time > nextDBUpdate) {
print("Time to read from the DB! " + Time.time); print("Time to read from the DB! " + Time.time);
nextDBUpdate = Time.time + dbUpdateDelta; nextDBUpdate = Time.time + dbUpdateDelta;
UpdateCellContents(); UpdateCellContents();
} }
} }
public void UpdateZ(float z) { public void UpdateZ(float z) {
Renderer currRend; Renderer currRend;
foreach (CellLoc c in cell_transforms.Keys) { foreach (CellLoc c in cell_transforms.Keys) {
currRend = cell_transforms[c].GetComponent<Renderer>(); currRend = cell_transforms[c].GetComponent<Renderer>();
currRend.enabled = (c.z <= z); currRend.enabled = (c.z <= z);
} }
npcManager.UpdateZ(z); npcManager.UpdateZ(z);
foreach (CellLoc c in portalTransforms.Keys) { foreach (CellLoc c in portalTransforms.Keys) {
currRend = portalTransforms[c].GetComponent<Renderer>(); currRend = portalTransforms[c].GetComponent<Renderer>();
currRend.enabled = (c.z <= z); currRend.enabled = (c.z <= z);
} }
zTop = z; zTop = z;
} }
public void CycleZ(bool up) { public void CycleZ(bool up) {
currentZTopIdx += (up ? 1 : -1); currentZTopIdx += (up ? 1 : -1);
if (currentZTopIdx < 0) currentZTopIdx = 0; if (currentZTopIdx < 0) currentZTopIdx = 0;
if (currentZTopIdx >= zPlanes.Length) currentZTopIdx = zPlanes.Length - 1; if (currentZTopIdx >= zPlanes.Length) currentZTopIdx = zPlanes.Length - 1;
UpdateZ(zPlanes[currentZTopIdx]); UpdateZ(zPlanes[currentZTopIdx]);
} }
private bool CoinFlip () { private bool CoinFlip () {
return (UnityEngine.Random.Range(0f,1f) >= 0.5); return (UnityEngine.Random.Range(0f,1f) >= 0.5);
} }
private void GameStart () { private void GameStart () {
enabled = true; enabled = true;
} }
private void GameOver () { private void GameOver () {
enabled = false; enabled = false;
} }
bool LoadMapCells(string filename) { bool LoadMapCells(string filename) {
if (File.Exists(filename)) { if (File.Exists(filename)) {
print ("Map file " + filename + " does exist."); print ("Map file " + filename + " does exist.");
} else { } else {
print ("Map file " + filename + " does not exist."); print ("Map file " + filename + " does not exist.");
} }
if (ourMap.LoadMap(filename, 0, 0, 0)) { if (ourMap.LoadMap(filename, 0, 0, 0)) {
print ("Loaded map. " + ourMap.cells.Count + " cells loaded."); print ("Loaded map. " + ourMap.cells.Count + " cells loaded.");
ourMap.ZPlanes.Sort(); ourMap.ZPlanes.Sort();
zPlanes = ourMap.ZPlanes.ToArray(); zPlanes = ourMap.ZPlanes.ToArray();
cellKeyList = new List<string>(ourMap.cells.Keys); cellKeyList = new List<string>(ourMap.cells.Keys);
cells.Clear(); cells.Clear();
foreach (string k in cellKeyList) { foreach (string k in cellKeyList) {
CellLoc mapPosition = new CellLoc(ourMap.cells[k].X, ourMap.cells[k].Y, ourMap.cells[k].Z); CellLoc mapPosition = new CellLoc(ourMap.cells[k].X, ourMap.cells[k].Y, ourMap.cells[k].Z);
CellLite newCell; CellLite newCell;
newCell.displayGraphic = ourMap.cells[k].DisplayGraphic; newCell.displayGraphic = ourMap.cells[k].DisplayGraphic;
newCell.isMapPortal = ourMap.cells[k].IsMapPortal; newCell.isMapPortal = ourMap.cells[k].IsMapPortal;
newCell.x = ourMap.cells[k].X; newCell.x = ourMap.cells[k].X;
newCell.y = ourMap.cells[k].Y; newCell.y = ourMap.cells[k].Y;
newCell.z = ourMap.cells[k].Z; newCell.z = ourMap.cells[k].Z;
cells.Add(mapPosition, newCell); cells.Add(mapPosition, newCell);
} }
return true; return true;
} else { } else {
print ("Failed to load map!"); print ("Failed to load map!");
return false; Application.Quit();
} return false;
} }
}
void UpdateCellContents() {
ourMap.UpdateCells(); void UpdateCellContents() {
} ourMap.UpdateCells();
}
String StringToAsciiHex(String s) {
byte[] ASCIIValues = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, (Encoding.Unicode).GetBytes(s)); String StringToAsciiHex(String s) {
return BitConverter.ToString(ASCIIValues); byte[] ASCIIValues = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, (Encoding.Unicode).GetBytes(s));
} return BitConverter.ToString(ASCIIValues);
}