public bool RelocateInGrid(Rect rect, out int row, out int column) { //Debug.DrawLine(new Vector3(rect.xMin, rect.yMin, 0), new Vector3(rect.xMax, rect.yMin, 0), Color.green, 10f); //Debug.DrawLine(new Vector3(rect.xMin, rect.yMin, 0), new Vector3(rect.xMin, rect.yMax, 0), Color.green, 10f); //Debug.DrawLine(new Vector3(rect.xMin, rect.yMax, 0), new Vector3(rect.xMax, rect.yMax, 0), Color.green, 10f); //Debug.DrawLine(new Vector3(rect.xMax, rect.yMin, 0), new Vector3(rect.xMax, rect.yMax, 0), Color.green, 10f); var chosenCell = this.bgCells.Where(c => rect.Overlaps(RectTransformHelper.GetRectInWorldPosition(c))) .Where(c => Vector2.Distance(RectTransformHelper.GetRectInWorldPosition(c).center, rect.center) < Constants.Distances.MaximumDistanceToUnfloat) .OrderBy(c => { //Debug.DrawLine(RectTransformHelper.GetRectInWorldPosition(c).center, rect.center, Color.red, 10f); return(Vector2.Distance(RectTransformHelper.GetRectInWorldPosition(c).center, rect.center)); }) .FirstOrDefault(); if (chosenCell == null) { row = 0; column = 0; return(false); } var objectCell = chosenCell.GetComponentInChildren <MapObjectCell>(); row = objectCell.Row; column = objectCell.Column; return(true); }
// Update is called once per frame void Update() { this.inputStates = gameManager.Controls.GetInputStatesOnPosition <MapActor>(this.gameObject); if (this.IsReadyToPickUp && this.IsFloating && !this.IsInDragMode && this.IsDraggable) { if (CheckDragMode()) { return; } } if (this.IsFloating && !this.IsInDragMode && pushawayCoroutines.Count == 0) { var firstOverlappingFloater = this.gameManager.MapGrid.GetOverlappingFloatingObjects(this).Except(new[] { this }).FirstOrDefault(); if (firstOverlappingFloater == null || firstOverlappingFloater.IsInDragMode) { return; } var thisRect = RectTransformHelper.GetRectInWorldPosition(this.RectTransform); var overlappingRect = RectTransformHelper.GetRectInWorldPosition(firstOverlappingFloater.RectTransform); var distance = (thisRect.center - overlappingRect.center); if (distance.magnitude > Constants.Distances.MinimumFloatingDistanceToPushaway) { return; } distance = new Vector2(distance.x + (distance.x >= 0 ? -(thisRect.size.x / 2) : (thisRect.size.x / 2)), distance.y + (distance.y >= 0 ? (thisRect.size.y / 2) : -(thisRect.size.y / 2))); pushawayCoroutines.Enqueue(StartCoroutine(Pushaway(distance))); } }
void Update() { if (mouseControl == null || !mouseControl.IsMouseEnabled) { return; } var mousePosition = this.mouseControl.GetWorldMousePosition(); for (int i = 0; i < cursors.Count; i++) { cursors[i].transform.position = mousePosition + (cursors[i].IsUsingHand ? cursors[i].OffsetWithHand : cursors[i].Offset); } Rect rect = new Rect(new Vector2(-Screen.currentResolution.width / 2 + 1, -Screen.currentResolution.height / 2 + 1), new Vector2(Screen.currentResolution.width - 2, Screen.currentResolution.height - 2)); Rect gameMapRect = RectTransformHelper.GetRectInWorldPosition(this.gameManager.MapGrid.MapPartTransform); var lastActivatedItem = gameManager.Inventory.GetLastActivatedItem(); activeCursor = gameMapRect.Contains(mousePosition) && lastActivatedItem != null?cursors.FirstOrDefault(cur => cur.ItemName == lastActivatedItem.ReferenceName) : this.handCursor; EnableActiveCursor(); //Debug.DrawLine(new Vector3(rect.xMin, rect.yMin, 0), new Vector3(rect.xMax, rect.yMin, 0), Color.green); //Debug.DrawLine(new Vector3(rect.xMin, rect.yMin, 0), new Vector3(rect.xMin, rect.yMax, 0), Color.green); //Debug.DrawLine(new Vector3(rect.xMin, rect.yMax, 0), new Vector3(rect.xMax, rect.yMax, 0), Color.green); //Debug.DrawLine(new Vector3(rect.xMax, rect.yMin, 0), new Vector3(rect.xMax, rect.yMax, 0), Color.green); Cursor.visible = !rect.Contains(mousePosition) || activeCursor == null; }
public MapFloater[] GetOverlappingFloatingObjects(MapFloater floatingObject) { return(this.actorCells .Select(actor => actor.GetComponent <MapFloater>()) .Where(floater => floater != null) .Where(floater => floater.IsFloating && RectTransformHelper.GetRectInWorldPosition(floatingObject.RectTransform) .Overlaps(RectTransformHelper.GetRectInWorldPosition(floater.RectTransform))).ToArray()); }
void LateUpdate() { Rect gameMapRect = RectTransformHelper.GetRectInWorldPosition(gameManager.MapGrid.MapPartTransform); var halfSizeX = this.RectTransform.rect.size.x / 2; var halfSizeY = this.RectTransform.rect.size.y / 2; this.RectTransform.anchoredPosition = new Vector2(Mathf.Clamp(this.RectTransform.anchoredPosition.x, 0 + halfSizeX, gameMapRect.size.x - halfSizeX), Mathf.Clamp(this.RectTransform.anchoredPosition.y, -gameMapRect.size.y + halfSizeY, -halfSizeY)); }
public PositionActionState <T>[] GetInputStatesOnPosition <T>(GameObject boundaries) where T : MonoBehaviour { var rectTransform = boundaries.GetComponent <RectTransform>(); if (rectTransform == null) { return(new PositionActionState <T> [0]); } var mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0)); var actualRect = RectTransformHelper.GetRectInWorldPosition(rectTransform); bool isHovering = actualRect.Contains(mousePosition); bool isUsingMainAction = isHovering && Input.GetMouseButtonDown(Constants.Input.LeftMouseButton); bool isReleasingMainAction = isHovering && Input.GetMouseButtonUp(Constants.Input.LeftMouseButton); bool isMainActionHeldDown = Input.GetMouseButton(Constants.Input.LeftMouseButton); if (isReleasingMainAction) { Debug.Log("Released Main Action Button! " + boundaries.name); } T[] objects = boundaries.transform.OfType <RectTransform>() .Concat(new[] { rectTransform }) .Where(child => RectTransformHelper.GetRectInWorldPosition(child).Contains(mousePosition)) .Select(t => t.gameObject.GetComponent <T>()) .Where(actor => actor != null) .ToArray(); foreach (var obj in objects.Select(obj => obj.GetComponent <RectTransform>())) { var rect = RectTransformHelper.GetRectInWorldPosition(obj); Debug.DrawLine(new Vector3(rect.xMin, rect.yMin, 0), new Vector3(rect.xMax, rect.yMin, 0), Color.blue); Debug.DrawLine(new Vector3(rect.xMin, rect.yMin, 0), new Vector3(rect.xMin, rect.yMax, 0), Color.blue); Debug.DrawLine(new Vector3(rect.xMin, rect.yMax, 0), new Vector3(rect.xMax, rect.yMax, 0), Color.blue); Debug.DrawLine(new Vector3(rect.xMax, rect.yMin, 0), new Vector3(rect.xMax, rect.yMax, 0), Color.blue); } return(new PositionActionState <T>[1] { new PositionActionState <T>(mousePosition, isHovering, isUsingMainAction, isReleasingMainAction, isMainActionHeldDown, objects) }); }
private void RegisterClick(Vector2 screenPoint, int button, bool isTouch = false) { if (!_rdataClient.Authorized) { return; } PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); eventDataCurrentPosition.position = screenPoint; List <RaycastResult> results = new List <RaycastResult>(); EventSystem.current.RaycastAll(eventDataCurrentPosition, results); foreach (var result in results) { var selectedGameObject = result.gameObject; var tracker = selectedGameObject.GetComponent <RectTransformTracker>(); if (tracker == null) { continue; // Continue searching for the RectTransformTracker } Vector2 relativePosition; bool isHit = RectTransformHelper.ScreenPointToLocalPointInRectangleRelative(tracker.RectTransform, screenPoint, null, out relativePosition); // TODO: Provide camera for another types of canvas modes if (!isHit) { continue; // Redundant check, but who knows right? } if (RData.RDataLogging.DoLog) { Debug.Log("Clicked on " + tracker.gameObject.name + ", GameObjectGuid = " + tracker.GameObjectGuid + "; tracker.Context.Id = " + tracker.Context.Id + "; position.x = " + relativePosition.x + "; position.y = " + relativePosition.y); } var evt = new UiClickEvent(tracker.GameObjectGuid, relativePosition.x, relativePosition.y, isTouch, tracker.Context); RDataSingleton.Client.LogEvent(evt); break; // Found first RectTransformTracker, break. } }
protected override void WhenHitByHammer() { if (Floater.IsDragging) { return; } if (Floater.IsFloating) { int newRow, newColumn; var rect = RectTransformHelper.GetRectInWorldPosition(this.RectTransform); if (this.gameManager.MapGrid.RelocateInGrid(rect, out newRow, out newColumn)) { if (CheckGridCellIsAvailable(newRow, newColumn)) { if (HitByHammerUnfloatEffect != null) { var effect = Instantiate(HitByHammerUnfloatEffect); effect.transform.SetParent(this.transform, false); } if (controls.AdaptToTouch) { this.RectTransform.sizeDelta = initialSize; } this.gameManager.ScoreManager.IncreaseScore(Constants.Score.HitTrackWithHammer); Floater.Unfloat(newRow, newColumn); } else { Floater.InvalidBlink(); return; } } else { Floater.InvalidBlink(); return; } } else { if (this.CheckTrackIsOccupied()) { Floater.InvalidBlink(); return; } if (HitByHammerFloatEffect != null) { var effect = Instantiate(HitByHammerFloatEffect); effect.transform.SetParent(this.transform, false); } if (controls.AdaptToTouch) { this.RectTransform.sizeDelta = this.RectTransform.sizeDelta + Constants.Sizes.TrackExtraSizeForTouch; } this.gameManager.ScoreManager.IncreaseScore(Constants.Score.HitTrackWithHammer); Floater.Float(); } gameManager.TickTimers(); }