public void SetRoofTileID(int subGridIndex, int gridX, int gridY, int roofID) { GridIDs gridIDs = GetGridIDs(subGridIndex); if (!gridIDs.isNull) { try { gridIDs.roofTileIDs[gridX, gridY] = roofID; } catch (IndexOutOfRangeException) { ComponentSubGrid subGrid = grid.GetSubGrid(subGridIndex); if (subGrid != null) { gridIDs.roofTileIDs = new int[subGrid.gridSizeX, subGrid.gridSizeY]; } } } else { ComponentSubGrid subGrid = grid.GetSubGrid(subGridIndex); if (subGrid != null) { GridIDs newGridIDs = new GridIDs(subGrid.gridSizeX, subGrid.gridSizeY, subGridIndex); gridTileIDs.Add(newGridIDs); newGridIDs.roofTileIDs[gridX, gridY] = roofID; } } }
public List <ComponentNode> GetCrossSection(int units, ComponentNode node) { ComponentSubGrid grid = GetSubGrid(node.subGridIndex); List <ComponentNode> neighbours = new List <ComponentNode> (); for (int x = -units; x <= units; x++) { for (int y = -units; y <= units; y++) { if (x == 0 && y == 0) { continue; } int checkX = node.gridX + x; int checkY = node.gridY + y; if (checkX >= 0 && checkX < grid.gridSizeX && checkY >= 0 && checkY < grid.gridSizeY) { neighbours.Add(grid.grid[checkX, checkY]); } } } return(neighbours); }
public void SetupNewGrid(ComponentSubGrid newGrid, Vector3 bottomRightPos) { newGrid.transform.parent = transform; newGrid.Setup(nodeRadius, null); grids.Add(newGrid); newGrid.gridPlanes[0, 0].transform.parent = transform; newGrid.transform.parent = newGrid.gridPlanes[0, 0].transform; newGrid.gridPlanes[0, 0].transform.position = bottomRightPos; newGrid.transform.parent = transform; newGrid.gridPlanes[0, 0].transform.parent = newGrid.transform; }
public ComponentWall CreateTopWall(ComponentSubGrid grid, ComponentNode node, int wallID, bool interiorWall) { int sub = DetermineSubID(node, "Top"); // Create wall GameObject newWall = Instantiate(db.GetStoreObject(wallID, sub).gameObject_); ComponentWall componentWall = newWall.GetComponent <ComponentWall>(); if (node == null) { return(componentWall); } if (componentWall == null) { newWall.AddComponent <ComponentWall>(); } componentWall.gridX = node.gridX; componentWall.gridY = node.gridY; componentWall.parentNode = node; componentWall.wallState = ComponentWall.WallState.solid; Vector3 targetPos = node.worldPosition; Vector3 newPos = targetPos; newWall.transform.position = newPos; Vector3 oldEuler = newWall.transform.eulerAngles; float yRot = 0.0f; Vector3 newEuler = new Vector3(oldEuler.x, yRot, oldEuler.z); newWall.transform.eulerAngles = newEuler; newWall.transform.parent = wallsParent.transform; newWall.tag = "Wall"; if (parentWalls.showingTransparency) { newWall.layer = 2; } else { newWall.layer = 22; } topWall.walls.Add(newWall.GetComponent <ComponentWall>()); if (wallID != 12000) // if not building trans wall { if (interiorWall) { SetIntWallID(componentWall, node, wallID); } else { SetExtWallID(componentWall, node, wallID); } } return(componentWall); }
public void SetupNewGrid(Vector2 dimensions) { GameObject newSubGridGO = new GameObject(gameObject.name + "Grid" + grids.Count); newSubGridGO.transform.parent = transform; ComponentSubGrid newSubGrid = newSubGridGO.AddComponent <ComponentSubGrid>(); newSubGrid.parentGrid = this; newSubGrid.subGridIndex = grids.Count; newSubGrid.Setup(dimensions, nodeRadius, null); grids.Add(newSubGrid); }
public List <ComponentNode> GetSideNeighbours(ComponentNode node, string side) { ComponentSubGrid grid = GetSubGrid(node.subGridIndex); List <ComponentNode> neighbours = new List <ComponentNode>(); int xValMin = 0; int xValMax = 0; int yValMin = 0; int yValMax = 0; switch (side) { case "Top": yValMin = -1; yValMax = 1; break; case "Right": xValMin = -1; xValMax = 1; break; case "Left": xValMin = -1; xValMax = 1; break; case "Bottom": yValMin = -1; yValMax = 1; break; } for (int x = xValMin; x <= xValMax; x++) { for (int y = yValMin; y <= yValMax; y++) { if (x == 0 && y == 0) { continue; } int checkX = node.gridX + x; int checkY = node.gridY + y; if (checkX >= 0 && checkX < grid.gridSizeX && checkY >= 0 && checkY < grid.gridSizeY) { neighbours.Add(grid.grid[checkX, checkY]); } } } return(neighbours); }
public void SetAllTileIDs(int subGridIndex, int[,] tileIDs, int[,] intWallIDs, int[,] extWallIDs, int[,] roofIDs) { ComponentSubGrid subGrid = grid.GetSubGrid(subGridIndex); if (tileIDs == null && subGrid != null) { tileIDs = new int[subGrid.gridSizeX, subGrid.gridSizeY]; for (int i = 0; i < subGrid.gridSizeX; i++) { for (int j = 0; j < subGrid.gridSizeY; j++) { tileIDs[i, j] = 10002; } } } if (intWallIDs == null && subGrid != null) { intWallIDs = new int[subGrid.gridSizeX, subGrid.gridSizeY]; for (int i = 0; i < subGrid.gridSizeX; i++) { for (int j = 0; j < subGrid.gridSizeY; j++) { intWallIDs[i, j] = 12002; } } } if (extWallIDs == null && subGrid != null) { extWallIDs = new int[subGrid.gridSizeX, subGrid.gridSizeY]; for (int i = 0; i < subGrid.gridSizeX; i++) { for (int j = 0; j < subGrid.gridSizeY; j++) { extWallIDs[i, j] = 12003; } } } if (roofIDs == null && subGrid != null) { roofIDs = new int[subGrid.gridSizeX, subGrid.gridSizeY]; for (int i = 0; i < subGrid.gridSizeX; i++) { for (int j = 0; j < subGrid.gridSizeY; j++) { roofIDs[i, j] = 11000; } } } gridTileIDs.Add(new GridIDs(subGridIndex, tileIDs, intWallIDs, extWallIDs, roofIDs)); }
public Grid_s(Vector3 position, ComponentSubGrid subGrid, int[,] tileIDs, int[,] intWallIDs, int[,] extWallIDs, int[,] roofIDs) { posX = position.x; posY = position.y; posZ = position.z; scaleX = subGrid.gridWorldSize.x; scaleZ = subGrid.gridWorldSize.y; gridRotY = subGrid.gridEulerRotation.y; subGridIndex = subGrid.subGridIndex; gridTileIDs = tileIDs; intWallTileIDs = intWallIDs; extWallTileIDs = extWallIDs; roofTileIDs = roofIDs; }
public int DetermineSubID(ComponentNode node, string side) { ComponentGrid componentGrid = parentWalls.gameObject.GetComponent <ComponentGrid>(); ComponentSubGrid grid = componentGrid.GetSubGrid(subGridIndex); ComponentNode neighbourNode = GetNeighbouringComponentNode(grid, componentGrid.gameObject.name, node); if (node != null) { if (node.doorway == ComponentNode.DoorwayValue.doorway) { return(1); // sub index for left side doorway walls } else if (node.window == ComponentNode.WindowValue.largewindow) { return(2); } else if (node.window == ComponentNode.WindowValue.mediumwindow) { return(3); } else if (node.window == ComponentNode.WindowValue.smallwindow) { return(4); } } if (neighbourNode != null) { if (neighbourNode.doorway == ComponentNode.DoorwayValue.doorway) { return(1); // sub index for left side doorway walls } else if (neighbourNode.window == ComponentNode.WindowValue.largewindow) { return(2); } else if (neighbourNode.window == ComponentNode.WindowValue.mediumwindow) { return(3); } else if (neighbourNode.window == ComponentNode.WindowValue.smallwindow) { return(4); } } return(0); }
public ComponentSubGrid GetSubGrid() { RaycastHit[] hits = Physics.RaycastAll(transform.position, Vector3.down); foreach (RaycastHit hit in hits) { if (hit.transform.tag == "Floor") { GameObject subGridObject = hit.transform.parent.parent.gameObject; ComponentSubGrid subGrid = subGridObject.GetComponent <ComponentSubGrid>(); if (subGrid != null) { return(subGrid); } } } print("returning null"); return(null); }
// Gets the node in the middle of a side public ComponentNode GetMiddleNode(WallSection wallSection) { ComponentSubGrid grid = parentWalls.GetComponent <ComponentGrid>().GetSubGrid(subGridIndex); foreach (ComponentWall wall in wallSection.walls) { ComponentNode parentNode = wall.parentNode; switch (wallSection.side) { case "Top": if (parentNode.gridX == grid.gridSizeX - 1 && parentNode.gridY == grid.gridSizeY / 2) { return(parentNode); } break; case "Right": if (parentNode.gridX == grid.gridSizeX / 2 && parentNode.gridY == 0) { return(parentNode); } break; case "Left": if (parentNode.gridX == grid.gridSizeX / 2 && parentNode.gridY == grid.gridSizeY - 1) { return(parentNode); } break; case "Bottom": if (parentNode.gridX == 0 && parentNode.gridY == grid.gridSizeY / 2) { return(parentNode); } break; } } return(new ComponentNode()); }
public ComponentNode GetNeighbouringComponentNode(ComponentSubGrid grid, string component, ComponentNode node) { Vector3 nodePos = node.worldPosition; int xVal = (node.gridX == grid.gridSizeX - 1) ? 0 : 1; int yVal = (node.gridY == grid.gridSizeY - 1) ? 0 : 1; for (int x = -xVal; x <= xVal; x++) { // Checks if its next to another component for (int y = -yVal; y <= yVal; y++) { if (x == 0 && y == 0) { continue; // skip the middle value, only do the edges } float newX = nodePos.x + y * grid.nodeRadius * 2; float newZ = nodePos.z + x * grid.nodeRadius * 2; Vector3 posToCheck = new Vector3(newX, 1, newZ); Debug.DrawRay(posToCheck, Vector3.down, (x == 0 && y == 0) ? Color.blue : Color.green); //Debug.Break (); RaycastHit hit; if (Physics.Raycast(posToCheck, Vector3.down, out hit)) { FloorTile np = hit.transform.gameObject.GetComponent <FloorTile>(); if (np != null) { if (np.node.componentEdge) { if (np.component != component) { return(np.node); } } } } } } return(new ComponentNode()); }
public void CreateWalls(int subGridIndex_, int[,] intWallIDs, int[,] extWallIDs) { subGridIndex = subGridIndex_; if (db == null || dm == null) { dm = GameObject.Find("DispensaryManager").GetComponent <DispensaryManager>(); db = GameObject.Find("Database").GetComponent <Database>(); } if (wallsParent != null) { Destroy(wallsParent.gameObject); // Destroys all walls } WallSection previousTop = topWall; WallSection previousRight = rightWall; WallSection previousLeft = leftWall; WallSection previousBottom = bottomWall; topWall = new WallSection("Top", new List <ComponentWall>()); topWall.reference = this; if (previousTop != null) { topWall.transparent = previousTop.transparent; } rightWall = new WallSection("Right", new List <ComponentWall>()); rightWall.reference = this; if (previousRight != null) { rightWall.transparent = previousRight.transparent; } leftWall = new WallSection("Left", new List <ComponentWall>()); leftWall.reference = this; if (previousLeft != null) { leftWall.transparent = previousLeft.transparent; } bottomWall = new WallSection("Bottom", new List <ComponentWall>()); bottomWall.reference = this; if (previousBottom != null) { bottomWall.transparent = previousBottom.transparent; } List <ComponentNode> edgeNodes = GetEdgeNodes(); wallsParent = new GameObject("ComponentWalls"); wallsParent.transform.parent = transform; ComponentSubGrid grid = parentWalls.GetComponent <ComponentGrid>().GetSubGrid(subGridIndex); foreach (ComponentNode node in edgeNodes) { int intWallID = -1; if (intWallIDs != null) { try { intWallID = intWallIDs[node.gridX, node.gridY]; if (intWallID == 0) { intWallID = 12002; } } catch (IndexOutOfRangeException) { intWallID = 12002; } } else { intWallID = 12002; } int extWallID = -1; if (extWallIDs != null) { try { extWallID = extWallIDs[node.gridX, node.gridY]; if (extWallID == 0) { extWallID = 12003; } } catch (IndexOutOfRangeException) { extWallID = 12003; } } else { extWallID = 12003; } // Right Row if (node.gridY == 0) { ComponentWall intComponentWall = CreateRightWall(grid, node, intWallID, true); ComponentWall extComponentWall = CreateRightWall(grid, node, extWallID, false); RaycastHit hit; if (Physics.Raycast(intComponentWall.raycastObject.transform.position, Vector3.down, out hit)) // just do one walls raycast, because both walls are affected the same { FloorTile np = hit.transform.gameObject.GetComponent <FloorTile>(); if (np != null) { if (np.node.componentEdge) { if (np.component != parentWalls.gameObject.name) { Destroy(extComponentWall.gameObject); // dont do an external wall if theres another component over there if (parentWalls.showingTransparency) { Destroy(intComponentWall.gameObject); // Destroy solid wall intComponentWall = CreateRightWall(grid, node, 12000, true); // Make a trans wall (it identifies as male) } } else if (np.component == parentWalls.gameObject.name) { Destroy(intComponentWall.gameObject); // Destroy wall Destroy(extComponentWall.gameObject); // Destroy wall } } } if (hit.transform.tag == "BuildableZone") { if (intComponentWall != null && extComponentWall != null) { GameObject newWallTrim = Instantiate(db.GetStoreObject(9999, 0).gameObject_); newWallTrim.transform.parent = extComponentWall.transform; //Vector3 pos = componentWall.transform.position; //Vector3 trimPos = new Vector3(pos.x, .02f, pos.z); //newWallTrim.transform.position = trimPos + new Vector3(0, 0, -0.05f); //newWallTrim.transform.eulerAngles = componentWall.gameObject.transform.eulerAngles; //newWallTrim.transform.localScale = new Vector3(1, 2, .1f); } } } } // Left Row if (node.gridY == grid.gridSizeY - 1) { ComponentWall intComponentWall = CreateLeftWall(grid, node, intWallID, true); ComponentWall extComponentWall = CreateLeftWall(grid, node, extWallID, false); RaycastHit hit; if (Physics.Raycast(intComponentWall.raycastObject.transform.position, Vector3.down, out hit)) { FloorTile np = hit.transform.gameObject.GetComponent <FloorTile>(); if (np != null) { if (np.node.componentEdge) { if (np.component != parentWalls.gameObject.name) { Destroy(extComponentWall.gameObject); // dont do an external wall if theres another component over there if (parentWalls.showingTransparency) { Destroy(intComponentWall.gameObject); // Destroy solid wall intComponentWall = CreateLeftWall(grid, node, 12000, true); // Make a trans wall (it identifies as male) } } else if (np.component == parentWalls.gameObject.name) { Destroy(intComponentWall.gameObject); // Destroy wall Destroy(extComponentWall.gameObject); // Destroy wall } } } if (hit.transform.tag == "BuildableZone") { if (intComponentWall != null && extComponentWall != null) { GameObject newWallTrim = Instantiate(db.GetStoreObject(9999, 0).gameObject_); newWallTrim.transform.parent = extComponentWall.transform; //Vector3 pos = componentWall.transform.position; //Vector3 trimPos = new Vector3(pos.x, .02f, pos.z); //newWallTrim.transform.position = trimPos + new Vector3(0, 0, -0.05f); //newWallTrim.transform.eulerAngles = componentWall.gameObject.transform.eulerAngles; //newWallTrim.transform.localScale = new Vector3(1, 2, .1f); } } } } // Top Row if (node.gridX == grid.gridSizeX - 1) { ComponentWall intComponentWall = CreateTopWall(grid, node, intWallID, true); ComponentWall extComponentWall = CreateTopWall(grid, node, extWallID, false); RaycastHit hit; if (Physics.Raycast(intComponentWall.raycastObject.transform.position, Vector3.down, out hit)) { FloorTile np = hit.transform.gameObject.GetComponent <FloorTile>(); if (np != null) { if (np.node.componentEdge) { if (np.component != parentWalls.gameObject.name) { Destroy(extComponentWall.gameObject); // dont do an external wall if theres another component over there if (parentWalls.showingTransparency) { Destroy(intComponentWall.gameObject); // Destroy solid wall intComponentWall = CreateTopWall(grid, node, 12000, true); // Make a trans wall (it identifies as male) } } else if (np.component == parentWalls.gameObject.name) { Destroy(intComponentWall.gameObject); // Destroy wall Destroy(extComponentWall.gameObject); // Destroy wall } } } if (hit.transform.tag == "BuildableZone") { if (intComponentWall != null && extComponentWall != null) { GameObject newWallTrim = Instantiate(db.GetStoreObject(9999, 0).gameObject_); newWallTrim.transform.parent = extComponentWall.transform; //Vector3 pos = componentWall.transform.position; //Vector3 trimPos = new Vector3(pos.x, .02f, pos.z); //newWallTrim.transform.position = trimPos + new Vector3(0, 0, -0.05f); //newWallTrim.transform.eulerAngles = componentWall.gameObject.transform.eulerAngles; //newWallTrim.transform.localScale = new Vector3(1, 2, .1f); } } } } // Bottom Row if (node.gridX == 0) { ComponentWall intComponentWall = CreateBottomWall(grid, node, intWallID, true); ComponentWall extComponentWall = CreateBottomWall(grid, node, extWallID, false); RaycastHit hit; if (Physics.Raycast(intComponentWall.raycastObject.transform.position, Vector3.down, out hit)) { FloorTile np = hit.transform.gameObject.GetComponent <FloorTile>(); if (np != null) { if (np.node.componentEdge) { if (np.component != parentWalls.gameObject.name) { Destroy(extComponentWall.gameObject); // dont do an external wall if theres another component over there if (parentWalls.showingTransparency) { Destroy(intComponentWall.gameObject); // Destroy solid wall intComponentWall = CreateBottomWall(grid, node, 12000, true); // Make a trans wall (it identifies as male) } } else if (np.component == parentWalls.gameObject.name) { Destroy(intComponentWall.gameObject); // Destroy wall Destroy(extComponentWall.gameObject); // Destroy wall } } } if (hit.transform.tag == "BuildableZone") { if (intComponentWall != null && extComponentWall != null) { GameObject newWallTrim = Instantiate(db.GetStoreObject(9999, 0).gameObject_); newWallTrim.transform.parent = extComponentWall.transform; //Vector3 pos = componentWall.transform.position; //Vector3 trimPos = new Vector3(pos.x, .02f, pos.z); //newWallTrim.transform.position = trimPos + new Vector3(0, 0, -0.05f); //newWallTrim.transform.eulerAngles = componentWall.gameObject.transform.eulerAngles; //newWallTrim.transform.localScale = new Vector3(1, 2, .1f); } } } } } }
public void RotateWallTileIDs(int subGridIndex) { foreach (GridIDs gridIDs in gridTileIDs) { ComponentSubGrid subGrid = grid.GetSubGrid(subGridIndex); if (gridIDs.subGridIndex == subGridIndex) { try { int oldIntWallX = gridIDs.intWallTileIDs.GetLength(0); int oldIntWallY = gridIDs.intWallTileIDs.GetLength(1); int newIntWallX = oldIntWallY; int newIntWallY = oldIntWallX; int[,] newWallTileIDs = new int[newIntWallX, newIntWallY]; for (int i = 0; i < newIntWallX; i++) { for (int j = 0; j < newIntWallY; j++) { newWallTileIDs[i, j] = gridIDs.intWallTileIDs[j, i]; } } gridIDs.intWallTileIDs = newWallTileIDs; } catch (NullReferenceException) { if (subGrid != null) { gridIDs.intWallTileIDs = new int[subGrid.gridSizeX, subGrid.gridSizeY]; for (int x = 0; x < subGrid.gridSizeX; x++) { for (int y = 0; y < subGrid.gridSizeY; y++) { gridIDs.intWallTileIDs[x, y] = 12002; } } } } try { int oldExtWallX = gridIDs.extWallTileIDs.GetLength(0); int oldExtWallY = gridIDs.extWallTileIDs.GetLength(1); int newExtWallX = oldExtWallY; int newExtWallY = oldExtWallX; int[,] newWallTileIDs = new int[newExtWallX, newExtWallY]; for (int i = 0; i < newExtWallX; i++) { for (int j = 0; j < newExtWallY; j++) { newWallTileIDs[i, j] = gridIDs.extWallTileIDs[j, i]; } } gridIDs.extWallTileIDs = newWallTileIDs; } catch (NullReferenceException) { if (subGrid != null) { gridIDs.extWallTileIDs = new int[subGrid.gridSizeX, subGrid.gridSizeY]; for (int x = 0; x < subGrid.gridSizeX; x++) { for (int y = 0; y < subGrid.gridSizeY; y++) { gridIDs.extWallTileIDs[x, y] = 12003; } } } } return; } } }
public ComponentSubGrid GetClosestComponentGrid(Vector3 worldPoint, string exception) { ComponentSubGrid closestSubGrid = null; float distance = 10000; MainStoreComponent main_c = dm.dispensary.Main_c; if (main_c != null && exception != "MainStore") { if (main_c.grid.grids.Count > 0) { foreach (ComponentSubGrid grid in main_c.grid.grids) { float newDistance = Vector3.Distance(worldPoint, grid.gameObject.transform.position); if (newDistance < distance) { distance = newDistance; closestSubGrid = grid; } } } } if (dm.dispensary.Storage_cs.Count > 0) { for (int i = 0; i < dm.dispensary.Storage_cs.Count; i++) { StorageComponent storage_c = dm.dispensary.Storage_cs[i]; if (storage_c.grid.grids.Count > 0 && exception != "Storage" + i) { foreach (ComponentSubGrid grid in storage_c.grid.grids) { float newDistance = Vector3.Distance(worldPoint, grid.gameObject.transform.position); if (newDistance < distance) { distance = newDistance; closestSubGrid = grid; } } } } } GlassShopComponent glass_c = dm.dispensary.Glass_c; if (glass_c != null && exception != "GlassShop") { if (glass_c.grid.grids.Count > 0) { foreach (ComponentSubGrid grid in glass_c.grid.grids) { float newDistance = Vector3.Distance(worldPoint, grid.gameObject.transform.position); if (newDistance < distance) { distance = newDistance; closestSubGrid = grid; } } } } SmokeLoungeComponent lounge_c = dm.dispensary.Lounge_c; if (lounge_c != null && exception != "SmokeLounge") { if (lounge_c.grid.grids.Count > 0) { foreach (ComponentSubGrid grid in lounge_c.grid.grids) { float newDistance = Vector3.Distance(worldPoint, grid.gameObject.transform.position); if (newDistance < distance) { distance = newDistance; closestSubGrid = grid; } } } } WorkshopComponent workshop_c = dm.dispensary.Workshop_c; if (workshop_c != null && exception != "Workshop") { if (workshop_c.grid.grids.Count > 0) { foreach (ComponentSubGrid grid in lounge_c.grid.grids) { float newDistance = Vector3.Distance(worldPoint, grid.gameObject.transform.position); if (newDistance < distance) { distance = newDistance; closestSubGrid = grid; } } } } if (dm.dispensary.Growroom_cs.Count > 0) { for (int i = 0; i < dm.dispensary.Growroom_cs.Count; i++) { GrowroomComponent growroom_c = dm.dispensary.Growroom_cs[i]; if (growroom_c.grid.grids.Count > 0 && exception != "Growroom" + i) { foreach (ComponentSubGrid grid in growroom_c.grid.grids) { float newDistance = Vector3.Distance(worldPoint, grid.gameObject.transform.position); if (newDistance < distance) { distance = newDistance; closestSubGrid = grid; } } } } } if (dm.dispensary.Processing_cs.Count > 0) { for (int i = 0; i < dm.dispensary.Processing_cs.Count; i++) { ProcessingComponent processing_c = dm.dispensary.Processing_cs[i]; if (processing_c.grid.grids.Count > 0 && exception != "Processing" + i) { foreach (ComponentSubGrid grid in processing_c.grid.grids) { float newDistance = Vector3.Distance(worldPoint, grid.gameObject.transform.position); if (newDistance < distance) { distance = newDistance; closestSubGrid = grid; } } } } } if (dm.dispensary.Hallway_cs.Count > 0) { for (int i = 0; i < dm.dispensary.Hallway_cs.Count; i++) { HallwayComponent hallway_c = dm.dispensary.Hallway_cs[i]; if (hallway_c.grid.grids.Count > 0 && exception != "Hallway" + i) { foreach (ComponentSubGrid grid in hallway_c.grid.grids) { float newDistance = Vector3.Distance(worldPoint, grid.gameObject.transform.position); if (newDistance < distance) { distance = newDistance; closestSubGrid = grid; } } } } } return(closestSubGrid); }
IEnumerator MoveProduct() { while (true) { currentDisplayShelf = null; currentShelf = null; bool movementConflict = false; if (currentProduct != null) { // Raise or lower shelf layer if (Input.GetKeyUp(dm.database.settings.GetRaiseShelfLayer())) { SetCurrentShelfLayer(currentShelfLayer + 1); } if (Input.GetKeyUp(dm.database.settings.GetLowerShelfLayer())) { SetCurrentShelfLayer(currentShelfLayer - 1); } // If moving a box bool productIsBox = currentProduct.currentProduct.IsBox(); if (productIsBox) { // Moving box print("Moving box"); } if (currentProduct.newStack != null) { currentProduct.newStack.CancelAddingBox(); currentProduct.newStack = null; if (currentProduct.currentPlaceholder != null) { currentProduct.currentPlaceholder.gameObject.SetActive(true); } } bool chooseContainerPanelOpen = uiM_v5.chooseContainerPanel.panelOpen; bool placingBudPanelOpen = uiM_v5.packagedBudPlacementPanel.panelOpen; // Raycasting Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] hits = Physics.RaycastAll(ray.origin, ray.direction); if (currentProduct.currentProduct.NeedsContainer()) { if (Input.GetKeyUp(dm.database.settings.GetOpenChooseContainerPanel().ToLower())) { if (chooseContainerPanelOpen) { uiM_v5.CloseChooseContainerPanel(); } else { currentProduct.currentPlaceholder.indicator.OpenChooseContainerPanel(currentProduct); } } } if (chooseContainerPanelOpen || placingBudPanelOpen) { // Dont carry on if either of these windows are open yield return(null); } else if (currentProduct.currentPlaceholder != null) { currentProduct.currentPlaceholder.GetComponent <BoxCollider>().enabled = false; currentProduct.currentPlaceholder.HighlightOff(); bool hitShelf = false; foreach (RaycastHit hit in hits) { if (hit.transform.tag == "DisplayShelf" || hit.transform.tag == "CheckoutCounter") { if (hit.transform.tag == "DisplayShelf") { SetCurrentDisplayShelf(hit.transform.gameObject.GetComponent <StoreObjectFunction_DisplayShelf>()); } else if (hit.transform.tag == "CheckoutCounter") { StoreObjectFunction_DisplayShelf shelf = hit.transform.gameObject.GetComponent <StoreObjectFunction_DisplayShelf>(); if (shelf != null) { SetCurrentDisplayShelf(shelf); } else { hitShelf = false; break; } } } if (hit.transform.gameObject.layer == 21) { currentShelf = hit.transform.gameObject.GetComponent <Shelf>(); if (currentShelf.shelfLayer == GetCurrentShelfLayer()) { hitShelf = true; currentProduct.currentPlaceholder.transform.position = hit.point; } SetCurrentDisplayShelf(currentShelf.parentShelf); } if (hit.transform.gameObject.layer == 17) { //print("Hitting product"); } if (hit.transform.tag == "StorageBox" && productIsBox) { Box beingMoved = currentProduct.currentPlaceholder.GetComponent <Box>(); StorageBox storageBoxBeingMoved = (StorageBox)beingMoved.product; Box hitBox = hit.transform.GetComponent <Box>(); if (hitBox != null) { BoxStack stack = hitBox.parentBoxStack; if (stack != null) { if (stack.boxList.Count <= 2) { currentProduct.newStack = stack; Box toSend = Instantiate(beingMoved); StorageBox newStorageBox = new StorageBox(storageBoxBeingMoved.productReference, toSend.gameObject); newStorageBox.uniqueID = storageBoxBeingMoved.uniqueID; toSend.product = newStorageBox; currentProduct.newStack.StartAddingBox(toSend); beingMoved.gameObject.SetActive(false); // need to handle carrying across contents as well } else { // Stack is full stack.gameObject.GetComponent <Highlighter>().ConstantOnImmediate(Color.red); } } else { print("Need to create a new stack"); } } } } if (!hitShelf && hits.Length > 0 && !productIsBox) { // Find the nearest display shelf try { currentDisplayShelf = GetClosestDisplayShelf(hits[0].point); currentShelf = currentDisplayShelf.GetShelf(GetCurrentShelfLayer(), hits[0].point); Vector3 closestPoint = currentShelf.GetCollider().ClosestPoint(hits[0].point); currentProduct.currentPlaceholder.transform.position = closestPoint; } catch (System.NullReferenceException) { // Failed to find a nearby shelf } } else if (!hitShelf && hits.Length > 0 && productIsBox) { try { RaycastHit toUse = hits[0]; bool outdoorHit = false; ComponentSubGrid closestSubGrid = dm.dispensary.GetClosestSubGrid(toUse.point); ComponentGrid grid = closestSubGrid.parentGrid; FloorTile hitTile = null; foreach (RaycastHit hit in hits) { if (hit.transform.tag == "Floor") { hitTile = hit.transform.GetComponent <FloorTile>(); } } if (hitTile != null) { outdoorHit = false; } else { outdoorHit = true; } ComponentNode nodeToSnapTo = null; GameObject tempTileGO = Instantiate(dm.database.GetFloorTile(10000).gameObject_); BoxCollider placeholderTile = tempTileGO.GetComponent <BoxCollider>(); if (hitTile != null) { outdoorHit = false; nodeToSnapTo = hitTile.node; } if (nodeToSnapTo == null) { nodeToSnapTo = GetClosestEdgeNode(grid, toUse, outdoorHit); } Vector3 snapPos = nodeToSnapTo.worldPosition; Vector3 newPos = new Vector3(snapPos.x, snapPos.y + placeholderTile.bounds.extents.y, snapPos.z); if (!dm.actionManager.snapToGrid) { // If not snapping to grid and didnt hit something outside if (!outdoorHit) { foreach (RaycastHit hit in hits) { if (hit.transform.tag == "Floor") { newPos = hit.point; } } } } currentProduct.currentPlaceholder.transform.position = newPos; Destroy(tempTileGO.gameObject); } catch (System.NullReferenceException) { } } if (currentDisplayShelf != null && currentShelf != null) { // Disable Colliders currentDisplayShelf.GetComponent <BoxCollider>().enabled = false; currentShelf.GetCollider().enabled = false; // Perform collision check BoxCollider productCollider = currentProduct.currentPlaceholder.GetComponent <BoxCollider>(); productCollider.enabled = true; Vector3 size = productCollider.bounds.size; // /14 //Vector3 center = productCollider.bounds.center; productCollider.enabled = false; //size.x = Mathf.Abs(size.x); //size.y = Mathf.Abs(size.y); //size.z = Mathf.Abs(size.z); //float oldY = size.y; //size.y = size.z; //size.z = oldY; Vector3 center = new Vector3(productCollider.transform.position.x, productCollider.transform.position.y + size.y / 2, productCollider.transform.position.z); Collider[] results = Physics.OverlapBox(center, size / 2); if (results.Length > 0) { //print(results.Length); bool conflict = true; /*foreach (Collider col in results) * { * if (col.tag == "Shelf") * { * print("COnflict with shelf 1"); * conflict = true; * break; * } * else if (col.gameObject.layer == 21) * { // Shelf layer * print("COnflict with shelf 2"); * conflict = true; * break; * } * else if (col.gameObject.layer == 17) * { // Product layer * print("COnflict with product"); * conflict = true; * break; * } * }*/ if (conflict) { movementConflict = true; currentProduct.currentPlaceholder.HighlightOn(Color.red); } results = null; } // Re-enable colliders currentDisplayShelf.GetComponent <BoxCollider>().enabled = true; currentShelf.GetCollider().enabled = true; } else { //print("Didnt check"); } currentProduct.currentPlaceholder.GetComponent <BoxCollider>().enabled = true; } else { print("Placeholder doesnt exist"); } if (Input.GetMouseButtonUp(0) && !dm.PointerOverUI) { // Left Click if (!movementConflict) { if (currentProduct.currentProduct.NeedsContainer()) { if (currentProduct.currentContainer != null) { Box.PackagedBud packagedBud = currentProduct.GetPackagedBud(); if (packagedBud != null) { currentProduct.currentPlaceholder.indicator.OpenPackagedBudPlacementPanel(currentProduct.currentContainer, packagedBud); } } else { currentProduct.currentPlaceholder.indicator.OpenChooseContainerPanel(currentProduct); } } else if (!productIsBox) { Box.PackagedProduct packagedProduct = null; bool isPackagedProduct = false; try { packagedProduct = (Box.PackagedProduct)currentProduct.currentProduct; isPackagedProduct = true; } catch (System.InvalidCastException) { // Wasnt a packaged product // Do nothing, allow to carry on isPackagedProduct = false; } if (isPackagedProduct) { if (packagedProduct != null) { packagedProduct.MoveProduct(Dispensary.JobType.StoreBudtender, currentProduct.currentPlaceholder.transform.position, currentDisplayShelf); currentProduct.FinishMovement(); // Try moving next packaged product StartCoroutine(StartMovingNextPackagedProduct()); yield return(new WaitForSeconds(.0125f)); // Needs to last longer than the waitforseconds(.01f) packagedProduct.parentBox.RemoveProduct(packagedProduct); uiM_v5.leftBarMainSelectionsPanel.UpdateBoxScrollable(); yield break; /*Product newProduct = null; * CurrentProduct oldProduct = currentProduct; * try * { // Use packaged product reference * newProduct = CreateProduct(packagedProduct.packagedProductReference, packagedProduct.parentBox.transform.position); * } * catch (System.NullReferenceException) * { // Use product reference * newProduct = CreateProduct(packagedProduct.productReference, packagedProduct.parentBox.transform.position); * } * if (newProduct != null) * { * newProduct.productGO.gameObject.SetActive(false); * newProduct.MoveProduct(Dispensary.JobType.StoreBudtender, currentProduct.currentPlaceholder.transform.position, currentDisplayShelf); * currentProduct.currentProduct.uniqueID = newProduct.uniqueID; * currentProduct.FinishMovement(); * * // Try moving next packaged product * StartCoroutine(StartMovingNextPackagedProduct()); * yield return new WaitForSeconds(.0125f); // Needs to last longer than the waitforseconds(.01f) * packagedProduct.parentBox.RemoveProduct(packagedProduct); * uiM_v5.leftBarMainSelectionsPanel.UpdateBoxScrollable(); * yield break; * }*/ } } else { // Is not a packaged product0 currentProduct.currentProduct.MoveProduct(Dispensary.JobType.StoreBudtender, currentProduct.currentPlaceholder.transform.position, currentDisplayShelf); uiM_v5.leftBarMainSelectionsPanel.RemoveProduct(currentProduct.currentProduct, false); if (moveMode == MoveMode.single) { FinishedMovingSingleProduct(); } else if (moveMode == MoveMode.multiple) { FinishedMovingMultipleProducts(); } yield break; } } else if (productIsBox) { bool newStackExists = false; StorageBox beingMoved = (StorageBox)currentProduct.currentProduct; if (currentProduct.newStack != null) { newStackExists = true; } if (newStackExists) { beingMoved.MoveProduct(Dispensary.JobType.StoreBudtender, currentProduct.currentPlaceholder.transform.position, currentProduct.newStack); } else if (currentDisplayShelf != null) { beingMoved.MoveProduct(Dispensary.JobType.StoreBudtender, currentProduct.currentPlaceholder.transform.position, currentDisplayShelf); } } } else { print("Cant move here: wont fit"); } } } yield return(null); } }
public void CreateRoof(ComponentSubGrid grid, int[,] roofIDs) { subGridIndex = grid.subGridIndex; if (db == null || dm == null) { dm = GameObject.Find("DispensaryManager").GetComponent <DispensaryManager>(); db = GameObject.Find("Database").GetComponent <Database>(); } if (roofTilesParent != null) { Destroy(roofTilesParent.gameObject); } roofTiles = new GameObject[grid.gridSizeX, grid.gridSizeY]; GameObject tileParent = new GameObject("RoofTiles"); tileParent.transform.parent = transform; roofTilesParent = tileParent; Vector3 worldBottomLeft = transform.position - Vector3.right * grid.gridWorldSize.x / 2 - Vector3.forward * grid.gridWorldSize.y / 2; for (int x = 0; x < grid.gridSizeX; x++) { for (int y = 0; y < grid.gridSizeY; y++) { Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * grid.nodeRadius * 2 + grid.nodeRadius) + Vector3.forward * (y * (grid.nodeRadius * 2) + grid.nodeRadius); int ID = -1; try { ID = roofIDs[x, y]; if (ID == 0) { ID = 11000; // Default roof tile } } catch (Exception ex) { ID = -1; } if (ID == -1) { ID = 11000; // Default roof tile } GameObject newPlane = Instantiate(db.GetStoreObject(ID, 0).gameObject_); switch (gameObject.name) { case "MainStoreComponent": case "MainStoreComponent_Copy": newPlane.name = "MainStoreComponent"; dm.dispensary.Main_c.SetRoofTileID(subGridIndex, x, y, ID); break; case "StorageComponent0": case "StorageComponent0_Copy": newPlane.name = "StorageComponent0"; dm.dispensary.Storage_cs[0].SetRoofTileID(subGridIndex, x, y, ID); break; case "StorageComponent1": case "StorageComponent1_Copy": newPlane.name = "StorageComponent1"; dm.dispensary.Storage_cs[1].SetRoofTileID(subGridIndex, x, y, ID); break; case "StorageComponent2": case "StorageComponent2_Copy": newPlane.name = "StorageComponent2"; dm.dispensary.Storage_cs[2].SetRoofTileID(subGridIndex, x, y, ID); break; case "GlassShopComponent": case "GlassShopComponent_Copy": newPlane.name = "GlassShopComponent"; dm.dispensary.Glass_c.SetRoofTileID(subGridIndex, x, y, ID); break; case "SmokeLoungeComponent": case "SmokeLoungeComponent_Copy": newPlane.name = "SmokeLoungeComponent"; dm.dispensary.Lounge_c.SetRoofTileID(subGridIndex, x, y, ID); break; case "WorkshopComponent": case "WorkshopComponent_Copy": newPlane.name = "WorkshopComponent"; dm.dispensary.Workshop_c.SetRoofTileID(subGridIndex, x, y, ID); break; case "GrowroomComponent0": case "GrowroomComponent0_Copy": newPlane.name = "GrowroomComponent0"; dm.dispensary.Growroom_cs[0].SetRoofTileID(subGridIndex, x, y, ID); break; case "GrowroomComponent1": case "GrowroomComponent1_Copy": newPlane.name = "GrowroomComponent1"; dm.dispensary.Growroom_cs[1].SetRoofTileID(subGridIndex, x, y, ID); break; case "ProcessingComponent0": case "ProcessingComponent0_Copy": newPlane.name = "ProcessingComponent0"; dm.dispensary.Processing_cs[0].SetRoofTileID(subGridIndex, x, y, ID); break; case "ProcessingComponent1": case "ProcessingComponent1_Copy": newPlane.name = "ProcessingComponent1"; dm.dispensary.Processing_cs[1].SetRoofTileID(subGridIndex, x, y, ID); break; case "HallwayComponent0": case "HallwayComponent0_Copy": newPlane.name = "HallwayComponent0"; dm.dispensary.Hallway_cs[0].SetRoofTileID(subGridIndex, x, y, ID); break; case "HallwayComponent1": case "HallwayComponent1_Copy": newPlane.name = "HallwayComponent1"; dm.dispensary.Hallway_cs[1].SetRoofTileID(subGridIndex, x, y, ID); break; case "HallwayComponent2": case "HallwayComponent2_Copy": newPlane.name = "HallwayComponent2"; dm.dispensary.Hallway_cs[2].SetRoofTileID(subGridIndex, x, y, ID); break; case "HallwayComponent3": case "HallwayComponent3_Copy": newPlane.name = "HallwayComponent3"; dm.dispensary.Hallway_cs[3].SetRoofTileID(subGridIndex, x, y, ID); break; case "HallwayComponent4": case "HallwayComponent4_Copy": newPlane.name = "HallwayComponent4"; dm.dispensary.Hallway_cs[4].SetRoofTileID(subGridIndex, x, y, ID); break; case "HallwayComponent5": case "HallwayComponent5_Copy": newPlane.name = "HallwayComponent5"; dm.dispensary.Hallway_cs[5].SetRoofTileID(subGridIndex, x, y, ID); break; } //newPlane.transform.localScale = new Vector3(grid.nodeRadius * 2, newPlane.transform.localScale.y, grid.nodeRadius * 2); Vector3 roofPos = new Vector3(worldPoint.x, 0, worldPoint.z); newPlane.transform.position = roofPos; RoofTile newRoofTile = newPlane.AddComponent <RoofTile>(); newRoofTile.gridX = x; newRoofTile.gridY = y; newRoofTile.tileID = ID; newRoofTile.component = newPlane.name; newPlane.tag = "Roof"; newPlane.layer = 16; newPlane.gameObject.transform.SetParent(roofTilesParent.transform, false); roofTiles[x, y] = newPlane; } } }