Ejemplo n.º 1
0
        /// <summary>
        /// Move ghost with successful raycastHit onto m_PlacementAreaMask
        /// </summary>
        protected virtual void MoveGhostWithRaycastHit(RaycastHit raycast)
        {
            // We successfully hit one of our placement areas
            // Try and get a placement area on the object we hit
            m_CurrentArea = raycast.collider.GetComponent <IPlacementArea>();

            if (m_CurrentArea == null)
            {
                Debug.LogError("There is not an IPlacementArea attached to " +
                               "the collider found on the m_PlacementAreaMask");
                return;
            }

            m_GridPosition = m_CurrentArea.WorldToGrid(
                raycast.point, m_CurrentTower.controller.dimensions);

            TowerFitStatus fits = m_CurrentArea.Fits(
                m_GridPosition, m_CurrentTower.controller.dimensions);

            m_CurrentTower.Show();
            m_GhostPlacementPossible = fits == TowerFitStatus.Fits && IsValidPurchase();

            m_CurrentTower.Move(
                m_CurrentArea.GridToWorld(m_GridPosition, m_CurrentTower.controller.dimensions),
                m_CurrentArea.transform.rotation,
                m_GhostPlacementPossible);
        }
        private void MoveGhostWithRaycastHit(RaycastHit raycast)
        {
            currentArea = raycast.collider.GetComponent <IPlacementArea>();

            if (currentArea == null)
            {
                Log.Error("There is not an IPlacementArea attached to the collider found on the m_PlacementAreaMask");
                return;
            }
            m_GridPosition = currentArea.WorldToGrid(raycast.point, entityDataTowerPreview.TowerData.Dimensions);
            TowerFitStatus fits = currentArea.Fits(m_GridPosition, entityDataTowerPreview.TowerData.Dimensions);

            SetVisiable(true);
            canPlace = fits == TowerFitStatus.Fits;
            Move(currentArea.GridToWorld(m_GridPosition, entityDataTowerPreview.TowerData.Dimensions),
                 currentArea.transform.rotation,
                 canPlace);
        }
Ejemplo n.º 3
0
        protected virtual void MoveGhostWithRaycastHit(RaycastHit raycast)
        {
            //m_CurrentArea = raycast.collider.GetComponent<BuildingPlaceDisplayMappingBind>().GetBindGameObject().GetComponent<IPlacementArea>();
            m_CurrentArea = raycast.collider.GetComponent <IPlacementArea>();
            if (m_CurrentArea == null)
            {
                Debug.LogError("There is not an IPlacementArea attached to the collider found on the m_PlacementAreaMask");
                return;
            }

            m_GridPosition = m_CurrentArea.WorldToGrid(raycast.point);
            TowerFitStatus fits = m_CurrentArea.Fits(m_GridPosition);

            m_CurrentGhost.Show();
            m_GhostPlacementPossible = fits == TowerFitStatus.Fits && IsValidPurchase();
            if (m_GhostPlacementPossible)
            {
                //print("可以放置");
            }
            m_CurrentGhost.transform.position = raycast.point;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Snaps a given world positionn to this grid
 /// </summary>
 public static Vector3 Snap(this IPlacementArea placementArea, Vector3 worldPosition, IntVector2 sizeOffset)
 {
     // Calculate the nearest grid location and then change that back to world space
     return(placementArea.GridToWorld(placementArea.WorldToGrid(worldPosition, sizeOffset), sizeOffset));
 }
Ejemplo n.º 5
0
        private void Update()
        {
            // Check collisions with placement areas
            var        mouseRay = HelperObjects.CameraComponent.ScreenPointToRay(Input.mousePosition);
            RaycastHit areaHit;

            Physics.Raycast(mouseRay, out areaHit);

            // Clear all potential tiles when leaving area
            if (targetArea == null && !firstPlacement)
            {
                clearAllOldTiles();
            }

            if (areaHit.collider != null)
            {
                targetArea = areaHit.collider.GetComponent <IPlacementArea>();

                if (targetArea != null)
                {
                    // If first placement, instantly move and enable
                    if (firstPlacement)
                    {
                        firstPlacement       = false;
                        transform.position   = areaHit.point;
                        transform.localScale = Vector3.one;
                        return;
                    }

                    var snappedPosition     = targetArea.Snap(areaHit.point, SizeOffset);
                    var snappedGridPosition = targetArea.WorldToGrid(areaHit.point, SizeOffset);

                    // Check if the ghost fits and isn't too close to the path
                    var        fits             = targetArea.Fits(snappedGridPosition, SizeOffset) == TowerFitStatus.Fits;
                    Collider[] collisions       = Physics.OverlapSphere(areaHit.point, PathCollisionRadius);
                    var        collidesWithPath = collisions.Any((c) => c.gameObject.layer == Constants.PATH_LAYER);
                    if (fits && !collidesWithPath)
                    {
                        transform.localScale = Vector3.one;

                        // Set new position and update placement tile state
                        if (snappedPosition != targetPosition)
                        {
                            clearOldTiles();
                            targetArea.Occupy(snappedGridPosition, SizeOffset, PlacementTileState.Potential);

                            targetPosition = snappedPosition;
                        }
                    }
                }
            }

            // Move ghost
            if (Vector3.SqrMagnitude(transform.position - targetPosition) > 0.01f)
            {
                transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, Dampening);
            }
            else
            {
                velocity = Vector3.zero;
            }

            // Build when clicked
            if (Input.GetMouseButtonDown(0))
            {
                if (targetArea != null && !GameManager.IsUiBlocking)
                {
                    BuildManager.I.ImplementMitigation(
                        targetArea,
                        targetArea.WorldToGrid(targetPosition, SizeOffset),
                        SizeOffset);

                    DestroyGhost();
                }
            }
            else if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
            {
                clearAllOldTiles();
                enabled = false;
                DestroyGhost();
            }
        }