- Home /
Dragabble Logic for Card Matching game
Hi friends,
I wish to make a game similar to "Threes"
video: http://www.youtube.com/watch?v=MdeQaBuN1aw
I have all the basic logic ready, and am able to make it work on "SWIPE". That is, when you swipe in any given direction, it checks the appropriate match, and so on.
But for DRAG I am unable to reproduce the logic.
If someone could help me out that'd be great.
Here is my current Drag script which is attached to each of the cards.
void HandleDrag( DragGesture gesture )
{
if( !enabled )
return;
if( gesture.Phase == ContinuousGesturePhase.Started )
{
Dragging = true;
draggingFinger = gesture.Fingers[0];
}
else if( Dragging )
{
// make sure this is the finger we started dragging with
if( gesture.Fingers[0] != draggingFinger )
return;
if( gesture.Phase == ContinuousGesturePhase.Updated )
{
Transform tf = transform;
bool continueMoveX = false;
// figure out our previous screen space finger position
Vector3 fingerPos3d, prevFingerPos3d;
// convert these to world-space coordinates, and compute the amount of motion we need to apply to the object
if(ProjectScreenPointOnDragPlane( tf.position, draggingFinger.PreviousPosition, out prevFingerPos3d ) &&
ProjectScreenPointOnDragPlane( tf.position, draggingFinger.Position, out fingerPos3d ))
{
int i, j;
Vector3 move = fingerPos3d - prevFingerPos3d;
int direction;
if(Mathf.Abs(move.x) > Mathf.Abs(move.y) && allowX)
{
allowY = false;
direction = getDirection(move.x);
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
if(this.gameObject == currentOnscreenCards[j, i])
{
continueMoveX = true;
thisObjectUsefulVar = j;
goto end;
}
}
}
end:
if(continueMoveX)
{
if((move.x >0 && thisObjectUsefulVar < 3))
{
if(Mathf.Abs(totalMoveX) < 1.19f)
{
Vector3 temp = tf.position;
temp.x += move.x;
totalMoveX += move.x;
tf.position = temp;
}
else
{
Vector3 temp = tf.position;
temp.x -= move.x;
temp.x += 1.19f;
allowX = false;
}
}
else if (move.x < 0 && thisObjectUsefulVar > 0)
{
if(Mathf.Abs(totalMoveX) < 1.19f)
{
Vector3 temp = tf.position;
temp.x += move.x;
totalMoveX += move.x;
tf.position = temp;
}
else
{
Vector3 temp = tf.position;
temp.x += move.x;
temp.x -= 1.19f;
allowX = false;
}
}
}
}
else if (allowY)
{
allowX = false;
if( rigidbody )
physxDragMove.y += move.y; // this will be used in FixedUpdate() to properly move the rigidbody
else
{
if(Mathf.Abs(totalMoveY) < 1.68f)
{
Vector3 temp = tf.position;
temp.y += move.y;
totalMoveY += move.y;
tf.position = temp;
}
else
{
Vector3 temp = tf.position;
if(move.y > 0)
{
temp.y -= move.y;
temp.y += 1.68f;
}
else
{
temp.y += move.y;
temp.y -= 1.68f;
}
allowY = false;
}
}
}
}
}
else
{
if(gesture.Phase == ContinuousGesturePhase.Ended)
{
if(Mathf.Abs(totalMoveX) < 1.19f/2f)
{
Vector3 temp = transform.position;
temp.x -= totalMoveX;
transform.position = temp;
}
else
{
Vector3 temp = transform.position;
if(totalMoveX > 0)
temp.x += 1.19f - totalMoveX;
else
temp.x -= 1.19f - Mathf.Abs(totalMoveX);
transform.position = temp;
}
if(Mathf.Abs(totalMoveY) < 1.68f/2f)
{
Vector3 temp = transform.position;
temp.y -= totalMoveY;
transform.position = temp;
}
else
{
Vector3 temp = transform.position;
if(totalMoveY > 0)
temp.y += 1.68f - totalMoveY;
else
temp.y -= 1.68f - Mathf.Abs(totalMoveY);
transform.position = temp;
}
}
Dragging = false;
allowX = true;
allowY = true;
totalMoveX = 0.0f;
totalMoveY = 0.0f;
}
}
}
Comment