Exemple #1
0
 /*
  * Show a preview of the currently chosen element on the grid, to indicate where the selected element would be placed on the grid.
  * This is done by creating an object of the same size as the selected element and placing it right above the grid.
  * @param   context Information provided from the triggering event. In this case the movement of the mouse.
  */
 private void PreviewOnLocation(InputAction.CallbackContext context)
 {
     // The first case is used, if the player has an element in the inventory, but a preview-object hasn't been build yet.
     if (!_previewObject.go && selected.go)
     {
         // Create a preview-object, by copying the element in the inventory and scaling it to accomidate the right cellsize.
         _previewObject.go = Instantiate(selected.go);
         _previewObject.go.GetComponent <Renderer>().material = _previewMat;
         int   x           = _previewObject.go.GetComponent <GridElement>().size.x;
         int   y           = _previewObject.go.GetComponent <GridElement>().size.y;
         float sizeOffsetX = 1 * x / _previewObject.go.GetComponent <Renderer>().bounds.size.x;
         float sizeOffsetY = 1 * y / _previewObject.go.GetComponent <Renderer>().bounds.size.y;
         _previewObject.go.transform.localScale = new Vector3(_mainGrid.grid.FieldSize * sizeOffsetX, _mainGrid.grid.FieldSize * sizeOffsetY, 1);
     }
     // If there is an preview-object build already, place it right over the calculated coordinates.
     if (_previewObject.go)
     {
         Ray        ray = _camera.go.GetComponent <Camera>().ScreenPointToRay(Mouse.current.position.ReadValue());
         RaycastHit hit;
         Physics.Raycast(ray, out hit);
         Vector2Int temp = _mainGrid.grid.GetCoordinate(hit.point);
         Placer.PlacePreview(_mainGrid.grid, _previewObject.go, temp.x, temp.y);
     }
 }