Skip to content

Commit 83cabfc

Browse files
committed
Resolve conflict with master.
1 parent 9848f79 commit 83cabfc

File tree

1 file changed

+93
-52
lines changed

1 file changed

+93
-52
lines changed

unity/Assets/Scripts/DanceManager.cs

Lines changed: 93 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEngine;
44
using System.IO;
55
using System.Linq;
6+
using UnityEngine.SceneManagement;
67

78
namespace PoseTeacher
89
{
@@ -13,6 +14,7 @@ public class DanceManager : MonoBehaviour
1314

1415
PoseGetter selfPoseInputGetter;
1516

17+
public EndScoreScreen endScoreScreen;
1618
public GameObject videoCube;
1719
public DancePerformanceScriptableObject DancePerformanceObject;
1820

@@ -30,10 +32,11 @@ public class DanceManager : MonoBehaviour
3032
private AudioClip song;
3133
private AudioSource audioSource;
3234

33-
readonly List<(float, DanceData)> goals = new List<(float,DanceData)>();
35+
List<(float, DanceData)> goals = new List<(float,DanceData)>();
3436

3537
public float songTime => audioSource?.time ?? 0;
3638

39+
bool finished = false;
3740
int currentId = 0;
3841

3942
public void Awake()
@@ -46,53 +49,43 @@ public void Awake()
4649
{
4750
Instance = this;
4851
}
52+
53+
/* For checking if calibration worked, testing only
54+
GameObject calobjs = GameObject.Instantiate(Resources.Load<GameObject>("CalibrationObjects"));
55+
calobjs.transform.Find("Player").transform.position = PersistentData.Instance.playerPosition;
56+
calobjs.transform.Find("Kinect").transform.position = PersistentData.Instance.kinectPosition;
57+
calobjs.transform.Find("Teacher").transform.position = PersistentData.Instance.teacherPositions[0];
58+
*/
4959
}
5060

51-
// Start is called before the first frame update
61+
// Start is called before the first frame update
5262
public void Start()
5363
{
54-
avatarListSelf = new List<AvatarContainer>();
55-
avatarListTeacher = new List<AvatarContainer>();
56-
avatarListSelf.Add(new AvatarContainer(avatarContainerSelf));
57-
avatarListTeacher.Add(new AvatarContainer(avatarContainerTeacher));
58-
59-
audioSource = GetComponent<AudioSource>();
60-
song = DancePerformanceObject.SongObject.SongClip;
61-
audioSource.clip = song;
62-
danceData = DancePerformanceObject.danceData.LoadDanceDataFromScriptableObject();
63-
64-
for(int i = 0; i < DancePerformanceObject.goals.Count; i++)
65-
{
66-
goals.Add((DancePerformanceObject.goalStartTimestamps[i], DancePerformanceObject.goals[i]));
67-
}
68-
69-
selfPoseInputGetter = getPoseGetter(selfPoseInputSource);
70-
71-
audioSource.Play();
72-
Debug.Log("Successfull start initialization.");
64+
Setup();
65+
RestartSong();
7366
}
7467

7568
// Update is called once per frame
7669
public void Update()
7770
{
78-
float timeOffset = audioSource.time - danceData.poses[currentId].timestamp;
7971
currentSelfPose = selfPoseInputGetter.GetNextPose();
8072
AnimateSelf(currentSelfPose);
81-
if (goals.Count > 0 && audioSource.time >= goals[0].Item1)
73+
if (!finished)
8274
{
83-
ScoringManager.Instance.StartNewGoal(goals[0].Item2.poses, 0f);
84-
goals.RemoveAt(0);
75+
float timeOffset = audioSource.time - danceData.poses[currentId].timestamp;
76+
if (goals.Count > 0 && audioSource.time >= goals[0].Item1)
77+
{
78+
ScoringManager.Instance.StartNewGoal(goals[0].Item2.poses, 0f);
79+
goals.RemoveAt(0);
80+
}
81+
AnimateTeacher(danceData.GetInterpolatedPose(currentId, out currentId, timeOffset).toPoseData());
82+
83+
if (audioSource.time >= audioSource.clip.length)
84+
{
85+
FinishSong();
86+
}
8587
}
86-
AnimateTeacher(danceData.GetInterpolatedPose(currentId, out currentId, timeOffset).toPoseData());
87-
8888

89-
if (audioSource.time > danceData.poses[danceData.poses.Count - 1].timestamp)
90-
{
91-
audioSource.Stop();
92-
List<Scores> finalScores = ScoringManager.Instance.getFinalScores();
93-
Debug.Log(finalScores);
94-
//TODO: Add final score screen
95-
}
9689
}
9790

