Skip to content
This repository was archived by the owner on Sep 17, 2022. It is now read-only.

Commit 80ad12c

Browse files
Queue animator implemented.
1 parent 06619d0 commit 80ad12c

File tree

4 files changed

+234
-63
lines changed

4 files changed

+234
-63
lines changed

Assets/Animations.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

Assets/Materials.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

Assets/Scripts/ARgorithm/Animations/QueueAnimator.cs

Lines changed: 191 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,220 @@
55

66
using ARgorithm.Structure.Typing;
77

8+
interface IArrow
9+
{
10+
ContentType faceValue
11+
{
12+
get; set;
13+
}
14+
15+
Vector3 position
16+
{
17+
get; set;
18+
}
19+
20+
Vector3 scale
21+
{
22+
get; set;
23+
}
24+
25+
Quaternion rotation
26+
{
27+
get; set;
28+
}
29+
30+
GameObject arrow
31+
{
32+
get; set;
33+
}
34+
}
35+
836
namespace ARgorithm.Animations
937
{
38+
1039
public class QueueAnimator : MonoBehaviour
1140
{
12-
public void Declare(string name, List<ContentType> body, GameObject placeholder)
41+
class VariableArrow : IArrow
1342
{
43+
private ContentType _faceValue;
44+
private Vector3 _position;
45+
private Vector3 _scale;
46+
private GameObject _arrow;
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.arrow.transform.childCount; i++)
58+
{
59+
var child = this.arrow.transform.GetChild(i).gameObject;
60+
string text = _faceValue.Value.ToString();
61+
child.GetComponent<TextMeshPro>().SetText(text);
62+
}
63+
}
64+
}
65+
66+
public VariableArrow(ContentType value)
67+
{
68+
this.arrow = (GameObject)Instantiate(Resources.Load("Arrow") as GameObject);
69+
var cubeRenderer = this.arrow.GetComponent<Renderer>();
70+
cubeRenderer.material.SetColor("_Color", Color.magenta);
71+
this._scale = this.arrow.transform.localScale;
72+
this.faceValue = value;
73+
}
1474

75+
public Vector3 position
76+
{
77+
get
78+
{
79+
return _position;
80+
}
81+
set
82+
{
83+
this.arrow.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.arrow.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.arrow.transform.rotation = value;
111+
this._rotation = value;
112+
}
113+
}
114+
115+
public GameObject arrow
116+
{
117+
get
118+
{
119+
return this._arrow;
120+
}
121+
122+
set
123+
{
124+
this._arrow = value;
125+
}
126+
}
127+
}
128+
private GameObject placeholder;
129+
private LinkedList<IArrow> queueOfArrows;
130+
public void Declare(string name, List<ContentType> body, GameObject placeHolder)
131+
{
132+
this.placeholder = placeHolder;
133+
this.queueOfArrows = new LinkedList<IArrow>();
134+
if (body.Count == 0)
135+
return;
136+
var back = new VariableArrow(body[0]);
137+
back.position = placeHolder.transform.position;
138+
back.arrow.transform.SetParent(placeHolder.transform);
139+
this.queueOfArrows.AddLast(back);
140+
for (int i = 1; i < body.Count; i++)
141+
{
142+
var arrowObj = new VariableArrow(body[i]);
143+
arrowObj.arrow.transform.SetParent(placeHolder.transform);
144+
arrowObj.position = this.queueOfArrows.Last.Value.position;
145+
arrowObj.position -= new Vector3(arrowObj.scale.x * 1.25f, 0, 0);
146+
this.queueOfArrows.AddLast(arrowObj);
147+
}
15148
}
16149

17150
public void Pop()
18151
{
19-
152+
if (this.queueOfArrows.Count == 0)
153+
return;
154+
foreach (var arrow in queueOfArrows)
155+
{
156+
arrow.position += new Vector3(arrow.scale.x * 1.25f, 0, 0);
157+
}
158+
var arrowFirst = queueOfArrows.First.Value;
159+
Destroy(arrowFirst.arrow);
160+
queueOfArrows.RemoveFirst();
20161
}
21162

22163
public void Push(ContentType element)
23164
{
24-
165+
var arrow = new VariableArrow(element);
166+
if (queueOfArrows.Count == 0)
167+
{
168+
arrow.position = this.placeholder.transform.position;
169+
arrow.arrow.transform.SetParent(placeholder.transform);
170+
queueOfArrows.AddLast(arrow);
171+
return;
172+
}
173+
arrow.position = this.queueOfArrows.Last.Value.arrow.transform.position;
174+
arrow.position -= new Vector3(arrow.scale.x * 1.25f, 0, 0);
175+
arrow.arrow.transform.SetParent(placeholder.transform);
176+
queueOfArrows.AddLast(arrow);
25177
}
26178

27179
public void Front()
28180
{
29-
181+
if (this.queueOfArrows.Count == 0)
182+
return;
183+
Color targetColor = new Color(1, 1, 1, 1);
184+
Material materialToChange;
185+
materialToChange = this.queueOfArrows.First.Value.arrow.GetComponent<Renderer>().material;
186+
StartCoroutine(LerpFunctionHighlight(materialToChange, targetColor, Constants.ITER_TIMER));
30187
}
31188

