-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_ui.rs
345 lines (308 loc) · 10.2 KB
/
player_ui.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use crate::action_buttons::action_buttons;
use crate::city_ui::city_labels;
use crate::client::Features;
use crate::client_state::StateUpdate;
use crate::dialog_ui::{ok_button, OkTooltip};
use crate::layout_ui::{
bottom_center_texture, bottom_centered_text, bottom_right_texture, icon_pos,
left_mouse_button_pressed_in_rect, top_center_texture, ICON_SIZE,
};
use crate::map_ui::terrain_name;
use crate::render_context::RenderContext;
use crate::resource_ui::{new_resource_map, resource_name};
use crate::tooltip::show_tooltip_for_rect;
use crate::unit_ui;
use macroquad::math::vec2;
use macroquad::prelude::*;
use server::action::Action;
use server::consts::ARMY_MOVEMENT_REQUIRED_ADVANCE;
use server::game::{Game, GameState};
use server::move_units::{CurrentMove, MoveState};
use server::playing_actions::PlayingAction;
use server::resource::ResourceType;
use server::unit::MovementAction;
pub fn player_select(rc: &RenderContext) -> StateUpdate {
let game = rc.game;
let i = game
.human_players()
.iter()
.position(|&p| p == game.starting_player_index)
.unwrap();
let mut players: Vec<_> = game.human_players();
players.rotate_left(i);
let size = 40.;
let mut y = (players.len() as f32 * -size) / 2.;
for player_index in players {
let pl = game.get_player(player_index);
let shown = rc.shown_player.index == pl.index;
let screen = rc.state.screen_size;
let pos = vec2(screen.x, screen.y / 2.0) + vec2(-size, y);
let color = rc.player_color(pl.index);
let w = if shown { size + 10. } else { size };
let x = pos.x - w + size;
draw_rectangle(x, pos.y, w, size, color);
draw_rectangle_lines(x, pos.y, w, size, 2.0, BLACK);
let text = format!("{}", pl.victory_points(game));
let state = rc.state;
state.draw_text(&text, pos.x + 10., pos.y + 22.);
if game.active_player() == pl.index {
draw_texture_ex(
&rc.assets().active_player,
x - 25.,
pos.y + 10.,
WHITE,
DrawTextureParams {
dest_size: Some(vec2(20., 20.)),
..Default::default()
},
);
}
let rect = Rect::new(x, pos.y, w, size);
let tooltip = if state.control_player.is_some_and(|p| p == pl.index) {
format!("{} (You)", pl.get_name())
} else {
pl.get_name()
};
show_tooltip_for_rect(rc, &[tooltip], rect);
if !shown && left_mouse_button_pressed_in_rect(rect, rc) {
return StateUpdate::SetShownPlayer(pl.index);
}
y += size;
}
StateUpdate::None
}
pub fn top_icon_with_label(
rc: &RenderContext,
label: &str,
texture: &Texture2D,
p: Vec2,
tooltip: &str,
) {
let state = rc.state;
let dimensions = state.measure_text(label);
let x = (ICON_SIZE - dimensions.width) / 2.0;
state.draw_text(
label,
state.screen_size.x / 2.0 + p.x + x,
p.y + ICON_SIZE + 30.,
);
top_center_texture(rc, texture, p, tooltip);
}
pub fn bottom_icon_with_label(
rc: &RenderContext,
label: &str,
texture: &Texture2D,
p: Vec2,
tooltip: &str,
) {
let state = rc.state;
let dimensions = state.measure_text(label);
let x = (ICON_SIZE - dimensions.width) / 2.0;
state.draw_text(
label,
rc.state.screen_size.x / 2.0 + p.x + x,
rc.state.screen_size.y + p.y + 35.,
);
bottom_center_texture(rc, texture, p, tooltip);
}
pub fn show_top_center(rc: &RenderContext) {
let player = rc.shown_player;
top_icon_with_label(
rc,
&format!("{}", &player.victory_points(rc.game)),
&rc.assets().victory_points,
icon_pos(3, 0),
"Victory Points",
);
let amount = new_resource_map(&player.resources);
let limit = new_resource_map(&player.resource_limit);
for (i, r) in ResourceType::all().iter().rev().enumerate() {
let a = amount[r];
let l = limit[r];
let t = if l > 0 {
format!("{a}/{l}")
} else {
format!("{a}")
};
top_icon_with_label(
rc,
&t,
&rc.assets().resources[r],
icon_pos(2 - i as i8, 0),
resource_name(*r),
);
}
}
pub fn show_top_left(rc: &RenderContext) {
let state = rc.state;
let mut p = vec2(10., 10.);
let mut label = |label: &str| {
p = vec2(p.x, p.y + 25.);
if p.y > state.screen_size.y - 150. {
p = vec2(p.x + 350., 85.);
}
state.draw_text(label, p.x, p.y);
};
let game = rc.game;
match &game.state {
GameState::Finished => label("Finished"),
_ => label(&format!("Age {}", game.age)),
}
match &game.state {
GameState::StatusPhase(ref p) => label(&format!("Status Phase: {p:?}")),
_ => label(&format!("Round {}", game.round)),
}
let player = rc.shown_player;
label(&player.get_name());
label(&format!("Civ {}", player.civilization.name));
label(&format!(
"Leader {}",
if let Some(l) = &player.active_leader() {
&l.name
} else {
"-"
}
));
if game.current_player_index == player.index {
match &game.state {
GameState::StatusPhase(_) | GameState::Finished => {}
_ => label(&format!("{} actions left", game.actions_left)),
}
if let Some(moves) = move_state(&game.state) {
let movement_actions_left = moves.movement_actions_left;
label(&format!("Move units: {movement_actions_left} moves left"));
match moves.current_move {
CurrentMove::Fleet { .. } => label(
"May continue to move the fleet in the same sea without using movement actions",
),
CurrentMove::Embark { .. } => {
label("May continue to embark units without using movement actions");
}
CurrentMove::None => {}
}
}
}
if let GameState::Combat(c) = &game.state {
if c.attacker == player.index {
label(&format!("Attack - combat round {}", c.round));
} else if c.defender == player.index {
label(&format!("Defend - combat round {}", c.round));
}
}
if rc.shown_player_is_active() || state.active_dialog.show_for_other_player() {
for m in state.active_dialog.help_message(rc) {
label(&m);
}
}
if rc.shown_player_is_active() {
if let Some(u) = &state.pending_update {
for m in &u.info {
label(m);
}
}
}
if let Some(position) = state.focused_tile {
label(&format!(
"{}/{}",
position,
game.map
.get(position)
.map_or("outside the map", terrain_name),
));
if let Some(c) = game.get_any_city(position) {
for l in city_labels(game, c) {
label(&l);
}
}
for (p, unit) in unit_ui::units_on_tile(game, position) {
let army_move = game
.get_player(p)
.has_advance(ARMY_MOVEMENT_REQUIRED_ADVANCE);
label(&unit_ui::unit_label(&unit, army_move));
}
}
}
fn move_state(state: &GameState) -> Option<&MoveState> {
match state {
GameState::Combat(c) => move_state(&c.initiation),
GameState::ExploreResolution(r) => Some(&r.move_state),
GameState::Movement(m) => Some(m),
_ => None,
}
}
pub fn show_global_controls(rc: &RenderContext, features: &Features) -> StateUpdate {
let assets = rc.assets();
let can_control = rc.can_control();
if can_control {
let game = rc.game;
if let Some(tooltip) = can_end_move(game) {
if bottom_right_texture(rc, &assets.end_turn, icon_pos(-4, -1), tooltip) {
return end_move(game);
}
}
if game.can_redo() && bottom_right_texture(rc, &assets.redo, icon_pos(-5, -1), "Redo") {
return StateUpdate::Execute(Action::Redo);
}
if game.can_undo() && bottom_right_texture(rc, &assets.undo, icon_pos(-6, -1), "Undo") {
return StateUpdate::Execute(Action::Undo);
}
if can_control {
let update = action_buttons(rc);
if !matches!(update, StateUpdate::None) {
return update;
}
}
}
if features.import_export {
if bottom_right_texture(rc, &assets.export, icon_pos(-1, -3), "Export") {
return StateUpdate::Export;
};
if bottom_right_texture(rc, &assets.import, icon_pos(-2, -3), "Import") {
return StateUpdate::Import;
};
}
StateUpdate::None
}
fn can_end_move(game: &Game) -> Option<&str> {
match game.state {
GameState::Movement { .. } => Some("End movement"),
GameState::Playing => Some("End turn"),
_ => None,
}
}
fn end_move(game: &Game) -> StateUpdate {
if let GameState::Movement(m) = &game.state {
let movement_actions_left = m.movement_actions_left;
return StateUpdate::execute_with_warning(
Action::Movement(MovementAction::Stop),
if movement_actions_left > 0 {
vec![format!("{movement_actions_left} movement actions left")]
} else {
vec![]
},
);
}
let left = game.actions_left;
StateUpdate::execute_with_warning(
Action::Playing(PlayingAction::EndTurn),
if left > 0 {
vec![format!("{left} actions left")]
} else {
vec![]
},
)
}
pub fn choose_player_dialog(
rc: &RenderContext,
choices: &[usize],
execute: impl Fn(usize) -> Action,
) -> StateUpdate {
let player = rc.shown_player.index;
if rc.can_control_active_player() && choices.contains(&player) {
bottom_centered_text(rc, &format!("Select {}", rc.shown_player.get_name()));
if ok_button(rc, OkTooltip::Valid("Select".to_string())) {
return StateUpdate::execute(execute(player));
}
}
StateUpdate::None
}