@@ -9,7 +9,7 @@ use server::status_phase::StatusPhaseAction;
9
9
10
10
use crate :: advance_ui:: { pay_advance_dialog, show_advance_menu, show_free_advance_menu} ;
11
11
use crate :: client_state:: {
12
- ActiveDialog , ControlPlayers , PendingUpdate , State , StateUpdate , StateUpdates ,
12
+ ActiveDialog , PendingUpdate , ShownPlayer , State , StateUpdate , StateUpdates ,
13
13
} ;
14
14
use crate :: collect_ui:: { click_collect_option, collect_resources_dialog} ;
15
15
use crate :: construct_ui:: pay_construction_dialog;
@@ -33,20 +33,6 @@ pub fn render_and_update(
33
33
sync_result : & GameSyncResult ,
34
34
features : & Features ,
35
35
) -> GameSyncRequest {
36
- match state. control_players {
37
- ControlPlayers :: None => {
38
- // todo add spectator mode
39
- return GameSyncRequest :: None ;
40
- }
41
- ControlPlayers :: All => { }
42
- ControlPlayers :: Own ( p) => {
43
- if game. active_player ( ) != p {
44
- // todo add spectator mode
45
- return GameSyncRequest :: None ;
46
- }
47
- }
48
- }
49
-
50
36
match sync_result {
51
37
GameSyncResult :: None => { }
52
38
GameSyncResult :: Update => {
@@ -63,82 +49,97 @@ pub fn render_and_update(
63
49
64
50
fn render ( game : & Game , state : & State , features : & Features ) -> StateUpdate {
65
51
let player_index = game. active_player ( ) ;
52
+ let player = & state. shown_player ( game) ;
66
53
clear_background ( WHITE ) ;
67
54
68
55
draw_map ( game, state) ;
69
56
let mut updates = StateUpdates :: new ( ) ;
70
- show_globals ( game) ;
57
+ let update = show_globals ( game, player) ;
58
+ updates. add ( update) ;
71
59
show_resources ( game, player_index) ;
72
60
show_wonders ( game, player_index) ;
73
61
74
- if root_ui ( ) . button ( vec2 ( 1200. , 130. ) , "Log" ) {
75
- return StateUpdate :: OpenDialog ( ActiveDialog :: Log ) ;
76
- } ;
77
62
if root_ui ( ) . button ( vec2 ( 1200. , 100. ) , "Advances" ) {
78
63
return StateUpdate :: OpenDialog ( ActiveDialog :: AdvanceMenu ) ;
79
64
} ;
80
- if features. import_export {
65
+ if root_ui ( ) . button ( vec2 ( 1200. , 130. ) , "Log" ) {
66
+ return StateUpdate :: OpenDialog ( ActiveDialog :: Log ) ;
67
+ } ;
68
+ let d = state. game_state_dialog ( game, & ActiveDialog :: None ) ;
69
+ if !matches ! ( d, ActiveDialog :: None )
70
+ && d. title ( ) != state. active_dialog . title ( )
71
+ && root_ui ( ) . button ( vec2 ( 1200. , 160. ) , format ! ( "Back to {}" , d. title( ) ) )
72
+ {
73
+ return StateUpdate :: OpenDialog ( d) ;
74
+ }
75
+
76
+ if features. import_export && player. can_control {
81
77
if root_ui ( ) . button ( vec2 ( 1200. , 290. ) , "Import" ) {
82
78
return StateUpdate :: Import ;
83
79
} ;
84
80
if root_ui ( ) . button ( vec2 ( 1250. , 290. ) , "Export" ) {
85
81
return StateUpdate :: Export ;
86
82
} ;
87
83
}
88
-
89
- if let Some ( u) = & state. pending_update {
90
- updates. add ( show_pending_update ( u) ) ;
91
- return updates. result ( ) ;
84
+ if player. can_control {
85
+ if let Some ( u) = & state. pending_update {
86
+ updates. add ( show_pending_update ( u, player) ) ;
87
+ return updates. result ( ) ;
88
+ }
92
89
}
93
90
94
- if game . state == server :: game :: GameState :: Playing {
91
+ if player . can_play_action {
95
92
updates. add ( show_increase_happiness ( game, player_index) ) ;
96
93
}
97
94
updates. add ( show_global_controls ( game, state) ) ;
98
95
99
96
updates. add ( match & state. active_dialog {
100
97
ActiveDialog :: None => StateUpdate :: None ,
101
98
ActiveDialog :: Log => show_log ( game) ,
102
- ActiveDialog :: TileMenu ( p) => show_tile_menu ( game, * p) ,
99
+ ActiveDialog :: TileMenu ( p) => show_tile_menu ( game, * p, player ) ,
103
100
ActiveDialog :: WaitingForUpdate => {
104
- active_dialog_window ( "Waiting for update" , |_ui| StateUpdate :: None )
101
+ active_dialog_window ( player , "Waiting for update" , |_ui| StateUpdate :: None )
105
102
}
106
103
107
104
// playing actions
108
- ActiveDialog :: IncreaseHappiness ( h) => increase_happiness_menu ( h) ,
109
- ActiveDialog :: AdvanceMenu => show_advance_menu ( game, player_index ) ,
110
- ActiveDialog :: AdvancePayment ( p) => pay_advance_dialog ( p) ,
111
- ActiveDialog :: ConstructionPayment ( p) => pay_construction_dialog ( game, p) ,
112
- ActiveDialog :: CollectResources ( c) => collect_resources_dialog ( game, c) ,
113
- ActiveDialog :: RecruitUnitSelection ( s) => recruit_unit_ui:: select_dialog ( game, s) ,
114
- ActiveDialog :: ReplaceUnits ( r) => recruit_unit_ui:: replace_dialog ( game, r) ,
115
- ActiveDialog :: MoveUnits ( s) => move_ui:: move_units_dialog ( game, s) ,
105
+ ActiveDialog :: IncreaseHappiness ( h) => increase_happiness_menu ( h, player ) ,
106
+ ActiveDialog :: AdvanceMenu => show_advance_menu ( game, player ) ,
107
+ ActiveDialog :: AdvancePayment ( p) => pay_advance_dialog ( p, player ) ,
108
+ ActiveDialog :: ConstructionPayment ( p) => pay_construction_dialog ( game, p, player ) ,
109
+ ActiveDialog :: CollectResources ( c) => collect_resources_dialog ( game, c, player ) ,
110
+ ActiveDialog :: RecruitUnitSelection ( s) => recruit_unit_ui:: select_dialog ( game, s, player ) ,
111
+ ActiveDialog :: ReplaceUnits ( r) => recruit_unit_ui:: replace_dialog ( game, r, player ) ,
112
+ ActiveDialog :: MoveUnits ( s) => move_ui:: move_units_dialog ( game, s, player ) ,
116
113
ActiveDialog :: CulturalInfluenceResolution ( c) => {
117
- influence_ui:: cultural_influence_resolution_dialog ( c)
114
+ influence_ui:: cultural_influence_resolution_dialog ( c, player )
118
115
}
119
116
120
117
//status phase
121
- ActiveDialog :: FreeAdvance => show_free_advance_menu ( game, player_index) ,
122
- ActiveDialog :: RaseSize1City => status_phase_ui:: raze_city_dialog ( ) ,
123
- ActiveDialog :: DetermineFirstPlayer => status_phase_ui:: determine_first_player_dialog ( game) ,
124
- ActiveDialog :: ChangeGovernmentType => status_phase_ui:: change_government_type_dialog ( game) ,
118
+ ActiveDialog :: FreeAdvance => show_free_advance_menu ( game, player) ,
119
+ ActiveDialog :: RazeSize1City => status_phase_ui:: raze_city_dialog ( player) ,
120
+ ActiveDialog :: DetermineFirstPlayer => {
121
+ status_phase_ui:: determine_first_player_dialog ( game, player)
122
+ }
123
+ ActiveDialog :: ChangeGovernmentType => {
124
+ status_phase_ui:: change_government_type_dialog ( game, player)
125
+ }
125
126
ActiveDialog :: ChooseAdditionalAdvances ( a) => {
126
- status_phase_ui:: choose_additional_advances_dialog ( game, a)
127
+ status_phase_ui:: choose_additional_advances_dialog ( game, a, player )
127
128
}
128
129
129
130
//combat
130
- ActiveDialog :: PlaceSettler => combat_ui:: place_settler_dialog ( ) ,
131
- ActiveDialog :: Retreat => combat_ui:: retreat_dialog ( ) ,
132
- ActiveDialog :: RemoveCasualties ( s) => combat_ui:: remove_casualties_dialog ( game, s) ,
131
+ ActiveDialog :: PlaceSettler => combat_ui:: place_settler_dialog ( player ) ,
132
+ ActiveDialog :: Retreat => combat_ui:: retreat_dialog ( player ) ,
133
+ ActiveDialog :: RemoveCasualties ( s) => combat_ui:: remove_casualties_dialog ( game, s, player ) ,
133
134
} ) ;
134
135
135
- updates. add ( try_click ( game, state, player_index ) ) ;
136
+ updates. add ( try_click ( game, state, player ) ) ;
136
137
137
138
updates. result ( )
138
139
}
139
140
140
- fn show_pending_update ( update : & PendingUpdate ) -> StateUpdate {
141
- active_dialog_window ( "Are you sure?" , |ui| {
141
+ fn show_pending_update ( update : & PendingUpdate , player : & ShownPlayer ) -> StateUpdate {
142
+ active_dialog_window ( player , "Are you sure?" , |ui| {
142
143
ui. label ( None , & format ! ( "Warning: {}" , update. warning. join( ", " ) ) ) ;
143
144
if ui. button ( None , "OK" ) {
144
145
return StateUpdate :: ResolvePendingUpdate ( true ) ;
@@ -150,7 +151,7 @@ fn show_pending_update(update: &PendingUpdate) -> StateUpdate {
150
151
} )
151
152
}
152
153
153
- pub fn try_click ( game : & Game , state : & State , player_index : usize ) -> StateUpdate {
154
+ pub fn try_click ( game : & Game , state : & State , player : & ShownPlayer ) -> StateUpdate {
154
155
if !is_mouse_button_pressed ( MouseButton :: Left ) {
155
156
return StateUpdate :: None ;
156
157
}
@@ -161,38 +162,42 @@ pub fn try_click(game: &Game, state: &State, player_index: usize) -> StateUpdate
161
162
return StateUpdate :: None ;
162
163
}
163
164
164
- match & state. active_dialog {
165
- ActiveDialog :: MoveUnits ( s) => move_ui:: click ( pos, s) ,
166
- ActiveDialog :: ReplaceUnits ( r) => recruit_unit_ui:: click_replace ( pos, r) ,
167
- ActiveDialog :: RemoveCasualties ( _s) => StateUpdate :: None ,
168
- ActiveDialog :: CollectResources ( col) => click_collect_option ( col, pos) ,
169
- ActiveDialog :: RaseSize1City => {
170
- if game. players [ player_index] . can_raze_city ( pos) {
171
- StateUpdate :: status_phase ( StatusPhaseAction :: RaseSize1City ( Some ( pos) ) )
172
- } else {
173
- StateUpdate :: None
165
+ if player. can_control {
166
+ match & state. active_dialog {
167
+ ActiveDialog :: MoveUnits ( s) => move_ui:: click ( pos, s) ,
168
+ ActiveDialog :: ReplaceUnits ( r) => recruit_unit_ui:: click_replace ( pos, r) ,
169
+ ActiveDialog :: RemoveCasualties ( _s) => StateUpdate :: None ,
170
+ ActiveDialog :: CollectResources ( col) => click_collect_option ( col, pos) ,
171
+ ActiveDialog :: RazeSize1City => {
172
+ if player. get ( game) . can_raze_city ( pos) {
173
+ StateUpdate :: status_phase ( StatusPhaseAction :: RaseSize1City ( Some ( pos) ) )
174
+ } else {
175
+ StateUpdate :: None
176
+ }
174
177
}
175
- }
176
- ActiveDialog :: PlaceSettler => {
177
- if game . players [ player_index ] . get_city ( pos) . is_some ( ) {
178
- StateUpdate :: Execute ( Action :: PlaceSettler ( pos ) )
179
- } else {
180
- StateUpdate :: None
178
+ ActiveDialog :: PlaceSettler => {
179
+ if player . get ( game ) . get_city ( pos ) . is_some ( ) {
180
+ StateUpdate :: Execute ( Action :: PlaceSettler ( pos) )
181
+ } else {
182
+ StateUpdate :: None
183
+ }
181
184
}
182
- }
183
- ActiveDialog :: IncreaseHappiness ( h ) => {
184
- if let Some ( city ) = game . players [ player_index ] . get_city ( pos ) {
185
- StateUpdate :: SetDialog ( ActiveDialog :: IncreaseHappiness ( add_increase_happiness (
186
- & game . players [ player_index ] ,
187
- city ,
188
- pos ,
189
- h ,
190
- ) ) )
191
- } else {
192
- StateUpdate :: None
185
+ ActiveDialog :: IncreaseHappiness ( h ) => {
186
+ if let Some ( city ) = player . get ( game ) . get_city ( pos ) {
187
+ StateUpdate :: SetDialog ( ActiveDialog :: IncreaseHappiness ( add_increase_happiness (
188
+ player . get ( game ) ,
189
+ city ,
190
+ pos ,
191
+ h ,
192
+ ) ) )
193
+ } else {
194
+ StateUpdate :: None
195
+ }
193
196
}
197
+ _ => StateUpdate :: OpenDialog ( ActiveDialog :: TileMenu ( pos) ) ,
194
198
}
195
- _ => StateUpdate :: OpenDialog ( ActiveDialog :: TileMenu ( pos) ) ,
199
+ } else {
200
+ StateUpdate :: OpenDialog ( ActiveDialog :: TileMenu ( pos) )
196
201
}
197
202
}
198
203
0 commit comments