ryot 0.2.2

MMORPG library based on the concepts of open tibia written in rust and bevy.
Documentation
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Bevy plugins and utilities for RyOT games.
//!
//! This module is intended to be used as a library dependency for RyOT games.
//! It provides common ways of dealing with OT content, such as loading sprites and appearances,
//! configuring the game, and handling asynchronous events.
use self::sprites::SpriteMaterial;
use crate::appearances::{ContentType, SpriteSheetDataSet};
#[cfg(feature = "debug")]
use crate::position::debug_sprite_position;
use crate::position::*;
use crate::{Layer, SpriteLayout, TILE_SIZE};
use bevy::app::{App, Plugin, Update};
use bevy::asset::embedded_asset;
use bevy::asset::{Asset, Assets, Handle};
use bevy::prelude::*;
use bevy::render::mesh::Indices;
use bevy::render::render_asset::RenderAssetUsages;
use bevy::render::render_resource::PrimitiveTopology;
use bevy::sprite::{Anchor, Material2dPlugin, MaterialMesh2dBundle};
use bevy::utils::HashMap;
use bevy_asset_loader::asset_collection::AssetCollection;
use bevy_asset_loader::loading_state::{LoadingState, LoadingStateAppExt};
use bevy_asset_loader::prelude::*;
use bevy_asset_loader::standard_dynamic_asset::StandardDynamicAssetArrayCollection;
use bevy_common_assets::json::JsonAssetPlugin;
use bevy_stroked_text::StrokedTextPlugin;
use std::marker::PhantomData;
use strum::IntoEnumIterator;

mod appearances;
pub use appearances::*;

mod async_events;
pub use async_events::*;

pub mod camera;

mod conditions;
pub use conditions::*;

mod game;
pub use game::*;

#[cfg(feature = "lmdb")]
pub mod lmdb;

pub mod map;

pub mod drawing;

pub mod sprites;

pub mod perspective;

pub(crate) mod sprite_animations;

pub use sprite_animations::{toggle_sprite_animation, AnimationDuration};

pub static RYOT_ANCHOR: Anchor = Anchor::BottomRight;
pub static GRID_LAYER: Layer = Layer::Hud(0);

/// A generic cache structure leveraging `HashMap` for storing and quickly accessing data.
/// This structure is particularly useful for caching expensive computations, assets, or
/// other data for rapid retrieval.
#[derive(Resource, Default, Deref, DerefMut)]
pub struct Cache<K, V>(HashMap<K, V>);

/// Defines system sets for managing cache-related systems.
/// This enum is used to organize and control the execution order of systems that interact with
/// caches, allowing for a structured update and clean-up process.
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum CacheSystems {
    UpdateCache,
    CleanCache,
}

/// The states that the content loading process can be in.
/// This is used to track the progress of the content loading process.
/// It's also used to determine if the content is ready to be used.
/// It's internally used by the `ContentPlugin` and should not be manipulated directly.
/// Can be checked by applications to perform actions that depend on the state of the content.
#[derive(States, Default, Clone, Eq, PartialEq, Debug, Hash)]
pub enum InternalContentState {
    #[default]
    LoadingContent,
    PreparingContent,
    Ready,
}

/// An asset that holds a collection of raw content configs.
#[derive(serde::Deserialize, Asset, TypePath)]
#[serde(transparent)]
pub struct Catalog {
    pub content: Vec<ContentType>,
}

#[derive(Resource, Debug, Clone, Default, Deref, DerefMut)]
pub struct SpriteMeshes(pub HashMap<SpriteLayout, Handle<Mesh>>);

#[derive(Resource, Debug, Clone, Default, Deref, DerefMut)]
pub struct RectMeshes(pub HashMap<SpriteLayout, Handle<Mesh>>);

/// A trait that represents Preloaded and Content assets.
/// Most of the PreloadedAssets are not directly available to the game.
/// They are only used to prepare the ContentAssets and then discarded.
/// That's why there is a separation between PreloadedAssets and ContentAssets.
pub trait PreloadedContentAssets: PreloadedAssets + ContentAssets {}

