Skip to main content

souprune_api/
lib.rs

1//! WIT interface definitions for the SoupRune mod system.
2//!
3//! This crate hosts the WIT (WebAssembly Interface Types) file that defines the
4//! contract between Host (game engine) and Guest (mod). The WIT source lives in
5//! `wit/souprune-mod.wit` and is consumed by both `wasmtime::component::bindgen!`
6//! (host side) and `wit_bindgen::generate!` (guest side).
7//!
8//! Additionally, shared plain-Rust types that mirror WIT records are provided here
9//! so that engine code which does *not* link against wasmtime can still work with
10//! the same data shapes.
11//!
12//! SoupRune 模组系统的 WIT 接口定义。
13//! WIT 源文件位于 `wit/souprune-mod.wit`,由宿主侧和客体侧共同使用。
14
15/// Semantic input action, mirrors the WIT `action` enum.
16///
17/// 语义输入动作,对应 WIT 中的 `action` 枚举。
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19#[repr(u8)]
20pub enum Action {
21    Up = 0,
22    Down = 1,
23    Left = 2,
24    Right = 3,
25    Confirm = 4,
26    Cancel = 5,
27    Menu = 6,
28}
29
30/// 2D vector, mirrors the WIT `vec2` record.
31///
32/// 二维向量,对应 WIT 中的 `vec2` 记录。
33#[derive(Debug, Clone, Copy, Default, PartialEq)]
34pub struct Vec2 {
35    pub x: f32,
36    pub y: f32,
37}
38
39impl Vec2 {
40    pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
41
42    pub fn new(x: f32, y: f32) -> Self {
43        Self { x, y }
44    }
45}
46
47/// Named property for danmaku behaviors, mirrors the WIT `prop` record.
48///
49/// 弹幕行为的命名属性,对应 WIT 中的 `prop` 记录。
50#[derive(Debug, Clone)]
51pub struct Prop {
52    pub name: String,
53    pub value: f32,
54}
55
56/// Bullet context passed to danmaku callbacks, mirrors the WIT `bullet-context` record.
57///
58/// 传递给弹幕回调的上下文,对应 WIT 中的 `bullet-context` 记录。
59#[derive(Debug, Clone, Default)]
60pub struct BulletContext {
61    pub elapsed: f32,
62    pub delta_time: f32,
63    pub spawn_pos: Vec2,
64    pub offset: Vec2,
65    pub initial_angle: f32,
66    pub initial_radius: f32,
67    pub player_pos: Vec2,
68    pub props: Vec<Prop>,
69}
70
71/// Output from a danmaku on-update call, mirrors the WIT `bullet-output` record.
72///
73/// 弹幕 on-update 调用的输出,对应 WIT 中的 `bullet-output` 记录。
74#[derive(Debug, Clone, Copy, Default)]
75pub struct BulletOutput {
76    pub offset: Vec2,
77    pub rotation: f32,
78    /// Opacity override. Negative means no change.
79    pub opacity: f32,
80    /// Scale delta (0.0 = no change from base scale).
81    pub scale_x: f32,
82    pub scale_y: f32,
83}
84
85impl BulletOutput {
86    pub const ZERO: Self = Self {
87        offset: Vec2::ZERO,
88        rotation: 0.0,
89        opacity: -1.0,
90        scale_x: 0.0,
91        scale_y: 0.0,
92    };
93
94    pub fn new(x: f32, y: f32) -> Self {
95        Self {
96            offset: Vec2::new(x, y),
97            ..Self::ZERO
98        }
99    }
100
101    pub fn with_rotation(mut self, rotation: f32) -> Self {
102        self.rotation = rotation;
103        self
104    }
105}
106
107// ============================================================================
108// Spawn Pattern Types
109// ============================================================================
110
111/// A computed spawn point, mirrors the WIT `spawn-point` record.
112#[derive(Debug, Clone, Copy, Default)]
113pub struct SpawnPoint {
114    pub x: f32,
115    pub y: f32,
116    pub angle: f32,
117    pub radius: f32,
118}
119
120/// Context for spawn pattern generation, mirrors the WIT `spawn-context` record.
121#[derive(Debug, Clone, Copy, Default)]
122pub struct SpawnContext {
123    pub center_x: f32,
124    pub center_y: f32,
125    pub player_x: f32,
126    pub player_y: f32,
127    pub time: f32,
128}
129
130/// Named parameter for spawn patterns, mirrors the WIT `pattern-param` record.
131#[derive(Debug, Clone)]
132pub struct PatternParam {
133    pub name: String,
134    pub value: f64,
135}
136
137/// Returns the path to the WIT directory shipped with this crate.
138///
139/// 返回此 crate 附带的 WIT 目录路径。
140pub fn wit_dir() -> &'static str {
141    concat!(env!("CARGO_MANIFEST_DIR"), "/wit")
142}