32189
public void Back()
33190
{
191+
if (this.queueOfArrows.Count == 0)
192+
return;
193+
Color targetColor = new Color(1, 1, 1, 1);
194+
Material materialToChange;
195+
materialToChange = this.queueOfArrows.Last.Value.arrow.GetComponent<Renderer>().material;
196+
StartCoroutine(LerpFunctionHighlight(materialToChange, targetColor, Constants.ITER_TIMER));
197+
}
198+
199+
IEnumerator LerpFunctionHighlight(Material materialToChange, Color endValue, float duration)
200+
{
201+
float time = 0;
202+
Color startValue = materialToChange.color;
203+
204+
while (time < duration)
205+
{
206+
materialToChange.color = Color.Lerp(startValue, endValue, time / duration);
207+
208+
time += Time.deltaTime;
209+
yield return null;
210+
}
211+
materialToChange.color = endValue;
212+
213+
time = 0;
214+
while (time < duration)
215+
{
216+
materialToChange.color = Color.Lerp(endValue, startValue, time / duration);
34217

218+
time += Time.deltaTime;
219+
yield return null;
220+
}
221+
materialToChange.color = startValue;
35222
}
36223
}
37224
}

Assets/Scripts/ARgorithm/Animations/QueueTestingScript.cs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,6 @@
66
using Newtonsoft.Json.Linq;
77

88

9-
interface IArrow
10-
{
11-
ContentType faceValue
12-
{
13-
get; set;
14-
}
15-
16-
Vector3 position
17-
{
18-
get; set;
19-
}
20-
21-
Vector3 scale
22-
{
23-
get; set;
24-
}
25-
26-
Quaternion rotation
27-
{
28-
get; set;
29-
}
30-
31-
GameObject arrow
32-
{
33-
get; set;
34-
}
35-
}
369

3710
public class QueueTestingScript : MonoBehaviour
3811
{
@@ -185,42 +158,59 @@ public void frontButton()
185158
Front();
186159
}
187160

161+
public void backButton()
162+
{
163+
Back();
164+
}
165+
//Add Last and Remove First. (deque)
188166
private void QueueDeclare<T>(List<int> body, GameObject placeHolder)
189167
{
190168
this.placeHolder = placeHolder;
191169
this.queueOfArrows = new LinkedList<IArrow>();
192170
if (body.Count == 0)
193171
return;
194-
foreach(var data in body)
172+
var back = new VariableArrow<int>(new ContentType(body[0]));
173+
back.position = placeHolder.transform.position;
174+
back.arrow.transform.SetParent(placeHolder.transform);
175+
this.queueOfArrows.AddLast(back);
176+
for(int i=1; i<body.Count; i++)
195177
{
196-
var arrow = new VariableArrow<int>(new ContentType(data));
197-
queueOfArrows.AddLast(arrow);
178+
var arrowObj = new VariableArrow<int>(new ContentType(body[i]));
179+
arrowObj.arrow.transform.SetParent(placeHolder.transform);
180+
arrowObj.position = this.queueOfArrows.Last.Value.position;
181+
arrowObj.position -= new Vector3(arrowObj.scale.x * 1.25f, 0, 0);
182+
this.queueOfArrows.AddLast(arrowObj);
198183
}
199184
}
200185

201-
public void Push(int value)
186+
public void Push(int element)
202187
{
203-
var topOfStack = new VariableArrow<int>(new ContentType(value));
188+
var arrow = new VariableArrow<int>(new ContentType(element));
204189
if (queueOfArrows.Count == 0)
205190
{
206-
topOfStack.position = this.placeHolder.transform.position;
207-
topOfStack.arrow.transform.SetParent(placeHolder.transform);
208-
queueOfArrows.Enqueue(topOfStack);
191+
arrow.position = this.placeHolder.transform.position;
192+
arrow.arrow.transform.SetParent(placeHolder.transform);
193+
queueOfArrows.AddLast(arrow);
209194
return;
210195
}
211-
topOfStack.position = this.queueOfArrows.Peek().arrow.transform.position;
212-
topOfStack.position += new Vector3(topOfStack.scale.x * 1.5f, 0, 0);
213-
topOfStack.arrow.transform.SetParent(placeHolder.transform);
214-
queueOfArrows.Enqueue(topOfStack);
196+
arrow.position = this.queueOfArrows.Last.Value.arrow.transform.position;
197+
arrow.position -= new Vector3(arrow.scale.x * 1.25f, 0, 0);
198+
arrow.arrow.transform.SetParent(placeHolder.transform);
199+
queueOfArrows.AddLast(arrow);
215200
}
216201

217202
public void Pop()
218203
{
219204
if (this.queueOfArrows.Count == 0)
220205
return;
221-
var topOfStack = this.queueOfArrows.Peek();
222-
this.queueOfArrows.Dequeue();
223-
Destroy(topOfStack.arrow);
206+
foreach(var arrow in queueOfArrows)
207+
{
208+
arrow.position += new Vector3(arrow.scale.x * 1.25f, 0, 0);
209+
}
210+
var arrowFirst = queueOfArrows.First.Value;
211+
Destroy(arrowFirst.arrow);
212+
queueOfArrows.RemoveFirst();
213+
224214
}
225215

226216
public void Front()
@@ -229,7 +219,17 @@ public void Front()
229219
return;
230220
Color targetColor = new Color(1, 1, 1, 1);
231221
Material materialToChange;
232-
materialToChange = this.queueOfArrows.Peek().arrow.GetComponent<Renderer>().material;
222+
materialToChange = this.queueOfArrows.First.Value.arrow.GetComponent<Renderer>().material;
223+
StartCoroutine(LerpFunctionHighlight(materialToChange, targetColor, Constants.ITER_TIMER));
224+
}
225+
226+
public void Back()
227+
{
228+
if (this.queueOfArrows.Count == 0)
229+
return;
230+
Color targetColor = new Color(1, 1, 1, 1);
231+
Material materialToChange;
232+
materialToChange = this.queueOfArrows.Last.Value.arrow.GetComponent<Renderer>().material;
233233
StartCoroutine(LerpFunctionHighlight(materialToChange, targetColor, Constants.ITER_TIMER));
234234
}
235235

0 commit comments

Comments
 (0)