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::SubmitChat
46            | InputAction::CancelChat
47    )
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn movement_and_combat_dispatch_to_net() {
56        assert!(should_dispatch_to_net(InputAction::Interact));
57        assert!(should_dispatch_to_net(InputAction::Interact));
58        assert!(should_dispatch_to_net(InputAction::StartChat {
59            whisper: false
60        }));
61        assert!(!should_dispatch_to_net(InputAction::ToggleHelp));
62    }
63
64    #[test]
65    fn play_hud_includes_map_target() {
66        let mut target = MapTargetState::default();
67        target.active = true;
68        target.cursor_x = 10.0;
69        target.cursor_y = 20.0;
70        let hud = build_play_hud(PlayHudInput {
71            movement_label: "N",
72            sprint_mode: false,
73            map_target: &target,
74            auto_nav_goal: None,
75            mouse_hover: None,
76            view_mode: HudViewMode::default(),
77        });
78        assert_eq!(hud.map_target, Some((10.0, 20.0)));
79    }
80}