aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/strategy_cursor_hud.rs
blob: 1f13ba84721608390770721da666b3614068d1f6 (plain) (blame)
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
use super::{
    mission_overlay::add_overlay_sprite, screen_tl_quad_vertices_pixels, DirectoryVfs,
    EnvironmentGpuRange, StaticPreviewScene, VulkanSmokeRenderer, VulkanStaticMesh,
};
use fparkan_assets::strategy_cursor::{self, CursorStrip, StrategyCursor, PHASES};
use std::path::Path;
use winit::window::Window;

pub(super) struct StrategyCursorHud {
    strips: [CursorDraw; StrategyCursor::ALL.len()],
    visible: bool,
}

struct CursorDraw {
    range: EnvironmentGpuRange,
    size: [f32; 2],
    hotspot: [f32; 2],
    delay_ms: u32,
    phase: u32,
    last_change_ms: u32,
}

impl StrategyCursorHud {
    pub(super) fn load(root: &Path, preview: &mut StaticPreviewScene) -> Result<Self, String> {
        let [arrow, place, wrong_place, guard] = strategy_cursor::load(&DirectoryVfs::new(root))?;
        let mut load = |strip: CursorStrip| {
            let size = [
                (strip.image.width / PHASES) as f32,
                strip.image.height as f32,
            ];
            Ok::<_, String>(CursorDraw {
                range: add_overlay_sprite(
                    preview,
                    strip.image,
                    [0.0, 0.0, size[0], size[1]],
                    [1.0; 3],
                    1.0,
                )?,
                size,
                hotspot: strip.hotspot.map(|axis| axis as f32),
                delay_ms: strip.phase_delay_ms,
                phase: 0,
                last_change_ms: 0,
            })
        };
        Ok(Self {
            strips: [load(arrow)?, load(place)?, load(wrong_place)?, load(guard)?],
            visible: false,
        })
    }

    pub(super) fn ranges(&self) -> [usize; StrategyCursor::ALL.len()] {
        std::array::from_fn(|i| self.strips[i].range.range_index)
    }

    pub(super) fn sync_visibility(&mut self, window: &Window, visible: bool, mouse_look: bool) {
        if self.visible != visible {
            self.visible = visible;
            window.set_cursor_visible(!visible && !mouse_look);
        }
    }

    pub(super) fn draw(
        &mut self,
        renderer: &mut VulkanSmokeRenderer,
        mesh: &mut VulkanStaticMesh,
        now_ms: u32,
        cursor: Option<([f32; 2], StrategyCursor)>,
    ) -> Result<(), String> {
        for (kind, strip) in StrategyCursor::ALL.into_iter().zip(&mut self.strips) {
            let position = cursor
                .filter(|(_, current)| kind == *current)
                .map(|(xy, _)| xy);
            renderer
                .set_draw_range_index_count(
                    strip.range.range_index,
                    if position.is_some() { 6 } else { 0 },
                )
                .map_err(|error| error.to_string())?;
            let Some(position) = position else {
                continue;
            };
            let half = strip.size.map(|axis| axis * 0.5);
            let center =
                std::array::from_fn(|i| position[i].round_ties_even() - strip.hotspot[i] + half[i]);
            let left = strip.phase as f32 / PHASES as f32;
            let right = (strip.phase + 1) as f32 / PHASES as f32;
            let vertices = screen_tl_quad_vertices_pixels(
                center,
                half[0],
                half[1],
                [640.0, 480.0],
                [1.0; 3],
                1.0,
                [[left, 0.0], [left, 1.0], [right, 1.0], [right, 0.0]],
            );
            let start = strip.range.vertex_start;
            mesh.vertices[start..start + vertices.len()].copy_from_slice(&vertices);
            // iron3d+0x57500 draws the current phase, then advances one frame if due.
            if now_ms.saturating_sub(strip.last_change_ms) > strip.delay_ms {
                strip.phase = (strip.phase + 1) % PHASES;
                strip.last_change_ms = now_ms;
            }
        }
        Ok(())
    }
}