77
88namespace ARgorithm . Animations
99{
10+ interface ITile
11+ {
12+ ContentType faceValue
13+ {
14+ get ; set ;
15+ }
16+
17+ Vector3 position
18+ {
19+ get ; set ;
20+ }
21+
22+ Vector3 scale
23+ {
24+ get ; set ;
25+ }
26+
27+ Quaternion rotation
28+ {
29+ get ; set ;
30+ }
31+
32+ GameObject tile
33+ {
34+ get ; set ;
35+ }
36+ }
37+
38+
1039 public class StackAnimator : MonoBehaviour
1140 {
12- public void Declare ( string name , List < ContentType > body , GameObject placeholder )
41+ class VariableTile : ITile
1342 {
43+ private ContentType _faceValue ;
44+ private Vector3 _position ;
45+ private Vector3 _scale ;
46+ private GameObject _tile ;
47+ private Quaternion _rotation ;
48+ public ContentType faceValue
49+ {
50+ get
51+ {
52+ return _faceValue ;
53+ }
54+ set
55+ {
56+ _faceValue = value ;
57+ for ( int i = 0 ; i < this . tile . transform . childCount ; i ++ )
58+ {
59+ var child = this . tile . transform . GetChild ( i ) . gameObject ;
60+ string text = _faceValue . Value . ToString ( ) ;
61+ child . GetComponent < TextMeshPro > ( ) . SetText ( text ) ;
62+ }
63+ }
64+ }
1465
66+ public VariableTile ( ContentType value )
67+ {
68+ this . tile = ( GameObject ) Instantiate ( Resources . Load ( "Tile" ) as GameObject ) ;
69+ var cubeRenderer = this . tile . GetComponent < Renderer > ( ) ;
70+ cubeRenderer . material . SetColor ( "_Color" , Color . blue ) ;
71+ this . _scale = this . tile . transform . localScale ;
72+ this . faceValue = value ;
73+ }
74+
75+ public Vector3 position
76+ {
77+ get
78+ {
79+ return _position ;
80+ }
81+ set
82+ {
83+ this . tile . transform . localPosition = value ;
84+ this . _position = value ;
85+ }
86+ }
87+
88+ public Vector3 scale
89+ {
90+ get
91+ {
92+ return _scale ;
93+ }
94+
95+ set
96+ {
97+ this . tile . transform . localScale = value ;
98+ this . _scale = value ;
99+ }
100+ }
101+
102+ public Quaternion rotation
103+ {
104+ get
105+ {
106+ return _rotation ;
107+ }
108+ set
109+ {
110+ this . tile . transform . rotation = value ;
111+ this . _rotation = value ;
112+ }
113+ }
114+
115+ public GameObject tile
116+ {
117+ get
118+ {
119+ return this . _tile ;
120+ }
121+
122+ set
123+ {
124+ this . _tile = value ;
125+ }
126+ }
127+ }
128+ private GameObject placeHolder ;
129+ private Stack < ITile > stackOfTiles ;
130+ public void Declare ( string name , List < ContentType > body , GameObject place )
131+ {
132+ this . placeHolder = place ;
133+ this . stackOfTiles = new Stack < ITile > ( ) ;
134+ if ( body . Count == 0 )
135+ return ;
136+ var bottom = new VariableTile ( body [ 0 ] ) ;
137+ bottom . position = placeHolder . transform . position ;
138+ bottom . position += new Vector3 ( 0 , bottom . scale . y * 0.5f , 0 ) ;
139+ bottom . tile . transform . SetParent ( placeHolder . transform ) ;
140+ this . stackOfTiles . Push ( bottom ) ;
141+ for ( int i = 1 ; i < body . Count ; i ++ )
142+ {
143+ var tileObj = new VariableTile ( body [ i ] ) ;
144+ tileObj . tile . transform . SetParent ( placeHolder . transform ) ;
145+ tileObj . position = bottom . position ;
146+ tileObj . rotation = placeHolder . transform . rotation ;
147+ float offset = tileObj . scale . y * 0.5f ;
148+ tileObj . position += new Vector3 ( 0 , offset + tileObj . scale . y , 0 ) ;
149+ this . stackOfTiles . Push ( tileObj ) ;
150+ bottom = tileObj ;
151+ }
15152 }
16153
17154 public void Push ( ContentType element )
18155 {
19-
156+ var topOfStack = new VariableTile ( element ) ;
157+ if ( stackOfTiles . Count == 0 )
158+ {
159+ topOfStack . position = this . placeHolder . transform . position ;
160+ topOfStack . position += new Vector3 ( 0 , topOfStack . scale . y * 0.5f , 0 ) ;
161+ topOfStack . tile . transform . SetParent ( placeHolder . transform ) ;
162+ stackOfTiles . Push ( topOfStack ) ;
163+ return ;
164+ }
165+ topOfStack . position = this . stackOfTiles . Peek ( ) . tile . transform . position ;
166+ topOfStack . position += new Vector3 ( 0 , topOfStack . scale . y * 1.5f , 0 ) ;
167+ topOfStack . tile . transform . SetParent ( placeHolder . transform ) ;
168+ stackOfTiles . Push ( topOfStack ) ;
20169 }
21170
22171 public void Pop ( )
23172 {
24-
173+ if ( this . stackOfTiles . Count == 0 )
174+ return ;
175+ var topOfStack = this . stackOfTiles . Peek ( ) ;
176+ this . stackOfTiles . Pop ( ) ;
177+ Destroy ( topOfStack . tile ) ;
25178 }
26179
27180 public void Top ( )
28181 {
29-
182+ if ( this . stackOfTiles . Count == 0 )
183+ return ;
184+ Color targetColor = new Color ( 1 , 1 , 1 , 1 ) ;
185+ Material materialToChange ;
186+ materialToChange = this . stackOfTiles . Peek ( ) . tile . GetComponent < Renderer > ( ) . material ;
187+ StartCoroutine ( LerpFunctionHighlight ( materialToChange , targetColor , Constants . ITER_TIMER ) ) ;
30188 }
31189
190+ IEnumerator LerpFunctionHighlight ( Material materialToChange , Color endValue , float duration )
191+ {
192+ float time = 0 ;
193+ Color startValue = materialToChange . color ;
194+
195+ while ( time < duration )
196+ {
197+ materialToChange . color = Color . Lerp ( startValue , endValue , time / duration ) ;
198+
199+ time += Time . deltaTime ;
200+ yield return null ;
201+ }
202+ materialToChange . color = endValue ;
203+
204+ time = 0 ;
205+ while ( time < duration )
206+ {
207+ materialToChange . color = Color . Lerp ( endValue , startValue , time / duration ) ;
208+
209+ time += Time . deltaTime ;
210+ yield return null ;
211+ }
212+ materialToChange . color = startValue ;
213+ }
32214 }
33215}
0 commit comments