private void Awake() { _selectionPanel.go = _selectionPanelGO; _results = new int[(int)Mathf.Pow(2, _variableCount.value)]; _outputs = new MultiCellElement[_outputCount.value]; _mainCamera.go = this.gameObject; _grid = new Grid(100, 100, 5, Vector3.zero); InputHandler.instance.ChangeGIT(GameInputType.Plotter); InputHandler.instance.floatingText = _floatingText; _mainGrid.grid = _grid; GameObject empty = Resources.Load <GameObject>("Prefabs/emptyCube"); _filler.go = empty; for (int i = 0; i < _width; i++) { for (int j = 0; j < _height; j++) { GameObject tempObj = Placer.PlaceObject(_grid, empty, i, j); } } int offSetX = _startingPointGo.GetComponent <GridElement>().size.x; int offSetY = _startingPointGo.GetComponent <GridElement>().size.y; for (int i = 0; i < _variableCount.value; i++) { GameObject tempMCE = Placer.PlaceMultiCellObject(_grid, _startingPointGo, _startingpoints[i].x, _startingpoints[i].y, _filler.go); if (tempMCE.GetComponentInChildren <TMP_Text>()) { tempMCE.GetComponentInChildren <TMP_Text>().text = ((Charge)i + 1).ToString(); } Vector2Int tempVecOutgoingSocket = new Vector2Int(_startingpoints[i].x + offSetX, _startingpoints[i].y); GameObject tempObj = Placer.PlaceObject(_grid, _outGoingSocket, _startingpoints[i].x + offSetX, _startingpoints[i].y); tempMCE.GetComponent <MultiCellElement>().AddToList(tempMCE.GetComponent <MultiCellElement>().outgoingSockets, tempVecOutgoingSocket); tempMCE.GetComponent <MultiCellElement>().outgoingSocketObjects.Add(tempObj.GetComponent <OutGoingSocket>()); } for (int j = 0; j < _outputCount.value; j++) { GameObject tempMCE = Placer.PlaceMultiCellObject(_grid, _endPointGo, _endPoints[j].x, _endPoints[j].y, _filler.go); if (tempMCE.GetComponentInChildren <TMP_Text>()) { tempMCE.GetComponentInChildren <TMP_Text>().text = j.ToString(); } Vector2Int tempVecIncomingSocket = new Vector2Int(_endPoints[j].x - 1, _endPoints[j].y); GameObject tempObj = Placer.PlaceObject(_grid, _incomingSocket, tempVecIncomingSocket.x, tempVecIncomingSocket.y); tempObj.GetComponent <SocketOfGridElement>().coordsToTarget = new Vector2Int(_endPoints[j].x, _endPoints[j].y); tempObj.GetComponent <SocketOfGridElement>().targetGo = tempMCE; tempMCE.GetComponent <MultiCellElement>().AddToList(tempMCE.GetComponent <MultiCellElement>().incomingSockets, tempVecIncomingSocket); _outputs[j] = tempMCE.GetComponent <MultiCellElement>(); } }
/* * Method to handle left-mouse clicks. These can lead to various cases, depending on the clicked Object in the scene. * @param context Context of the Actionevent. In this case the method is triggered by leftclick. The information about the event are not used further. */ private void LeftClickPC(InputAction.CallbackContext context) { // First, cast a ray from the camera through the mouse-cursor and check for hits. Ray ray = _camera.go.GetComponent <Camera>().ScreenPointToRay(Mouse.current.position.ReadValue()); RaycastHit hit; // If an object was hit, check various cases. if (Physics.Raycast(ray, out hit)) { // Get the coordinates of the hit object in the grid. Vector2Int temp = _mainGrid.grid.GetCoordinate(hit.point); // If the hit object in the grid is a multiCellElement, show it's truthtable in the selection UI-Panel and return. if (_mainGrid.grid.GetObjFromCoordinate(temp).GetComponent <MultiCellElement>()) { FillSelectionResultTable(_mainGrid.grid.GetObjFromCoordinate(temp).GetComponent <MultiCellElement>()); return; } // If anything else was hit, hide the selection UI-panel. else { selectionPanel.go.SetActive(false); } // If the player hasn't selected a specific object to place, start placing connections as long as the left-mouse button is held down. // This is done by changing input-behaviour to place a connection every frame, that the mouse has moved. // Either the player has already placed a connection-element, in that case only the input-behaviour has to be changed. // Otherwise, the connection-element has to be placed in the inventory. if (!selected.go) { _inputs.Schaltnetz.PreviewBuilding.performed += PlaceConnection; selected.go = _connection.go; UpdatePreview(); } else if (selected.go == _connection.go) { _inputs.Schaltnetz.PreviewBuilding.performed += PlaceConnection; } // If the player has something in the inventory, that is not null or a connection-element, takes this route. if (temp.x >= 0) { // Check if the player still has to place incoming or outgoingsockets. If so, try to place the correct element on the clicked spot. // If there are no left sockets left to place, skip this part // If there are sockets to place, first the incoming, then the outgoing sockets are going to be placed. if (_incomingSocketsToPlace > 0 || _outgoingSocketsToPlace > 0) { if (_incomingSocketsToPlace > 0) { PlaceIncomingSocket(temp, tempTargetCoord, _filler.go); // After placing the last incoming socket, change the element in the inventory for the next placement. if (_incomingSocketsToPlace <= 0) { selected.go = _outgoingSocketPrefab; UpdatePreview(); } } // After all incoming sockets have been placed, place all outgoing sockets. else { PlaceOutgoingSocket(temp, tempTargetCoord, _filler.go); // When all outgoingsockets are placed, reset the inventory and unlock the selection. if (_outgoingSocketsToPlace <= 0) { _selectionBlocked = false; selected.go = null; UpdatePreview(); } } } // This section handles the case, that the player wants to place an element, that is not a connection and has no sockets left to place. else { // Variable used to store a reference to the newly placed element. GameObject tempObj; // Give the player auditive feedback about the placement. AudioManager.instance.PlaySound(clips[0]); // If the element in the inventory only is 1x1 gridcells big, just place it on the grid. if (selected.go.GetComponent <GridElement>().size.x == 1 && selected.go.GetComponent <GridElement>().size.y == 1) { // Place the object and take a reference. tempObj = Placer.PlaceObject(_mainGrid.grid, selected.go, temp.x, temp.y, _filler.go); } // Bigger elements need to be placed differently, since there can happen multiple conflicts while placing them on the grid. else { // Place the object and take a reference. tempObj = Placer.PlaceMultiCellObject(_mainGrid.grid, selected.go, temp.x, temp.y, _filler.go); // Bigger elements get a text to identify them on the board. They get a Tag with the given name that is placed right above them. if (tempObj.GetComponentInChildren <TMP_Text>()) { tempObj.GetComponentInChildren <TMP_Text>().text = tempObj.GetComponent <MultiCellElement>().name; } } // After placing an object, check if the object needs to have incoming or outgoing sockets. If so, set the counters and lock the selection. // This way, the player will have to place all remaining sockets before being able to place more elements. if (tempObj && (tempObj.GetComponent <GridElement>().incomingSocketCount > 0 || tempObj.GetComponent <GridElement>().outComingSocketCount > 0)) { // Block the player from placing other elements, while having to place sockets. _selectionBlocked = true; // Save the coordinates of the newly placed object for when the incoming and outgoing sockets are being planced. tempTargetCoord = temp; // Increase the counter to show how many sockets need to be placed. _incomingSocketsToPlace = tempObj.GetComponent <GridElement>().incomingSocketCount; _outgoingSocketsToPlace = tempObj.GetComponent <GridElement>().outComingSocketCount; // Start by placing incoming sockets by filling the inventory with an incoming socket. selected.go = _incomingSocketPrefab; // Update the preview, so the player can see where the element would be placed. UpdatePreview(); } } } } }