public void SpawnPreview(GameObject prefab) { m_CurrentPreview = GameObject.Instantiate(prefab); m_CurrentPreview.transform.position = Vector3.one * 10000f; //m_CurrentPreview.transform.SetParent(m_Transform); m_CurrentPreviewPiece = m_CurrentPreview.GetComponent <BuildingPiece>(); m_CurrentPreviewPiece.SetState(PieceState.Preview); m_CurrentPrefab = prefab.GetComponent <BuildingPiece>(); }
public void PlacePiece() { if (m_CurrentPreview == null) { return; } GameObject placedPiece = GameObject.Instantiate(m_CurrentPrefab.gameObject, m_CurrentPreview.transform.position, m_CurrentPreview.transform.rotation); placedPiece.transform.SetParent(null); BuildingPiece piece = placedPiece.GetComponent <BuildingPiece>(); piece.SetState(PieceState.Preview); bool isFreeObject = !piece.RequiresSockets && !piece.TerrainProtection; if (isFreeObject) { //Debug.Log("Is free object!"); bool placed = false; Collider[] overlappingStuff = Physics.OverlapBox(m_CurrentPreviewPiece.Bounds.center, m_CurrentPreviewPiece.Bounds.size / 2f, m_CurrentPreviewPiece.transform.rotation, m_FreePlacementMask, QueryTriggerInteraction.Ignore); /*Debug.Log(m_CurrentPreviewPiece.Bounds.size); * var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); * cube.transform.position = CurrentPreviewPiece.Bounds.center; * cube.transform.rotation = CurrentPreviewPiece.transform.rotation; * cube.transform.localScale = CurrentPreviewPiece.Bounds.size;*/ foreach (var col in overlappingStuff) { var bp = col.GetComponent <BuildingPiece>(); if (bp != null && bp != m_CurrentPreviewPiece) { piece.transform.SetParent(bp.Building.transform, true); piece.AttachedOn = bp; piece.SetState(PieceState.Placed); piece.Building = bp.Building; piece.Building.AddPiece(piece); //Debug.Log("attached on something"); placed = true; break; } } if (!placed) { var newBuilding = new GameObject("Building", typeof(BuildingHolder)); piece.transform.SetParent(newBuilding.transform, true); piece.Building = newBuilding.GetComponent <BuildingHolder>(); piece.Building.AddPiece(piece); Collider[] colliders; if (piece.HasSupport(out colliders)) { if (colliders.Length > 0) { var colPiece = colliders [0].GetComponent <BuildingPiece> (); if (colPiece != null) { piece.AttachedOn = colPiece; } } } piece.SetState(PieceState.Placed); } } else { if (m_LastValidSocket && m_LastValidSocket.Piece.Building != null) { //m_LastPlacedPiece = piece; piece.transform.SetParent(m_LastValidSocket.Piece.Building.transform, true); piece.AttachedOn = m_LastValidSocket.Piece; piece.SetState(PieceState.Placed); m_LastValidSocket.OccupyNeighbours(m_FreePlacementMask, m_BuildingPieceMask, piece); piece.Building = m_LastValidSocket.Piece.Building; piece.Building.AddPiece(piece); } else { var newBuilding = new GameObject("Building", typeof(BuildingHolder)); piece.transform.SetParent(newBuilding.transform, true); piece.Building = newBuilding.GetComponent <BuildingHolder>(); piece.Building.AddPiece(piece); Collider[] colliders; if (piece.HasSupport(out colliders)) { if (colliders.Length > 0) { var colPiece = colliders [0].GetComponent <BuildingPiece> (); if (colPiece != null) { piece.AttachedOn = colPiece; } } } piece.SetState(PieceState.Placed); } } m_RotationOffset = 0f; if (piece.PlacementFX) { GameObject.Instantiate(piece.PlacementFX, piece.transform.position, piece.transform.rotation); } m_LastValidSocket = null; m_HasSocket = false; }