77public class UIManager : MonoBehaviour
88{
99 private BuildingPlacer _buildingPlacer ;
10+ private int _myPlayerId ;
1011
1112 public Color validTextColor ;
1213 public Color invalidTextColor ;
@@ -66,6 +67,9 @@ public class UIManager : MonoBehaviour
6667 [ Header ( "Main Menu Panel" ) ]
6768 public GameObject mainMenuPanel ;
6869
70+ [ Header ( "Misc" ) ]
71+ public Image playerIndicatorImage ;
72+
6973 private void Awake ( )
7074 {
7175 _buildingPlacer = GetComponent < BuildingPlacer > ( ) ;
@@ -95,18 +99,6 @@ private void Awake()
9599
96100 mainMenuPanel . SetActive ( false ) ;
97101
98- // create texts for each in-game resource (gold, wood, stone...)
99- _resourceTexts = new Dictionary < InGameResource , Text > ( ) ;
100- foreach ( KeyValuePair < InGameResource , GameResource > pair in Globals . GAME_RESOURCES )
101- {
102- GameObject display = Instantiate ( gameResourceDisplayPrefab ) ;
103- display . name = pair . Key . ToString ( ) ;
104- _resourceTexts [ pair . Key ] = display . transform . Find ( "Text" ) . GetComponent < Text > ( ) ;
105- display . transform . Find ( "Icon" ) . GetComponent < Image > ( ) . sprite = Resources . Load < Sprite > ( $ "Textures/GameResources/{ pair . Key } ") ;
106- _SetResourceText ( pair . Key , pair . Value . Amount ) ;
107- display . transform . SetParent ( resourcesUIParent , false ) ;
108- }
109-
110102 // create buttons for each building type
111103 _buildingButtons = new Dictionary < string , Button > ( ) ;
112104 for ( int i = 0 ; i < Globals . BUILDING_DATA . Length ; i ++ )
@@ -128,10 +120,6 @@ private void Awake()
128120 _AddBuildingButtonListener ( b , i ) ;
129121 button . transform . SetParent ( buildingMenu , false ) ;
130122 _buildingButtons [ data . code ] = b ;
131- if ( ! Globals . BUILDING_DATA [ i ] . CanBuy ( ) )
132- {
133- b . interactable = false ;
134- }
135123 button . GetComponent < BuildingButton > ( ) . Initialize ( Globals . BUILDING_DATA [ i ] ) ;
136124 }
137125
@@ -142,6 +130,30 @@ private void Awake()
142130 ToggleSelectionGroupButton ( i , false ) ;
143131 }
144132
133+ private void Start ( )
134+ {
135+ _myPlayerId = GameManager . instance . gamePlayersParameters . myPlayerId ;
136+
137+ // set player indicator color to match my player color
138+ Color c = GameManager . instance . gamePlayersParameters . players [ _myPlayerId ] . color ;
139+ c = Utils . LightenColor ( c , 0.2f ) ;
140+ playerIndicatorImage . color = c ;
141+
142+ // create texts for each in-game resource (gold, wood, stone...)
143+ _resourceTexts = new Dictionary < InGameResource , Text > ( ) ;
144+ foreach ( KeyValuePair < InGameResource , GameResource > pair in Globals . GAME_RESOURCES [ _myPlayerId ] )
145+ {
146+ GameObject display = Instantiate ( gameResourceDisplayPrefab ) ;
147+ display . name = pair . Key . ToString ( ) ;
148+ _resourceTexts [ pair . Key ] = display . transform . Find ( "Text" ) . GetComponent < Text > ( ) ;
149+ display . transform . Find ( "Icon" ) . GetComponent < Image > ( ) . sprite = Resources . Load < Sprite > ( $ "Textures/GameResources/{ pair . Key } ") ;
150+ _SetResourceText ( pair . Key , pair . Value . Amount ) ;
151+ display . transform . SetParent ( resourcesUIParent , false ) ;
152+ }
153+
154+ _CheckBuyLimits ( ) ;
155+ }
156+
145157 private void OnEnable ( )
146158 {
147159 EventManager . AddListener ( "UpdateResourceTexts" , _OnUpdateResourceTexts ) ;
@@ -153,6 +165,7 @@ private void OnEnable()
153165 EventManager . AddListener ( "DeselectUnit" , _OnDeselectUnit ) ;
154166 EventManager . AddListener ( "PlaceBuildingOn" , _OnPlaceBuildingOn ) ;
155167 EventManager . AddListener ( "PlaceBuildingOff" , _OnPlaceBuildingOff ) ;
168+ EventManager . AddListener ( "SetPlayer" , _OnSetPlayer ) ;
156169 }
157170
158171 private void OnDisable ( )
@@ -166,6 +179,7 @@ private void OnDisable()
166179 EventManager . RemoveListener ( "DeselectUnit" , _OnDeselectUnit ) ;
167180 EventManager . RemoveListener ( "PlaceBuildingOn" , _OnPlaceBuildingOn ) ;
168181 EventManager . RemoveListener ( "PlaceBuildingOff" , _OnPlaceBuildingOff ) ;
182+ EventManager . RemoveListener ( "SetPlayer" , _OnSetPlayer ) ;
169183 }
170184
171185 public void ToggleSelectionGroupButton ( int groupIndex , bool on )
@@ -275,7 +289,7 @@ private void _CheckBuyLimits()
275289 ) ;
276290 Text txt = resourceDisplay . Find ( "Text" ) . GetComponent < Text > ( ) ;
277291 int resourceAmount = int . Parse ( txt . text ) ;
278- if ( Globals . GAME_RESOURCES [ resourceCode ] . Amount < resourceAmount )
292+ if ( Globals . GAME_RESOURCES [ _myPlayerId ] [ resourceCode ] . Amount < resourceAmount )
279293 txt . color = invalidTextColor ;
280294 else
281295 txt . color = validTextColor ;
@@ -300,7 +314,7 @@ private void _SetResourceText(InGameResource resource, int value)
300314
301315 private void _OnUpdateResourceTexts ( )
302316 {
303- foreach ( KeyValuePair < InGameResource , GameResource > pair in Globals . GAME_RESOURCES )
317+ foreach ( KeyValuePair < InGameResource , GameResource > pair in Globals . GAME_RESOURCES [ _myPlayerId ] )
304318 _SetResourceText ( pair . Key , pair . Value . Amount ) ;
305319
306320 _CheckBuyLimits ( ) ;
@@ -341,7 +355,7 @@ private void _OnUpdatePlacedBuildingProduction(object data)
341355 private void _OnCheckBuildingButtons ( )
342356 {
343357 foreach ( BuildingData data in Globals . BUILDING_DATA )
344- _buildingButtons [ data . code ] . interactable = data . CanBuy ( ) ;
358+ _buildingButtons [ data . code ] . interactable = data . CanBuy ( _myPlayerId ) ;
345359 }
346360
347361 private void _OnHoverBuildingButton ( object data )
@@ -383,6 +397,17 @@ private void _OnPlaceBuildingOff()
383397 placedBuildingProductionRectTransform . gameObject . SetActive ( false ) ;
384398 }
385399
400+ private void _OnSetPlayer ( object data )
401+ {
402+ int playerId = ( int ) data ;
403+ _myPlayerId = playerId ;
404+ Color c = GameManager . instance . gamePlayersParameters . players [ _myPlayerId ] . color ;
405+ c = Utils . LightenColor ( c , 0.2f ) ;
406+ playerIndicatorImage . color = c ;
407+ _OnUpdateResourceTexts ( ) ;
408+ _CheckBuyLimits ( ) ;
409+ }
410+
386411 public void SetInfoPanel ( UnitData data )
387412 {
388413 SetInfoPanel ( data . unitName , data . description , data . cost ) ;
@@ -405,10 +430,9 @@ public void SetInfoPanel(string title, string description, List<ResourceValue> r
405430 t = g . transform ;
406431 t . Find ( "Text" ) . GetComponent < Text > ( ) . text = resource . amount . ToString ( ) ;
407432 t . Find ( "Icon" ) . GetComponent < Image > ( ) . sprite = Resources . Load < Sprite > ( $ "Textures/GameResources/{ resource . code } ") ;
408- // check to see if resource requirement is not
409- // currently met - in that case, turn the text into the "invalid"
410- // color
411- if ( Globals . GAME_RESOURCES [ resource . code ] . Amount < resource . amount )
433+ // check to see if resource requirement is not currently met -
434+ // in that case, turn the text into the "invalid" color
435+ if ( Globals . GAME_RESOURCES [ _myPlayerId ] [ resource . code ] . Amount < resource . amount )
412436 t . Find ( "Text" ) . GetComponent < Text > ( ) . color = invalidTextColor ;
413437 t . SetParent ( _infoPanelResourcesCostParent , false ) ;
414438 }
0 commit comments