From 99f6cae55f44e213dfc70a2252db3c16f9ec524e Mon Sep 17 00:00:00 2001 From: megameta Date: Sun, 1 Dec 2013 19:00:07 -0600 Subject: [PATCH] Core Files Includes all files necessary to generate a level. Terrain and network checking included but still buggy. --- AgentHandler.cs | 168 +++++++++++++ AgentObj.cs | 22 ++ Architect.cs | 606 ++++++++++++++++++++++++++++++++++++++++++++++ CubeObj.cs | 90 +++++++ FaceObj.cs | 99 ++++++++ RoomObj.cs | 130 ++++++++++ RoomRect.cs | 15 ++ SceneBuilder.cs | 393 ++++++++++++++++++++++++++++++ TerrainManager.cs | 86 +++++++ TileObj.cs | 26 ++ 10 files changed, 1635 insertions(+) create mode 100644 AgentHandler.cs create mode 100644 AgentObj.cs create mode 100644 Architect.cs create mode 100644 CubeObj.cs create mode 100644 FaceObj.cs create mode 100644 RoomObj.cs create mode 100644 RoomRect.cs create mode 100644 SceneBuilder.cs create mode 100644 TerrainManager.cs create mode 100644 TileObj.cs diff --git a/AgentHandler.cs b/AgentHandler.cs new file mode 100644 index 0000000..63b191f --- /dev/null +++ b/AgentHandler.cs @@ -0,0 +1,168 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +public class AgentHandler : MonoBehaviour { + + public int agentCount = 20; + public int agentLife = 100; + public float evap = 0.0032f; + public int stepCount = 0; + public int cubeHeldCount = 0; + public float pickupModifier = 10f; // Modifies the thereshold chance for pickup. Higher modifier = greater chance of pickup + public float dropModifier = 10f; // Modifes the threshold chance for drop. Higher modifier = greater chance of drop + + protected List agentList = new List(); + + Architect architect; + TerrainManager terrainManager; + SceneBuilder sceneBuilder; + + void Awake(){ + architect = GetComponent(); + terrainManager = GetComponent(); + sceneBuilder = GetComponent(); + } + + public void SeedAgents(){ + for(int agent = 0; agent < agentCount; agent++){ + AgentObj newAgent = new AgentObj(sceneBuilder.levelSize, sceneBuilder.seedLayers, agentLife); + agentList.Add(newAgent); + newAgent.cubeNeighborhood = architect.NeighborCheck(newAgent.agentArrayPosition); + } + } + + void Update(){ + if(agentList.Count != 0){ + foreach(AgentObj agent in agentList){ + if(!agent.holdingCube) PickUp (agent); + RandomWalk(agent); + if(agent.holdingCube) Drop (agent); + //agent.agentLife--; + } + stepCount++; + } + } + + void PickUp(AgentObj agent){ + + CubeObj[,,] neighborhood = agent.cubeNeighborhood; + float spontPick = 0.1f; + float amplifPick = 1.0f; + + // Pickup targets are always 1 y coord below the worker + // If the pickup candidate cell is full and is not already flagged as picked up by another worker, then run stochastic check for pickup. + // If the roll is successful, then pick up. + + if(neighborhood[1, 0, 1] != null && neighborhood[1,0,1].heldByAgent == false){ + //Pickup targets are only influenced by the eight adjacent neighbors that share the same y coord + int neighborCount = 0; + + for(int x = 0; x < neighborhood.GetLength(0); x++){ + for(int z = 0; z < neighborhood.GetLength(2); z++){ + if(neighborhood[x, 0, z] != null && !(x == 1 && z == 1)){ + neighborCount++; + } + } + } + float pickupChance = spontPick / (amplifPick * neighborCount); + float pickupRoll = Random.Range(0.000f,1.000f); + pickupChance = pickupChance * pickupModifier; + + if (pickupRoll < pickupChance){ + CubeObj pickupTarget = neighborhood[1, 0, 1]; + agent.holdingCube = true; + agent.cubeHeld = pickupTarget; + pickupTarget.heldByAgent = true; + cubeHeldCount++; + } + } + } + + void RandomWalk(AgentObj agent){ + + List viableMoveList = new List(); + + CubeObj[,,] neighborhood = agent.cubeNeighborhood; + + for(int x = 0; x < neighborhood.GetLength(0); x++){ + for(int y = 0; y < neighborhood.GetLength(1); y++){ + for(int z = 0; z < neighborhood.GetLength(2); z++){ + if(neighborhood[x,y,z] != null){ + viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition); + viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(0,1,0)); + viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(0,-1,0)); + viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(1,0,0)); + viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(-1,1,0)); + viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(0,0,1)); + viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(0,0,-1)); + } + } + } + } + + // Scrub list of coordinates outside the level size + + viableMoveList.RemoveAll(coord => ( + coord.x >= sceneBuilder.levelSize.x || + coord.y >= sceneBuilder.levelSize.y || + coord.z >= sceneBuilder.levelSize.z || + coord.x < 0 || + coord.y < 0 || + coord.z < 0 + )); + + //Select a move from the viable move list and update agent's cubeNeighborhood array + + Vector3 newPosition = viableMoveList[Random.Range(0, viableMoveList.Count)]; + agent.agentArrayPosition = newPosition; + agent.cubeNeighborhood = architect.NeighborCheck(agent.agentArrayPosition); + } + + void Drop(AgentObj agent){ + float spontDrop = 0.001f; + float drop1 = 0.01f; + float amplifDrop = 0.036f; + float dropChance = 0f; + float dropRoll = Random.Range(0.000f, 1.000f); + + if(!terrainManager.CheckForCube(agent.agentArrayPosition) && agent.holdingCube){ + + // Find number of neighbors for current agent position + + CubeObj[,,] neighborhood = agent.cubeNeighborhood; + int neighborCount = architect.CountNeighbors(neighborhood); + + // Find latest drop time in neighborhood + + float latestDropTime = terrainManager.LatestDropInNeighborhood(agent.agentArrayPosition); + + if(neighborCount == 0) dropChance = spontDrop; + else { + dropChance = (drop1 + amplifDrop * (neighborCount - 1)) * Mathf.Exp(-(Time.time - latestDropTime) * evap); + } + + dropChance *= dropModifier; + + if(dropRoll < dropChance){ + CubeObj dropCube = agent.cubeHeld; + Vector3 thisPosition = agent.agentArrayPosition; + + terrainManager.cubePositionArray[(int)dropCube.cubeArrayPosition.x, (int)dropCube.cubeArrayPosition.y, (int)dropCube.cubeArrayPosition.z] = 0; + terrainManager.cubePositionArray[(int)thisPosition.x, (int)thisPosition.y, (int)thisPosition.z] = 1; + + dropCube.cubeArrayPosition = thisPosition; + dropCube.cubeGameObj.transform.position = thisPosition * sceneBuilder.cubeSize; + + agent.holdingCube = false; + dropCube.heldByAgent = false; + } + + } + } + + public void KillAgents(){ + agentList.Clear(); + } +} diff --git a/AgentObj.cs b/AgentObj.cs new file mode 100644 index 0000000..5475652 --- /dev/null +++ b/AgentObj.cs @@ -0,0 +1,22 @@ +using UnityEngine; +using System.Collections; + +public class AgentObj { + + public Vector3 levelSize; + public int seedLayers; + public Vector3 agentArrayPosition; + public bool holdingCube = false; + public CubeObj cubeHeld; + public int agentLife; + public GameObject agentGameObj; + public CubeObj[,,] cubeNeighborhood; + + public AgentObj(Vector3 levelSize, int seedLayers, int agentLife){ + this.levelSize = levelSize; + this.seedLayers = seedLayers; + this.agentLife = agentLife; + + agentArrayPosition = new Vector3(Random.Range(0,(int)levelSize.x), Random.Range (0, (int)seedLayers), Random.Range(0, (int)levelSize.z)); + } +} diff --git a/Architect.cs b/Architect.cs new file mode 100644 index 0000000..806b50e --- /dev/null +++ b/Architect.cs @@ -0,0 +1,606 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +[RequireComponent(typeof(TerrainManager))] +public class Architect : MonoBehaviour { + + int cubeNumber; + int faceNumber; + int[,] tileTypeArray; + TerrainManager terrainManager; + SceneBuilder sceneBuilder; + + void Awake(){ + terrainManager = GetComponent(); + sceneBuilder = GetComponent(); + } + + public void GenerateCube(int cubeNumber, int cubeSize, Vector3 cubeArrayPosition, bool interior){ + + CubeObj thisCubeObj = new CubeObj(cubeNumber, cubeSize, interior, cubeArrayPosition); + terrainManager.cubeMasterList.Add( thisCubeObj ); + thisCubeObj.cubeArrayPosition = cubeArrayPosition; + Vector3 cubePosition = new Vector3(cubeArrayPosition.x * cubeSize, cubeArrayPosition.y * cubeSize, cubeArrayPosition.z * cubeSize); + thisCubeObj.cubeGameObj.transform.parent = gameObject.transform; + thisCubeObj.cubeGameObj.transform.position = cubePosition; + } + /* + public void GenerateTerrain(CubeObj thisCube, int faceNumber){ + int cubeSize = thisCube.cubeSize; + FaceObj startFace = thisCube.faceList[faceNumber]; + + int roomTryCountPool = (int)Mathf.Floor(Mathf.Log(cubeSize) * 100); + int roomCountPool = (int)Mathf.Floor(cubeSize / 2); + + for(int roomCount = 0; roomCount < roomCountPool; roomCount++){ + RoomObj(startFace, roomCount); + + } + + } + */ + + public CubeObj[,,] NeighborCheck(Vector3 checkCoords){ + List cubeMasterList = terrainManager.cubeMasterList; + CubeObj[,,] localNeighborArray = new CubeObj[3,3,3]; + + // Translate global cubePositionArray into localNeighborArray containing the cube types at the 26 coords adjacent to the parameter checkCoords + // FirstOrDefault searches for cube at the position. If no cube is found or the coordinates are outside the level, returns default value 0 + + for(int x = 0; x < localNeighborArray.GetLength(0); x++){ + for(int y = 0; y < localNeighborArray.GetLength(1); y++){ + for(int z = 0; z < localNeighborArray.GetLength(2); z++){ + CubeObj thisCube = cubeMasterList.FirstOrDefault(c => c.cubeArrayPosition == (checkCoords - new Vector3(x - 1, y - 1, z - 1))); + if(thisCube != null){ + localNeighborArray[x,y,z] = thisCube; + } + else { + localNeighborArray[x,y,z] = null; + } + } + } + } + + /* Old neighborhood code dependent upon arrays, not viable for agent locations near edge of level + localNeighborArray[0,0,0] = cubePositionArray[(int)checkCoords.x - 1, (int)checkCoords.y - 1, (int)checkCoords.z - 1]; + localNeighborArray[1,0,0] = cubePositionArray[(int)checkCoords.x - 0, (int)checkCoords.y - 1, (int)checkCoords.z - 1]; + localNeighborArray[2,0,0] = cubePositionArray[(int)checkCoords.x + 1, (int)checkCoords.y - 1, (int)checkCoords.z - 1]; + localNeighborArray[0,1,0] = cubePositionArray[(int)checkCoords.x - 1, (int)checkCoords.y - 0, (int)checkCoords.z - 1]; + localNeighborArray[1,1,0] = cubePositionArray[(int)checkCoords.x - 0, (int)checkCoords.y - 0, (int)checkCoords.z - 1]; + localNeighborArray[2,1,0] = cubePositionArray[(int)checkCoords.x + 1, (int)checkCoords.y - 0, (int)checkCoords.z - 1]; + localNeighborArray[0,2,0] = cubePositionArray[(int)checkCoords.x - 1, (int)checkCoords.y + 1, (int)checkCoords.z - 1]; + localNeighborArray[1,2,0] = cubePositionArray[(int)checkCoords.x - 0, (int)checkCoords.y + 1, (int)checkCoords.z - 1]; + localNeighborArray[2,2,0] = cubePositionArray[(int)checkCoords.x + 1, (int)checkCoords.y + 1, (int)checkCoords.z - 1]; + + localNeighborArray[0,0,1] = cubePositionArray[(int)checkCoords.x - 1, (int)checkCoords.y - 1, (int)checkCoords.z - 0]; + localNeighborArray[1,0,1] = cubePositionArray[(int)checkCoords.x - 0, (int)checkCoords.y - 1, (int)checkCoords.z - 0]; + localNeighborArray[2,0,1] = cubePositionArray[(int)checkCoords.x + 1, (int)checkCoords.y - 1, (int)checkCoords.z - 0]; + localNeighborArray[0,1,1] = cubePositionArray[(int)checkCoords.x - 1, (int)checkCoords.y - 0, (int)checkCoords.z - 0]; + localNeighborArray[1,1,1] = cubePositionArray[(int)checkCoords.x - 0, (int)checkCoords.y - 0, (int)checkCoords.z - 0]; + localNeighborArray[2,1,1] = cubePositionArray[(int)checkCoords.x + 1, (int)checkCoords.y - 0, (int)checkCoords.z - 0]; + localNeighborArray[0,2,1] = cubePositionArray[(int)checkCoords.x - 1, (int)checkCoords.y + 1, (int)checkCoords.z - 0]; + localNeighborArray[1,2,1] = cubePositionArray[(int)checkCoords.x - 0, (int)checkCoords.y + 1, (int)checkCoords.z - 0]; + localNeighborArray[2,2,1] = cubePositionArray[(int)checkCoords.x + 1, (int)checkCoords.y + 1, (int)checkCoords.z - 0]; + + localNeighborArray[0,0,2] = cubePositionArray[(int)checkCoords.x - 1, (int)checkCoords.y - 1, (int)checkCoords.z + 1]; + localNeighborArray[1,0,2] = cubePositionArray[(int)checkCoords.x - 0, (int)checkCoords.y - 1, (int)checkCoords.z + 1]; + localNeighborArray[2,0,2] = cubePositionArray[(int)checkCoords.x + 1, (int)checkCoords.y - 1, (int)checkCoords.z + 1]; + localNeighborArray[0,1,2] = cubePositionArray[(int)checkCoords.x - 1, (int)checkCoords.y - 0, (int)checkCoords.z + 1]; + localNeighborArray[1,1,2] = cubePositionArray[(int)checkCoords.x - 0, (int)checkCoords.y - 0, (int)checkCoords.z + 1]; + localNeighborArray[2,1,2] = cubePositionArray[(int)checkCoords.x + 1, (int)checkCoords.y - 0, (int)checkCoords.z + 1]; + localNeighborArray[0,2,2] = cubePositionArray[(int)checkCoords.x - 1, (int)checkCoords.y + 1, (int)checkCoords.z + 1]; + localNeighborArray[1,2,2] = cubePositionArray[(int)checkCoords.x - 0, (int)checkCoords.y + 1, (int)checkCoords.z + 1]; + localNeighborArray[2,2,2] = cubePositionArray[(int)checkCoords.x + 1, (int)checkCoords.y + 1, (int)checkCoords.z + 1]; + */ + + return localNeighborArray; + } + + public int CountNeighbors(CubeObj[,,] neighborhood){ + + int neighborCount = 0; + + for(int x = 0; x < neighborhood.GetLength(0); x++){ + for(int y = 0; y < neighborhood.GetLength(1); y++){ + for(int z = 0; z < neighborhood.GetLength(2); z++){ + if(neighborhood[x,y,z] != null && (x != 1 && y != 1 && z != 1)){ + neighborCount++; + } + } + } + } + + return neighborCount; + } + + public void GenerateTextures(CubeObj cube){ + foreach(FaceObj face in cube.faceList){ + int texSize = face.faceSize * terrainManager.tileResolution; + face.faceTexture = new Texture2D(texSize,texSize); + int tileResolution = terrainManager.tileResolution; + + for (int y = 0; y < face.faceSize; y++){ + for(int x = 0; x < face.faceSize; x++){ + Color[] pixels = terrainManager.tileAtlas.GetPixels (face.tileTypeArray[x,y] * tileResolution, 0, tileResolution, tileResolution); + face.faceTexture.SetPixels(x * tileResolution, y * tileResolution, tileResolution, tileResolution, pixels); + } + } + + face.faceTexture.filterMode = FilterMode.Point; + face.faceTexture.wrapMode = TextureWrapMode.Clamp; + face.faceTexture.Apply(); + face.faceMeshRenderer.material.mainTexture = face.faceTexture; + + } + } + + public static int TileRead(int xCoord, int yCoord, FaceObj originFace){ + + /* + For parameter xCoord and yCoord: + Assume that the origin face's pivot acts as a global 0,0 origin across a 2D plane formed by the origin face and its adjacent faces + E.g. if a requested coordinate is "beneath" the origin face, then the yCoord should be passed to TileRead() as a negative + + NOTE: The xCoord and yCoord are zero-indexed + + */ + int faceSize = originFace.faceSize; + int faceNumber = originFace.faceNumber; + CubeObj onCube = originFace.onCube; + int edgeNumber = GetEdgeNumber(xCoord, yCoord, faceSize); + int faceZeroIndexSize = faceSize - 1; + + // If xCoord and yCoord both exceed the faceSize, then this tile is eliminated when the faces are folded to form a cube and should return null + + if (xCoord >= faceSize && yCoord >= faceSize){ + return 0; + } + + // If the xCoord and yCoord are within the face's bounds, just return the tileTypeArray for the parameter coordinates + + else if(xCoord < faceSize && yCoord < faceSize){ + return originFace.tileTypeArray[xCoord, yCoord]; + } + + // If the coordinates are neither within the origin face's bounds nor outside the x and y dimension bounds of the face, then they must be on an adjacent face + + else{ + if(faceNumber == 0){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (yCoord - faceSize)); + Debug.Log ("Array size is X: " +onCube.faceList[4].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[4].tileTypeArray.GetLength(1)); + return onCube.faceList[4].tileTypeArray[xCoord, yCoord - faceSize]; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord - faceSize)+ " Y: " + (yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[1].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[1].tileTypeArray.GetLength(1)); + return onCube.faceList[1].tileTypeArray[xCoord - faceSize, yCoord]; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (faceSize + yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[2].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[2].tileTypeArray.GetLength(1)); + return onCube.faceList[2].tileTypeArray[xCoord, faceSize + yCoord]; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize + xCoord)+ " Y: " + (yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[3].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[3].tileTypeArray.GetLength(1)); + return onCube.faceList[3].tileTypeArray[faceSize + xCoord, yCoord]; + } + } + + else if(faceNumber == 1){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize - (yCoord - faceZeroIndexSize))+ " Y: " + (xCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[4].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[4].tileTypeArray.GetLength(1)); + return onCube.faceList[4].tileTypeArray[faceSize - (yCoord - faceZeroIndexSize), xCoord]; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize - (xCoord - faceZeroIndexSize))+ " Y: " + (faceZeroIndexSize - yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[5].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[5].tileTypeArray.GetLength(1)); + return onCube.faceList[5].tileTypeArray[faceSize - (xCoord - faceZeroIndexSize), faceZeroIndexSize - yCoord]; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize + yCoord)+ " Y: " + (faceZeroIndexSize - xCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[2].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[2].tileTypeArray.GetLength(1)); + return onCube.faceList[2].tileTypeArray[faceSize + yCoord, faceZeroIndexSize - xCoord]; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize + xCoord)+ " Y: " + (yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[0].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[0].tileTypeArray.GetLength(1)); + return onCube.faceList[0].tileTypeArray[faceSize + xCoord, yCoord]; + } + } + + else if(faceNumber == 2){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (yCoord - faceSize)); + Debug.Log ("Array size is X: " +onCube.faceList[0].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[0].tileTypeArray.GetLength(1)); + return onCube.faceList[0].tileTypeArray[xCoord, yCoord - faceSize]; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceZeroIndexSize - yCoord)+ " Y: " + (xCoord - faceSize)); + Debug.Log ("Array size is X: " +onCube.faceList[1].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[1].tileTypeArray.GetLength(1)); + return onCube.faceList[1].tileTypeArray[faceZeroIndexSize - yCoord, xCoord - faceSize]; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (faceSize + yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[5].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[5].tileTypeArray.GetLength(1)); + return onCube.faceList[5].tileTypeArray[xCoord, faceSize + yCoord]; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(yCoord)+ " Y: " + (-xCoord - 1)); + Debug.Log ("Array size is X: " +onCube.faceList[3].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[3].tileTypeArray.GetLength(1)); + return onCube.faceList[3].tileTypeArray[yCoord, -xCoord - 1]; + } + } + + else if(faceNumber == 3){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(yCoord - faceSize)+ " Y: " + (faceZeroIndexSize - xCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[4].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[4].tileTypeArray.GetLength(1)); + return onCube.faceList[4].tileTypeArray[yCoord - faceSize, faceZeroIndexSize - xCoord]; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord - faceSize)+ " Y: " + (yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[0].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[0].tileTypeArray.GetLength(1)); + return onCube.faceList[0].tileTypeArray[xCoord - faceSize, yCoord]; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(-yCoord - 1)+ " Y: " + (xCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[2].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[2].tileTypeArray.GetLength(1)); + return onCube.faceList[2].tileTypeArray[-yCoord - 1, xCoord]; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(-xCoord - 1)+ " Y: " + (faceZeroIndexSize - yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[5].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[5].tileTypeArray.GetLength(1)); + return onCube.faceList[5].tileTypeArray[-xCoord - 1, faceZeroIndexSize - yCoord]; + } + } + + else if(faceNumber == 4){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (yCoord - faceSize)); + Debug.Log ("Array size is X: " +onCube.faceList[5].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[5].tileTypeArray.GetLength(1)); + return onCube.faceList[5].tileTypeArray[xCoord, yCoord - faceSize]; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(yCoord)+ " Y: " + (faceSize - (xCoord - faceZeroIndexSize))); + Debug.Log ("Array size is X: " +onCube.faceList[1].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[1].tileTypeArray.GetLength(1)); + return onCube.faceList[1].tileTypeArray[yCoord, faceSize - (xCoord - faceZeroIndexSize)]; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (faceSize + yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[0].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[0].tileTypeArray.GetLength(1)); + return onCube.faceList[0].tileTypeArray[xCoord, faceSize + yCoord]; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceZeroIndexSize - yCoord)+ " Y: " + (faceSize + xCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[3].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[3].tileTypeArray.GetLength(1)); + return onCube.faceList[3].tileTypeArray[faceZeroIndexSize - yCoord, faceSize + xCoord]; + } + } + + else if(faceNumber == 5){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (yCoord - faceSize)); + Debug.Log ("Array size is X: " +onCube.faceList[2].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[2].tileTypeArray.GetLength(1)); + return onCube.faceList[2].tileTypeArray[xCoord, yCoord - faceSize]; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize - (xCoord - faceZeroIndexSize))+ " Y: " + (faceZeroIndexSize - yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[1].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[1].tileTypeArray.GetLength(1)); + return onCube.faceList[1].tileTypeArray[faceSize - (xCoord - faceZeroIndexSize), faceZeroIndexSize - yCoord]; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (faceSize + yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[4].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[4].tileTypeArray.GetLength(1)); + return onCube.faceList[4].tileTypeArray[xCoord, faceSize + yCoord]; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(-xCoord - 1)+ " Y: " + (faceZeroIndexSize - yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[3].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[3].tileTypeArray.GetLength(1)); + return onCube.faceList[3].tileTypeArray[-xCoord - 1, faceZeroIndexSize - yCoord]; + } + } + return 0; + } + } + + public static bool TileWrite(int xCoord, int yCoord, int tileType, FaceObj originFace){ + + /* + For parameter xCoord and yCoord: + Assume that the origin face's pivot acts as a global 0,0 origin across a 2D plane formed by the origin face and its adjacent faces + E.g. if a requested coordinate is "beneath" the origin face, then the yCoord should be passed to TileWrite() as a negative + + Return true if the write was successful and will appear in a cube face's tileTypeArray. If the coordinate will be lost in a fold, return false. + + NOTE: The xCoord and yCoord are zero-indexed + + */ + + // If xCoord and yCoord both exceed the faceSize, then this tile is eliminated when the faces are folded to form a cube and should return false + + int faceSize = originFace.faceSize; + int faceNumber = originFace.faceNumber; + CubeObj onCube = originFace.onCube; + int faceZeroIndexSize = faceSize - 1; + + if (xCoord >= faceSize && yCoord >= faceSize){ + Debug.Log ("In the null fold zone"); + return false; + } + + else if(xCoord < faceSize && yCoord < faceSize){ + originFace.tileTypeArray[xCoord,yCoord] = tileType; + Debug.Log ("On the proper face"); + return true; + } + + else{ + + // If the specified tile is a valid post-fold coordinate, then set that tile and return true + + int edgeNumber = GetEdgeNumber(xCoord, yCoord, faceSize); + + if(faceNumber == 0){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (yCoord - faceSize)); + Debug.Log ("Array size is X: " +onCube.faceList[4].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[4].tileTypeArray.GetLength(1)); + onCube.faceList[4].tileTypeArray[xCoord, yCoord - faceSize] = tileType; + return true; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord - faceSize)+ " Y: " + (yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[1].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[1].tileTypeArray.GetLength(1)); + onCube.faceList[1].tileTypeArray[xCoord - faceSize, yCoord] = tileType; + return true; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (faceSize + yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[2].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[2].tileTypeArray.GetLength(1)); + onCube.faceList[2].tileTypeArray[xCoord, faceSize + yCoord] = tileType; + return true; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize + xCoord)+ " Y: " + (yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[3].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[3].tileTypeArray.GetLength(1)); + onCube.faceList[3].tileTypeArray[faceSize + xCoord, yCoord] = tileType; + return true; + } + } + + if(faceNumber == 1){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize - (yCoord - faceZeroIndexSize))+ " Y: " + (xCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[4].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[4].tileTypeArray.GetLength(1)); + onCube.faceList[4].tileTypeArray[faceSize - (yCoord - faceZeroIndexSize), xCoord] = tileType; + return true; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize - (xCoord - faceZeroIndexSize))+ " Y: " + (faceZeroIndexSize - yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[5].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[5].tileTypeArray.GetLength(1)); + onCube.faceList[5].tileTypeArray[faceSize - (xCoord - faceZeroIndexSize), faceZeroIndexSize - yCoord] = tileType; + return true; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize + yCoord)+ " Y: " + (faceZeroIndexSize - xCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[2].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[2].tileTypeArray.GetLength(1)); + onCube.faceList[2].tileTypeArray[faceSize + yCoord, faceZeroIndexSize - xCoord] = tileType; + return true; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize + xCoord)+ " Y: " + (yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[0].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[0].tileTypeArray.GetLength(1)); + onCube.faceList[0].tileTypeArray[faceSize + xCoord, yCoord] = tileType; + return true; + } + } + + if(faceNumber == 2){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (yCoord - faceSize)); + Debug.Log ("Array size is X: " +onCube.faceList[0].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[0].tileTypeArray.GetLength(1)); + onCube.faceList[0].tileTypeArray[xCoord, yCoord - faceSize] = tileType; + return true; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceZeroIndexSize - yCoord)+ " Y: " + (xCoord - faceSize)); + Debug.Log ("Array size is X: " +onCube.faceList[1].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[1].tileTypeArray.GetLength(1)); + onCube.faceList[1].tileTypeArray[faceZeroIndexSize - yCoord, xCoord - faceSize] = tileType; + return true; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (faceSize + yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[5].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[5].tileTypeArray.GetLength(1)); + onCube.faceList[5].tileTypeArray[xCoord, faceSize + yCoord] = tileType; + return true; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(yCoord)+ " Y: " + (-xCoord - 1)); + Debug.Log ("Array size is X: " +onCube.faceList[3].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[3].tileTypeArray.GetLength(1)); + onCube.faceList[3].tileTypeArray[yCoord, -xCoord - 1] = tileType; + return true; + } + } + + if(faceNumber == 3){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(yCoord - faceSize)+ " Y: " + (faceZeroIndexSize - xCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[4].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[4].tileTypeArray.GetLength(1)); + onCube.faceList[4].tileTypeArray[yCoord - faceSize, faceZeroIndexSize - xCoord] = tileType; + return true; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord - faceSize)+ " Y: " + (yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[0].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[0].tileTypeArray.GetLength(1)); + onCube.faceList[0].tileTypeArray[xCoord - faceSize, yCoord] = tileType; + return true; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(-yCoord - 1)+ " Y: " + (xCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[2].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[2].tileTypeArray.GetLength(1)); + onCube.faceList[2].tileTypeArray[-yCoord - 1, xCoord] = tileType; + return true; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(-xCoord - 1)+ " Y: " + (faceZeroIndexSize - yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[5].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[5].tileTypeArray.GetLength(1)); + onCube.faceList[5].tileTypeArray[-xCoord - 1, faceZeroIndexSize - yCoord] = tileType; + return true; + } + } + + if(faceNumber == 4){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (yCoord - faceSize)); + Debug.Log ("Array size is X: " +onCube.faceList[5].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[5].tileTypeArray.GetLength(1)); + onCube.faceList[5].tileTypeArray[xCoord, yCoord - faceSize] = tileType; + return true; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(yCoord)+ " Y: " + (faceSize - (xCoord - faceZeroIndexSize))); + Debug.Log ("Array size is X: " +onCube.faceList[1].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[1].tileTypeArray.GetLength(1)); + onCube.faceList[1].tileTypeArray[yCoord, faceSize - (xCoord - faceZeroIndexSize)] = tileType; + return true; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (faceSize + yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[0].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[0].tileTypeArray.GetLength(1)); + onCube.faceList[0].tileTypeArray[xCoord, faceSize + yCoord] = tileType; + return true; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceZeroIndexSize - yCoord)+ " Y: " + (faceSize + xCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[3].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[3].tileTypeArray.GetLength(1)); + onCube.faceList[3].tileTypeArray[faceZeroIndexSize - yCoord, faceSize + xCoord] = tileType; + return true; + } + } + + if(faceNumber == 5){ + switch(edgeNumber){ + case 0: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (yCoord - faceSize)); + Debug.Log ("Array size is X: " +onCube.faceList[2].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[2].tileTypeArray.GetLength(1)); + onCube.faceList[2].tileTypeArray[xCoord, yCoord - faceSize] = tileType; + return true; + case 1: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(faceSize - (xCoord - faceZeroIndexSize))+ " Y: " + (faceZeroIndexSize - yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[1].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[1].tileTypeArray.GetLength(1)); + onCube.faceList[1].tileTypeArray[faceSize - (xCoord - faceZeroIndexSize), faceZeroIndexSize - yCoord] = tileType; + return true; + case 2: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(xCoord)+ " Y: " + (faceSize + yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[4].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[4].tileTypeArray.GetLength(1)); + onCube.faceList[4].tileTypeArray[xCoord, faceSize + yCoord] = tileType; + return true; + case 3: + Debug.Log ("xCoord: " +xCoord+ " yCoord: " +yCoord); + Debug.Log ("faceSize: " +faceSize); + Debug.Log ("Requesting read at X: " +(-xCoord - 1)+ " Y: " + (faceZeroIndexSize - yCoord)); + Debug.Log ("Array size is X: " +onCube.faceList[3].tileTypeArray.GetLength(0)+ " Y: " +onCube.faceList[3].tileTypeArray.GetLength(1)); + onCube.faceList[3].tileTypeArray[-xCoord - 1, faceZeroIndexSize - yCoord] = tileType; + return true; + } + } + Debug.Log ("fell into a black hole!"); + return false; + } + } + + public static int GetEdgeNumber(int xCoord, int yCoord, int faceSize){ + + if(yCoord >= faceSize && xCoord < faceSize){ + return 0; + } + + else if (xCoord >= faceSize && yCoord < faceSize){ + return 1; + } + + else if (xCoord < faceSize && yCoord < 0){ + return 2; + } + + else if (xCoord < 0 && yCoord < faceSize){ + return 3; + } + return 0; + } +} + diff --git a/CubeObj.cs b/CubeObj.cs new file mode 100644 index 0000000..ed5f6a5 --- /dev/null +++ b/CubeObj.cs @@ -0,0 +1,90 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +public class CubeObj { + + public int cubeNumber; + public int cubeSize; + public int numFaces; + public bool interior; + public GameObject cubeGameObj; + public Vector3 cubeArrayPosition; + public bool heldByAgent; + public float dropTime; + public float positionSum; + public int networkSize; + + public List faceList = new List(); + public List roomList = new List(); + + public CubeObj(int cubeNumber, int cubeSize, bool interior, Vector3 cubeArrayPosition){ + this.cubeNumber = cubeNumber; + this.cubeSize = cubeSize; + this.cubeArrayPosition = cubeArrayPosition; + heldByAgent = false; + dropTime = Time.time; + + cubeGameObj = new GameObject(); + cubeGameObj.name = "cube" +cubeNumber; + + if(interior){ + cubeGameObj.transform.position = new Vector3(cubeSize * .5f, cubeSize * .5f, -cubeSize * .5f); + } else { + cubeGameObj.transform.position = new Vector3(cubeSize * .5f, cubeSize * .5f, cubeSize * .5f); + } + + for(int face = 0; face < 6; face++){ + FaceObj newFace = new FaceObj(this, face); + faceList.Add (newFace); + } + + // Postion the face meshes as an inverted cross to prepare for either internal or external wrapping to form a cube + + foreach(FaceObj face in faceList){ + + switch(face.faceNumber){ + case 0: + break; + case 1: + face.faceGameObj.transform.position = face.faceGameObj.transform.position + new Vector3(face.faceSize, 0, 0); + break; + case 2: + face.faceGameObj.transform.position = face.faceGameObj.transform.position + new Vector3(0, -face.faceSize, 0); + break; + case 3: + face.faceGameObj.transform.position = face.faceGameObj.transform.position + new Vector3(-face.faceSize, 0, 0); + break; + case 4: + face.faceGameObj.transform.position = face.faceGameObj.transform.position + new Vector3(0, face.faceSize, 0); + break; + case 5: + face.faceGameObj.transform.position = face.faceGameObj.transform.position + new Vector3(0, face.faceSize * 2, 0); + break; + } + } + + //Wrap the face meshes to form a cube + + if(interior){ + faceList[1].faceGameObj.transform.RotateAround(faceList[1].faceGameObj.transform.position, faceList[1].faceGameObj.transform.up, 90f); + faceList[2].faceGameObj.transform.RotateAround(faceList[0].faceGameObj.transform.position, faceList[0].faceGameObj.transform.right, 90f); + faceList[3].faceGameObj.transform.RotateAround(faceList[0].faceGameObj.transform.position, faceList[0].faceGameObj.transform.up, -90f); + faceList[4].faceGameObj.transform.RotateAround(faceList[4].faceGameObj.transform.position, faceList[4].faceGameObj.transform.right, -90f); + faceList[5].faceGameObj.transform.RotateAround(faceList[4].faceGameObj.transform.position, faceList[4].faceGameObj.transform.right, -90f); + faceList[5].faceGameObj.transform.RotateAround(faceList[5].faceGameObj.transform.position, faceList[5].faceGameObj.transform.right, -90f); + } else { + faceList[1].faceGameObj.transform.RotateAround(faceList[1].faceGameObj.transform.position, faceList[1].faceGameObj.transform.up, -90f); + faceList[2].faceGameObj.transform.RotateAround(faceList[0].faceGameObj.transform.position, faceList[0].faceGameObj.transform.right, -90f); + faceList[3].faceGameObj.transform.RotateAround(faceList[0].faceGameObj.transform.position, faceList[0].faceGameObj.transform.up, 90f); + faceList[4].faceGameObj.transform.RotateAround(faceList[4].faceGameObj.transform.position, faceList[4].faceGameObj.transform.right, 90f); + faceList[5].faceGameObj.transform.RotateAround(faceList[4].faceGameObj.transform.position, faceList[4].faceGameObj.transform.right, 90f); + faceList[5].faceGameObj.transform.RotateAround(faceList[5].faceGameObj.transform.position, faceList[5].faceGameObj.transform.right, 90f); + } + } + + public void PositionSum(){ + positionSum = cubeGameObj.transform.position.x + cubeGameObj.transform.position.y + cubeGameObj.transform.position.z; + } +} diff --git a/FaceObj.cs b/FaceObj.cs new file mode 100644 index 0000000..4cc8dc3 --- /dev/null +++ b/FaceObj.cs @@ -0,0 +1,99 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +public class FaceObj { + + public int cubeNumber; + public int faceNumber; + public int faceSize; + public int[,] tileTypeArray; + public Texture2D faceTexture; + public GameObject faceGameObj; + public MeshFilter faceMeshFilter; + public MeshRenderer faceMeshRenderer; + public MeshCollider faceMeshCollider; + public int faceVertSize; + public int faceVertNumber; + public int faceTileNumber; + public int faceTriNumber; + public int faceZeroIndexSize; + public Mesh faceMesh; + public int cubeFaceLayer = 9; + public int tileSize = 1; + public CubeObj onCube; + + TerrainManager terrainManager; + + Vector3[] faceVerts; + Vector3[] faceNormals; + Vector2[] faceUV; + int[] faceTriangles; + + public List roomList = new List(); + public List tileList = new List(); + + public FaceObj(CubeObj thisCube, int faceNumber){ + onCube = thisCube; + this.faceNumber = faceNumber; + + cubeNumber = thisCube.cubeNumber; + faceSize = thisCube.cubeSize; + faceZeroIndexSize = faceSize - 1; + tileTypeArray = new int[faceSize, faceSize]; + faceGameObj = new GameObject(); + faceGameObj.name = "cubeFace" +faceNumber; + faceGameObj.transform.parent = thisCube.cubeGameObj.transform; + faceGameObj.layer = cubeFaceLayer; + faceMeshFilter = faceGameObj.AddComponent(); + faceMeshRenderer = faceGameObj.AddComponent(); + faceMeshCollider = faceGameObj.AddComponent(); + faceVertSize = faceSize + 1; + faceVertNumber = faceVertSize * faceVertSize; + faceTileNumber = faceSize * faceSize; + faceTriNumber = faceTileNumber * 2; + + + faceVerts = new Vector3[faceVertNumber]; + faceNormals = new Vector3[faceVertNumber]; + faceUV = new Vector2[faceVertNumber]; + faceTriangles = new int[faceTriNumber * 3]; + + for(int y = 0; y < faceVertSize; y++){ + for(int x = 0; x < faceVertSize; x++){ + faceVerts[y * faceVertSize + x] = new Vector3(x * tileSize, y * tileSize, 0); + faceNormals[y * faceVertSize + x] = Vector3.forward; + faceUV[y * faceVertSize + x] = new Vector2((float)x / faceSize, (float)y / faceSize); + } + } + + for(int y = 0; y < faceSize; y++){ + for(int x = 0; x < faceSize; x++){ + int tileIndex = y * faceSize + x; + int triangleOffset = tileIndex * 6; + + faceTriangles[triangleOffset + 0] = y * faceVertSize + x; + faceTriangles[triangleOffset + 1] = y * faceVertSize + x + 1; + faceTriangles[triangleOffset + 2] = y * faceVertSize + x + faceVertSize; + + faceTriangles[triangleOffset + 3] = y * faceVertSize + x + 1; + faceTriangles[triangleOffset + 4] = y * faceVertSize + x + faceVertSize + 1; + faceTriangles[triangleOffset + 5] = y * faceVertSize + x + faceVertSize; + + } + } + + faceMesh = new Mesh(); + faceMesh.vertices = faceVerts; + faceMesh.triangles = faceTriangles; + faceMesh.normals = faceNormals; + faceMesh.uv = faceUV; + + faceMeshFilter.mesh = faceMesh; + faceMeshCollider.sharedMesh = faceMesh; + + faceMeshRenderer.material = Resources.Load("PreMaterial"); + + } +} diff --git a/RoomObj.cs b/RoomObj.cs new file mode 100644 index 0000000..6106b7a --- /dev/null +++ b/RoomObj.cs @@ -0,0 +1,130 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +public class RoomObj { + + public int cubeNumber; + public int faceNumber; + public int faceSize; + public int xStart; + public int yStart; + public int xSize; + public int ySize; + public int roomType; + public int roomNumber; + public int[,] roomTileArray; + public bool overlap; + public bool createSuccess; + + public List connectRoomList = new List(); + + FaceObj onFace; + + /* + + //Rotation holds values for the times by which the room is to be rotated (by ninety degrees clockwise) on its face + + public enum Rotation { + ZERO, + ONE, + TWO, + THREE + } + + */ + + List roomSizeList = new List() { + {new int[4,4]}, + {new int[4,8]}, + {new int[8,8]}, + {new int[16,16]}, + {new int[12, 16]} + }; + + public RoomObj(CubeObj onCube, FaceObj originFace, int roomCount){ + onFace = originFace; + this.cubeNumber = onFace.cubeNumber; + this.faceNumber = onFace.faceNumber; + this.faceSize = onFace.faceSize; + + if(roomCount == 0){ + xSize = Random.Range(5, faceSize - 2); + ySize = Random.Range(5, faceSize - 2); + xStart = (int)Mathf.Floor((faceSize - xSize) / 2); + yStart = (int)Mathf.Floor((faceSize - xSize) / 2); + } + else{ + xStart = Random.Range(0, faceSize); + yStart = Random.Range(0, faceSize); + roomType = Random.Range(0, roomSizeList.Count); + } + + roomTileArray = roomSizeList[roomType]; + xSize = roomTileArray.GetLength(0); + ySize = roomTileArray.GetLength(1); + + for(int y = 0; y < ySize; y++){ + for(int x = 0; x < xSize; x++){ + if (x == xSize - 1 || y == ySize - 1 || x == 0 || y == 0){ + roomTileArray[x,y] = TileObj.TileDict["wall"]; + } + else{ + roomTileArray[x,y] = TileObj.TileDict["floor"]; + } + } + } + + createSuccess = true; + + } + + /* When checking for overlap, edges of a face are associated with the these numbers: + * + * 0 + * __________ + * | | + * 3 | | 1 + * | | + * |_________| + * + * 2 + * + */ + + + public void CheckOverlap(FaceObj thisFace){ + Debug.Log ("Face number: " +thisFace.faceNumber+ " Room number: " +thisFace.roomList.Count+ " Now beginning CheckOverlap"); + Debug.Log ("xStart: " +xStart+ " yStart: " +yStart); + Debug.Log ("xSize: " +xSize+ " ySize: " +ySize); + for(int y = yStart; y < yStart + ySize; y++){ + for(int x = xStart; x < xStart + xSize; x++){ + Debug.Log ("X: " +x+ " Y: " +y); + + Debug.Log ("TileRead for faceNumber: " +faceNumber+ " Room Number: "+roomNumber); + int thisTile = Architect.TileRead(x,y, thisFace); + if(thisTile == 0) { + Debug.Log ("Not overlapping"); + overlap = false; + } + else{ + overlap = true; + Debug.Log ("-----------------------Overlap, discard--------------------------------"); + break; + } + } + } + } + + public void DrawRoom(FaceObj thisFace){ + for(int y = yStart; y < yStart + ySize; y++){ + for(int x = xStart; x < xStart + xSize; x++){ + Debug.Log ("TileWrite for FaceNumber: " + thisFace.faceNumber+ " Room Number: " +roomNumber); + Debug.Log ("Passing X: " +x+ " and Y: " +y+ " to TileWrite"); + bool drawn = Architect.TileWrite(x,y,roomTileArray[x - xStart, y - yStart], thisFace); + if (drawn) Debug.Log("!!!!!!Draw successful!!!!!!!!!!"); + } + } + } +} diff --git a/RoomRect.cs b/RoomRect.cs new file mode 100644 index 0000000..e7d665a --- /dev/null +++ b/RoomRect.cs @@ -0,0 +1,15 @@ +using UnityEngine; +using System.Collections; + +public class RoomRect : MonoBehaviour { + + // Use this for initialization + void Start () { + + } + + // Update is called once per frame + void Update () { + + } +} diff --git a/SceneBuilder.cs b/SceneBuilder.cs new file mode 100644 index 0000000..c346355 --- /dev/null +++ b/SceneBuilder.cs @@ -0,0 +1,393 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +[RequireComponent(typeof(Architect))] +[RequireComponent(typeof(TerrainManager))] + +public class SceneBuilder : MonoBehaviour { + + public int cubeSize; + public Vector3 levelSize = new Vector3(10f, 10f, 10f); + public enum ModelType { + LASIUS_NIGER + } + public ModelType modelType; + public int desiredNetworkSize = 0; + + public int seedLayers = 2; + public int targetBuildSteps; + + protected float cubeSpacing; + protected bool levelLocked = false; + protected bool cubeSeed = false; + protected bool agentSeed = false; + protected bool terrainSeed = false; + protected bool networkCheck = false; + protected int levelDimension; + protected bool stillChecking = true; + protected bool pathFind = false; + + protected CubeObj networkCube; + + Architect architect; + TerrainManager terrainManager; + AgentHandler agentHandler; + + List closedList = new List(); + List openList = new List(); + List hitList = new List(); + + List drawRays = new List(); + + void Awake(){ + architect = GetComponent(); + terrainManager = GetComponent(); + agentHandler = GetComponent(); + cubeSpacing = (1 + cubeSize * .05f); + } + + void Update(){ + foreach(Ray ray in drawRays){ + Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.magenta, Mathf.Infinity, false); + } + } + + void OnGUI(){ + + if(cubeSeed == false) GUI.enabled = true; + else GUI.enabled = false; + + if(GUI.Button(new Rect(10, 10, 150, 30), "Seed Cubes")){ + SeedCubes(); + cubeSeed = true; + } + + if(cubeSeed == true && agentSeed == false) GUI.enabled = true; + else GUI.enabled = false; + + if(GUI.Button (new Rect(10, 50, 150, 30), "Seed Agents")){ + agentSeed = true; + agentHandler.SeedAgents(); + } + + if(agentSeed == true && levelLocked == false) GUI.enabled = true; + else GUI.enabled = false; + + + if(GUI.Button(new Rect(10, 90, 150, 30), "Use This Build")){ + levelLocked = true; + LockBuild(); + } + + /* + if(levelLocked == true && networkCheck == false) GUI.enabled = true; + else GUI.enabled = false; + + if(GUI.Button(new Rect(10, 130, 150, 30), "Network Check")){ + networkCheck = true; + NetworkCheck(); + } + + if(networkCheck == true) GUI.enabled = true; + else GUI.enabled = false; + + if(GUI.Button(new Rect(10, 170, 150, 30), "Pathfind")){ + pathFind = true; + PathFind(); + } + */ + + GUI.enabled = true; + + GUI.Label(new Rect(10, 205, 150, 20), "Cube Size: " +cubeSize); + cubeSize = (int)Mathf.Floor(GUI.HorizontalSlider(new Rect(10, 230, 150, 30), cubeSize, 2f, 32f)); + + GUI.Label(new Rect(10, 325, 150, 20), "Step count: " +agentHandler.stepCount.ToString()); + GUI.Label(new Rect(10, 350, 150, 20), "Pickup count: " +agentHandler.cubeHeldCount.ToString()); + } + + void SeedCubes(){ + int[,,] cubePositionArray = terrainManager.cubePositionArray; + int cubeNumber = 0; + + + // Seed cube position array and generate seed cubes in scene + // NOTE: y is set to break the loop at seedLayers, seeding only those y coords up to the user-specified seedLayers public var in this script. + + for(int y = 0; y < seedLayers; y++){ + for(int z = 0; z < levelSize.z; z++){ + for(int x = 0; x < levelSize.x; x++){ + cubePositionArray[x,y,z] = 1; + architect.GenerateCube(cubeNumber, cubeSize, new Vector3(x, y, z), true); + cubeNumber++; + } + } + } + } + + void LockBuild(){ + + // Kill all agents + + agentHandler.KillAgents(); + + // Eliminate floor cubes (i.e. all cubes with y = 0 in the cube array) + + List cubeTrashList = terrainManager.cubeMasterList.Where (c => c.cubeArrayPosition.y == 0).ToList(); + foreach(CubeObj cube in cubeTrashList){ + Destroy(cube.cubeGameObj); + terrainManager.cubeMasterList.Remove(cube); + } + + // Push cubes away from one another by cubeSpacing in all dimensions + + List cubeList = terrainManager.cubeMasterList.Select(c => c.cubeGameObj).ToList(); + foreach(GameObject cube in cubeList){ + Vector3 originalPosition = cube.transform.position; + Vector3 pushVector = new Vector3(cubeSpacing, cubeSpacing, cubeSpacing); + Vector3 newPosition = Vector3.Scale(originalPosition, pushVector); + + // Augment y coord to produce level with more verticality + + int verticalStep = cubeSize + (int)pushVector.y; + int verticalRand = (int)Random.Range(0, levelSize.y); + newPosition = newPosition + new Vector3(0, verticalStep * verticalRand, 0); + cube.transform.position = newPosition; + } + } + + void NetworkCheck(){ + + List largestClosedList = new List(); + + foreach(CubeObj seed in terrainManager.cubeMasterList){ + + bool continueCasting = true; + + do { + if(!openList.Any()) openList.Add(seed.cubeGameObj); + + foreach(GameObject cube in openList){ + Transform cubeTransform = cube.transform; + + RaycastHit cast1; + RaycastHit cast2; + RaycastHit cast3; + RaycastHit cast4; + RaycastHit cast5; + RaycastHit cast6; + + if(Physics.Raycast(cubeTransform.position, cubeTransform.up, out cast1, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast1.collider.gameObject) && !openList.Any(c => c == cast1.collider.gameObject)) hitList.Add (cast1.collider.gameObject); + } + if(Physics.Raycast(cubeTransform.position, -cubeTransform.up, out cast2, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast2.collider.gameObject) && !openList.Any(c => c == cast2.collider.gameObject)) hitList.Add (cast2.collider.gameObject); + } + if(Physics.Raycast(cubeTransform.position, cubeTransform.right, out cast3, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast3.collider.gameObject) && !openList.Any(c => c == cast3.collider.gameObject)) hitList.Add (cast3.collider.gameObject); + } + if(Physics.Raycast(cubeTransform.position, -cubeTransform.right, out cast4, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast4.collider.gameObject) && !openList.Any(c => c == cast4.collider.gameObject)) hitList.Add (cast4.collider.gameObject); + } + if(Physics.Raycast(cubeTransform.position, cubeTransform.forward, out cast5, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast5.collider.gameObject) && !openList.Any(c => c == cast5.collider.gameObject)) hitList.Add (cast5.collider.gameObject); + } + if(Physics.Raycast(cubeTransform.position, -cubeTransform.forward, out cast6, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast6.collider.gameObject) && !openList.Any(c => c == cast6.collider.gameObject)) hitList.Add (cast6.collider.gameObject); + } + } + + if(hitList.Any ()) { + foreach(GameObject toClosed in openList) closedList.Add (toClosed); + openList.Clear(); + + foreach(GameObject toOpen in hitList) openList.Add(toOpen); + hitList.Clear(); + + } else { + foreach(GameObject toClosed in openList) closedList.Add (toClosed); + openList.Clear(); + + seed.networkSize = closedList.Count(); + if(closedList.Count() > largestClosedList.Count()){ + largestClosedList.Clear(); + foreach(GameObject c in closedList){ + largestClosedList.Add(c); + } + } + closedList.Clear(); + continueCasting = false; + } + } + while (continueCasting == true); + } + + // Find the cube with the largest networkSize and SHOW VISUAL RAYCASTS FOR DEBUG + + networkCube = terrainManager.cubeMasterList.Aggregate((c1,c2) => c1.networkSize > c2.networkSize ? c1 : c2); + if (networkCube != null) Debug.Log ("We have a max network cube!"); + + bool debugCast = true; + int iteration = 0; + + do { + + Debug.Log ("This is iteration: " +iteration); + if(!openList.Any()) openList.Add(networkCube.cubeGameObj); + + foreach(GameObject cube in openList){ + Transform cubeTransform = cube.transform; + + RaycastHit cast1; + RaycastHit cast2; + RaycastHit cast3; + RaycastHit cast4; + RaycastHit cast5; + RaycastHit cast6; + + Ray ray1 = new Ray(cubeTransform.position, cubeTransform.up); + Ray ray2 = new Ray(cubeTransform.position, -cubeTransform.up); + Ray ray3 = new Ray(cubeTransform.position, cubeTransform.right); + Ray ray4 = new Ray(cubeTransform.position, -cubeTransform.right); + Ray ray5 = new Ray(cubeTransform.position, cubeTransform.forward); + Ray ray6 = new Ray(cubeTransform.position, -cubeTransform.forward); + + drawRays.Add(ray1); + drawRays.Add(ray2); + drawRays.Add(ray3); + drawRays.Add(ray4); + drawRays.Add(ray5); + drawRays.Add(ray6); + + + if(Physics.Raycast(ray1, out cast1, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast1.collider.gameObject) && !openList.Any(c => c == cast1.collider.gameObject)) hitList.Add (cast1.collider.gameObject); + } + if(Physics.Raycast(ray2, out cast2, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast2.collider.gameObject) && !openList.Any(c => c == cast2.collider.gameObject)) hitList.Add (cast2.collider.gameObject); + } + if(Physics.Raycast(ray3, out cast3, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast3.collider.gameObject) && !openList.Any(c => c == cast3.collider.gameObject)) hitList.Add (cast3.collider.gameObject); + } + if(Physics.Raycast(ray4, out cast4, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast4.collider.gameObject) && !openList.Any(c => c == cast4.collider.gameObject)) hitList.Add (cast4.collider.gameObject); + } + if(Physics.Raycast(ray5, out cast5, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast5.collider.gameObject) && !openList.Any(c => c == cast5.collider.gameObject)) hitList.Add (cast5.collider.gameObject); + } + if(Physics.Raycast(ray6, out cast6, 1000f, terrainManager.cubeFaceLayerMask)){ + if(!closedList.Any(c => c == cast6.collider.gameObject) && !openList.Any(c => c == cast6.collider.gameObject)) hitList.Add (cast6.collider.gameObject); + } + } + + if(hitList.Any ()) { + foreach(GameObject toClosed in openList) closedList.Add (toClosed); + openList.Clear(); + + foreach(GameObject toOpen in hitList) openList.Add(toOpen); + hitList.Clear(); + + } else { + foreach(GameObject toClosed in openList) closedList.Add (toClosed); + openList.Clear(); + debugCast = false; + } + iteration++; + } + while (debugCast == true); + + foreach(GameObject networkedCube in largestClosedList){ + Renderer[] faces = GetComponentsInChildren(); + foreach(Renderer face in faces){ + face.material.color = Color.cyan; + } + } + + // Mark the networkCube in red + + foreach(FaceObj face in networkCube.faceList){ + face.faceGameObj.renderer.material.color = Color.red; + } + + // Clear cubeObjs not part of the longestClosedList network from the cubeMasterList and their cubeGameObj from the Scene + + List destroyList = new List(); + + foreach(CubeObj checkCube in terrainManager.cubeMasterList){ + GameObject destroyCandidate = checkCube.cubeGameObj; + Debug.Log ("Largest closed list has length: " +largestClosedList.Count()); + if(!largestClosedList.Any(c => c.GetInstanceID() == destroyCandidate.GetInstanceID())){ + Destroy(destroyCandidate); + destroyList.Add(checkCube); + } + } + + foreach(CubeObj destroyCube in destroyList){ + terrainManager.cubeMasterList.Remove(destroyCube); + } + + /* + foreach(CubeObj cube in terrainManager.cubeMasterList){ + + Transform cubeTransform = cube.cubeGameObj.transform; + Vector3 cubePosition = cubeTransform.position; + + bool cast1 = Physics.Raycast(cubePosition, cubeTransform.up, terrainManager.cubeFaceLayerMask); + bool cast2 = Physics.Raycast(cubePosition, -cubeTransform.up, terrainManager.cubeFaceLayerMask); + bool cast3 = Physics.Raycast(cubePosition, cubeTransform.right, terrainManager.cubeFaceLayerMask); + bool cast4 = Physics.Raycast(cubePosition, -cubeTransform.right, terrainManager.cubeFaceLayerMask); + bool cast5 = Physics.Raycast(cubePosition, cubeTransform.forward, terrainManager.cubeFaceLayerMask); + bool cast6 = Physics.Raycast(cubePosition, -cubeTransform.forward, terrainManager.cubeFaceLayerMask); + + if(!cast1 && !cast2 && !cast3 && !cast4 && !cast5 && !cast6){ + foreach(FaceObj face in cube.faceList){ + face.faceGameObj.renderer.material.color = Color.red; + Destroy(cube.cubeGameObj); + } + } else { + foreach(FaceObj face in cube.faceList){ + face.faceGameObj.renderer.material.color = Color.green; + } + + } + } + */ + } + + void PathFind(){ + + // Calculate positionSum for each cube + + foreach(CubeObj cube in terrainManager.cubeMasterList){ + cube.PositionSum(); + } + + // Determine start cube, end cube, check for accessibility, and set their material colors to mark them in the Scene + + CubeObj startCube = terrainManager.cubeMasterList.Aggregate((c1, c2) => c1.positionSum > c2.positionSum ? c1 : c2); + CubeObj endCube = terrainManager.cubeMasterList.Aggregate((c1,c2) => c1.positionSum < c2.positionSum ? c1 : c2); + + foreach(FaceObj face in startCube.faceList){ + face.faceGameObj.renderer.material.color = Color.blue; + } + + foreach(FaceObj face in endCube.faceList){ + face.faceGameObj.renderer.material.color = Color.yellow; + } + + + + } + + void SeedTerrain(){ + + + + // Seed each cube with rooms + + + + } +} diff --git a/TerrainManager.cs b/TerrainManager.cs new file mode 100644 index 0000000..3315590 --- /dev/null +++ b/TerrainManager.cs @@ -0,0 +1,86 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +[RequireComponent(typeof(Architect))] +public class TerrainManager : MonoBehaviour { + + public Texture2D tileAtlas; + public int tileResolution; + public int numCubeFaces = 6; + public LayerMask cubeFaceLayerMask; + public int[,,] cubePositionArray; + public List cubeMasterList = new List(); + public List faceMasterList = new List(); + public List roomMasterList = new List(); + + Vector3 levelSize; + SceneBuilder sceneBuilder; + + void Awake(){ + sceneBuilder = GetComponent(); + levelSize = sceneBuilder.levelSize; + cubePositionArray = new int[(int)levelSize.x, (int)levelSize.y, (int)levelSize.z]; + tileAtlas = Resources.Load("spriteAtlas"); + tileResolution = tileAtlas.height; + levelSize = sceneBuilder.levelSize; + cubePositionArray = new int[(int)levelSize.x, (int)levelSize.y, (int)levelSize.z]; + } + + public bool CheckForCube(Vector3 coords){ + if(cubePositionArray[(int)coords.x, (int)coords.y, (int)coords.z] == 1){ + return true; + } else { + return false; + } + } + + public float LatestDropInNeighborhood(Vector3 checkCoords){ + + List neighborCoords = new List(); + + neighborCoords.Add(new Vector3(checkCoords.x - 1, checkCoords.y - 1, checkCoords.z - 1)); + neighborCoords.Add(new Vector3(checkCoords.x - 0, checkCoords.y - 1, checkCoords.z - 1)); + neighborCoords.Add(new Vector3(checkCoords.x + 1, checkCoords.y - 1, checkCoords.z - 1)); + neighborCoords.Add(new Vector3(checkCoords.x - 1, checkCoords.y - 0, checkCoords.z - 1)); + neighborCoords.Add(new Vector3(checkCoords.x - 0, checkCoords.y - 0, checkCoords.z - 1)); + neighborCoords.Add(new Vector3(checkCoords.x + 1, checkCoords.y - 0, checkCoords.z - 1)); + neighborCoords.Add(new Vector3(checkCoords.x - 1, checkCoords.y + 1, checkCoords.z - 1)); + neighborCoords.Add(new Vector3(checkCoords.x - 0, checkCoords.y + 1, checkCoords.z - 1)); + neighborCoords.Add(new Vector3(checkCoords.x + 1, checkCoords.y + 1, checkCoords.z - 1)); + neighborCoords.Add(new Vector3(checkCoords.x - 1, checkCoords.y - 1, checkCoords.z - 0)); + neighborCoords.Add(new Vector3(checkCoords.x - 0, checkCoords.y - 1, checkCoords.z - 0)); + neighborCoords.Add(new Vector3(checkCoords.x + 1, checkCoords.y - 1, checkCoords.z - 0)); + neighborCoords.Add(new Vector3(checkCoords.x - 1, checkCoords.y - 0, checkCoords.z - 0)); + neighborCoords.Add(new Vector3(checkCoords.x - 0, checkCoords.y - 0, checkCoords.z - 0)); + neighborCoords.Add(new Vector3(checkCoords.x + 1, checkCoords.y - 0, checkCoords.z - 0)); + neighborCoords.Add(new Vector3(checkCoords.x - 1, checkCoords.y + 1, checkCoords.z - 0)); + neighborCoords.Add(new Vector3(checkCoords.x - 0, checkCoords.y + 1, checkCoords.z - 0)); + neighborCoords.Add(new Vector3(checkCoords.x + 1, checkCoords.y + 1, checkCoords.z - 0)); + neighborCoords.Add(new Vector3(checkCoords.x - 1, checkCoords.y - 1, checkCoords.z + 1)); + neighborCoords.Add(new Vector3(checkCoords.x - 0, checkCoords.y - 1, checkCoords.z + 1)); + neighborCoords.Add(new Vector3(checkCoords.x + 1, checkCoords.y - 1, checkCoords.z + 1)); + neighborCoords.Add(new Vector3(checkCoords.x - 1, checkCoords.y - 0, checkCoords.z + 1)); + neighborCoords.Add(new Vector3(checkCoords.x - 0, checkCoords.y - 0, checkCoords.z + 1)); + neighborCoords.Add(new Vector3(checkCoords.x + 1, checkCoords.y - 0, checkCoords.z + 1)); + neighborCoords.Add(new Vector3(checkCoords.x - 1, checkCoords.y + 1, checkCoords.z + 1)); + neighborCoords.Add(new Vector3(checkCoords.x - 0, checkCoords.y + 1, checkCoords.z + 1)); + neighborCoords.Add(new Vector3(checkCoords.x + 1, checkCoords.y + 1, checkCoords.z + 1)); + + float latestDropTime = 0f; + + foreach(Vector3 cubeCoords in neighborCoords){ + CubeObj cube = cubeMasterList.FirstOrDefault(i => i.cubeArrayPosition == cubeCoords); + if(cube != null){ + float dropTime = cube.dropTime; + if (dropTime > latestDropTime){ + latestDropTime = dropTime; + } + } + } + + return latestDropTime; + + } +} diff --git a/TileObj.cs b/TileObj.cs new file mode 100644 index 0000000..cf33273 --- /dev/null +++ b/TileObj.cs @@ -0,0 +1,26 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +public class TileObj : MonoBehaviour { + + FaceObj faceObj; + RoomObj roomObj; + int tileType; + + public static Dictionary TileDict = new Dictionary(){ + {"unknown", 0}, + {"floor", 1}, + {"wall", 2}, + {"water", 3}, + {"upstairs", 4}, + {"downstairs", 5} + }; + + public TileObj(int tileType, FaceObj face, RoomObj room){ + this.tileType = tileType; + faceObj = face; + roomObj = room; + } +}