oxygengine_visual_novel/
lib.rs

1extern crate oxygengine_animation as anim;
2extern crate oxygengine_core as core;
3
4pub mod background;
5pub mod character;
6pub mod dialogue;
7pub mod resource;
8pub mod scene;
9pub mod script;
10pub mod story;
11pub mod system;
12pub mod vn_story_asset_protocol;
13
14#[cfg(test)]
15mod tests;
16
17pub mod prelude {
18    pub use crate::background::*;
19    pub use crate::character::*;
20    pub use crate::dialogue::*;
21    pub use crate::resource::*;
22    pub use crate::scene::*;
23    pub use crate::script::*;
24    pub use crate::story::*;
25    pub use crate::system::*;
26    pub use crate::vn_story_asset_protocol::*;
27}
28
29use crate::{
30    resource::VnStoryManager,
31    system::{vn_story_system, VnStorySystemCache, VnStorySystemResources},
32};
33use anim::curve::{Curved, CurvedChange};
34use core::{
35    app::AppBuilder,
36    assets::database::AssetsDatabase,
37    ecs::pipeline::{PipelineBuilder, PipelineBuilderError},
38    Scalar,
39};
40use serde::{Deserialize, Serialize};
41use std::ops::{Add, Mul, Sub};
42
43pub type Scale = Position;
44
45#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)]
46pub struct Position(pub Scalar, pub Scalar);
47
48impl Curved for Position {
49    fn zero() -> Self {
50        Self(0.0, 0.0)
51    }
52
53    fn one() -> Self {
54        Self(1.0, 1.0)
55    }
56
57    fn negate(&self) -> Self {
58        Self(-self.0, -self.1)
59    }
60
61    fn scale(&self, value: Scalar) -> Self {
62        Self(self.0 * value, self.1 * value)
63    }
64
65    fn inverse_scale(&self, value: Scalar) -> Self {
66        Self(self.0 / value, self.1 / value)
67    }
68
69    fn length(&self) -> Scalar {
70        self.length_squared().sqrt()
71    }
72
73    fn length_squared(&self) -> Scalar {
74        self.0 * self.0 + self.1 * self.1
75    }
76
77    fn get_axis(&self, index: usize) -> Option<Scalar> {
78        match index {
79            0 => Some(self.0),
80            1 => Some(self.1),
81            _ => None,
82        }
83    }
84
85    fn interpolate(&self, other: &Self, factor: Scalar) -> Self {
86        let diff = *other - *self;
87        diff * factor + *self
88    }
89
90    fn is_valid(&self) -> bool {
91        self.0.is_valid() && self.1.is_valid()
92    }
93}
94
95impl CurvedChange for Position {
96    fn offset(&self, other: &Self) -> Self {
97        Self(self.0 + other.0, self.1 + other.1)
98    }
99
100    fn delta(&self, other: &Self) -> Self {
101        Self(other.0 - self.0, other.1 - self.1)
102    }
103
104    fn dot(&self, other: &Self) -> Scalar {
105        self.0 * other.0 + self.1 * other.1
106    }
107}
108
109impl Add<Self> for Position {
110    type Output = Self;
111
112    fn add(self, other: Self) -> Self {
113        Self(self.0 + other.0, self.1 + other.1)
114    }
115}
116
117impl Sub<Self> for Position {
118    type Output = Self;
119
120    fn sub(self, other: Self) -> Self {
121        Self(self.0 - other.0, self.1 - other.1)
122    }
123}
124
125impl Mul<Scalar> for Position {
126    type Output = Self;
127
128    fn mul(self, other: Scalar) -> Self {
129        Self(self.0 * other, self.1 * other)
130    }
131}
132
133impl From<(Scalar, Scalar)> for Position {
134    fn from(value: (Scalar, Scalar)) -> Self {
135        Self(value.0, value.1)
136    }
137}
138
139impl From<[Scalar; 2]> for Position {
140    fn from(value: [Scalar; 2]) -> Self {
141        Self(value[0], value[1])
142    }
143}
144
145#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)]
146pub struct Color(pub Scalar, pub Scalar, pub Scalar);
147
148impl Curved for Color {
149    fn zero() -> Self {
150        Self(0.0, 0.0, 0.0)
151    }
152
153    fn one() -> Self {
154        Self(1.0, 1.0, 1.0)
155    }
156
157    fn negate(&self) -> Self {
158        Self(-self.0, -self.1, -self.2)
159    }
160
161    fn scale(&self, value: Scalar) -> Self {
162        Self(self.0 * value, self.1 * value, self.2 * value)
163    }
164
165    fn inverse_scale(&self, value: Scalar) -> Self {
166        Self(self.0 / value, self.1 / value, self.2 / value)
167    }
168
169    fn length(&self) -> Scalar {
170        self.length_squared().sqrt()
171    }
172
173    fn length_squared(&self) -> Scalar {
174        self.0 * self.0 + self.1 * self.1 + self.2 * self.2
175    }
176
177    fn get_axis(&self, index: usize) -> Option<Scalar> {
178        match index {
179            0 => Some(self.0),
180            1 => Some(self.1),
181            2 => Some(self.2),
182            _ => None,
183        }
184    }
185
186    fn interpolate(&self, other: &Self, factor: Scalar) -> Self {
187        let diff = *other - *self;
188        diff * factor + *self
189    }
190
191    fn is_valid(&self) -> bool {
192        self.0.is_valid() && self.1.is_valid() && self.2.is_valid()
193    }
194}
195
196impl CurvedChange for Color {
197    fn offset(&self, other: &Self) -> Self {
198        Self(self.0 + other.0, self.1 + other.1, self.2 + other.2)
199    }
200
201    fn delta(&self, other: &Self) -> Self {
202        Self(other.0 - self.0, other.1 - self.1, other.2 - self.2)
203    }
204
205    fn dot(&self, other: &Self) -> Scalar {
206        self.0 * other.0 + self.1 * other.1 + self.2 * other.2
207    }
208}
209
210impl Add<Self> for Color {
211    type Output = Self;
212
213    fn add(self, other: Self) -> Self {
214        Self(self.0 + other.0, self.1 + other.1, self.2 + other.2)
215    }
216}
217
218impl Sub<Self> for Color {
219    type Output = Self;
220
221    fn sub(self, other: Self) -> Self {
222        Self(self.0 - other.0, self.1 - other.1, self.2 - other.2)
223    }
224}
225
226impl Mul<Scalar> for Color {
227    type Output = Self;
228
229    fn mul(self, other: Scalar) -> Self {
230        Self(self.0 * other, self.1 * other, self.2 * other)
231    }
232}
233
234impl From<(Scalar, Scalar, Scalar)> for Color {
235    fn from(value: (Scalar, Scalar, Scalar)) -> Self {
236        Self(value.0, value.1, value.2)
237    }
238}
239
240impl From<[Scalar; 3]> for Color {
241    fn from(value: [Scalar; 3]) -> Self {
242        Self(value[0], value[1], value[2])
243    }
244}
245
246pub fn bundle_installer<PB>(builder: &mut AppBuilder<PB>, _: ()) -> Result<(), PipelineBuilderError>
247where
248    PB: PipelineBuilder,
249{
250    builder.install_resource(VnStoryManager::default());
251    builder.install_resource(VnStorySystemCache::default());
252    builder.install_system::<VnStorySystemResources>("vn-story", vn_story_system, &[])?;
253    Ok(())
254}
255
256pub fn protocols_installer(database: &mut AssetsDatabase) {
257    database.register(vn_story_asset_protocol::VnStoryAssetProtocol);
258}