//给塔用的 public virtual void UpdateTargetPos(IPlacementArea targetArea = null, IntVector2 destination = default(IntVector2)) { if (placementArea != null) { placementArea.Clear(gridPosition, dimensions); } placementArea = targetArea; gridPosition = destination; if (targetArea != null) { transform.position = placementArea.GridToWorld(destination, dimensions); transform.rotation = placementArea.transform.rotation; targetArea.Occupy(destination, dimensions); targetArea.SetController(transform); } }
/// <summary> /// Provide the tower with data to initialize with /// </summary> /// <param name="targetArea">The placement area configuration</param> /// <param name="destination">The destination position</param> public virtual void Initialize(IPlacementArea targetArea, IntVector2 destination) { placementArea = targetArea; gridPosition = destination; if (targetArea != null) { transform.position = placementArea.GridToWorld(destination, dimensions); transform.rotation = placementArea.transform.rotation; targetArea.Occupy(destination, dimensions); } SetLevel(0); if (LevelManager.instanceExists) { LevelManager.instance.levelStateChanged += OnLevelStateChanged; } }
public void ImplementMitigation(IPlacementArea area, IntVector2 gridPosition, IntVector2 sizeOffset) { if (currentCourseOfAction != null) { // Withdraw cost PlayerStats.I.Worth -= currentCourseOfAction.GetValue(); // Occupy area in grid area.Occupy(gridPosition, sizeOffset, PlacementTileState.Filled); // Initialize the mitigation var mitigation = UnityHelper .Instantiate(CourseOfActionPrefab, area.GridToWorld(gridPosition, sizeOffset)) .GetComponent <MitigationBehaviour>(); mitigation.Initialize(area, gridPosition, sizeOffset, currentCourseOfAction); ExitBuildMode(); } }
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(); } }