-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.rs
267 lines (236 loc) · 9.48 KB
/
client.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
use macroquad::input::{is_mouse_button_pressed, mouse_position, MouseButton};
use macroquad::prelude::clear_background;
use macroquad::prelude::*;
use server::action::Action;
use server::content::custom_phase_actions::CustomPhaseEventAction;
use server::game::Game;
use server::position::Position;
use crate::advance_ui::{pay_advance_dialog, show_free_advance_menu, show_paid_advance_menu};
use crate::client_state::{
ActiveDialog, CameraMode, DialogChooser, State, StateUpdate, StateUpdates,
};
use crate::collect_ui::collect_dialog;
use crate::construct_ui::pay_construction_dialog;
use crate::dialog_ui::{cancel_button, ok_button, OkTooltip};
use crate::event_ui::custom_phase_event_origin;
use crate::happiness_ui::{increase_happiness_click, increase_happiness_menu};
use crate::hex_ui::pixel_to_coordinate;
use crate::layout_ui::{bottom_centered_text, icon_pos, top_right_texture};
use crate::log_ui::show_log;
use crate::map_ui::{draw_map, explore_dialog, show_tile_menu};
use crate::player_ui::{player_select, show_global_controls, show_top_center, show_top_left};
use crate::render_context::RenderContext;
use crate::status_phase_ui::raze_city_confirm_dialog;
use crate::unit_ui::unit_selection_click;
use crate::{
custom_actions_ui, custom_phase_ui, dialog_ui, influence_ui, map_ui, move_ui, recruit_unit_ui,
status_phase_ui, tooltip,
};
fn render_with_mutable_state(game: &Game, state: &mut State, features: &Features) -> StateUpdate {
tooltip::update(state);
if !state.active_dialog.is_modal() {
map_ui::pan_and_zoom(state);
}
if matches!(state.active_dialog, ActiveDialog::Log) {
state.log_scroll += mouse_wheel().1;
}
set_y_zoom(state);
render(&state.render_context(game), features)
}
fn set_y_zoom(state: &mut State) {
let s = state.screen_size;
state.camera.zoom.y = state.camera.zoom.x * s.x / s.y;
}
fn render(rc: &RenderContext, features: &Features) -> StateUpdate {
clear_background(WHITE);
let state = &rc.state;
let show_map = !state.active_dialog.is_modal();
let mut updates = StateUpdates::new();
if show_map {
updates.add(rc.with_camera(CameraMode::World, draw_map));
}
if !state.active_dialog.is_modal() {
show_top_left(rc);
}
if show_map {
show_top_center(rc);
}
if !state.active_dialog.is_modal() {
updates.add(player_select(rc));
updates.add(show_global_controls(rc, features));
}
if top_right_texture(rc, &rc.assets().log, icon_pos(-1, 0), "Show log") {
if let ActiveDialog::Log = state.active_dialog {
return StateUpdate::CloseDialog;
}
return StateUpdate::OpenDialog(ActiveDialog::Log);
};
if top_right_texture(rc, &rc.assets().advances, icon_pos(-2, 0), "Show advances") {
if state.active_dialog.is_advance() {
return StateUpdate::CloseDialog;
}
return StateUpdate::OpenDialog(ActiveDialog::AdvanceMenu);
};
let can_control = rc.can_control();
if can_control {
if let Some(u) = &state.pending_update {
updates.add(dialog_ui::show_pending_update(u, rc));
}
}
if can_control || state.active_dialog.show_for_other_player() {
updates.add(render_active_dialog(rc));
}
if let Some(pos) = state.focused_tile {
if matches!(state.active_dialog, ActiveDialog::None) {
updates.add(show_tile_menu(rc, pos));
}
}
updates.add(try_click(rc));
updates.result()
}
pub async fn init(features: &Features) -> State {
State::new(features).await
}
pub fn render_and_update(
game: &Game,
state: &mut State,
sync_result: &GameSyncResult,
features: &Features,
) -> GameSyncRequest {
match sync_result {
GameSyncResult::None => {}
GameSyncResult::Update => {
state.update_from_game(game);
}
GameSyncResult::WaitingForUpdate => {
state.set_dialog(ActiveDialog::WaitingForUpdate);
}
}
let update = render_with_mutable_state(game, state, features);
state.update(game, update)
}
fn render_active_dialog(rc: &RenderContext) -> StateUpdate {
let state = rc.state;
match &state.active_dialog {
ActiveDialog::None
| ActiveDialog::WaitingForUpdate
| ActiveDialog::CulturalInfluence(_)
| ActiveDialog::PositionRequest(_) => StateUpdate::None,
ActiveDialog::DialogChooser(d) => dialog_chooser(rc, d),
ActiveDialog::Log => show_log(rc),
// playing actions
ActiveDialog::IncreaseHappiness(h) => increase_happiness_menu(rc, h),
ActiveDialog::AdvanceMenu => show_paid_advance_menu(rc),
ActiveDialog::AdvancePayment(p) => pay_advance_dialog(p, rc),
ActiveDialog::ConstructionPayment(p) => pay_construction_dialog(rc, p),
ActiveDialog::CollectResources(c) => collect_dialog(rc, c),
ActiveDialog::RecruitUnitSelection(s) => recruit_unit_ui::select_dialog(rc, s),
ActiveDialog::ReplaceUnits(r) => recruit_unit_ui::replace_dialog(rc, r),
ActiveDialog::CulturalInfluenceResolution(r) => {
influence_ui::cultural_influence_resolution_dialog(rc, r)
}
ActiveDialog::ExploreResolution(r) => explore_dialog(rc, r),
ActiveDialog::MoveUnits(_) => move_ui::move_units_dialog(rc),
ActiveDialog::MovePayment(p) => move_ui::move_payment_dialog(rc, p),
//status phase
ActiveDialog::FreeAdvance => show_free_advance_menu(rc),
ActiveDialog::RazeSize1City => status_phase_ui::raze_city_dialog(rc),
ActiveDialog::CompleteObjectives => status_phase_ui::complete_objectives_dialog(rc),
ActiveDialog::ChangeGovernmentType => status_phase_ui::change_government_type_dialog(rc),
ActiveDialog::ChooseAdditionalAdvances(a) => {
status_phase_ui::choose_additional_advances_dialog(rc, a)
}
ActiveDialog::DetermineFirstPlayer => status_phase_ui::determine_first_player_dialog(rc),
ActiveDialog::Sports((p, pos)) => custom_actions_ui::sports(rc, p, *pos),
ActiveDialog::Taxes(p) => custom_actions_ui::taxes(rc, p),
ActiveDialog::Theaters(p) => custom_actions_ui::theaters(rc, p),
ActiveDialog::PaymentRequest(c) => custom_phase_ui::custom_phase_payment_dialog(rc, c),
ActiveDialog::PlayerRequest(r) => custom_phase_ui::player_request_dialog(rc, r),
ActiveDialog::ResourceRewardRequest(p) => custom_phase_ui::payment_reward_dialog(rc, p),
ActiveDialog::AdvanceRewardRequest(r) => {
custom_phase_ui::advance_reward_dialog(rc, r, &custom_phase_event_origin(rc).name())
}
ActiveDialog::UnitTypeRequest(r) => custom_phase_ui::unit_request_dialog(rc, r),
ActiveDialog::UnitsRequest(r) => custom_phase_ui::select_units_dialog(rc, r),
ActiveDialog::BoolRequest => custom_phase_ui::bool_request_dialog(rc),
}
}
fn dialog_chooser(rc: &RenderContext, c: &DialogChooser) -> StateUpdate {
bottom_centered_text(rc, &c.title);
if ok_button(rc, OkTooltip::Valid("OK".to_string())) {
StateUpdate::OpenDialog(c.yes.clone())
} else if cancel_button(rc) {
StateUpdate::OpenDialog(c.no.clone())
} else {
StateUpdate::None
}
}
pub fn try_click(rc: &RenderContext) -> StateUpdate {
let game = rc.game;
let state = &rc.state;
let mouse_pos = state.camera.screen_to_world(mouse_position().into());
let pos = Position::from_coordinate(pixel_to_coordinate(mouse_pos));
if rc.can_control() {
if let ActiveDialog::CulturalInfluence(b) = &state.active_dialog {
return influence_ui::hover(rc, mouse_pos, b);
}
}
if !game.map.tiles.contains_key(&pos) {
return StateUpdate::None;
}
if !is_mouse_button_pressed(MouseButton::Left) {
return StateUpdate::None;
}
if rc.can_control() {
let update = controlling_player_click(rc, mouse_pos, pos);
if !matches!(update, StateUpdate::None) {
return update;
}
}
StateUpdate::SetFocusedTile(pos)
}
fn controlling_player_click(rc: &RenderContext, mouse_pos: Vec2, pos: Position) -> StateUpdate {
match &rc.state.active_dialog {
ActiveDialog::CollectResources(_) => StateUpdate::None,
ActiveDialog::MoveUnits(s) => move_ui::click(rc, pos, s, mouse_pos),
ActiveDialog::ReplaceUnits(s) => unit_selection_click(rc, pos, mouse_pos, s, |new| {
StateUpdate::OpenDialog(ActiveDialog::ReplaceUnits(new.clone()))
}),
ActiveDialog::RazeSize1City => raze_city_confirm_dialog(rc, pos),
ActiveDialog::PositionRequest(r) => {
if r.choices.contains(&pos) {
StateUpdate::Execute(Action::CustomPhaseEvent(
CustomPhaseEventAction::SelectPosition(pos),
))
} else {
StateUpdate::None
}
}
ActiveDialog::UnitsRequest(s) => unit_selection_click(rc, pos, mouse_pos, s, |new| {
StateUpdate::OpenDialog(ActiveDialog::UnitsRequest(new.clone()))
}),
ActiveDialog::IncreaseHappiness(h) => increase_happiness_click(rc, pos, h),
_ => StateUpdate::SetFocusedTile(pos),
}
}
pub struct Features {
pub import_export: bool,
pub assets_url: String,
}
impl Features {
#[must_use]
pub fn get_asset(&self, asset: &str) -> String {
format!("{}{}", self.assets_url, asset)
}
}
pub enum GameSyncRequest {
None,
ExecuteAction(Action),
Import,
Export,
}
pub enum GameSyncResult {
None,
Update,
WaitingForUpdate,
}