-
Notifications
You must be signed in to change notification settings - Fork 2
/
DealScene.cs
106 lines (90 loc) · 2.23 KB
/
DealScene.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
using Godot;
using System;
using System.Collections.Generic;
public class DealScene : Spatial
{
ADeal _deal;
public ADeal Deal {
get { return _deal; }
set {
_deal = value;
BindDeal (_deal);
}
}
public IList<CardScene> CardScenes { get; set; }
[Export]
public float CardSpacing { get; set; } = 2.5f;
[Export]
public float CardRotation { get; set; } = 1;
void BindDeal (ADeal deal)
{
if (_deal == null) {
return;
}
Reset();
var cardSceneRes = ResourceLoader.Load<PackedScene> ("res://CardScene.tscn");
var idx = 0;
foreach (var slot in deal.CardSlots) {
var cardScene = (CardScene) cardSceneRes.Instance ();
cardScene.Card = slot.Card;
cardScene.Card.OnIsSelectInDealChanged += OnCardIsSelectedInDealChanged;
cardScene.Name = "Card" + idx;
cardScene.Translation = new Vector3 (0f, (deal.CardSlots.Count - 1) * CardSpacing / 2f - (idx * CardSpacing), 0f);
if (!deal.IsOpen) {
cardScene.Rotate (new Vector3 (0, 1, 0), Mathf.Pi);
}
cardScene.Rotate (new Vector3 (1f, 0f, 0f), Mathf.Deg2Rad (CardRotation));
AddCardScene (cardScene);
idx++;
}
}
void OnCardIsSelectedInDealChanged(object sender, EventArgs e)
{
var card = (ACard) sender;
var cardSc = GetCardSceneByCard(card);
if (cardSc != null) {
if (card.IsSelectedInDeal) {
cardSc.Translation -= new Vector3 (0.5f, 0f, 0f);
}
else {
cardSc.Translation += new Vector3 (0.5f, 0f, 0f);
}
}
}
CardScene GetCardSceneByCard (ACard card)
{
var cardIdx = Deal.GetSlotIndex(card);
if (cardIdx >= 0) {
return CardScenes[cardIdx];
}
return null;
}
void Reset ()
{
if (CardScenes != null) {
foreach (var cardScene in CardScenes) {
if (cardScene != null) {
RemoveChild (cardScene);
}
}
}
CardScenes = new List<CardScene> ();
}
void AddCardScene (CardScene cardScene)
{
AddChild(cardScene);
CardScenes.Add (cardScene);
}
public void RemoveCardScene (CardScene cardScene)
{
RemoveChild (cardScene);
var idx = CardScenes.IndexOf (cardScene);
CardScenes [idx] = null;
var card = Deal.CardSlots[idx].Card;
if (card != null) {
card.IsSelectedInDeal = false;
card.OnIsSelectInDealChanged -= OnCardIsSelectedInDealChanged;
Deal.CardSlots[idx] = null;
}
}
}