9891
public void OnApplicationQuit()
@@ -119,34 +112,82 @@ void AnimateTeacher(PoseData recorded_data)
119112
}
120113

121114
PoseGetter getPoseGetter(InputSource src) {
122-
123-
PoseGetter poseGetter;
124-
125115
switch (src)
126116
{
127-
128117
case InputSource.KINECT:
129-
poseGetter = new KinectPoseGetter() { VideoCube = videoCube};
130-
break;
118+
return new KinectPoseGetter() { VideoCube = videoCube};
131119
case InputSource.FILE:
132-
poseGetter = new FilePoseGetter(true) { ReadDataPath = fake_file };
133-
break;
120+
return new FilePoseGetter(true) { ReadDataPath = fake_file };
134121
default:
135-
poseGetter = new FilePoseGetter(true) { ReadDataPath = fake_file };
136-
break;
122+
return new FilePoseGetter(true) { ReadDataPath = fake_file };
137123
}
138-
139-
if(poseGetter != null)
124+
}
125+
126+
void FinishSong()
127+
{
128+
finished = true;
129+
audioSource.Stop();
130+
int totalScore = ScoringManager.Instance.getFinalScores().Item1;
131+
List<Scores> finalScores = ScoringManager.Instance.getFinalScores().Item2;
132+
133+
endScoreScreen.setValues(totalScore,
134+
finalScores.Where(element => element == Scores.GREAT).Count(),
135+
finalScores.Where(element => element == Scores.GOOD).Count(),
136+
finalScores.Where(element => element == Scores.BAD).Count(),
137+
totalScore > HighScoreData.Instance.GetHighScore(DancePerformanceObject.songId));
138+
endScoreScreen.gameObject.SetActive(true);
139+
HighScoreData.Instance.UpdateHighScore(DancePerformanceObject.songId, totalScore);
140+
}
141+
142+
void Setup()
143+
{
144+
if (PersistentData.Instance != null)
140145
{
141-
Debug.Log("created posegetter: " + poseGetter);
142-
return poseGetter;
146+
DancePerformanceObject = PersistentData.Instance.performance;
143147
}
144-
else
148+
avatarListSelf = new List<AvatarContainer>();
149+
avatarListTeacher = new List<AvatarContainer>();
150+
avatarListSelf.Add(new AvatarContainer(avatarContainerSelf));
151+
avatarListTeacher.Add(new AvatarContainer(avatarContainerTeacher));
152+
153+
if (PersistentData.Instance.calibrated)
154+
{
155+
avatarContainerTeacher.transform.position = PersistentData.Instance.teacherPositions[0];
156+
avatarContainerTeacher.transform.LookAt(PersistentData.Instance.playerPosition);
157+
avatarContainerTeacher.transform.Rotate(new Vector3(-avatarContainerTeacher.transform.rotation.eulerAngles.x, 180, -avatarContainerTeacher.transform.rotation.eulerAngles.z));
158+
159+
videoCube.transform.position = PersistentData.Instance.kinectPosition + Vector3.up;
160+
videoCube.transform.LookAt(PersistentData.Instance.playerPosition);
161+
videoCube.transform.Rotate(new Vector3(-videoCube.transform.rotation.eulerAngles.x, 180, -videoCube.transform.rotation.eulerAngles.z));
162+
}
163+
164+
audioSource = GetComponent<AudioSource>();
165+
song = DancePerformanceObject.SongObject.SongClip;
166+
audioSource.clip = song;
167+
danceData = DancePerformanceObject.danceData.LoadDanceDataFromScriptableObject();
168+
169+
selfPoseInputGetter = getPoseGetter(selfPoseInputSource);
170+
171+
}
172+
173+
public void RestartSong()
174+
{
175+
endScoreScreen.gameObject.SetActive(false);
176+
177+
goals = new List<(float, DanceData)>();
178+
for (int i = 0; i < DancePerformanceObject.goals.Count; i++)
145179
{
146-
Debug.Log("Could not create posegetter.");
147-
return null;
180+
goals.Add((DancePerformanceObject.goalStartTimestamps[i], DancePerformanceObject.goals[i]));
148181
}
182+
audioSource.time = 0;
183+
currentId = 0;
184+
finished = false;
185+
audioSource.PlayDelayed(0.5f);
186+
}
149187

188+
public void QuitToMenu()
189+
{
190+
SceneManager.LoadScene("StartMenu", LoadSceneMode.Single);
150191
}
151192
}
152-
}
193+
}

0 commit comments

Comments
 (0)