Commit d6d82d3b authored by Bošániová Monika's avatar Bošániová Monika
Browse files

autorotation working

parent 4a1b596a
......@@ -447,6 +447,8 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
moveList: []
rubicsCube: {fileID: 22452980}
cubeMap: {fileID: 981341155}
--- !u!114 &22452987
MonoBehaviour:
m_ObjectHideFlags: 0
......
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
......@@ -6,11 +7,19 @@ using UnityEngine;
public class Automate : MonoBehaviour
{
public List<string> moveList = new List<string>() { };
public GameObject rubicsCube;
public GameObject cubeMap;
//static List<string> undoMoveList = new List<string>() { };
public static int actualPosition = 0;
public static bool shuffleDone = false;
bool AutomatePlaying = false;
CubeMapMatrix cubeMap;
//CubeMapMatrix cubeMap;
Colors side = Colors.Blue;
private GameObject movingParts;
private Vector3 targetVector;
private float progressOfRotation;
private float speed = 300f;
private readonly List<string> allMoves = new List<string>()
{ "U", "D", "L", "R", "F", "B",
......@@ -25,21 +34,49 @@ public class Automate : MonoBehaviour
void Start()
{
cubeState = FindObjectOfType<CubeState>();
cubeMap = CubeMapMatrix.CubeMatrix;
//cubeMap = CubeMapMatrix.CubeMatrix;
CubeState.started = true;
//readCube = FindObjectOfType<ReadCube>();
movingParts = new GameObject();
//readCube = FindObjectOfType<ReadCube>();
////update 2D Map
//CubeMapMatrix.createMatrixAtStart();
//CubeMap cubeMap = FindObjectOfType<CubeMap>();
//cubeMap.Set();
}
////update 2D Map
//CubeMapMatrix.createMatrixAtStart();
//CubeMap cubeMap = FindObjectOfType<CubeMap>();
//cubeMap.Set();
}
// Update is called once per frame
void Update()
{
if (moveList.Count > 0 && !CubeState.autoRotating && CubeState.started && (shuffleDone || AutomatePlaying) && actualPosition < moveList.Count)
if (AutomatePlaying)
{
movingParts.transform.RotateAround(Vector3.zero, targetVector, 9f);
progressOfRotation += 10;
print(progressOfRotation + " : " + Time.deltaTime);
if(progressOfRotation == 100)
{
foreach (Transform child in movingParts.transform)
{
child.position = new Vector3(Mathf.Round(child.position.x), Mathf.Round(child.position.y), Mathf.Round(child.position.z));
}
AutomatePlaying = false;
Rotator.Update2DMap(movingParts, cubeMap);
var childsToEdit = new List<Transform>();
foreach (Transform child in movingParts.transform)
childsToEdit.Add(child);
foreach (Transform child in childsToEdit)
child.SetParent(rubicsCube.transform);
}
}
if (moveList.Count > 0 && !AutomatePlaying && CubeState.started && (shuffleDone) && actualPosition < moveList.Count)
{
print("1");
//do the move at the first index;
DoMove(moveList[actualPosition]);
actualPosition += 1;
......@@ -50,6 +87,8 @@ public class Automate : MonoBehaviour
if (shuffleDone && actualPosition == moveList.Count)
{
print("2");
shuffleDone = false;
actualPosition = 0;
moveList.Clear();
......@@ -100,10 +139,10 @@ public class Automate : MonoBehaviour
AutomatePlaying = false;
//dopisat aby sa negenerovali opacne
List<string> moves = new List<string>();
int shuffleLength = Random.Range(10, 30);
int shuffleLength = UnityEngine.Random.Range(10, 30);
for (int i = 0; i < shuffleLength; i++)
{
int randomMove = Random.Range(0, allMoves.Count);
int randomMove = UnityEngine.Random.Range(0, allMoves.Count);
moves.Add(allMoves[randomMove]);
}
moveList = moves;
......@@ -118,89 +157,192 @@ public class Automate : MonoBehaviour
actualPosition = 0;
//moveList = new List<string>();
//moveList = new List<string>() { "F'", "L2", "D'", "B", "U'", "F", "U", "L'", "F2", "U'", "R2", "L'", "D2", "D2", "R2", "R", "B'", "L", "F2", "F", "D2", "L'", "U", "R2", "R'", "L'", "B2", "L", "U'", "R'", "R2" };
//moveList = new List<string>() { "F'"};
//moveList = new List<string>() { "F'","L2","D'","B","U'","F","U","L'","F2", "U'", "R2", "L'", "D2", "D2", "R2", "R", "B'", "L", "F2", "F", "D2", "L'", "U", "R2", "R'", "L'", "B2", "L", "U'","R'","R2"};
}
void DoMove(string move)
{
//readCube.ReadState();
CubeState.autoRotating = true;
//cubeMap.callRotation(move);
if (move == "U")
{
RotateSide(cubeState.up, -90);
}
if(move == "U'")
{
RotateSide(cubeState.up, 90);
}
if(move == "U2")
{
RotateSide(cubeState.up, -180);
}
if (move == "D")
{
RotateSide(cubeState.down, -90);
}
if (move == "D'")
{
RotateSide(cubeState.down, 90);
}
if (move == "D2")
{
RotateSide(cubeState.down, -180);
}
if (move == "L")
{
RotateSide(cubeState.left, -90);
}
if (move == "L'")
{
RotateSide(cubeState.left, 90);
}
if (move == "L2")
{
RotateSide(cubeState.left, -180);
}
if (move == "R")
{
RotateSide(cubeState.right, -90);
}
if (move == "R'")
{
RotateSide(cubeState.right, 90);
}
if (move == "R2")
{
RotateSide(cubeState.right, -180);
}
if (move == "F")
{
RotateSide(cubeState.front, -90);
}
if (move == "F'")
{
RotateSide(cubeState.front, 90);
}
if (move == "F2")
{
RotateSide(cubeState.front, -180);
}
if (move == "B")
//CubeState.autoRotating = true;
////cubeMap.callRotation(move);
//if (move == "U")
//{
// RotateSide(cubeState.up, -90);
//}
//if(move == "U'")
//{
// RotateSide(cubeState.up, 90);
//}
//if(move == "U2")
//{
// RotateSide(cubeState.up, -180);
//}
//if (move == "D")
//{
// RotateSide(cubeState.down, -90);
//}
//if (move == "D'")
//{
// RotateSide(cubeState.down, 90);
//}
//if (move == "D2")
//{
// RotateSide(cubeState.down, -180);
//}
//if (move == "L")
//{
// RotateSide(cubeState.left, -90);
//}
//if (move == "L'")
//{
// RotateSide(cubeState.left, 90);
//}
//if (move == "L2")
//{
// RotateSide(cubeState.left, -180);
//}
//if (move == "R")
//{
// RotateSide(cubeState.right, -90);
//}
//if (move == "R'")
//{
// RotateSide(cubeState.right, 90);
//}
//if (move == "R2")
//{
// RotateSide(cubeState.right, -180);
//}
//if (move == "F")
//{
// RotateSide(cubeState.front, -90);
//}
//if (move == "F'")
//{
// RotateSide(cubeState.front, 90);
//}
//if (move == "F2")
//{
// RotateSide(cubeState.front, -180);
//}
//if (move == "B")
//{
// RotateSide(cubeState.back, -90);
//}
//if (move == "B'")
//{
// RotateSide(cubeState.back, 90);
//}
//if (move == "B2")
//{
// RotateSide(cubeState.back, -180);
//}
int angle = 0;
switch (move)
{
RotateSide(cubeState.back, -90);
}
if (move == "B'")
{
RotateSide(cubeState.back, 90);
case "R":
side = Colors.Orange;
angle = -90;
break;
case "R'":
side = Colors.Orange;
angle = 90;
break;
case "R2":
side = Colors.Orange;
angle = -180;
break;
case "L":
side = Colors.Red;
angle = -90;
break;
case "L'":
side = Colors.Red;
angle = 90;
break;
case "L2":
side = Colors.Red;
angle = -180;
break;
case "F":
side = Colors.Blue;
angle = -90;
break;
case "F'":
side = Colors.Blue;
angle = 90;
break;
case "F2":
side = Colors.Blue;
angle = -180;
break;
case "B":
side = Colors.Green;
angle = -90;
break;
case "B'":
side = Colors.Green;
angle = 90;
break;
case "B2":
side = Colors.Green;
angle = -180;
break;
case "U":
side = Colors.White;
angle = -90;
break;
case "U'":
side = Colors.White;
angle = 90;
break;
case "U2":
side = Colors.White;
angle = -180;
break;
case "D":
side = Colors.Yellow;
angle = -90;
break;
case "D'":
side = Colors.Yellow;
angle = 90;
break;
case "D2":
side = Colors.Yellow;
angle = -180;
break;
default: break;
}
if (move == "B2")
List<Transform> childsToEdit = new List<Transform>();
float epsilon = 0.01f;
foreach (Transform child in rubicsCube.transform)
{
RotateSide(cubeState.back, -180);
switch (side)
{
case Colors.Orange: if (Math.Abs(child.position.x - 1) < epsilon) childsToEdit.Add(child); break;
case Colors.Red: if (Math.Abs(child.position.x + 1) < epsilon) childsToEdit.Add(child); break;
case Colors.White: if (Math.Abs(child.position.y - 1) < epsilon) childsToEdit.Add(child); break;
case Colors.Yellow: if (Math.Abs(child.position.y + 1) < epsilon) childsToEdit.Add(child); break;
case Colors.Green: if (Math.Abs(child.position.z - 1) < epsilon) childsToEdit.Add(child); break;
case Colors.Blue: if (Math.Abs(child.position.z + 1) < epsilon) childsToEdit.Add(child); break;
default: print("something wrong"); break;
}
}
// set parent back
foreach (Transform child in childsToEdit)
child.SetParent(movingParts.transform);
print(movingParts.transform.childCount + " hehe");
RotateSide(angle);
}
void DoOppositeMove(string moveToMakeOpposite)
......@@ -267,10 +409,41 @@ public class Automate : MonoBehaviour
}
void RotateSide(List<GameObject> side, float angle)
void RotateSide(float angle)
{
//automatically rotate the side by the angle
PivotRotation pr = side[4].transform.parent.GetComponent<PivotRotation>();
//pr.StartAutoRotate(side, angle);
//PivotRotation pr = centre.transform.parent.GetComponent<PivotRotation>();
//pr.StartAutoRotate(centre, angle);
//Vector3 localForward = Vector3.zero - movingParts.transform.localPosition;
//targetQuaternion = Quaternion.AngleAxis(angle, localForward) * movingParts.transform.localRotation;
Vector3 axis = new Vector3(0, 0, 0);
switch (side)
{
case Colors.Orange: axis.x = 1; break;
case Colors.Red: axis.x = -1; break;
case Colors.White: axis.y = 1; break;
case Colors.Yellow: axis.y = -1; break;
case Colors.Green: axis.z = 1; break;
case Colors.Blue: axis.z = -1; break;
}
//var step = speed * Time.deltaTime;
//movingParts.transform.localRotation = Quaternion.RotateTowards(movingParts.transform.localRotation, Quaternion.LookRotation(axis, Vector3.up), 90);
//activeSide = side;
//autoRotating = true;
//movingParts.transform.RotateAround(Vector3.zero, axis, 90);
//foreach (Transform child in movingParts.transform)
//{
// child.position = new Vector3(Mathf.Round(child.position.x), Mathf.Round(child.position.y), Mathf.Round(child.position.z));
//}
AutomatePlaying = true;
progressOfRotation = 0;
targetVector = axis;
}
}
......@@ -96,12 +96,12 @@ public class PivotRotation : MonoBehaviour
localForward = Vector3.zero - side[4].transform.parent.transform.localPosition;
}
public void StartAutoRotate(List<GameObject> side, float angle)
public void StartAutoRotate(GameObject centre, float angle)
{
cubeState.PickUp(side);
Vector3 localForward = Vector3.zero - side[4].transform.parent.transform.localPosition;
//cubeState.PickUp(side);
Vector3 localForward = Vector3.zero - centre.transform.parent.transform.localPosition;
targetQuaternion = Quaternion.AngleAxis(angle, localForward) * transform.localRotation;
activeSide = side;
//activeSide = side;
autoRotating = true;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment