示例#1
0
        public bool HasSpace(LayerMask mask, BuildingPiece placedPiece)
        {
            // Get the objects that overlap this socket.
            var overlappingStuff = Physics.OverlapSphere(transform.position, Radius, mask, QueryTriggerInteraction.Ignore);

            foreach (var col in overlappingStuff)
            {
                if (m_Piece != placedPiece)
                {
                    if (!m_Piece.Building.HasCollider(col) && col as TerrainCollider == null)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!m_Piece.HasCollider(col) && col as TerrainCollider == null)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
示例#2
0
        private void Awake()
        {
            var sphere = gameObject.AddComponent <SphereCollider>();

            sphere.isTrigger = true;
            sphere.radius    = Radius;
            m_Piece          = GetComponentInParent <BuildingPiece>();
        }
示例#3
0
 public void AddPiece(BuildingPiece piece)
 {
     if (!m_Pieces.Contains(piece))
     {
         m_Pieces.Add(piece);
         piece.GetComponent <EntityEventHandler>().Death.AddListener(() => OnPieceDeath(piece));
     }
 }
示例#4
0
        private void HandleSnapPreview(Collider[] buildingPieces)
        {
            // m_LastOverlappingPieces = buildingPieces;

            Camera cam = Camera.main;
            Ray    ray = cam.ViewportPointToRay(Vector3.one * 0.5f);

            float  smallestAngleToSocket = Mathf.Infinity;
            Socket targetSocket          = null;

            for (int bp = 0; bp < buildingPieces.Length; bp++)
            {
                BuildingPiece buildingPiece = buildingPieces[bp].GetComponent <BuildingPiece>();
                if (buildingPiece == null || buildingPiece.Sockets.Length == 0)
                {
                    continue;
                }

                for (int s = 0; s < buildingPiece.Sockets.Length; s++)
                {
                    Socket socket = buildingPiece.Sockets[s];

                    if (socket.SupportsPiece(m_CurrentPreviewPiece))
                    {
                        if ((socket.transform.position - m_Transform.position).sqrMagnitude < Mathf.Pow(m_BuildRange, 2))
                        {
                            float angleToSocket = Vector3.Angle(ray.direction, socket.transform.position - ray.origin);

                            if (angleToSocket < smallestAngleToSocket && angleToSocket < 35f)
                            {
                                smallestAngleToSocket = angleToSocket;
                                targetSocket          = socket;
                            }
                        }
                    }
                }
            }

            if (targetSocket != null)
            {
                Socket.PieceOffset pieceOffset;
                if (targetSocket.GetPieceOffsetByName(m_CurrentPrefab.Name, out pieceOffset))
                {
                    m_CurrentPreview.transform.position = targetSocket.transform.position + targetSocket.transform.TransformVector(pieceOffset.PositionOffset);
                    m_CurrentPreview.transform.rotation = targetSocket.transform.rotation * pieceOffset.RotationOffset;
                    m_LastValidSocket = targetSocket;
                    m_HasSocket       = true;

                    return;
                }
            }

            if (!RaycastAndPlace())
            {
                HandleFreePreview();
            }
        }
示例#5
0
        public void ClearPreview()
        {
            if (m_CurrentPreview != null)
            {
                GameObject.Destroy(m_CurrentPreview.gameObject);

                m_CurrentPreview      = null;
                m_CurrentPreviewPiece = null;
            }
        }
示例#6
0
        private void OnChanged_SelectedBuildable()
        {
            // If we have the building plan in our hands..
            if (m_Player.EquippedItem.Get() != null && m_Player.EquippedItem.Get().HasProperty("Allows Building"))
            {
                m_SelectedPiece = m_Player.SelectedBuildable.Get();
            }

            UpdatePreview();
        }
示例#7
0
        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>();
        }
示例#8
0
 public void OnPieceDeath(BuildingPiece piece)
 {
     for (int i = 0; i < m_Occupiers.Count; i++)
     {
         if (piece == m_Occupiers[i].Occupier)
         {
             m_OccupiedSpaces.RemoveAt(i);
             m_Occupiers.RemoveAt(i);
         }
     }
 }
示例#9
0
 public void OccupySpaces(BuildingSpace[] spacesToOccupy, BuildingPiece piece)
 {
     for (int i = 0; i < spacesToOccupy.Length; i++)
     {
         if (!m_OccupiedSpaces.Contains(spacesToOccupy[i]))
         {
             m_OccupiedSpaces.Add(spacesToOccupy[i]);
             m_Occupiers.Add(new SpaceOccupier(spacesToOccupy[i], piece));
         }
     }
 }
示例#10
0
        public bool SupportsPiece(BuildingPiece piece)
        {
            for (int i = 0; i < m_PieceOffsets.Count; i++)
            {
                if (m_PieceOffsets[i] != null && m_PieceOffsets[i].Piece != null && m_PieceOffsets[i].Piece.Name == piece.Name && !m_OccupiedSpaces.Contains(piece.NeededSpace))
                {
                    return(true);
                }
            }

            return(false);
        }
示例#11
0
        private void OnPieceDeath(BuildingPiece piece)
        {
            m_Pieces.Remove(piece);

            for (int p = 0; p < m_Pieces.Count; p++)
            {
                for (int s = 0; s < m_Pieces[p].Sockets.Length; s++)
                {
                    m_Pieces[p].Sockets[s].OnPieceDeath(piece);
                }
            }
        }
示例#12
0
        /// <summary>
        /// Is this point stable?
        /// </summary>
        public bool IsStable(BuildingPiece piece, LayerMask mask)
        {
            var hits = Physics.RaycastAll(piece.transform.position + piece.transform.TransformVector(m_Position), piece.transform.TransformDirection(m_Direction), m_Distance, mask, QueryTriggerInteraction.Ignore);

            for (int i = 0; i < hits.Length; i++)
            {
                if (!piece.HasCollider(hits[i].collider))
                {
                    return(true);
                }
            }

            return(false);
        }
示例#13
0
        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;
        }
示例#14
0
 public SpaceOccupier(BuildingSpace occupiedSpace, BuildingPiece occupier)
 {
     OccupiedSpace = occupiedSpace;
     Occupier      = occupier;
 }
示例#15
0
        public void OccupyNeighbours(LayerMask freePlacementMask, LayerMask buildingMask, BuildingPiece placedPiece)
        {
            Collider[] overlappingStuff = Physics.OverlapBox(placedPiece.Bounds.center, placedPiece.Bounds.extents, placedPiece.transform.rotation, freePlacementMask, QueryTriggerInteraction.Collide);

            //Debug.DrawRay(placedPiece.Bounds.center, Vector3.up * 5f, Color.yellow, 5f);

            for (int i = 0; i < overlappingStuff.Length; i++)
            {
                Socket s = overlappingStuff[i].GetComponent <Socket>();

                if (s && s.SupportsPiece(placedPiece) && !s.HasSpace(freePlacementMask, placedPiece))
                {
                    s.OccupySpaces(placedPiece.SpacesToOccupy, placedPiece);
                }
            }
        }
示例#16
0
 public void OnDrawGizmosSelected(BuildingPiece piece)
 {
     Gizmos.color = Color.red;
     Gizmos.DrawRay(piece.transform.position + piece.transform.TransformVector(m_Position), piece.transform.TransformDirection(m_Direction).normalized *m_Distance);
 }