Skip to main content

flatland_play_loop/
dispatch.rs

1//! Which `InputAction`s stay local vs go to the server.
2
3use flatland_client_ui::{HudViewMode, InputAction, MapTargetState, PlayHud};
4
5/// Local HUD inputs for building `PlayHud`.
6pub struct PlayHudInput<'a> {
7    pub movement_label: &'a str,
8    pub sprint_mode: bool,
9    pub map_target: &'a MapTargetState,
10    pub auto_nav_goal: Option<(f32, f32)>,
11    pub mouse_hover: Option<(f32, f32)>,
12    pub view_mode: HudViewMode,
13}
14
15pub fn build_play_hud(input: PlayHudInput<'_>) -> PlayHud<'_> {
16    PlayHud {
17        movement: input.movement_label,
18        sprint_mode: input.sprint_mode,
19        map_target: if input.map_target.active {
20            Some((input.map_target.cursor_x, input.map_target.cursor_y))
21        } else {
22            None
23        },
24        auto_nav_goal: input.auto_nav_goal,
25        mouse_hover: input.mouse_hover,
26        view_mode: input.view_mode,
27    }
28}
29
30/// Actions handled entirely on the client — not sent as game intents.
31pub fn should_dispatch_to_net(action: InputAction) -> bool {
32    !matches!(
33        action,
34        InputAction::None
35            | InputAction::Quit
36            | InputAction::ToggleHelp
37            | InputAction::CycleHudView
38            | InputAction::ToggleSprintMode
39            | InputAction::ToggleMapTarget
40            | InputAction::ConfirmMapTarget
41            | InputAction::CancelMapTarget
42            | InputAction::MapTargetNudge { .. }
43            | InputAction::CancelAutoNav
44            | InputAction::StopMovement
45            | InputAction::StartChat { .. }
46            | InputAction::SubmitChat
47            | InputAction::CancelChat
48    )
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn movement_and_combat_dispatch_to_net() {
57        assert!(should_dispatch_to_net(InputAction::Interact));
58        assert!(should_dispatch_to_net(InputAction::Interact));
59        assert!(!should_dispatch_to_net(InputAction::ToggleHelp));
60        assert!(!should_dispatch_to_net(InputAction::StartChat { whisper: false }));
61    }
62
63    #[test]
64    fn play_hud_includes_map_target() {
65        let mut target = MapTargetState::default();
66        target.active = true;
67        target.cursor_x = 10.0;
68        target.cursor_y = 20.0;
69        let hud = build_play_hud(PlayHudInput {
70            movement_label: "N",
71            sprint_mode: false,
72            map_target: &target,
73            auto_nav_goal: None,
74            mouse_hover: None,
75            view_mode: HudViewMode::default(),
76        });
77        assert_eq!(hud.map_target, Some((10.0, 20.0)));
78    }
79}