@@ -38,49 +38,54 @@ void Setup() {
3838
3939void Avatar0 () {
4040 setValueSentOnAllFaces (AVATAR);
41- setColor (PATH_COLOR);
42- setColorOnFace (AVATAR_COLOR, heading);
41+ setColor (AVATAR_COLOR);
4342 revealed = true ; // will become revealed path after adventurer leaves
4443 state = Avatar;
4544}
4645
4746void Avatar () {
48- if ( buttonSingleClicked ()) { // rotate right
49- byte prev = heading;
50- heading = (heading + 1 ) % 6 ;
51- setColorOnFace (AVATAR_COLOR, heading);
52- setColorOnFace (PATH_COLOR, prev) ;
53- }
54- if ( buttonDoubleClicked () || buttonMultiClicked ()) { // rotate left
55- byte prev = heading ;
56- heading = (heading + 5 ) % 6 ;
57- setColorOnFace (AVATAR_COLOR, heading);
58- setColorOnFace (PATH_COLOR, prev);
47+ FOREACH_FACE (f) {
48+ if (! isValueReceivedOnFaceExpired (f)) {
49+ switch ( getLastValueReceivedOnFace (f)) {
50+ case MOVE: // avatar is being pulled to neighbor revert to path
51+ state = Path0 ;
52+ break ;
53+ case RESET:
54+ state = ResetBroadcast0 ;
55+ break ;
56+ }
57+ }
5958 }
6059 if (buttonLongPressed ()) {
61- state = AvatarMoving0 ;
60+ state = ResetBroadcast0 ;
6261 }
6362}
6463
6564void AvatarMoving0 () {
66- setValueSentOnFace (MOVE, heading);
65+ setValueSentOnFace (MOVE, heading); // tell neighbor avatar is moving here
6766 state = AvatarMoving;
6867}
6968
7069void AvatarMoving () {
71- if (! isValueReceivedOnFaceExpired (heading)) {
72- switch ( getLastValueReceivedOnFace (heading) ) {
73- case AVATAR:
74- state = Path0; // avatar moved onto next path, become an empty path tile
75- break ;
76- case WALL_REVEALED:
77- state = Avatar0; // wall has been revealed, avatar is still here
78- break ;
79- case RESET: // we are only listening on one face here, but catch this in case a reset has happened somehow
80- state = ResetBroadcast0 ;
81- break ;
70+ bool doneMoving = true ;
71+ FOREACH_FACE (f ) {
72+ if (! isValueReceivedOnFaceExpired (f)) {
73+ switch ( getLastValueReceivedOnFace (f)) {
74+ case AVATAR: // wait for all neighbors to be not AVATARs
75+ doneMoving = false ;
76+ break ;
77+ case RESET:
78+ state = ResetBroadcast0;
79+ break ;
80+ }
8281 }
8382 }
83+ if (doneMoving) { // after avatar is confirmed to be here then transition to actual Avatar state
84+ state = Avatar0;
85+ }
86+ if (buttonLongPressed ()) {
87+ state = ResetBroadcast0;
88+ }
8489}
8590
8691void Path0 () {
@@ -90,18 +95,31 @@ void Path0() {
9095}
9196
9297void Path () {
98+ if (buttonSingleClicked ()) {
99+ FOREACH_FACE (f) { // check if avatar is on neighbor
100+ if (!isValueReceivedOnFaceExpired (f)) {
101+ switch (getLastValueReceivedOnFace (f)) {
102+ case AVATAR: // reveal and pull avatar
103+ revealed = true ;
104+ heading = f;
105+ state = AvatarMoving0;
106+ break ;
107+ }
108+ }
109+ }
110+ }
93111 FOREACH_FACE (f) {
94112 if (!isValueReceivedOnFaceExpired (f)) {
95113 switch (getLastValueReceivedOnFace (f)) {
96- case MOVE:
97- state = Avatar0;
98- break ;
99114 case RESET:
100115 state = ResetBroadcast0;
101116 break ;
102117 }
103118 }
104119 }
120+ if (buttonLongPressed ()) {
121+ state = ResetBroadcast0;
122+ }
105123}
106124
107125void Wall0 () {
@@ -111,13 +129,22 @@ void Wall0() {
111129}
112130
113131void Wall () {
132+ if (buttonSingleClicked ()) {
133+ FOREACH_FACE (f) { // check if avatar is on neighbor
134+ if (!isValueReceivedOnFaceExpired (f)) {
135+ switch (getLastValueReceivedOnFace (f)) {
136+ case AVATAR: // reveal and don't pull avatar
137+ revealed = true ;
138+ heading = f;
139+ state = Wall0;
140+ break ;
141+ }
142+ }
143+ }
144+ }
114145 FOREACH_FACE (f) {
115146 if (!isValueReceivedOnFaceExpired (f)) {
116147 switch (getLastValueReceivedOnFace (f)) {
117- case MOVE:
118- revealed = true ;
119- state = Wall0;
120- break ;
121148 case RESET:
122149 state = ResetBroadcast0;
123150 break ;
@@ -155,6 +182,9 @@ void ResetIgnore() {
155182 if (timer.isExpired ()) {
156183 state = Reset0;
157184 }
185+ if (buttonLongPressed ()) {
186+ state = ResetBroadcast0;
187+ }
158188}
159189
160190
@@ -168,6 +198,9 @@ void Reset() {
168198 if (timer.isExpired ()) {
169199 state = Setup;
170200 }
201+ if (buttonLongPressed ()) {
202+ state = ResetBroadcast0;
203+ }
171204}
172205
173206void setup () {
0 commit comments