/// A trait that represents assets that are preloaded within ryot.
/// It contains preloaded assets and mutable preparation methods for the ContentAssets.
pub trait PreloadedAssets:
    Resource + AppearanceAssets + AssetCollection + Send + Sync + 'static
{
    fn catalog_content(&self) -> Handle<Catalog>;
    fn set_sprite_sheets_data(&mut self, sprite_sheet_set: SpriteSheetDataSet);
}

/// The main ContentAssets of a Ryot game, is prepared by preparer systems
/// during the loading process and is used by the game to access the content.
pub trait ContentAssets: Resource + AppearanceAssets + Send + Sync + 'static {
    fn sprite_sheet_data_set(&self) -> Option<&SpriteSheetDataSet>;
    fn get_texture(&self, file: &str) -> Option<Handle<Image>>;
    fn get_atlas_layout(&self, layout: SpriteLayout) -> Option<Handle<TextureAtlasLayout>>;
}

pub trait AppearanceAssets: Resource + AssetCollection + Send + Sync + 'static {
    fn appearances(&self) -> Handle<Appearance>;
    fn prepared_appearances_mut(&mut self) -> &mut PreparedAppearances;
    fn prepared_appearances(&self) -> &PreparedAppearances;
}

/// A plugin that registers implementations of ContentAssets and loads them.
/// It inits the necessary resources and adds the necessary systems and plugins to load
/// the content assets.
///
/// It also manages the loading state of the content assets, the lifecycle of the content
/// and the events that allow lazy loading of sprites.
macro_rules! content_plugin {
    ($plugin_name:ident, $content_assets:tt) => {
        pub struct $plugin_name<C: $content_assets>(PhantomData<C>);

        impl<C: $content_assets> Default for $plugin_name<C> {
            fn default() -> Self {
                Self(PhantomData)
            }
        }
    };
}

content_plugin!(BaseContentPlugin, AppearanceAssets);

impl<C: AppearanceAssets + Default> Plugin for BaseContentPlugin<C> {
    fn build(&self, app: &mut App) {
        app.init_state::<InternalContentState>()
            .init_resource::<C>()
            .register_type::<TilePosition>()
            .add_plugins(AppearanceAssetPlugin)
            .add_loading_state(
                LoadingState::new(InternalContentState::LoadingContent)
                    .continue_to_state(InternalContentState::PreparingContent)
                    .load_collection::<C>(),
            )
            .add_systems(
                OnEnter(InternalContentState::PreparingContent),
                prepare_appearances::<C>,
            );
    }
}

content_plugin!(MetaContentPlugin, AppearanceAssets);

impl<C: AppearanceAssets + Default> Plugin for MetaContentPlugin<C> {
    fn build(&self, app: &mut App) {
        app.add_plugins(BaseContentPlugin::<C>::default())
            .add_systems(
                OnEnter(InternalContentState::PreparingContent),
                transition_to_ready.after(prepare_appearances::<C>),
            );
    }
}

content_plugin!(VisualContentPlugin, PreloadedContentAssets);

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum SpriteSystems {
    Load,
    Initialize,
    Update,
}

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum AnimationSystems {
    Initialize,
    Update,
}

