-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCubeView.cs
196 lines (173 loc) · 5.66 KB
/
CubeView.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
public class CubeView : RubiksView {
//CubeRotation
private GameObject[] sectionGroup; // Container for the cubes belonging to the face to rotate
public GameObject sectionPivot; // Pivot objects
private CubeSection[] sections; // Reference to the objects to be rotated
private int i = 0; // Counter used for assigning cubes to the face group
private float rotationAngle;
public static bool isRotating = true;
private Vector3 rotationDirection;
private Vector3 pivotPos;
private float timer;
private void Awake()
{
sectionPivot = GameObject.FindWithTag("Pivot");
}
void Start()
{
i = 0;
sectionGroup = new GameObject[9];
sectionPivot.transform.parent = sectionPivot.transform;
}
private void Update()
{
if (isRotating)
{
CubeSelectionRotation(rotationDirection);
}
}
public void RotateSection(Vector3 dir, bool clockWise, bool center)
{
Vector3 findalDir = dir;
timer = Time.time;
i = 0;
sectionPivot.transform.rotation = Quaternion.identity;
if (center)
{
GatherCenterSectionArray(dir);
}
else
{
GatherFaceSectionArray(dir);
}
if(clockWise)
{
findalDir *= -1;
}
rotationDirection = findalDir;
isRotating = true;
}
/// <summary>
/// Parents all the cubes int the cube section determined by dir.
/// </summary>
/// <param name="dir"></param>
private void GatherCenterSectionArray(Vector3 dir)
{
foreach (GameObject g in app.model.cubes)
{
if(dir == Vector3.right)
{
if (g.transform.position.x == 0)
{
g.transform.parent = sectionPivot.transform;
i++;
}
}
if (dir == Vector3.up)
{
if (g.transform.position.y == 0)
{
g.transform.parent = sectionPivot.transform;
i++;
}
}
if (dir == Vector3.forward)
{
if (g.transform.position.z == 0)
{
g.transform.parent = sectionPivot.transform;
i++;
}
}
}
}
/// <summary>
/// Parents all the cubes int the cube section determined by dir.
/// </summary>
/// <param name="dir"></param>
private void GatherFaceSectionArray(Vector3 dir)
{
Vector3 dirMask = new Vector3(Mathf.Abs(dir.x), Mathf.Abs(dir.y), Mathf.Abs(dir.z));
foreach (GameObject g in app.model.cubes)
{
Vector3 maskedPosition = Vector3.Scale(dirMask,g.transform.position);
if (dir == maskedPosition)
{
g.transform.SetParent(sectionPivot.transform);
i++;
}
}
if (i < 9)
{
foreach (GameObject g in app.model.cubes)
{
g.transform.SetParent(app.model.rubiksCube.transform);
g.transform.position = new Vector3(Mathf.Round(g.transform.position.x), Mathf.Round(g.transform.position.y), Mathf.Round(g.transform.position.z));
}
isRotating = false;
}
}
/// <summary>
/// The actual rotation of the face
/// </summary>
/// <param name="dir"></param>
void CubeSelectionRotation( Vector3 dir)
{
float t = Time.time - timer;
sectionPivot.transform.rotation = Quaternion.Slerp( Quaternion.identity, Quaternion.Euler(dir * 90), t * 3);
isRotating = true;
if (Quaternion.Angle(sectionPivot.transform.rotation, Quaternion.Euler(dir * 90)) < 1f )
{
sectionPivot.transform.rotation = Quaternion.Euler(dir * 90);
foreach(GameObject g in app.model.cubes)
{
g.transform.SetParent(app.model.rubiksCube.transform);
g.transform.position = new Vector3(Mathf.Round(g.transform.position.x), Mathf.Round(g.transform.position.y), Mathf.Round(g.transform.position.z));
}
isRotating = false;
}
}
}
//TODO Remove this if it's not nessisary
// Dont think we will be needing this one here;
// |
// V
/// <summary>
/// Used to select a ever changing cube groups.
/// </summary>
public struct CubeSection
{
public CubeSection(Vector3 _direction, string _name)
{
this.positionMask = this.direction = _direction;
name = _name;
}
public CubeSection(CenterRow row, string _name)
{
switch (row)
{
case CenterRow.Standing:
direction = Vector3.forward;
positionMask = new Vector3(1,1,0);
break;
case CenterRow.Mid:
direction = Vector3.right;
positionMask = new Vector3(0,1,1);
break;
case CenterRow.Equator:
direction = Vector3.up;
positionMask = new Vector3(1,0,1);
break;
default:
positionMask = direction = Vector3.zero;
break;
}
name =_name;
}
public string name;
public Vector3 direction;
public Vector3 positionMask;
}