private void clearOldTiles() { var oldFits = targetArea.Fits(targetGridPosition, SizeOffset) == TowerFitStatus.Fits; if (oldFits) { targetArea.Clear(targetGridPosition, SizeOffset); } }
/// <summary> /// Checks the position of the <see cref="m_CurrentTower"/> /// on the <see cref="m_CurrentArea"/> /// </summary> /// <returns> /// True if the placement is valid /// </returns> /// <exception cref="InvalidOperationException"> /// Throws exception if the check is done in <see cref="State.Normal"/> state /// </exception> public bool IsGhostAtValidPosition() { if (!isBuilding) { throw new InvalidOperationException( "Trying to check ghost position when not in a build mode"); } if (m_CurrentTower == null) { return(false); } if (m_CurrentArea == null) { return(false); } TowerFitStatus fits = m_CurrentArea.Fits(m_GridPosition, m_CurrentTower.controller.dimensions); return(fits == TowerFitStatus.Fits); }
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); }
/// <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); }
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; }
public bool TryBuildTower() { if (currentArea == null) { Log.Error("Current area is null"); return(false); } Vector3 position = Vector3.zero; Quaternion rotation = currentArea.transform.rotation; TowerFitStatus fits = currentArea.Fits(m_GridPosition, entityDataTowerPreview.TowerData.Dimensions); if (fits == TowerFitStatus.Fits) { position = currentArea.GridToWorld(m_GridPosition, entityDataTowerPreview.TowerData.Dimensions); currentArea.Occupy(m_GridPosition, entityDataTowerPreview.TowerData.Dimensions); GameEntry.Event.Fire(this, BuildTowerEventArgs.Create(entityDataTowerPreview.TowerData, currentArea, m_GridPosition, position, rotation)); return(true); } return(false); }
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(); } }