impl<C: PreloadedContentAssets + Default> Plugin for VisualContentPlugin<C> {
    fn build(&self, app: &mut App) {
        app.add_plugins(BaseContentPlugin::<C>::default())
            .configure_loading_state(
                LoadingStateConfig::new(InternalContentState::LoadingContent)
                    .register_dynamic_asset_collection::<StandardDynamicAssetArrayCollection>()
                    .with_dynamic_assets_file::<StandardDynamicAssetArrayCollection>(
                        "dynamic.atlases.ron",
                    ),
            )
            .init_resource::<SpriteMeshes>()
            .init_resource::<RectMeshes>()
            .add_plugins(JsonAssetPlugin::<Catalog>::new(&["json"]))
            .add_optional_plugin(StrokedTextPlugin)
            .add_plugins(Material2dPlugin::<SpriteMaterial>::default())
            .add_systems(
                OnEnter(InternalContentState::PreparingContent),
                (prepare_content::<C>, transition_to_ready)
                    .chain()
                    .after(prepare_appearances::<C>),
            )
            .init_resource::<sprite_animations::SpriteAnimationEnabled>()
            .init_resource::<sprite_animations::SynchronizedAnimationTimers>()
            .init_resource::<sprites::LoadedAppearances>()
            .add_event::<sprites::LoadAppearanceEvent>()
            .add_systems(
                Update,
                (
                    #[cfg(feature = "debug")]
                    debug_sprite_position,
                    sprites::load_from_entities_system.in_set(SpriteSystems::Load),
                    sprites::process_load_events_system::<C>
                        .pipe(sprites::load_sprite_system::<C>)
                        .pipe(sprites::store_loaded_appearances_system)
                        .run_if(on_event::<sprites::LoadAppearanceEvent>())
                        .in_set(SpriteSystems::Load),
                    sprites::ensure_appearance_initialized.in_set(SpriteSystems::Initialize),
                    sprites::update_sprite_system.in_set(SpriteSystems::Update),
                    sprite_animations::initialize_animation_sprite_system
                        .in_set(AnimationSystems::Initialize),
                    sprite_animations::tick_animation_system
                        .run_if(resource_exists_and_equals(
                            sprite_animations::SpriteAnimationEnabled(true),
                        ))
                        .in_set(AnimationSystems::Update),
                )
                    .chain()
                    .run_if(in_state(InternalContentState::Ready)),
            )
            .add_systems(
                PostUpdate,
                (
                    update_sprite_position,
                    (move_sprites_with_animation, finish_position_animation).chain(),
                ),
            );

        embedded_asset!(app, "shaders/sprite.wgsl");
    }
}

/// A system that prepares the content assets for use in the game.
/// It transforms the raw content configs into sprite sheet sets and stores them in
/// a way that the game can use them.
///
/// This is the last step of the content loading process, triggering the sprite loading process.
fn prepare_content<C: PreloadedContentAssets>(
    mut contents: ResMut<Assets<Catalog>>,
    mut content_assets: ResMut<C>,
    mut meshes: ResMut<Assets<Mesh>>,
    mut sprite_meshes: ResMut<SpriteMeshes>,
    mut rect_meshes: ResMut<RectMeshes>,
    atlas_layouts: Res<Assets<TextureAtlasLayout>>,
) {
    debug!("Preparing content");
    let layout = content_assets
        .get_atlas_layout(SpriteLayout::OneByOne)
        .expect("OneByOne layout not found");

    let atlas_layout = atlas_layouts.get(layout).expect("No atlas layout");

    TILE_SIZE
        .set(atlas_layout.textures[0].size().as_uvec2())
        .expect("Failed to initialize tile size");

    let catalog = contents
        .get(content_assets.catalog_content())
        .expect("No catalog loaded");

    content_assets.set_sprite_sheets_data(SpriteSheetDataSet::from_content(&catalog.content));
    contents.remove(content_assets.catalog_content());
    for sprite_layout in SpriteLayout::iter() {
        sprite_meshes.insert(
            sprite_layout,
            meshes.add(Rectangle::from_size(
                sprite_layout.get_size(&tile_size()).as_vec2() * 2.,
            )),
        );
        rect_meshes.insert(
            sprite_layout,
            meshes.add(Rectangle::from_size(
                sprite_layout.get_size(&tile_size()).as_vec2(),
            )),
        );
    }

    debug!("Finished preparing content");
}

fn transition_to_ready(mut state: ResMut<NextState<InternalContentState>>) {
    state.set(InternalContentState::Ready);
}

/// Quick way to create WASM compatible windows with a title.
pub fn entitled_window(title: String) -> WindowPlugin {
    WindowPlugin {
        primary_window: Some(Window {
            name: Some(title.clone()),
            title,
            // Bind to canvas included in `index.html`
            canvas: Some("#bevy".to_owned()),
            #[cfg(target_arch = "wasm32")]
            mode: bevy::window::WindowMode::SizedFullscreen,
            ..default()
        }),
        ..default()
    }
}

