-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameBehavior.cs
157 lines (141 loc) · 5.64 KB
/
GameBehavior.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
namespace FreeCell
{
/// <summary>
/// This is a global class that is used to index cards, decks, the card sprites and also tracks the deck that the mouse is currently over (for drop detection).
/// </summary>
public class GameBehavior : MonoBehaviour
{
public static GameBehavior Instance;
/// <summary>
/// This is maintained as the deck that the mouse is currently over. It is used when dropping a card or stack of cards as the target deck.
/// </summary>
public static Deck MouseOverDeck;
public static Card MouseOverCard;
/// <summary>
/// This must link to the card prefab so that they can be Instantiated at run time.
/// </summary>
public UnityEngine.Object CardPrefab;
/// <summary>
/// This must link to the Deck prefab so that they can be Instantiated at run time.
/// </summary>
public UnityEngine.Object DeckPrefab;
/// <summary>
/// This array must have 54 entries and link to each card sprite. The last one should be the card back image for when the card is flipped face down.
/// </summary>
[HideInInspector]
public Sprite[] CardSprites;
/// <summary>
/// An unused alternate set of card faces. This should have exactly 54 items. 52 card faces in 0-51. A back card face at 52 and an empty deck image at 53.
/// To use this set programatically run CardSprites = ComplexCardSprites;
/// </summary>
//public Sprite[] ComplexCardSprites;
/// <summary>
/// The set of card faces. This should have exactly 54 items. 52 card faces in 0-51. A back card face at 52 and an empty deck image at 53.
/// </summary>
/* For the purposes of Staircase Rummy, there are exactly 56 items. 54 total card faces from 0-53, where 52 and 53 are the two jokers.
* The back card face and deck image are at 54 and 55 are for the card and deck image respectively */
public Sprite[] SimpleCardSprites;
/// <summary>
/// A list of all instantiated CardBehaviors.
/// </summary>
public static List<Card> Cards = new List<Card>();
public GameBehavior()
{
Instance = this;
}
public void Awake()
{
CardSprites = SimpleCardSprites;
//LoadSprites();
}
// Untested dynamic loading of card sprites. Use at your own risks or use different lists of card faces as shown above.
//void LoadSprites()
//{
// var loadSimple = true;
// for (var suit = 1; suit <= 4; suit++)
// {
// var suitName = ((CardSuit)suit).ToString().ToLower();
// for (var rank = 1; rank <= 13; rank++)
// {
// var rankName = rank.ToString();
// switch (rank)
// {
// case 1:
// rankName = "ace";
// break;
// case 11:
// rankName = "jack";
// break;
// case 12:
// rankName = "queen";
// break;
// case 13:
// rankName = "king";
// break;
// }
// var fileName = String.Format("{0}_of_{1}", rankName, suitName);
// CardSprites[Card.CardIndex(suit, rank)] = Resources.Load<Sprite>("Playing Cards/" + (loadSimple ? "Simple/" : "") + fileName);
// }
// }
//}
/// <summary>
/// Toggles game pausing.
/// </summary>
public void TogglePause()
{
if (IsPaused)
{
foreach (var card in GameObject.FindObjectsOfType<Card>())
{
card.GetComponent<SpriteRenderer>().enabled = true;
}
Time.timeScale = 1;
}
else
{
foreach (var card in GameObject.FindObjectsOfType<Card>())
{
card.GetComponent<SpriteRenderer>().enabled = false;
}
Time.timeScale = 0;
}
}
/// <summary>
/// Is the game paused.
/// </summary>
public static bool IsPaused { get { return Time.timeScale == 0f; } }
/// <summary>
/// Unpause the game.
/// </summary>
public static void Unpause()
{
Time.timeScale = 1;
}
/// <summary>
/// Pause the game.
/// </summary>
public static void Pause()
{
Time.timeScale = 0;
}
/// <summary>
/// Find a specific card.
/// </summary>
public static Card Find(int number, CardSuit suit)
{
return (from c in Cards where c.Number == number && c.Suit == suit select c).FirstOrDefault();
}
/// <summary>
/// Find a specific card in a specific type of deck.
/// </summary>
public static Card Find(int number, CardSuit suit, DeckType type)
{
return (from c in Cards where c.Deck.Type == type && c.Number == number && c.Suit == suit select c).FirstOrDefault();
}
}
}