1
+ /*
2
+ * This script controls the UI Canvas which facilitates the player's selection
3
+ * of a particular sandwich ingredient. When the player presses a button corresponding
4
+ * to an ingredient category (bread, meat, cheese, veggies, or sauce), a panel
5
+ * will pop up with the various options of specific items in that category.
6
+ *
7
+ * The player can navigate this menu with the Adaptive Controller's D-pad, and
8
+ * their current selected ingredient is highlighted. They can press the plate button
9
+ * to close the menu and add that ingredient to the sandwich, or they can press any
10
+ * other button to close the panel and complete a different action.
11
+ */
12
+
13
+ using UnityEngine ;
14
+ using UnityEngine . UI ;
15
+ using static IngredientManager ;
16
+
17
+ public class IngredientOptionUIController : Singleton < IngredientOptionUIController >
18
+ {
19
+ private Ingredient [ ] [ ] currentGridLayout ;
20
+ private int currentRow = 0 ;
21
+ private int currentColumn = 0 ;
22
+ private Animator animator ;
23
+ [ SerializeField ] public GameObject WhiteBreadTransform ;
24
+
25
+ private PlayerInputManager adaptiveControllerInputManager ;
26
+ private PlayerInputManager debugInputManager ;
27
+
28
+ [ SerializeField ] private GameObject ingredientRowPrefab ;
29
+ [ SerializeField ] private GameObject ingredientImagePrefab ;
30
+
31
+ // Ingredient Panel Parent Objects
32
+ private Transform currentIngredientPanel ;
33
+ [ SerializeField ] private Transform breadPanel ;
34
+ [ SerializeField ] private Transform meatPanel ;
35
+ [ SerializeField ] private Transform cheesePanel ;
36
+ [ SerializeField ] private Transform veggiePanel ;
37
+ [ SerializeField ] private Transform saucePanel ;
38
+
39
+ #region IngredientSprites
40
+ [ Header ( "Ingredient Sprites" ) ]
41
+ [ SerializeField ] private Sprite whiteBreadSprite ;
42
+ [ SerializeField ] private Sprite wholeWheatBreadSprite ;
43
+ [ SerializeField ] private Sprite pattySprite ;
44
+ [ SerializeField ] private Sprite baconSprite ;
45
+ [ SerializeField ] private Sprite cheddarSprite ;
46
+ [ SerializeField ] private Sprite swissSprite ;
47
+ [ SerializeField ] private Sprite lettuceSprite ;
48
+ [ SerializeField ] private Sprite tomatoSprite ;
49
+ [ SerializeField ] private Sprite onionSprite ;
50
+ [ SerializeField ] private Sprite mushroomSprite ;
51
+ [ SerializeField ] private Sprite ketchupSprite ;
52
+ [ SerializeField ] private Sprite mustardSprite ;
53
+ [ SerializeField ] private Sprite mayoSprite ;
54
+ #endregion
55
+
56
+ #region Ingredient Layouts
57
+ private Ingredient [ ] [ ] breadLayout = new Ingredient [ 2 ] [ ]
58
+ {
59
+ new Ingredient [ 1 ] { Ingredient . WhiteBread } ,
60
+ new Ingredient [ 1 ] { Ingredient . WholeWheatBread }
61
+ } ;
62
+
63
+ private Ingredient [ ] [ ] meatLayout = new Ingredient [ 1 ] [ ]
64
+ {
65
+ new Ingredient [ 2 ] { Ingredient . Patty , Ingredient . Bacon } ,
66
+ } ;
67
+
68
+ private Ingredient [ ] [ ] cheeseLayout = new Ingredient [ 1 ] [ ]
69
+ {
70
+ new Ingredient [ 2 ] { Ingredient . Cheddar , Ingredient . Swiss } ,
71
+ } ;
72
+
73
+ private Ingredient [ ] [ ] veggieLayout = new Ingredient [ 2 ] [ ]
74
+ {
75
+ new Ingredient [ 2 ] { Ingredient . Onion , Ingredient . Mushroom } ,
76
+ new Ingredient [ 2 ] { Ingredient . Tomato , Ingredient . Lettuce }
77
+ } ;
78
+
79
+ private Ingredient [ ] [ ] sauceLayout = new Ingredient [ 1 ] [ ]
80
+ {
81
+ new Ingredient [ 3 ] { Ingredient . Ketchup , Ingredient . Mayo , Ingredient . Mustard } ,
82
+ } ;
83
+ #endregion
84
+
85
+ // Start is called before the first frame update
86
+ void Start ( )
87
+ {
88
+ adaptiveControllerInputManager = GameObject . Find ( "Input_AdaptiveController" ) . GetComponent < PlayerInputManager > ( ) ;
89
+ debugInputManager = GameObject . Find ( "Input_Debug" ) . GetComponent < PlayerInputManager > ( ) ;
90
+
91
+ // Find all the ingredient panels in children of this object
92
+ foreach ( Transform child in transform )
93
+ {
94
+ switch ( child . name )
95
+ {
96
+ case "BreadPanel" :
97
+ breadPanel = child ;
98
+ break ;
99
+ case "MeatPanel" :
100
+ meatPanel = child ;
101
+ break ;
102
+ case "CheesePanel" :
103
+ cheesePanel = child ;
104
+ break ;
105
+ case "VeggiePanel" :
106
+ veggiePanel = child ;
107
+ break ;
108
+ case "SaucePanel" :
109
+ saucePanel = child ;
110
+ break ;
111
+ default :
112
+ break ;
113
+ }
114
+ }
115
+ animator = GetComponent < Animator > ( ) ;
116
+
117
+ ClearUI ( ) ;
118
+ }
119
+
120
+ // Note: This will only be updating ingredients within the same selected ingredient category
121
+ private void UpdateInputManagerSelectedObject ( Ingredient ingredient )
122
+ {
123
+ if ( adaptiveControllerInputManager ) { adaptiveControllerInputManager . selectedIngredient = ingredient ; }
124
+ if ( debugInputManager ) { debugInputManager . selectedIngredient = ingredient ; }
125
+ }
126
+
127
+ // Figure out the specific sprite you want for each ingredient
128
+ private Sprite GetSpriteForIngredient ( Ingredient ingredient )
129
+ {
130
+ return ingredient switch
131
+ {
132
+ Ingredient . WhiteBread => whiteBreadSprite ,
133
+ Ingredient . WholeWheatBread => wholeWheatBreadSprite ,
134
+ Ingredient . Patty => pattySprite ,
135
+ Ingredient . Bacon => baconSprite ,
136
+ Ingredient . Cheddar => cheddarSprite ,
137
+ Ingredient . Swiss => swissSprite ,
138
+ Ingredient . Lettuce => lettuceSprite ,
139
+ Ingredient . Tomato => tomatoSprite ,
140
+ Ingredient . Onion => onionSprite ,
141
+ Ingredient . Mushroom => mushroomSprite ,
142
+ Ingredient . Ketchup => ketchupSprite ,
143
+ Ingredient . Mustard => mustardSprite ,
144
+ Ingredient . Mayo => mayoSprite ,
145
+ _ => null
146
+ } ;
147
+ }
148
+
149
+ // Get the appropriate panel for the ingredient category
150
+ private Transform GetIngredientPanel ( IngredientCategory ingredientCategory )
151
+ {
152
+ return ingredientCategory switch
153
+ {
154
+ IngredientCategory . Bread => breadPanel ,
155
+ IngredientCategory . Meat => meatPanel ,
156
+ IngredientCategory . Cheese => cheesePanel ,
157
+ IngredientCategory . Veggie => veggiePanel ,
158
+ IngredientCategory . Sauce => saucePanel ,
159
+ _ => null
160
+ } ;
161
+ }
162
+
163
+ // Note: This function is called only when switching to a new ingredient category
164
+ public void SetUpIngredientGridLayout ( IngredientCategory ingredientCategory )
165
+ {
166
+ ClearUI ( ) ;
167
+
168
+ currentGridLayout = ingredientCategory switch
169
+ {
170
+ IngredientCategory . Bread => breadLayout ,
171
+ IngredientCategory . Meat => meatLayout ,
172
+ IngredientCategory . Cheese => cheeseLayout ,
173
+ IngredientCategory . Veggie => veggieLayout ,
174
+ IngredientCategory . Sauce => sauceLayout ,
175
+ _ => null
176
+ } ;
177
+
178
+ // Sanity check: the current grid layout should never be null here
179
+ if ( currentGridLayout == null ) { return ; }
180
+
181
+ Ingredient defaultIngredient = currentGridLayout [ currentRow ] [ currentColumn ] ;
182
+ UpdateInputManagerSelectedObject ( defaultIngredient ) ;
183
+
184
+ // draw the appropriate grid UI
185
+ currentIngredientPanel = GetIngredientPanel ( ingredientCategory ) ;
186
+ foreach ( Ingredient [ ] row in currentGridLayout )
187
+ {
188
+ GameObject rowObject = Instantiate ( ingredientRowPrefab , currentIngredientPanel ) ;
189
+ foreach ( Ingredient ingredient in row )
190
+ {
191
+ GameObject ingredientObject = Instantiate ( ingredientImagePrefab , rowObject . transform ) ;
192
+ ingredientObject . transform . GetChild ( 0 ) . gameObject . GetComponent < Image > ( ) . enabled = false ;
193
+ ingredientObject . transform . GetChild ( 1 ) . gameObject . GetComponent < Image > ( ) . sprite = GetSpriteForIngredient ( ingredient ) ;
194
+ }
195
+ }
196
+
197
+ HighlightSelectedIngredient ( currentRow , currentColumn ) ;
198
+ AnimationManager . Instance . SelectedIngredientAnimation ( defaultIngredient ) ;
199
+ }
200
+
201
+ private void HighlightSelectedIngredient ( int rowIdx , int colIdx )
202
+ {
203
+ if ( currentGridLayout == null || currentIngredientPanel == null ) { return ; }
204
+
205
+ // turn off all the highlights
206
+ foreach ( Transform ingredientRow in currentIngredientPanel )
207
+ {
208
+ foreach ( Transform ingredientImage in ingredientRow )
209
+ {
210
+ ingredientImage . GetChild ( 0 ) . gameObject . GetComponent < Image > ( ) . enabled = false ;
211
+ }
212
+ }
213
+
214
+ // turn on the highlight for the selected ingredient
215
+ Transform selectedIngredientImage = currentIngredientPanel . GetChild ( rowIdx ) . GetChild ( colIdx ) ;
216
+ selectedIngredientImage . GetChild ( 0 ) . gameObject . GetComponent < Image > ( ) . enabled = true ;
217
+ }
218
+
219
+ public void SelectDifferentIngredientInGrid ( int xDir , int yDir )
220
+ {
221
+ if ( currentGridLayout == null )
222
+ {
223
+ // There is no grid UI -> input ignored
224
+ return ;
225
+ }
226
+
227
+ // navigate the grid for the current category
228
+ int newRow = currentRow + yDir ;
229
+ int newColumn = currentColumn + xDir ;
230
+ if ( newRow < 0 || newRow >= currentGridLayout . Length
231
+ || newColumn < 0 || newColumn >= currentGridLayout [ newRow ] . Length )
232
+ {
233
+ // out of bounds -> input ignored
234
+ return ;
235
+ }
236
+ else
237
+ {
238
+ Ingredient newIngredient = currentGridLayout [ newRow ] [ newColumn ] ;
239
+ UpdateInputManagerSelectedObject ( newIngredient ) ;
240
+
241
+ currentColumn = newColumn ;
242
+ currentRow = newRow ;
243
+
244
+ HighlightSelectedIngredient ( currentRow , currentColumn ) ;
245
+ AudioManager . Instance . PlayIngredientUISwitchSound ( ) ;
246
+ AnimationManager . Instance . SelectedIngredientAnimation ( newIngredient ) ;
247
+ }
248
+ }
249
+
250
+ private void DeleteChildrenOfTransform ( Transform transform )
251
+ {
252
+ foreach ( Transform child in transform )
253
+ {
254
+ Destroy ( child . gameObject ) ;
255
+ }
256
+ }
257
+
258
+
259
+ public void ClearUI ( )
260
+ {
261
+ foreach ( Transform ingredientPanel in transform )
262
+ {
263
+ DeleteChildrenOfTransform ( ingredientPanel ) ;
264
+ }
265
+
266
+ currentGridLayout = null ;
267
+ currentIngredientPanel = null ;
268
+ currentRow = 0 ;
269
+ currentColumn = 0 ;
270
+
271
+ AnimationManager . Instance . LowerAllIngredients ( ) ;
272
+ }
273
+ }
0 commit comments