Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smooth camera drag. #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Assets/Polkadot Unity SDK/DemoGame/Scripts/GridManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class GridManager : Singleton<GridManager>

public event SwipeHandler OnSwipeEvent;

public delegate void DragHandler(Vector3 positionDiff);

public event DragHandler OnDragEvent;

[SerializeField]
public GameObject PlayerGrid;

Expand Down Expand Up @@ -58,6 +62,12 @@ public class GridManager : Singleton<GridManager>

private Vector2 touchEnd;

private Vector2 dragPosition;

private float dragSpeed = 60f;

private bool isDraging = false;

private bool isSwiping;

public float swipeThreshold = 50f; // Minimum distance for a swipe
Expand Down Expand Up @@ -128,6 +138,30 @@ private void HandleInput(TouchPhase phase, Vector2 position)
}
break;
}
// camera drag movement
switch (phase)
{
case TouchPhase.Began:
if (!isPointerOverUI && !isDraging)
{
dragPosition = position;
isDraging = true;
}
break;

case TouchPhase.Moved:
if (isDraging)
{
var positionDiff = new Vector3(dragPosition.x - position.x, dragPosition.y - position.y, 0.0f);
dragPosition = position;
OnDragEvent?.Invoke(positionDiff / dragSpeed);
}
break;

case TouchPhase.Ended:
isDraging = false;
break;
}
}

public void OnZoomClicked()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public override void EnterState()
OnChangedHexaPlayer(Storage.HexaGame.HexaTuples[PlayerIndex].player);
OnChangedHexaBoard(Storage.HexaGame.HexaTuples[PlayerIndex].board);

Grid.OnSwipeEvent += OnSwipeEvent;
Grid.OnDragEvent += OnDragEvent;

Storage.OnChangedHexaBoard += OnChangedHexaBoard;
Storage.OnChangedHexaPlayer += OnChangedHexaPlayer;
Expand All @@ -181,7 +181,7 @@ public override void ExitState()
// remove container
FlowController.VelContainer.RemoveAt(1);

Grid.OnSwipeEvent -= OnSwipeEvent;
Grid.OnDragEvent -= OnDragEvent;

Storage.OnChangedHexaBoard -= OnChangedHexaBoard;
Storage.OnChangedHexaPlayer -= OnChangedHexaPlayer;
Expand All @@ -193,7 +193,7 @@ public override void ExitState()
Storage.OnNextBlocknumber -= OnNextBlockNumber;
}

private void OnSwipeEvent(Vector3 direction)
private void OnDragEvent(Vector3 direction)
{
Grid.MoveCamera(direction);
}
Expand Down
Loading