@@ -22,22 +22,38 @@ public class StartBlockController : MonoBehaviour
22
22
public Session session ;
23
23
24
24
StartBlockState state = StartBlockState . Waiting ;
25
-
26
25
SpriteRenderer spriteRenderer ;
26
+ Coroutine runningSequence ;
27
+
27
28
28
29
void Awake ( )
29
30
{
30
31
spriteRenderer = GetComponent < SpriteRenderer > ( ) ;
31
32
SetState ( StartBlockState . Waiting ) ;
32
33
}
33
34
34
- public void SetState ( StartBlockState newState )
35
+
36
+ IEnumerator RunSequence ( )
37
+ {
38
+ GetReady ( ) ;
39
+
40
+ // Take the delay time (seconds) for the next trial, wait for that time
41
+ // If we move from the start block too early, StopCoroutine(runningSequence); will halt the execution of this coroutine
42
+ // System.Convert: Safely convert to single (float)
43
+ float delayTime = System . Convert . ToSingle ( session . nextTrial . settings [ "delay_time" ] ) ;
44
+ yield return new WaitForSeconds ( delayTime ) ;
45
+
46
+ Go ( ) ;
47
+ }
48
+
49
+ void SetState ( StartBlockState newState )
35
50
{
36
- StartBlockState oldState = state ;
37
51
state = newState ;
38
52
53
+ // modify colour based on state
39
54
switch ( state )
40
55
{
56
+ // could be dictionary
41
57
case StartBlockState . Waiting :
42
58
spriteRenderer . color = mainColor ;
43
59
break ;
@@ -59,13 +75,7 @@ public void ResetToNormal()
59
75
void GetReady ( )
60
76
{
61
77
Debug . Log ( "Get ready..." ) ;
62
-
63
78
SetState ( StartBlockState . GetReady ) ;
64
-
65
- // Take the delay time (seconds) for the next trial, and invoke the "Go" method after that time
66
- // safely convert to single (float)
67
- float delayTime = System . Convert . ToSingle ( session . nextTrial . settings [ "delay_time" ] ) ;
68
- Invoke ( "Go" , delayTime ) ;
69
79
}
70
80
71
81
void Go ( )
@@ -82,7 +92,8 @@ void OnMouseEnter()
82
92
switch ( state )
83
93
{
84
94
case StartBlockState . Waiting :
85
- GetReady ( ) ;
95
+ // begin the sequence
96
+ runningSequence = StartCoroutine ( RunSequence ( ) ) ;
86
97
break ;
87
98
}
88
99
}
@@ -92,7 +103,9 @@ void OnMouseExit()
92
103
switch ( state )
93
104
{
94
105
case StartBlockState . GetReady :
95
- CancelInvoke ( "Go" ) ;
106
+ // stop the sequence
107
+ Debug . Log ( "Moved too early!" ) ;
108
+ StopCoroutine ( runningSequence ) ;
96
109
ResetToNormal ( ) ;
97
110
break ;
98
111
case StartBlockState . Go :
0 commit comments