private static void panelCreator(NetworkMessage message) { CreatePanelMessage panelMessage = message.ReadMessage <CreatePanelMessage>(); InteractionPanel newPanel = Instantiate <InteractionPanel>(_instance.panelPrefabs[panelMessage.prefabIndex]); Vector3 pos = Vector3.zero; Quaternion rot = Quaternion.identity; getCellCenter(panelMessage.x, panelMessage.y, newPanel.dimensionX, newPanel.dimensionY, out pos, out rot); newPanel.transform.position = pos + Vector3.up * MoveDownUp.HEIGHT; newPanel.transform.rotation = rot; newPanel.setActionSet(panelMessage.actionSet); }
private static CreatePanelMessage createRandomPanel() { InteractionPanel chosenPanel = null; int chosenX, chosenY; PanelActionSetBase chosenActionSet; while (true) { float totalWeight = 0.0f; foreach (InteractionPanel panel in _panelsToChooseFrom) { totalWeight += panel.spawnWeight; } float r = Random.value * totalWeight; foreach (InteractionPanel panel in _panelsToChooseFrom) { if (panel.spawnWeight > r) { chosenPanel = panel; break; } else { r -= panel.spawnWeight; } } List <int> availableCellLocationsX = new List <int>(); List <int> availableCellLocationsY = new List <int>(); for (int x = 0; x < CELLS_X - chosenPanel.dimensionX + 1; x++) { for (int y = 0; y < CELLS_Y - chosenPanel.dimensionY + 1; y++) { bool isAnyCellFilled = false; for (int dx = 0; dx < chosenPanel.dimensionX; dx++) { for (int dy = 0; dy < chosenPanel.dimensionY; dy++) { if (_isCellFilled[x + dx, y + dy]) { isAnyCellFilled = true; break; } } if (isAnyCellFilled) { break; } } if (!isAnyCellFilled) { availableCellLocationsX.Add(x); availableCellLocationsY.Add(y); } } } if (availableCellLocationsX.Count == 0) { _panelsToChooseFrom.Remove(chosenPanel); if (_panelsToChooseFrom.Count == 0) { throw new System.Exception("Cannot fit any more panels!"); } continue; } int chosenCellIndex = Random.Range(0, availableCellLocationsX.Count); chosenX = availableCellLocationsX[chosenCellIndex]; chosenY = availableCellLocationsY[chosenCellIndex]; chosenActionSet = chosenPanel.createViableActionSet(_createdPanels); break; } ; emptyCells -= chosenPanel.dimensionY * chosenPanel.dimensionX; _createdPanels.Add(chosenActionSet.panelLabel); for (int dx = 0; dx < chosenPanel.dimensionX; dx++) { for (int dy = 0; dy < chosenPanel.dimensionY; dy++) { _isCellFilled[chosenX + dx, chosenY + dy] = true; } } chosenActionSet.currentVariantIndex = Random.Range(0, chosenActionSet.getVariantCount()); CreatePanelMessage creationMessage = new CreatePanelMessage(chosenActionSet, chosenX, chosenY, _instance.panelPrefabs.IndexOf(chosenPanel)); return(creationMessage); }
private static CreatePanelMessage createRandomPanel() { InteractionPanel chosenPanel = null; int chosenX, chosenY; PanelActionSetBase chosenActionSet; while(true) { float totalWeight = 0.0f; foreach (InteractionPanel panel in _panelsToChooseFrom) { totalWeight += panel.spawnWeight; } float r = Random.value * totalWeight; foreach (InteractionPanel panel in _panelsToChooseFrom) { if (panel.spawnWeight > r) { chosenPanel = panel; break; } else { r -= panel.spawnWeight; } } List<int> availableCellLocationsX = new List<int>(); List<int> availableCellLocationsY = new List<int>(); for (int x = 0; x < CELLS_X - chosenPanel.dimensionX + 1; x++) { for (int y = 0; y < CELLS_Y - chosenPanel.dimensionY + 1; y++) { bool isAnyCellFilled = false; for (int dx = 0; dx < chosenPanel.dimensionX; dx++) { for (int dy = 0; dy < chosenPanel.dimensionY; dy++) { if (_isCellFilled[x + dx, y + dy]) { isAnyCellFilled = true; break; } } if (isAnyCellFilled) { break; } } if (!isAnyCellFilled) { availableCellLocationsX.Add(x); availableCellLocationsY.Add(y); } } } if (availableCellLocationsX.Count == 0) { _panelsToChooseFrom.Remove(chosenPanel); if (_panelsToChooseFrom.Count == 0) { throw new System.Exception("Cannot fit any more panels!"); } continue; } int chosenCellIndex = Random.Range(0, availableCellLocationsX.Count); chosenX = availableCellLocationsX[chosenCellIndex]; chosenY = availableCellLocationsY[chosenCellIndex]; chosenActionSet = chosenPanel.createViableActionSet(_createdPanels); break; }; emptyCells -= chosenPanel.dimensionY * chosenPanel.dimensionX; _createdPanels.Add(chosenActionSet.panelLabel); for (int dx = 0; dx < chosenPanel.dimensionX; dx++) { for (int dy = 0; dy < chosenPanel.dimensionY; dy++) { _isCellFilled[chosenX + dx, chosenY + dy] = true; } } chosenActionSet.currentVariantIndex = Random.Range(0, chosenActionSet.getVariantCount()); CreatePanelMessage creationMessage = new CreatePanelMessage(chosenActionSet, chosenX, chosenY, _instance.panelPrefabs.IndexOf(chosenPanel)); return creationMessage; }