/// Helper trait to add plugins only if they haven't been added already.
/// This is useful for external plugins that are used by multiple plugins or dependencies
/// and should only be added once.
///
/// # Example
/// You have a UI plugin dependent on Egui but you also use Bevy's inspector plugin that uses Egui.
/// You can use add_optional_plugin(EguiPlugin) in your UI plugin to avoid adding EguiPlugin twice,
/// clashing with the inspector plugin.
///
/// So instead of having
/// ```rust
/// use bevy::prelude::*;
/// use bevy::time::TimePlugin;
///
/// pub struct MyPlugin;
///
/// impl Plugin for MyPlugin {
///     fn build(&self, app: &mut App) {
///         if !app.is_plugin_added::<TimePlugin>() {
///             app.add_plugins(TimePlugin);
///         }
///
///        //...
///     }
/// }
/// ```
/// You can do
/// ```rust
/// use bevy::prelude::*;
/// use bevy::time::TimePlugin;
/// use ryot::prelude::OptionalPlugin;
///
/// pub struct MyPlugin;
///
/// impl Plugin for MyPlugin {
///     fn build(&self, app: &mut App) {
///        app.add_optional_plugin(TimePlugin);
///
///        //...
///     }
/// }
/// ```
pub trait OptionalPlugin {
    fn add_optional_plugin<T: Plugin>(&mut self, plugin: T) -> &mut Self;
}

impl OptionalPlugin for App {
    fn add_optional_plugin<T: Plugin>(&mut self, plugin: T) -> &mut Self {
        if !self.is_plugin_added::<T>() {
            self.add_plugins(plugin);
        }

        self
    }
}

#[derive(Component, Debug, Clone)]
pub struct GridView;

/// A system to spawn a grid of lines to represent the tiles in the game using a custom color.
pub fn spawn_grid<C: ContentAssets>(
    color: Color,
) -> impl FnMut(
    Commands,
    Res<Assets<TextureAtlasLayout>>,
    Res<C>,
    ResMut<Assets<Mesh>>,
    ResMut<Assets<ColorMaterial>>,
) {
    move |mut commands: Commands,
          atlas_layouts: Res<Assets<TextureAtlasLayout>>,
          content_assets: Res<C>,
          mut meshes: ResMut<Assets<Mesh>>,
          mut materials: ResMut<Assets<ColorMaterial>>| {
        let mut positions = Vec::new();
        let mut colors = Vec::new();
        let mut indices = Vec::new();
        let mut idx = 0;

        let (bottom_left_tile, top_right_tile) = (TilePosition::MIN, TilePosition::MAX);
        let (bottom_left, top_right) = (Vec2::from(bottom_left_tile), Vec2::from(top_right_tile));

        let layout = content_assets
            .get_atlas_layout(SpriteLayout::OneByOne)
            .expect("OneByOne layout not found");

        let tile_size = atlas_layouts
            .get(layout)
            .expect("No atlas layout found")
            .textures[0]
            .size();

        for col in bottom_left_tile.x - 1..=top_right_tile.x {
            let x_offset = (col * tile_size.x as i32) as f32;

            positions.push([x_offset, bottom_left.y, 0.0]);
            positions.push([x_offset, top_right.y + tile_size.y, 0.0]);

            colors.extend(vec![color.as_rgba_f32(); 2]);

            indices.extend_from_slice(&[idx, idx + 1]);
            idx += 2;
        }

        for row in bottom_left_tile.y - 1..=top_right_tile.y {
            let y_offset = (row * tile_size.y as i32) as f32;

            positions.push([bottom_left.x - tile_size.x, y_offset, 0.0]);
            positions.push([top_right.x, y_offset, 0.0]);

            colors.extend(vec![color.as_rgba_f32(); 2]);

            indices.extend_from_slice(&[idx, idx + 1]);
            idx += 2;
        }

        let mut mesh = Mesh::new(PrimitiveTopology::LineList, RenderAssetUsages::default());
        mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
        mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
        mesh.insert_indices(Indices::U32(indices));

        let mesh_handle: Handle<Mesh> = meshes.add(mesh);

        commands.spawn((
            GridView,
            MaterialMesh2dBundle {
                mesh: mesh_handle.into(),
                transform: Transform::from_translation(Vec2::ZERO.extend(GRID_LAYER.z())),
                material: materials.add(ColorMaterial::default()),
                ..default()
            },
        ));
    }
}