3
3
using UnityEngine ;
4
4
using System . IO ;
5
5
using System . Linq ;
6
+ using UnityEngine . SceneManagement ;
6
7
7
8
namespace PoseTeacher
8
9
{
@@ -13,6 +14,7 @@ public class DanceManager : MonoBehaviour
13
14
14
15
PoseGetter selfPoseInputGetter ;
15
16
17
+ public EndScoreScreen endScoreScreen ;
16
18
public GameObject videoCube ;
17
19
public DancePerformanceScriptableObject DancePerformanceObject ;
18
20
@@ -30,10 +32,11 @@ public class DanceManager : MonoBehaviour
30
32
private AudioClip song ;
31
33
private AudioSource audioSource ;
32
34
33
- readonly List < ( float , DanceData ) > goals = new List < ( float , DanceData ) > ( ) ;
35
+ List < ( float , DanceData ) > goals = new List < ( float , DanceData ) > ( ) ;
34
36
35
37
public float songTime => audioSource ? . time ?? 0 ;
36
38
39
+ bool finished = false ;
37
40
int currentId = 0 ;
38
41
39
42
public void Awake ( )
@@ -46,53 +49,43 @@ public void Awake()
46
49
{
47
50
Instance = this ;
48
51
}
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
+ */
49
59
}
50
60
51
- // Start is called before the first frame update
61
+ // Start is called before the first frame update
52
62
public void Start ( )
53
63
{
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 ( ) ;
73
66
}
74
67
75
68
// Update is called once per frame
76
69
public void Update ( )
77
70
{
78
- float timeOffset = audioSource . time - danceData . poses [ currentId ] . timestamp ;
79
71
currentSelfPose = selfPoseInputGetter . GetNextPose ( ) ;
80
72
AnimateSelf ( currentSelfPose ) ;
81
- if ( goals . Count > 0 && audioSource . time >= goals [ 0 ] . Item1 )
73
+ if ( ! finished )
82
74
{
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
+ }
85
87
}
86
- AnimateTeacher ( danceData . GetInterpolatedPose ( currentId , out currentId , timeOffset ) . toPoseData ( ) ) ;
87
-
88
88
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
- }
96
89
}
97
90
98
91
public void OnApplicationQuit ( )
@@ -119,34 +112,82 @@ void AnimateTeacher(PoseData recorded_data)
119
112
}
120
113
121
114
PoseGetter getPoseGetter ( InputSource src ) {
122
-
123
- PoseGetter poseGetter ;
124
-
125
115
switch ( src )
126
116
{
127
-
128
117
case InputSource . KINECT :
129
- poseGetter = new KinectPoseGetter ( ) { VideoCube = videoCube } ;
130
- break ;
118
+ return new KinectPoseGetter ( ) { VideoCube = videoCube } ;
131
119
case InputSource . FILE :
132
- poseGetter = new FilePoseGetter ( true ) { ReadDataPath = fake_file } ;
133
- break ;
120
+ return new FilePoseGetter ( true ) { ReadDataPath = fake_file } ;
134
121
default :
135
- poseGetter = new FilePoseGetter ( true ) { ReadDataPath = fake_file } ;
136
- break ;
122
+ return new FilePoseGetter ( true ) { ReadDataPath = fake_file } ;
137
123
}
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 )
140
145
{
141
- Debug . Log ( "created posegetter: " + poseGetter ) ;
142
- return poseGetter ;
146
+ DancePerformanceObject = PersistentData . Instance . performance ;
143
147
}
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 ++ )
145
179
{
146
- Debug . Log ( "Could not create posegetter." ) ;
147
- return null ;
180
+ goals . Add ( ( DancePerformanceObject . goalStartTimestamps [ i ] , DancePerformanceObject . goals [ i ] ) ) ;
148
181
}
182
+ audioSource . time = 0 ;
183
+ currentId = 0 ;
184
+ finished = false ;
185
+ audioSource . PlayDelayed ( 0.5f ) ;
186
+ }
149
187
188
+ public void QuitToMenu ( )
189
+ {
190
+ SceneManager . LoadScene ( "StartMenu" , LoadSceneMode . Single ) ;
150
191
}
151
192
}
152
- }
193
+ }
0 commit comments