Skip to content

Instantly share code, notes, and snippets.

@michaelperce michaelperce/CardSlotHolder.cs Secret
Created May 14, 2020

Embed
What would you like to do?
Selection of scripts forming the core of interacting with the cards in your hand
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CardSlotHolder : MonoBehaviour {
public enum SlotType { EMPTY, GENERIC, GRABBAG};
public SlotType typeOfSlot;
public bool shouldSpawnCard = true;
public SingleCard cardHeld;
//Add reference to enemy
public CardSlotManager manager;
public bool isAdditionType;
public GameObject GenericToSpawn;
int difficulty = 0;
private void Start() {
difficulty = Random.Range(1, 3);
}
private void Update() {
if(typeOfSlot == SlotType.GENERIC && shouldSpawnCard && cardHeld == null) {
shouldSpawnCard = false;
StartCoroutine(ReloadCard());
}
}
IEnumerator ReloadCard() {
for (int i = 0; i < 3; i++) {
yield return new WaitForFixedUpdate();
}
if (!isAdditionType && !manager.isGraveyard) {
GetComponent<BoxCollider>().enabled = false;
GenericToSpawn.GetComponent<SingleCard>().cardValue = difficulty;
GameObject generalMinusCard = Instantiate(GenericToSpawn, transform.position, Quaternion.identity, transform);
generalMinusCard.GetComponent<DragTransform>().SlotIn(transform);
shouldSpawnCard = true;
}
}
public void GrabCardsFromBag() {
StartCoroutine(LoadGrabBagCard());
}
IEnumerator LoadGrabBagCard() {
for (int i = 0; i < 3; i++) {
yield return new WaitForFixedUpdate();
}
SingleCard cardFromGrabBag = manager.GetDeckManager().GetGrabBagCard();
GameObject generalMinusCard = Instantiate(GenericToSpawn, transform.position, Quaternion.identity, transform);
generalMinusCard.GetComponent<SingleCard>().SetValues(cardFromGrabBag, generalMinusCard, this);
generalMinusCard.GetComponent<SingleCard>().isGrabBagCard = true;
generalMinusCard.GetComponent<DragTransform>().SlotIn(transform);
generalMinusCard.GetComponent<BoxCollider2D>().enabled = true;
}
public void SetCard(GameObject newCard) {
SingleCard tempCard = newCard.GetComponent<SingleCard>();
if (cardHeld != null) {
if(tempCard != cardHeld) {
cardHeld.GetComponent<DragTransform>().Expunge();
}
}
cardHeld = tempCard;
if(manager)
manager.SlotUpdated(gameObject);
//Tell the enemy reference to check if all cards are filled
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CardSlotManager : MonoBehaviour {
public bool isGraveyard = false;
[Tooltip("+, -")]
public GameObject[] slotTypes;
[Tooltip("+, -")]
public GameObject[] symbolTypes;
public List<GameObject> equation;
public AnimationCurve customCombineLerp;
public AnimationCurve attackAnimCurve;
public AnimationCurve healAnimCurve;
public AnimationCurve slowAnimCurve;
public AnimationCurve rotateAnimCurve;
public GameObject flash;
public GameObject freshNewCard;
public PlayerHealthBar PHB;
Deck deckManager;
AudioSource SFXbox;
[Tooltip("Diamond, Heart, Spade, Club")]
public AudioClip[] effectSounds;
// Start is called before the first frame update
void Start() {
SFXbox = GetComponent<AudioSource>();
deckManager = FindObjectOfType<Deck>();
for (int i = 0; i < equation.Count; i += 2) {
CardSlotHolder currentHolder = equation[i].GetComponent<CardSlotHolder>();
if (currentHolder) {
currentHolder.manager = this;
}
}
}
public bool debugTesting = false;
private void Update() {
if (debugTesting) {
debugTesting = false;
GenerateSlots(true);
}
}
//Generate Slots Randomly
public void GenerateSlots() {
GenerateSlots(-1, -1);
}
//numSlotsToMake is -1, choose randomly
//isSubtraction: 0 == false, 1 == true, -1 == random
public void GenerateSlots(int numSlotsToMake, int isSubtraction) {
if (equation.Count > 0) {
foreach (GameObject go in equation) {
Destroy(go);
}
}
equation.Clear();
//Choose randomly
if (numSlotsToMake == -1) {
numSlotsToMake = Random.Range(1, 3);
}
GameObject initialSlot = Instantiate(slotTypes[0], transform);
equation.Add(initialSlot);
initialSlot.GetComponent<CardSlotHolder>().manager = this;
//Creates the slots
for (int i = 1; i < numSlotsToMake; i++) {
if (isSubtraction == -1) {
isSubtraction = Random.Range(0, 2);
}
GameObject tempCardSlot = Instantiate(slotTypes[isSubtraction], transform);
tempCardSlot.GetComponent<CardSlotHolder>().manager = this;
equation.Add(tempCardSlot);
}
//Adds the symbols between each slot
for (int index = 1; index < equation.Count; index++) {
for (int type = 0; type < slotTypes.Length; type++) {
if (equation[index].GetComponent<CardSlotHolder>().isAdditionType == slotTypes[type].GetComponent<CardSlotHolder>().isAdditionType) {
GameObject tempSymbol = Instantiate(symbolTypes[type], transform);
equation.Insert(index, tempSymbol);
index++;
break;
}
}
}
//Sets every object to the correct distances
int indexCenter = equation.Count / 2;
for (int i = 0; i < equation.Count; i++) {
equation[i].transform.localPosition = new Vector3(2 * (i - indexCenter), 0, 1);
}
}
/// <summary>
/// Generates Card Slots for an enemy that is actually the shopkeeper
/// </summary>
/// <param name="isShopGenertaion"></param>
public void GenerateSlots(bool isShopGenertaion) {
if (equation.Count > 0) {
foreach (GameObject go in equation) {
Destroy(go);
}
}
equation.Clear();
//Choose randomly
int numSlotsToMake = 3;
GameObject initialSlot = Instantiate(slotTypes[2], transform);
equation.Add(initialSlot);
initialSlot.GetComponent<CardSlotHolder>().manager = this;
//Creates the slots
for (int i = 1; i < numSlotsToMake; i++) {
GameObject tempCardSlot = Instantiate(slotTypes[0], transform);
tempCardSlot.GetComponent<CardSlotHolder>().manager = this;
equation.Add(tempCardSlot);
}
//Sets every object to the correct distances
int indexCenter = equation.Count / 2;
for (int i = 0; i < equation.Count; i++) {
equation[i].transform.localPosition = new Vector3(2 * (i - indexCenter), 0, 1);
equation[i].GetComponent<CardSlotHolder>().GrabCardsFromBag();
}
FindObjectOfType<ShoppingCart>().ShoppingTime();
}
public Deck GetDeckManager() { return deckManager; }
public void SlotUpdated(GameObject CSH) {
for(int i =0; i < equation.Count; i+= 2) {
if(equation[i] == CSH) {
continue;
}
else if(CSH.GetComponent<CardSlotHolder>().cardHeld == equation[i].GetComponent<CardSlotHolder>().cardHeld ) {
equation[i].GetComponent<CardSlotHolder>().cardHeld = null;
}
}
if (CheckIfFull()) {
//
//Do math
//
int totalValue = equation[0].GetComponent<CardSlotHolder>().cardHeld.cardValue;
CardDisplay.Suit currentSuit = equation[0].GetComponent<CardSlotHolder>().cardHeld.cardSuit;
for (int i = 1; i < equation.Count; i += 2) {
if (equation[i].tag == symbolTypes[0].tag) {
totalValue += equation[i + 1].GetComponent<CardSlotHolder>().cardHeld.cardValue;
}
else {
totalValue -= equation[i + 1].GetComponent<CardSlotHolder>().cardHeld.cardValue;
}
}
for(int i = 0; i < equation.Count; i++) {
if(equation[i].tag != symbolTypes[0].tag && equation[i].tag != symbolTypes[1].tag) {
equation[i].GetComponent<CardSlotHolder>().cardHeld.GetComponent<BoxCollider2D>().enabled = false;
}
}
if(totalValue >= 10) {
totalValue = 10;
}
StartCoroutine(CombineCards(totalValue, currentSuit));
}
}
float timeToPushCardsTogether = 0.5f;
IEnumerator CombineCards(int value, CardDisplay.Suit suit) {
float timeSinceStart = 0f;
SingleCard firstCard = equation[0].GetComponent<CardSlotHolder>().cardHeld;
//Move cards together
if (equation.Count > 2) {
while (timeSinceStart < timeToPushCardsTogether) {
yield return null;
timeSinceStart += Time.deltaTime;
for (int i = 0; i < equation.Count; i += 2) {
Transform currentTrans = equation[i].GetComponent<CardSlotHolder>().cardHeld.transform;
currentTrans.position = Vector2.Lerp(equation[i].transform.position, equation[1].transform.position, customCombineLerp.Evaluate(timeSinceStart / timeToPushCardsTogether));
}
}
}
//When they get close, make a white circle that overtakes the cards
//The circle "flashes" (lerps big and then small)
GameObject combineFlash;
if (equation.Count > 2) {
combineFlash = Instantiate(flash, equation[1].transform.position, Quaternion.identity);
}
else {
combineFlash = Instantiate(flash, equation[0].transform.position, Quaternion.identity);
}
combineFlash.transform.localScale = new Vector3(0, 0, 0);
timeSinceStart = 0;
float timeToExpand = 0.2f;
while (timeSinceStart < timeToExpand) {
yield return null;
timeSinceStart += Time.deltaTime;
combineFlash.transform.localScale = Vector3.Lerp(new Vector3(0, 0, 0), new Vector3(4, 4, 4), customCombineLerp.Evaluate(timeSinceStart / timeToExpand));
}
GameObject cardToAnimate = null;
//
//Instantiate the new card
if (!isGraveyard) {
GameObject newCard = Instantiate(freshNewCard, equation[equation.Count / 2].transform.position, Quaternion.identity);
newCard.transform.localScale = equation[0].transform.localScale;
SingleCard tempCard = new SingleCard(value, (int)suit);
newCard.GetComponent<SingleCard>().SetValues(tempCard, newCard);
yield return new WaitForFixedUpdate();
newCard.GetComponent<SingleCard>().SetSortingValue(5, true);
newCard.GetComponent<BoxCollider2D>().enabled = false;
cardToAnimate = newCard;
if(equation.Count > 2) {
deckManager.AddGrabBagCard(tempCard);
}
Destroy(newCard, timeSinceStart + 2f);
}
//
//Delete the other cards
for (int i = 0; i < equation.Count; i += 2) {
deckManager.CardPlayed(equation[i].GetComponent<CardSlotHolder>().cardHeld);
}
//
//Make flash effect go away
while (timeSinceStart > 0) {
yield return null;
timeSinceStart -= Time.deltaTime;
combineFlash.transform.localScale = Vector3.Lerp(new Vector3(0, 0, 0), new Vector3(4, 4, 4), customCombineLerp.Evaluate(timeSinceStart / timeToExpand));
}
Destroy(combineFlash);
//
//Effect takes place
//
if (!isGraveyard) {
Vector3 initPos = cardToAnimate.transform.position;
Vector3 initScale = cardToAnimate.transform.localScale;
Vector3 targetPos;
timeSinceStart = 0f;
//
//Blank Card
if (value <= 0) {
timeSinceStart = 0f;
combineFlash = Instantiate(flash, cardToAnimate.transform.position, Quaternion.identity);
combineFlash.transform.localScale = new Vector3(0, 0, 0);
while (timeSinceStart < timeToExpand) {
yield return null;
timeSinceStart += Time.deltaTime;
combineFlash.transform.localScale = Vector3.Lerp(new Vector3(0, 0, 0), new Vector3(4, 4, 4), customCombineLerp.Evaluate(timeSinceStart / timeToExpand));
}
//Delete the blank cards
Destroy(cardToAnimate);
//Make flash effect go away
while (timeSinceStart > 0) {
yield return null;
timeSinceStart -= Time.deltaTime;
combineFlash.transform.localScale = Vector3.Lerp(new Vector3(0, 0, 0), new Vector3(4, 4, 4), customCombineLerp.Evaluate(timeSinceStart / timeToExpand));
}
int numCardsToDraw = equation.Count / 2 + 1;
for (int i = 0; i < equation.Count; i++) {
if(equation[i].tag == "-" && equation[i+1].GetComponent<CardSlotHolder>().shouldSpawnCard) {
numCardsToDraw--;
}
}
for (int i = 0; i < numCardsToDraw; i++) {
deckManager.DrawCard();
}
yield break;
}
//
//Diamonds Hurt Red Enemies
//
if (suit == CardDisplay.Suit.DIAMOND) {
StartCoroutine(AttackAnimation(cardToAnimate, true));
}
//
//Spades Hurt Black Enemies
//
else if (suit == CardDisplay.Suit.SPADE) {
StartCoroutine(AttackAnimation(cardToAnimate, false));
}
//
//Hearts Heal the player
//
else if (suit == CardDisplay.Suit.HEART) {
targetPos = initPos;
targetPos.y = -10;
Vector3 tempVector = new Vector3(0, 0, 0);
Vector3 travelVector = targetPos - initPos;
PHB.HealDamage(value);
while (timeSinceStart < 1.5f) {
yield return null;
timeSinceStart += Time.deltaTime;
tempVector = healAnimCurve.Evaluate(timeSinceStart / 2f) * travelVector;
cardToAnimate.transform.position = initPos + tempVector;
cardToAnimate.transform.localScale = new Vector3(rotateAnimCurve.Evaluate(timeSinceStart / 1.5f) * initScale.x,
initScale.y, initScale.z);
}
}
//
//Clubs slow the enemy
//
else if (suit == CardDisplay.Suit.CLUB) {
targetPos = initPos;
targetPos.y = 6;
Vector3 tempVector = new Vector3(0, 0, 0);
Vector3 travelVector = targetPos - initPos;
EnemySpawner.instance.currentEnemy.GetComponent<Enemy>().SlowEffect(value);
while (timeSinceStart < 1.5f) {
yield return null;
timeSinceStart += Time.deltaTime;
tempVector = healAnimCurve.Evaluate(timeSinceStart / 2f) * travelVector;
cardToAnimate.transform.position = initPos + tempVector;
cardToAnimate.transform.localScale = new Vector3(rotateAnimCurve.Evaluate(timeSinceStart / 1.5f) * initScale.x,
initScale.y, initScale.z);
}
}
}
else {
deckManager.StopAllCoroutines();
deckManager.DrawCard();
}
}
IEnumerator AttackAnimation(GameObject cardToAnimate, bool isDiamondAttack) {
float timeSinceStart = 0f;
Vector3 initPos = cardToAnimate.transform.position;
Vector3 targetPos;
int value = cardToAnimate.GetComponent<SingleCard>().cardValue;
targetPos = initPos;
targetPos.y = 6;
Vector3 tempVector = new Vector3(0, 0, 0);
Vector3 travelVector = targetPos - initPos;
if (EnemySpawner.instance.currentEnemy == null) {
yield break;
}
Enemy enemyScript = EnemySpawner.instance.currentEnemy.GetComponent<Enemy>();
enemyScript.GetHit(value, isDiamondAttack);
if (isDiamondAttack && enemyScript.isRed
|| !isDiamondAttack && !enemyScript.isRed) {
SFXbox.clip = effectSounds[2];
SFXbox.Play();
while (timeSinceStart < 1.5f) {
yield return null;
timeSinceStart += Time.deltaTime;
tempVector = attackAnimCurve.Evaluate(timeSinceStart / 2f) * travelVector;
cardToAnimate.transform.position = initPos + tempVector;
}
}
else {
SFXbox.clip = effectSounds[1];
SFXbox.Play();
float deflectAmount = Random.Range(30f, 70f);
//Debug.Log(deflectAmount);
deflectAmount *= (Random.Range(0, 2) % 2 == 0) ? 1 : -1;
float startX = cardToAnimate.transform.position.x;
float deflectStartTime = 0f;
while (timeSinceStart < 1.5f) {
yield return null;
timeSinceStart += Time.deltaTime;
tempVector = attackAnimCurve.Evaluate(timeSinceStart / 2f) * travelVector;
if (cardToAnimate.transform.position.y >= 1) {
if (deflectStartTime < 0.01) {
deflectStartTime = timeSinceStart;
//Play sound
SFXbox.clip = effectSounds[0];
SFXbox.Play();
}
float newXPos = Mathf.Lerp(startX, startX + deflectAmount,
(timeSinceStart - deflectStartTime) / (1.15f - deflectStartTime));
tempVector.x = newXPos;
}
cardToAnimate.transform.position = initPos + tempVector;
}
//Debug.Log(cardToAnimate.transform.position);
}
}
public bool CheckIfFull() {
for (int i = 0; i < equation.Count; i += 2) {
CardSlotHolder currentSlot = equation[i].GetComponent<CardSlotHolder>();
if (currentSlot.cardHeld == null) {
return false;
}
if(currentSlot.typeOfSlot == CardSlotHolder.SlotType.GRABBAG) {
return false;
}
}
return true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class Deck : MonoBehaviour {
public GameObject card;
public GameObject cardGhosts;
public Vector3[] newCardSpawnLocations;
public int handSize = 3;
public AnimationCurve customSlideAnimation;
[Tooltip("The Cards in Your Deck")]
public List<SingleCard> cardPool;
[Tooltip("The Cards in Hand")]
public List<SingleCard> cardsInHand;
public List<GameObject> handCardObjects;
[Tooltip("The Cards in Your Graveyard")]
public List<SingleCard> discardPool;
public List<SingleCard> grabBagPool;
public GameObject goodbyeParticle;
public TextMeshPro deckCount;
public TextMeshPro discardCount;
public bool loadingNewCard = false;
public bool currentlyShopping = false;
AudioSource shuffleSFX;
// Start is called before the first frame update
void Start() {
cardPool = new List<SingleCard>();
cardsInHand = new List<SingleCard>();
discardPool = new List<SingleCard>();
shuffleSFX = GetComponent<AudioSource>();
//
//Generates the deck here
//Hearts and Clubs
for (int suit = 1; suit < 4; suit += 2) {
for (int value = 1; value < 5; value++) {
SingleCard tempCard = new SingleCard(value, suit);
cardPool.Add(tempCard);
}
}
//Diamonds and Spades
for (int numSets = 0; numSets < 2; numSets++) {
for (int suit = 0; suit < 4; suit += 2) {
for (int value = 1; value < 5; value++) {
SingleCard tempCard = new SingleCard(value, suit);
cardPool.Add(tempCard);
}
}
}
//cardPool.Add(new SingleCard(4, CardDisplay.Suit.CURSE));
//cardPool.Add(new SingleCard(4, CardDisplay.Suit.BLESS));
//Draw the starting hand
for (int i = 0; i < handSize; i++) {
DrawCard();
}
}
float timeCountDown = 2f;
// Update is called once per frame
void Update() {
//Death Reset
if (Time.timeScale == 0 && Input.anyKeyDown) {
SceneManager.LoadScene(1);
Time.timeScale = 1f;
}
//Shuffle test
if (Input.GetKeyDown(KeyCode.Alpha9)) {
discardPool.AddRange(cardPool);
cardPool.Clear();
UpdateNumCardsEachZone();
StartCoroutine(BackendDraw());
}
}
public void UpdateNumCardsEachZone() {
deckCount.text = cardPool.Count + "";
discardCount.text = discardPool.Count + "";
}
/// <summary>
/// Draw multiple cards at once
/// </summary>
/// <param name="numCardsToDraw"> The number of cards to draw at once</param>
public void DrawCards(int numCardsToDraw) {
for (int i = 0; i < numCardsToDraw; i++) {
DrawCard();
}
}
/// <summary>
/// Call this function to draw ONE card
/// </summary>
public void DrawCard() {
loadingNewCard = true;
StartCoroutine(BackendDraw());
}
public void NaturalDraw() {
loadingNewCard = true;
StartCoroutine(BackendDraw());
}
IEnumerator BackendDraw() {
if (cardPool.Count == 0) {
//If you still don't have a deck, just give up
if (discardPool.Count == 0) {
yield break;
}
cardPool.AddRange(discardPool);
shuffleSFX.Play();
yield return StartCoroutine(ShuffleEffect());
}
while(currentlyShopping) {
yield return null;
}
//Remove a random card from the deck
int drawnCardIndex = Random.Range(0, cardPool.Count);
SingleCard newCard = cardPool[drawnCardIndex];
cardPool.RemoveAt(drawnCardIndex);
UpdateNumCardsEachZone();
//Add that card to the hand pile
cardsInHand.Add(newCard);
//Instantiate a single card object
GameObject newCardObject = Instantiate(card, newCardSpawnLocations[0], Quaternion.Euler(0, 0, 45), transform);
newCardObject.GetComponent<SingleCard>().SetValues(newCard, newCardObject);
newCard.SetValues(newCardObject);
handCardObjects.Add(newCardObject);
//Push in from the bottom on left most side
StartCoroutine(slideInNewCard(newCardObject));
}
IEnumerator ShuffleEffect() {
float timeSinceStart = 0f;
float timeForEffect = shuffleSFX.clip.length * .9f;
int numGhostsToSpawn = discardPool.Count;
discardPool.Clear();
float timeBetweenEachGhost = (timeForEffect / 2f) / numGhostsToSpawn;
List<GameObject> ghostsAlreadySpawned = new List<GameObject>();
int numGhostsSpawned = 0;
//Actual start of shuffle effect
while (timeSinceStart < timeForEffect) {
timeSinceStart += Time.deltaTime;
yield return null;
if (numGhostsSpawned < numGhostsToSpawn) {
if (numGhostsSpawned * timeBetweenEachGhost < timeSinceStart) {
//Spawn a Ghost
GameObject newGhost = Instantiate(cardGhosts, discardCount.transform.position, Quaternion.identity, transform);
numGhostsSpawned++;
Destroy(newGhost, timeForEffect - timeSinceStart + .1f);
ghostsAlreadySpawned.Add(newGhost);
}
}
for (int i = 0; i < ghostsAlreadySpawned.Count; i++) {
float xPos = Mathf.Lerp(
7.4f, -7.4f,
customSlideAnimation.Evaluate(
(timeSinceStart - i * timeBetweenEachGhost) / (timeForEffect / 2))
);
float yPos = -0.04565f * Mathf.Pow(xPos, 2) - 2f;
ghostsAlreadySpawned[i].transform.position = new Vector3(xPos, yPos, 0);
}
}
//Cleanup
ghostsAlreadySpawned.Clear();
}
public void AddGrabBagCard(SingleCard cardToAdd) {
grabBagPool.Add(cardToAdd);
}
public SingleCard GetGrabBagCard() {
if(grabBagPool.Count >= 1) {
int index = Random.Range(0, grabBagPool.Count);
SingleCard card = grabBagPool[index];
grabBagPool.RemoveAt(index);
return card;
}
else {
int suit = Random.Range(0, 4);
int value = Random.Range(1, 4);
return new SingleCard(value, (CardDisplay.Suit)suit);
}
}
/// <summary>
/// Moves parameters and cards from zone to zone. Does NOT call an effect.
/// </summary>
/// <param name="cardThatWasPlayed">The SingleCard script attached to the played card</param>
public void CardPlayed(SingleCard cardThatWasPlayed) {
for (int i = 0; i < cardsInHand.Count; i++) {
if (cardThatWasPlayed.gameObject == handCardObjects[i]) {
handCardObjects[i].GetComponent<BoxCollider2D>().enabled = false;
//Move the card to a new zone
discardPool.Add(cardsInHand[i]);
cardsInHand.RemoveAt(i);
UpdateNumCardsEachZone();
//Create the discard particle
Instantiate(goodbyeParticle, handCardObjects[i].transform.position, Quaternion.identity);
handCardObjects.RemoveAt(i);
Destroy(cardThatWasPlayed.gameObject);
//Shift the cards around
StartCoroutine(AdjustAllCardPositions());
return;
}
}
Destroy(cardThatWasPlayed.gameObject);
}
public float timeToSlideInNew = 1f;
/// <summary>
/// Moves a new card from off screen into the hand
/// </summary>
/// <param name="newestCard">The object of the new card</param>
/// <returns></returns>
IEnumerator slideInNewCard(GameObject newestCard) {
yield return new WaitForFixedUpdate();
while (currentlyShopping) {
yield return null;
}
//Adjust the render order to display properly
for (int cardIndex = 0; cardIndex < cardsInHand.Count; cardIndex++) {
handCardObjects[cardIndex].GetComponent<SingleCard>().SetSortingValue(-2 * cardIndex, true);
}
//Set new card location according to theoretical arch
Vector3 newCardLocation = new Vector3(-8, 0, 10);
newCardLocation.y = -0.05f * Mathf.Pow(newCardLocation.x, 2) - 4f;
newestCard.transform.position = newCardLocation;
newestCard.GetComponent<SingleCard>().RefreshJumpingValues();
StartCoroutine(AdjustAllCardPositions());
}
public float timeToAdjustAllCards = 1f;
/// <summary>
/// Adjust the location of all cards in hand to the correct positions
/// </summary>
/// <returns></returns>
IEnumerator AdjustAllCardPositions() {
float timeSinceStart = 0f;
while (currentlyShopping) {
yield return null;
}
while (timeSinceStart < timeToAdjustAllCards) {
yield return null;
for (int cardIndex = 0; cardIndex < cardsInHand.Count; cardIndex++) {
float targetXPos = 0;
//Calculate the target position for the current card
if (cardsInHand.Count == 2) {
targetXPos = 2 - (cardIndex * 4f / (cardsInHand.Count - 1f));
}
else if (cardsInHand.Count > 2) {
targetXPos = 4 - (cardIndex * 8f / (cardsInHand.Count - 1f));
}
float targetYPos = EvaluateHandEquation(targetXPos);
if (!handCardObjects[cardIndex]) {
continue;
}
Transform currentCardTrans = handCardObjects[cardIndex].transform;
//
//Only sets values for cards not in slot & Not being held
if (!handCardObjects[cardIndex].GetComponent<DragTransform>().inSlot &&
!handCardObjects[cardIndex].GetComponent<DragTransform>().dragging) {
//Moves and rotates cards appropriately
Vector3 startingPos = handCardObjects[cardIndex].GetComponent<SingleCard>().startingPos;
float currentXPos = Mathf.Lerp(startingPos.x, targetXPos, customSlideAnimation.Evaluate(timeSinceStart / timeToAdjustAllCards));
float currentYPos = EvaluateHandEquation(currentXPos);
currentCardTrans.position = new Vector3(currentXPos, currentYPos, 2 * cardIndex);
float angle = Mathf.Lerp(20, -20, (currentXPos + 4) / 8);
if (angle < 0) {
angle += 360;
}
currentCardTrans.rotation = Quaternion.Euler(0, 0, angle);
}
}
timeSinceStart += Time.deltaTime;
}
//Update Reset Positions
for (int cardIndex = 0; cardIndex < cardsInHand.Count; cardIndex++) {
if (!handCardObjects[cardIndex].GetComponent<DragTransform>().inSlot &&
!handCardObjects[cardIndex].GetComponent<DragTransform>().dragging) {
handCardObjects[cardIndex].GetComponent<SingleCard>().RefreshJumpingValues();
}
else {
float targetXPos = 0;
if (cardsInHand.Count == 2) {
targetXPos = 2 - (cardIndex * 4f / (cardsInHand.Count - 1f));
}
else if (cardsInHand.Count > 2) {
targetXPos = 4 - (cardIndex * 8f / (cardsInHand.Count - 1f));
}
float targetYPos = EvaluateHandEquation(targetXPos);
float angle = Mathf.Lerp(20, -20, (targetXPos + 4) / 8);
if (angle < 0) {
angle += 360;
}
handCardObjects[cardIndex].GetComponent<SingleCard>().RefreshJumpingValues(new Vector3(targetXPos, targetYPos, 2 * cardIndex), Quaternion.Euler(0, 0, angle));
}
}
loadingNewCard = false;
}
public float EvaluateHandEquation(float xAxis) {
return -0.05f * Mathf.Pow(xAxis, 2) - 4f;
}
}
/*
* Old alternative method of adding new cards
Slide new card in from directly below
//Slide in according to the animation curve
while (timeSinceStart < timeToSlideInNew) {
yield return null;
newestCard.transform.position = Vector3.Lerp(newCardSpawnLocations[0], newCardSpawnLocations[1], customSlideAnimation.Evaluate(timeSinceStart / timeToSlideInNew));
timeSinceStart += Time.deltaTime;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SingleCard : MonoBehaviour {
public bool debugTesting = false;
public bool isGrabBagCard = false;
public int cardValue;
public CardDisplay.Suit cardSuit;
[Tooltip("1,2,3,4,5")]
public GameObject[] valueDisplayPrefabs;
CardDisplay ownDisplay;
GameObject ownDisplayObject;
public Transform currentTrans;
SpriteRenderer renderBox;
AudioSource SFXbox;
public AudioClip[] dealSounds;
public Vector3 startingPos;
public Quaternion startingRotation;
public int startingLevel;
public CardSlotHolder GrabBagHolder;
public SingleCard(int outsideValue, int outsideSuit) {
cardValue = outsideValue;
cardSuit = (CardDisplay.Suit)outsideSuit;
}
public SingleCard(int outsideValue, CardDisplay.Suit outsideSuit) {
cardValue = outsideValue;
cardSuit = outsideSuit;
}
// Start is called before the first frame update
void Start() {
renderBox = GetComponent<SpriteRenderer>();
ResetDisplay();
SFXbox = GetComponent<AudioSource>();
SFXbox.clip = dealSounds[Random.Range(0, dealSounds.Length)];
SFXbox.Play();
}
// Update is called once per frame
void Update() {
if (debugTesting) {
debugTesting = false;
RandomDisplay();
}
}
void RandomDisplay() {
cardValue = Random.Range(6, 8);
cardSuit = (CardDisplay.Suit)Random.Range(4, 7);
ResetDisplay();
}
public void ResetDisplay() {
if (cardValue <= 0) {
if (ownDisplayObject) {
Destroy(ownDisplayObject);
}
ownDisplayObject = null;
return;
}
if (ownDisplayObject) {
Destroy(ownDisplayObject);
}
if(cardValue >= 10) {
cardValue = 10;
}
ownDisplayObject = Instantiate(valueDisplayPrefabs[cardValue - 1], transform.position + new Vector3(0, 0, -1), transform.rotation, transform);
ownDisplay = ownDisplayObject.GetComponent<CardDisplay>();
ownDisplay.currentSuit = cardSuit;
ownDisplay.resetDisplay();
}
public void SetValues(GameObject otherObject) {
currentTrans = otherObject.transform;
}
public void SetValues(SingleCard otherCard, GameObject otherObject) {
cardValue = otherCard.cardValue;
cardSuit = otherCard.cardSuit;
currentTrans = otherObject.transform;
ResetDisplay();
}
public void SetValues(SingleCard otherCard, GameObject otherObject, CardSlotHolder CSH) {
SetValues(otherCard, otherObject);
GrabBagHolder = CSH;
}
public void SetSortingValue(int newSortingLevel, bool isMovingUp) {
StartCoroutine(LateSetSorting(newSortingLevel, isMovingUp));
}
IEnumerator LateSetSorting(int newSortingLevel, bool isMovingUp) {
if (isMovingUp) {
yield return new WaitForFixedUpdate();
if (ownDisplay) {
ownDisplay.layerOrder = newSortingLevel + 1;
ownDisplay.resetDisplay();
}
yield return new WaitForFixedUpdate();
yield return new WaitForFixedUpdate();
renderBox.sortingOrder = newSortingLevel;
}
else {
yield return new WaitForFixedUpdate();
renderBox.sortingOrder = newSortingLevel;
yield return new WaitForFixedUpdate();
yield return new WaitForFixedUpdate();
if (ownDisplay) {
ownDisplay.layerOrder = newSortingLevel + 1;
ownDisplay.resetDisplay();
}
}
}
public void RefreshJumpingValues() {
startingPos = currentTrans.position;
startingRotation = currentTrans.rotation;
startingLevel = currentTrans.GetComponent<SpriteRenderer>().sortingOrder;
}
public void RefreshJumpingValues(Vector3 position, Quaternion rotation) {
startingPos = position;
startingRotation = rotation;
}
bool isInProgress = false;
bool inProgressState = false;
void OnMouseEnter() {
//Lerp position to y = -3.5
//set z rotation to 0
//set sorting value to 2
DragTransform DT = GetComponent<DragTransform>();
if (DT.IsDragging() || DT.inSlot) {
return;
}
StopAllCoroutines();
AnimationCancel();
StartCoroutine(HoverMoveUp(true));
}
void OnMouseExit() {
//
//Check to see if dragging is true
//If so, do nothing
//Otherwise, trigger the lerp back to correct position
//
DragTransform DT = GetComponent<DragTransform>();
if (DT.IsDragging() || DT.inSlot) {
return;
}
StopAllCoroutines();
AnimationCancel();
StartCoroutine(HoverMoveUp(false));
}
void AnimationCancel() {
StopAllCoroutines();
if (isInProgress) {
if (inProgressState) {
transform.position = new Vector3(startingPos.x, -3.5f, startingPos.z);
transform.rotation = Quaternion.identity;
}
else {
transform.position = startingPos;
transform.rotation = startingRotation;
}
SetSortingValue((inProgressState) ? 2 : startingLevel, inProgressState);
}
}
public float timeToMoveSpots = 0.5f;
IEnumerator HoverMoveUp(bool isMovingUp) {
isInProgress = true;
inProgressState = isMovingUp;
float timeSinceStart = (isMovingUp) ? 0f : timeToMoveSpots;
while ((isMovingUp && timeSinceStart < timeToMoveSpots) || (!isMovingUp && timeSinceStart > 0)) {
yield return null;
transform.position = Vector3.Lerp(startingPos, new Vector3(startingPos.x, -3.5f, startingPos.z), timeSinceStart / timeToMoveSpots);
transform.rotation = Quaternion.Slerp(startingRotation, Quaternion.identity, timeSinceStart / timeToMoveSpots);
timeSinceStart += (isMovingUp) ? Time.deltaTime : -1 * Time.deltaTime;
}
yield return LateSetSorting((isMovingUp) ? 2 : startingLevel, isMovingUp);
isInProgress = false;
}
public override string ToString() {
return cardValue + " of " + cardSuit + "s";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.