Skip to content

Commit

Permalink
Merge pull request #51 from boardgamers/layout
Browse files Browse the repository at this point in the history
Layout, combat
  • Loading branch information
AlphaCodingPilot authored Oct 7, 2023
2 parents e655f2b + 962031c commit 51e48fa
Show file tree
Hide file tree
Showing 21 changed files with 374 additions and 468 deletions.
8 changes: 5 additions & 3 deletions client/src/advance_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl HasPayment for AdvancePayment {
}

pub fn show_advance_menu(game: &Game, player_index: usize) -> StateUpdate {
show_generic_advance_menu(game, player_index, true, |name| {
show_generic_advance_menu("Advances", game, player_index, true, |name| {
StateUpdate::SetDialog(ActiveDialog::AdvancePayment(AdvancePayment::new(
game,
player_index,
Expand All @@ -88,18 +88,19 @@ pub fn show_advance_menu(game: &Game, player_index: usize) -> StateUpdate {
}

pub fn show_free_advance_menu(game: &Game, player_index: usize) -> StateUpdate {
show_generic_advance_menu(game, player_index, false, |name| {
show_generic_advance_menu("Select a free advance", game, player_index, false, |name| {
StateUpdate::status_phase(StatusPhaseAction::FreeAdvance(name))
})
}

pub fn show_generic_advance_menu(
title: &str,
game: &Game,
player_index: usize,
close_button: bool,
new_update: impl Fn(String) -> StateUpdate,
) -> StateUpdate {
dialog_window(close_button, |ui| {
dialog_window(title, close_button, |ui| {
for a in get_all() {
let name = a.name;
let p = game.get_player(player_index);
Expand All @@ -125,6 +126,7 @@ pub fn show_generic_advance_menu(

pub fn pay_advance_dialog(ap: &AdvancePayment) -> StateUpdate {
payment_dialog(
&format!("Pay for advance {}", ap.name),
ap,
AdvancePayment::valid,
|ap| {
Expand Down
2 changes: 1 addition & 1 deletion client/src/collect_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl CollectResources {
}

pub fn collect_resources_dialog(game: &Game, collect: &CollectResources) -> StateUpdate {
active_dialog_window(|ui| {
active_dialog_window("Collect Resources", |ui| {
let r: ResourcePile = collect
.collections
.clone()
Expand Down
7 changes: 3 additions & 4 deletions client/src/combat_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use crate::unit_ui;
use crate::unit_ui::UnitSelection;

pub fn retreat_dialog() -> StateUpdate {
active_dialog_window(|ui| {
ui.label(None, "Do you want to retreat?");
active_dialog_window("Do you want to retreat?", |ui| {
if ui.button(None, "Retreat") {
return retreat(true);
}
Expand All @@ -27,8 +26,7 @@ fn retreat(retreat: bool) -> StateUpdate {
}

pub fn place_settler_dialog() -> StateUpdate {
active_dialog_window(|ui| {
ui.label(None, "Select a city to place a settler in.");
active_dialog_window("Select a city to place a settler in.", |_| {
StateUpdate::None
})
}
Expand Down Expand Up @@ -87,6 +85,7 @@ impl ConfirmSelection for RemoveCasualtiesSelection {
pub fn remove_casualties_dialog(game: &Game, sel: &RemoveCasualtiesSelection) -> StateUpdate {
unit_ui::unit_selection_dialog::<RemoveCasualtiesSelection>(
game,
"Remove casualties",
sel,
|new| StateUpdate::SetDialog(ActiveDialog::RemoveCasualties(new.clone())),
|new: RemoveCasualtiesSelection| {
Expand Down
6 changes: 6 additions & 0 deletions client/src/construct_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub fn add_construct_button(
return StateUpdate::SetDialog(ActiveDialog::ConstructionPayment(
ConstructionPayment::new(
game,
name,
menu.player_index,
menu.city_position,
ConstructionProject::Building(building.clone(), pos),
Expand Down Expand Up @@ -81,6 +82,7 @@ pub fn add_wonder_buttons(game: &Game, menu: &CityMenu, ui: &mut Ui) -> StateUpd
return StateUpdate::SetDialog(ActiveDialog::ConstructionPayment(
ConstructionPayment::new(
game,
&w.name,
menu.player_index,
menu.city_position,
ConstructionProject::Wonder(w.name.clone()),
Expand All @@ -93,6 +95,7 @@ pub fn add_wonder_buttons(game: &Game, menu: &CityMenu, ui: &mut Ui) -> StateUpd

pub fn pay_construction_dialog(game: &Game, payment: &ConstructionPayment) -> StateUpdate {
payment_dialog(
&format!("Pay for {}", payment.name),
payment,
|cp| cp.payment.get(ResourceType::Discount).selectable.current == 0,
|cp| match &cp.project {
Expand Down Expand Up @@ -170,6 +173,7 @@ pub enum ConstructionProject {

#[derive(Clone)]
pub struct ConstructionPayment {
pub name: String,
pub player_index: usize,
pub city_position: Position,
pub project: ConstructionProject,
Expand All @@ -180,6 +184,7 @@ pub struct ConstructionPayment {
impl ConstructionPayment {
pub fn new(
game: &Game,
name: &str,
player_index: usize,
city_position: Position,
project: ConstructionProject,
Expand Down Expand Up @@ -211,6 +216,7 @@ impl ConstructionPayment {
let payment = ConstructionPayment::new_payment(&payment_options);

ConstructionPayment {
name: name.to_string(),
player_index,
city_position,
project,
Expand Down
18 changes: 13 additions & 5 deletions client/src/dialog_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@ use macroquad::ui::{root_ui, Ui};

use crate::ui_state::StateUpdate;

pub fn active_dialog_window<F>(f: F) -> StateUpdate
pub fn active_dialog_window<F>(title: &str, f: F) -> StateUpdate
where
F: FnOnce(&mut Ui) -> StateUpdate,
{
dialog_window(false, f)
dialog_window(title, false, f)
}

pub fn dialog_window<F>(close_button: bool, f: F) -> StateUpdate
pub fn closeable_dialog_window<F>(title: &str, f: F) -> StateUpdate
where
F: FnOnce(&mut Ui) -> StateUpdate,
{
let window = Window::new(hash!(), vec2(100., 100.), vec2(1000., 1000.))
dialog_window(title, true, f)
}

pub fn dialog_window<F>(title: &str, close_button: bool, f: F) -> StateUpdate
where
F: FnOnce(&mut Ui) -> StateUpdate,
{
let window = Window::new(hash!(), vec2(1100., 400.), vec2(800., 350.))
.titlebar(true)
.movable(true)
.movable(false)
.label(title)
.close_button(close_button);

let ui = &mut root_ui();
Expand Down
32 changes: 15 additions & 17 deletions client/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::hex_ui::pixel_to_coordinate;
use crate::log_ui::show_log;
use crate::map_ui::{draw_map, show_tile_menu};
use crate::player_ui::{show_global_controls, show_globals, show_resources, show_wonders};
use crate::ui_state::{ActiveDialog, State, StateUpdate, StateUpdates};
use crate::ui_state::{ActiveDialog, PendingUpdate, State, StateUpdate, StateUpdates};
use crate::{combat_ui, influence_ui, move_ui, recruit_unit_ui, status_phase_ui};

const EXPORT_FILE: &str = "game.json";
Expand All @@ -48,23 +48,23 @@ fn game_loop(game: &mut Game, state: &State) -> StateUpdate {
show_resources(game, player_index);
show_wonders(game, player_index);

if root_ui().button(vec2(1200., 310.), "Log") {
if root_ui().button(vec2(1200., 130.), "Log") {
return StateUpdate::OpenDialog(ActiveDialog::Log);
};
if root_ui().button(vec2(1200., 350.), "Advances") {
if root_ui().button(vec2(1200., 100.), "Advances") {
return StateUpdate::OpenDialog(ActiveDialog::AdvanceMenu);
};
if root_ui().button(vec2(1200., 450.), "Import") {
if root_ui().button(vec2(1200., 290.), "Import") {
import(game);
return StateUpdate::Cancel;
};
if root_ui().button(vec2(1250., 450.), "Export") {
if root_ui().button(vec2(1250., 290.), "Export") {
export(game);
return StateUpdate::None;
};

if state.pending_update.is_some() {
updates.add(show_pending_update(state));
if let Some(u) = &state.pending_update {
updates.add(show_pending_update(u));
return updates.result();
}

Expand Down Expand Up @@ -124,16 +124,14 @@ fn export(game: &Game) {
.expect("Failed to write export file");
}

fn show_pending_update(state: &State) -> StateUpdate {
active_dialog_window(|ui| {
if let Some(update) = &state.pending_update {
ui.label(None, &format!("Warning: {}", update.warning.join(", ")));
if ui.button(None, "OK") {
return StateUpdate::ResolvePendingUpdate(true);
}
if ui.button(None, "Cancel") {
return StateUpdate::ResolvePendingUpdate(false);
}
fn show_pending_update(update: &PendingUpdate) -> StateUpdate {
active_dialog_window("Are you sure?", |ui| {
ui.label(None, &format!("Warning: {}", update.warning.join(", ")));
if ui.button(None, "OK") {
return StateUpdate::ResolvePendingUpdate(true);
}
if ui.button(None, "Cancel") {
return StateUpdate::ResolvePendingUpdate(false);
}
StateUpdate::None
})
Expand Down
6 changes: 3 additions & 3 deletions client/src/happiness_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ fn increase_happiness_new_steps(
}

pub fn increase_happiness_menu(h: &IncreaseHappiness) -> StateUpdate {
active_dialog_window(|ui| {
active_dialog_window("Increase Happiness", |ui| {
ui.label(None, &format!("Cost: {:?}", h.cost));
if ui.button(None, "Cancel") {
return StateUpdate::Cancel;
}
Expand All @@ -86,13 +87,12 @@ pub fn increase_happiness_menu(h: &IncreaseHappiness) -> StateUpdate {
happiness_increases: h.steps.clone(),
}));
}
ui.label(None, &format!("Cost: {:?}", h.cost));
StateUpdate::None
})
}

pub fn show_increase_happiness(game: &Game, player_index: usize) -> StateUpdate {
if can_play_action(game) && root_ui().button(vec2(1200., 480.), "Increase Happiness") {
if can_play_action(game) && root_ui().button(vec2(1200., 60.), "Increase Happiness") {
return StateUpdate::SetDialog(ActiveDialog::IncreaseHappiness(IncreaseHappiness::new(
game.get_player(player_index)
.cities
Expand Down
3 changes: 1 addition & 2 deletions client/src/influence_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ pub fn closest_city(game: &Game, menu: &CityMenu) -> Position {
}

pub fn cultural_influence_resolution_dialog(c: &CulturalInfluenceResolution) -> StateUpdate {
active_dialog_window(|ui| {
ui.label(None, "Cultural Influence Resolution");
active_dialog_window("Cultural Influence Resolution", |ui| {
if ui.button(
None,
format!(
Expand Down
4 changes: 2 additions & 2 deletions client/src/log_ui.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use server::game::Game;

use crate::dialog_ui::dialog_window;
use crate::dialog_ui::closeable_dialog_window;
use crate::ui_state::StateUpdate;

pub fn show_log(game: &Game) -> StateUpdate {
dialog_window(true, |ui| {
closeable_dialog_window("Log", |ui| {
game.log.iter().for_each(|l| {
ui.label(None, l);
});
Expand Down
93 changes: 46 additions & 47 deletions client/src/map_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use server::position::Position;
use server::unit::{MovementRestriction, Unit};

use crate::city_ui::{draw_city, show_city_menu};
use crate::dialog_ui::dialog_window;
use crate::dialog_ui::closeable_dialog_window;
use crate::ui_state::{can_play_action, ActiveDialog, CityMenu, State, StateUpdate};
use crate::{collect_ui, hex_ui, unit_ui};

Expand Down Expand Up @@ -128,54 +128,53 @@ pub fn show_generic_tile_menu(
suffix: Vec<String>,
additional: impl FnOnce(&mut Ui) -> StateUpdate,
) -> StateUpdate {
dialog_window(true, |ui| {
ui.label(
None,
&format!(
"{}/{}",
position,
game.map
.tiles
.get(&position)
.map_or("outside the map", terrain_name),
),
);
let units: Vec<(&Unit, String)> = unit_ui::units_on_tile(game, position)
.map(|(p, u)| {
let unit = game.get_player(p).get_unit(u).unwrap();
(unit, unit_ui::label(unit))
})
.collect();

let units_str = &units.iter().map(|(_, l)| l).join(", ");
if !units_str.is_empty() {
ui.label(None, units_str);
}
for s in suffix {
ui.label(None, &s);
}
closeable_dialog_window(
&format!(
"{}/{}",
position,
game.map
.tiles
.get(&position)
.map_or("outside the map", terrain_name),
),
|ui| {
let units: Vec<(&Unit, String)> = unit_ui::units_on_tile(game, position)
.map(|(p, u)| {
let unit = game.get_player(p).get_unit(u).unwrap();
(unit, unit_ui::label(unit))
})
.collect();

let settlers = &units
.iter()
.filter_map(|(unit, _)| {
if unit.can_found_city(game) {
Some(unit)
} else {
None
}
})
.collect::<Vec<_>>();
let units_str = &units.iter().map(|(_, l)| l).join(", ");
if !units_str.is_empty() {
ui.label(None, units_str);
}
for s in suffix {
ui.label(None, &s);
}

if can_play_action(game) && !settlers.is_empty() && ui.button(None, "Settle") {
let settler = settlers
let settlers = &units
.iter()
.find(|u| u.movement_restriction != MovementRestriction::None)
.unwrap_or(&settlers[0]);
return StateUpdate::execute(Action::Playing(PlayingAction::FoundCity {
settler: settler.id,
}));
}
.filter_map(|(unit, _)| {
if unit.can_found_city(game) {
Some(unit)
} else {
None
}
})
.collect::<Vec<_>>();

if can_play_action(game) && !settlers.is_empty() && ui.button(None, "Settle") {
let settler = settlers
.iter()
.find(|u| u.movement_restriction != MovementRestriction::None)
.unwrap_or(&settlers[0]);
return StateUpdate::execute(Action::Playing(PlayingAction::FoundCity {
settler: settler.id,
}));
}

additional(ui)
})
additional(ui)
},
)
}
1 change: 1 addition & 0 deletions client/src/move_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::unit_ui::UnitSelection;
pub fn move_units_dialog(game: &Game, sel: &MoveSelection) -> StateUpdate {
unit_ui::unit_selection_dialog::<MoveSelection>(
game,
"Move Units",
sel,
|new| update_possible_destinations(game, new.clone()),
|_new| StateUpdate::None,
Expand Down
Loading

0 comments on commit 51e48fa

Please sign in to comment.