Skip to content

Commit

Permalink
Added easing/tweening, some bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SoundGoddess committed Jun 13, 2016
1 parent 58d97b7 commit 6e43101
Show file tree
Hide file tree
Showing 29 changed files with 756 additions and 302 deletions.
173 changes: 79 additions & 94 deletions Source/MonoGame.Ruge/CardEngine/Card.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,25 @@
/*
© 2016 The Ruge Project (http://ruge.metasmug.com/)
Licensed under MIT (see License.txt)
/* Attribution (a) 2016 The Ruge Project (http://ruge.metasmug.com/)
* Licensed under NWO-CS (see License.txt)
*/

using System;
using System.Collections.Generic;
using MonoGame.Ruge.DragonDrop;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Ruge.Glide;

namespace MonoGame.Ruge.CardEngine {

public enum Suit {

clubs,
hearts,
diamonds,
spades

};

public enum CardColor {

red,
black

}

public enum Rank {
_A,
_2,
_3,
_4,
_5,
_6,
_7,
_8,
_9,
_10,
_J,
_Q,
_K
}

public class Card : IDragonDropItem {

// Z-Index constants
protected const int ON_TOP = 1000;

// it's a tween thing
private Tweener tween = new Tweener();

private readonly SpriteBatch spriteBatch;
public CardType cardType;

private Vector2 _position;
public Vector2 Position {
Expand All @@ -61,7 +30,7 @@ public Vector2 Position {

if (Child != null) {

Vector2 pos = new Vector2(_position.X + stack.offset.X, _position.Y + stack.offset.Y);
var pos = new Vector2(_position.X + stack.offset.X, _position.Y + stack.offset.Y);

Child.Position = pos;
Child.snapPosition = pos;
Expand All @@ -86,28 +55,62 @@ public Vector2 Position {
public int ZIndex { get; set; } = 1;

public bool isSnapAnimating = false;
private bool startTween = true;
public bool snap = true;
public float snapSpeed = 25f;
public float snapTime = .7f;

// public Tweener tweenager = new Tweener();

public CardColor color {
get {
if (suit.Equals(Suit.hearts) || suit.Equals(Suit.diamonds)) return CardColor.red;
else return CardColor.black;


if (cardType.deckType == DeckType.hex) {

switch ((HexSuit) cardType.suit) {

case HexSuit.beakers:
case HexSuit.planets:
return CardColor.black;
default:
return CardColor.white;
}
}
if (cardType.deckType == DeckType.playing) {

switch ((PlayingSuit) cardType.suit) {

case PlayingSuit.diamonds:
case PlayingSuit.hearts:
return CardColor.red;
default:
return CardColor.black;
}
}

return CardColor.none;

}
}

public Rank rank;
public Suit suit;

public bool isFaceUp = false;
public Enum suit;
public Enum rank;

public Card(Rank rank, Suit suit, Texture2D cardBack, SpriteBatch spriteBatch) {
public Card(DeckType deckType, Enum suit, Enum rank, Texture2D cardBack, SpriteBatch spriteBatch) {

cardType = new CardType(deckType) {
suit = suit,
rank = rank
};

this.spriteBatch = spriteBatch;
this.rank = rank;
this.suit = suit;
this.rank = rank;

this.spriteBatch = spriteBatch;
this.cardBack = cardBack;

Tween.TweenerImpl.SetLerper<Vector2Lerper>(typeof(Vector2));
}


Expand Down Expand Up @@ -135,7 +138,6 @@ public bool Contains(Vector2 pointToCheck) {

public void Update(GameTime gameTime) {


if (IsSelected) {
var fixChild = Child;

Expand All @@ -147,9 +149,30 @@ public void Update(GameTime gameTime) {

if (isSnapAnimating) {

isSnapAnimating = !SnapAnimation();
// isSnapAnimating = !SnapAnimation();
if (startTween) {
tween.Tween(this, new { Position = snapPosition }, snapTime)
.Ease(Ease.ElasticOut)
.OnComplete(afterTween);

startTween = false;
}

}


tween.Update(float.Parse(gameTime.ElapsedGameTime.Seconds + "." + gameTime.ElapsedGameTime.Milliseconds));

}

private void afterTween() {

ZIndex -= ON_TOP;

if (stack.crunchStacks) stack.UpdatePositions();

isSnapAnimating = false;
startTween = true;

}

Expand All @@ -161,64 +184,28 @@ public void Draw(GameTime gameTime) {

public void MoveToEmptyStack(Stack newStack) {

stack.table.Save();

if (newStack.Count == 0) newStack.addCard(this, true);

}



public void SetParent(Card parent) {

stack.table.Save();

parent.Child = this;
parent.stack.addCard(this, true);

}



/// <summary>
/// Animation for returning the card to its original position if it can't find a new place to snap to
/// </summary>
/// <returns>returns true if the card is back in its original position; otherwise it increments the animation</returns>
private bool SnapAnimation() {

var backAtOrigin = false;

var pos = Position;

float distance = (float)Math.Sqrt(Math.Pow(snapPosition.X - pos.X, 2) + (float)Math.Pow(snapPosition.Y - pos.Y, 2));
float directionX = (snapPosition.X - pos.X) / distance;
float directionY = (snapPosition.Y - pos.Y) / distance;

pos.X += directionX * snapSpeed;
pos.Y += directionY * snapSpeed;


if (Math.Sqrt(Math.Pow(pos.X - Position.X, 2) + Math.Pow(pos.Y - Position.Y, 2)) >= distance) {

Position = snapPosition;

backAtOrigin = true;

ZIndex -= ON_TOP;

if (stack.crunchStacks) stack.UpdatePositions();

}
else Position = pos;

return backAtOrigin;

}



#region events

public event EventHandler Selected;

public void OnSelected() {

// Console.WriteLine("mouse: " + suit + "-" + rank + " - selected");

if (IsDraggable) {
IsSelected = true;
Expand All @@ -231,9 +218,7 @@ public void OnSelected() {
public event EventHandler Deselected;

public void OnDeselected() {

// Console.WriteLine("mouse: " + suit + "-" + rank + " - deselected");


IsSelected = false;

if (Position != snapPosition) isSnapAnimating = true;
Expand Down
33 changes: 33 additions & 0 deletions Source/MonoGame.Ruge/CardEngine/CardType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Attribution (a) 2016 The Ruge Project (http://ruge.metasmug.com/)
// Licensed under NWO-CS (see License.txt)

using System;

namespace MonoGame.Ruge.CardEngine {

public enum DeckType { hex, playing, tarot }

public enum HexSuit { beakers, chips, dna, planets }
public enum HexRank { _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _A, _B, _C, _D, _E, _F }

public enum PlayingSuit { clubs, hearts, diamonds, spades }
public enum PlayingRank { _A, _2, _3, _4, _5, _6, _7, _8, _9, _10, _J, _Q, _K }

public enum TarotSuit { major, coins, wands, swords, cups }
public enum TarotRank { _ace, _2, _3, _4, _5, _6, _7, _8, _9, _10, _page, _knight, _queen, _king }
public enum TarotRankMajor { _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21 }

public enum CardColor { red, black, white, none }

public class CardType {

public Enum suit;
public Enum rank;
public DeckType deckType;

public CardType(DeckType deckType) {
this.deckType = deckType;
}

}
}
44 changes: 28 additions & 16 deletions Source/MonoGame.Ruge/CardEngine/Deck.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/*
© 2016 The Ruge Project (http://ruge.metasmug.com/)
Licensed under MIT (see License.txt)
/* Attribution (a) 2016 The Ruge Project (http://ruge.metasmug.com/)
* Licensed under NWO-CS (see License.txt)
*/

using System;
Expand All @@ -14,9 +11,12 @@ namespace MonoGame.Ruge.CardEngine {

public class Deck : Stack {

public Deck(Texture2D cardBack, Texture2D slotTex, SpriteBatch spriteBatch, int stackOffsetH, int stackOffsetV)
: base(cardBack, slotTex, spriteBatch, stackOffsetH, stackOffsetV) {
public DeckType deckType;

public Deck(Table table, DeckType deckType, Texture2D cardBack, Texture2D slotTex, SpriteBatch spriteBatch, int stackOffsetH, int stackOffsetV)
: base(table, cardBack, slotTex, spriteBatch, stackOffsetH, stackOffsetV) {

this.deckType = deckType;
type = StackType.deck;

}
Expand All @@ -28,16 +28,28 @@ public void freshDeck() {

cards.Clear();

foreach (Suit mySuit in Enum.GetValues(typeof(Suit))) {

foreach (Rank myRank in Enum.GetValues(typeof(Rank))) {

cards.Add(new Card(myRank, mySuit, cardBack, spriteBatch));

if (deckType == DeckType.hex)
foreach (HexSuit mySuit in Enum.GetValues(typeof(HexSuit)))
foreach (HexRank myRank in Enum.GetValues(typeof(HexRank)))
cards.Add(new Card(deckType, mySuit, myRank, cardBack, spriteBatch));

else if (deckType == DeckType.playing)
foreach (PlayingSuit mySuit in Enum.GetValues(typeof(PlayingSuit)))
foreach (PlayingRank myRank in Enum.GetValues(typeof(PlayingRank)))
cards.Add(new Card(deckType, mySuit, myRank, cardBack, spriteBatch));

else if (deckType == DeckType.tarot) {

foreach (TarotSuit mySuit in Enum.GetValues(typeof(TarotSuit))) {

if (mySuit == TarotSuit.major)
foreach (TarotRankMajor myRank in Enum.GetValues(typeof(TarotRankMajor)))
cards.Add(new Card(deckType, mySuit, myRank, cardBack, spriteBatch));
else
foreach (TarotRank myRank in Enum.GetValues(typeof(TarotRank)))
cards.Add(new Card(deckType, mySuit, myRank, cardBack, spriteBatch));
}

}

}

/// <summary>
Expand All @@ -48,7 +60,7 @@ public void testDeck(int numCards) {

cards.Clear();

var subDeck = new Deck(cardBack, slot.Texture, spriteBatch, stackOffsetHorizontal, stackOffsetVertical);
var subDeck = new Deck(table, deckType, cardBack, slot.Texture, spriteBatch, stackOffsetHorizontal, stackOffsetVertical);
subDeck.freshDeck();
subDeck.shuffle();

Expand Down
Loading

0 comments on commit 6e43101

Please sign in to comment.