/// <summary> /// Place the ghost at the pointer's position /// </summary> /// <param name="pointer">The pointer to place the ghost at</param> /// <exception cref="InvalidOperationException">If we're not in the correct state</exception> protected void PlaceGhost(UIPointer pointer) { if (m_CurrentTower == null || !isBuilding) { throw new InvalidOperationException( "Trying to position a tower ghost while the UI is not currently in a building state."); } MoveGhost(pointer); if (m_CurrentArea != null) { TowerFitStatus fits = m_CurrentArea.Fits(m_GridPosition, m_CurrentTower.controller.dimensions); if (fits == TowerFitStatus.Fits) { // Place the ghost Tower controller = m_CurrentTower.controller; Tower createdTower = Instantiate(controller); createdTower.Initialize(m_CurrentArea, m_GridPosition); CancelGhostPlacement(); } } }
/// <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); }
/// <summary> /// Modifies the valid rendering of the ghost tower once there is enough currency /// </summary> protected virtual void OnCurrencyChanged() { if (!isBuilding || m_CurrentTower == null || m_CurrentArea == null) { return; } TowerFitStatus fits = m_CurrentArea.Fits(m_GridPosition, m_CurrentTower.controller.dimensions); bool valid = fits == TowerFitStatus.Fits && IsValidPurchase(); m_CurrentTower.Move(m_CurrentArea.GridToWorld(m_GridPosition, m_CurrentTower.controller.dimensions), m_CurrentArea.transform.rotation, valid); if (valid && !m_GhostPlacementPossible && ghostBecameValid != null) { m_GhostPlacementPossible = true; ghostBecameValid(); } }
/// <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); }
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); }