anvil/camera.rs
1//! `anvil-game.camera`: the game's map camera (anvil-game 1.25.0), client
2//! state rather than game state. Only this client's view moves, as with the
3//! player's own keys and mouse; nothing goes to the game state or to the other
4//! players in multiplayer.
5//!
6//! **When.** On the game's main thread after an outer frame, in a session, as
7//! for [`World`](crate::world::World): from a handler of an anvil-game event or
8//! from work queued with [`Game::run_on_main_thread`]. Anywhere else, in the
9//! main menu and during a load, [`ErrorCode::WrongPhase`].
10//!
11//! [`Game::run_on_main_thread`]: crate::game::Game::run_on_main_thread
12//! [`ErrorCode::WrongPhase`]: crate::ErrorCode::WrongPhase
13
14use std::fmt;
15use std::mem::size_of;
16
17use crate::error::{check, Result};
18use crate::host::Host;
19use crate::sys::{
20 AnvilGameCameraService, AnvilGameCameraView, ANVIL_GAME_CAMERA_LOCKED,
21 ANVIL_GAME_CAMERA_SERVICE_VERSION,
22};
23use crate::table::Table;
24
25/// The service's name.
26pub const SERVICE: &std::ffi::CStr = c"anvil-game.camera";
27/// The service's major version.
28pub const VERSION: u32 = ANVIL_GAME_CAMERA_SERVICE_VERSION;
29
30/// The map camera, as [`Camera::view`] reads it, in map units: `[1]` is the
31/// height above the map's plane, `[0]` and `[2]` lie on it. After a pan the
32/// game moves both points towards their targets over the next frames.
33#[derive(Clone, Copy, Debug, Default, PartialEq)]
34pub struct View {
35 /// Where the camera is.
36 pub position: [f32; 3],
37 /// The point it looks at.
38 pub look_at: [f32; 3],
39 /// The game holds the camera still (a menu is open, or its graphics
40 /// cannot draw): [`Camera::pan`] answers `WrongPhase`.
41 pub locked: bool,
42}
43
44/// The `anvil-game.camera` service.
45#[derive(Clone, Copy)]
46pub struct Camera {
47 table: Table<AnvilGameCameraService>,
48}
49
50impl fmt::Debug for Camera {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 f.debug_struct("Camera")
53 .field("size", &self.table.size())
54 .finish()
55 }
56}
57
58impl Camera {
59 /// The service; `None` without anvil-game 1.25.0 or on a game build whose
60 /// camera symbols are declared absent.
61 pub fn get(host: &Host) -> Option<Self> {
62 // SAFETY: the table type of anvil-game.camera version 1.
63 let table = unsafe { host.service::<AnvilGameCameraService>(SERVICE, VERSION) }?;
64 Some(Self { table })
65 }
66
67 /// The C table, for what this crate does not wrap.
68 pub fn c_api(&self) -> Table<AnvilGameCameraService> {
69 self.table
70 }
71
72 /// The map camera now.
73 pub fn view(&self) -> Result<View> {
74 let f = function!(self.table, AnvilGameCameraService, view)?;
75 let mut out = AnvilGameCameraView {
76 size: size_of::<AnvilGameCameraView>() as u32,
77 position: [0.0; 3],
78 look_at: [0.0; 3],
79 flags: 0,
80 };
81 // SAFETY: the service's function with a valid out pointer.
82 check(unsafe { f(&mut out) }, || "camera view")?;
83 Ok(View {
84 position: out.position,
85 look_at: out.look_at,
86 locked: out.flags & ANVIL_GAME_CAMERA_LOCKED != 0,
87 })
88 }
89
90 /// Pans the map camera for this frame as the arrow keys do: `right` and
91 /// `up` from -1 to 1, a longer vector shortened to length 1, at the
92 /// arrows' speed (the player's scroll speed, the camera's height and this
93 /// frame's time). Call it once a frame while the input lasts; a second
94 /// call in the same frame pans twice. `WrongPhase` also while the camera
95 /// is locked, `InvalidArgument` for a value that is not finite; `(0, 0)`
96 /// does nothing.
97 pub fn pan(&self, right: f32, up: f32) -> Result<()> {
98 let f = function!(self.table, AnvilGameCameraService, pan)?;
99 // SAFETY: the service's function.
100 check(unsafe { f(right, up) }, || "camera pan")
101 }
102}