Skip to content

Commit

Permalink
chore: moving more boxes out of ryot crate
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrossi committed Apr 24, 2024
1 parent 360e1db commit d597661
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 140 deletions.
2 changes: 1 addition & 1 deletion crates/ryot/src/bevy_ryot/drawing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::time::Duration;

use crate::bevy_ryot::{GameObjectId, RyotContentState};
use crate::position::SpriteMovement;
use bevy::prelude::*;
use bevy::render::view::{check_visibility, VisibilitySystems, VisibleEntities};
use ryot_content::prelude::FrameGroup;
Expand All @@ -14,6 +13,7 @@ mod commands;
pub use commands::*;

mod systems;
use crate::prelude::SpriteMovement;
pub use systems::*;

pub struct DrawingPlugin;
Expand Down
17 changes: 16 additions & 1 deletion crates/ryot/src/bevy_ryot/game/elevation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::bevy_ryot::sprites::SPRITE_BASE_SIZE;
use bevy::prelude::*;
use itertools::Itertools;
use ryot_content::prelude::{EntityType, GameObjectId, VisualElements};
use ryot_content::prelude::{EntityType, GameObjectId, SpriteLayout, VisualElements};
use ryot_tiled::prelude::*;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
Expand Down Expand Up @@ -87,3 +87,18 @@ pub(crate) fn apply_elevation(
});
}
}

pub fn elevate_position(
position: &TilePosition,
layout: SpriteLayout,
layer: Layer,
elevation: Elevation,
) -> Vec3 {
let anchor = Vec2::new(
elevation.elevation.clamp(0.0, 1.0),
(-elevation.elevation).clamp(-1.0, 0.0),
);
position.to_vec3(&layer)
- (SpriteLayout::OneByOne.get_size(&tile_size()).as_vec2() * anchor).extend(0.)
- (layout.get_size(&tile_size()).as_vec2() * Vec2::new(0.5, -0.5)).extend(0.)
}
13 changes: 9 additions & 4 deletions crates/ryot/src/bevy_ryot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//! 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,
//! configuring the game, and handling asynchronous events.
#[cfg(feature = "debug")]
use crate::position::debug_sprite_position;
use crate::prelude::*;
use bevy::app::{App, Plugin, Update};
use bevy::prelude::*;
Expand All @@ -20,14 +18,16 @@ pub mod drawing;

pub mod sprites;

pub mod position;
pub(crate) mod sprite_animations;

pub use sprite_animations::{toggle_sprite_animation, AnimationDuration};

pub struct RyotLegacySpritePlugin;

impl Plugin for RyotLegacySpritePlugin {
fn build(&self, app: &mut App) {
app.add_plugins(ryot_sprites::prelude::RyotSpritePlugin);
app.add_plugins(RyotSpritePlugin);

app.add_optional_plugin(StrokedTextPlugin)
.init_resource::<sprite_animations::SpriteAnimationEnabled>()
Expand All @@ -45,7 +45,12 @@ impl Plugin for RyotLegacySpritePlugin {
.pipe(sprites::store_loaded_appearances_system)
.run_if(on_event::<sprites::LoadAppearanceEvent>())
.in_set(SpriteSystems::Load),
sprites::initialize_sprite_material.in_set(SpriteSystems::Initialize),
(
#[cfg(feature = "debug")]
sprites::debug_sprites,
sprites::initialize_elevation,
)
.in_set(SpriteSystems::Initialize),
sprites::update_sprite_system.in_set(SpriteSystems::Update),
sprite_animations::initialize_animation_sprite_system
.in_set(AnimationSystems::Initialize),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use glam::Vec3;

#[cfg(feature = "bevy")]
use crate::bevy_ryot::elevation::Elevation;
use crate::sprites::elevate_position;
#[cfg(feature = "bevy")]
use crate::prelude::elevation::elevate_position;
#[cfg(feature = "bevy")]
use ryot_content::prelude::SpriteLayout;
use ryot_tiled::prelude::*;
Expand Down
30 changes: 12 additions & 18 deletions crates/ryot/src/bevy_ryot/sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use self::sprite_animations::{
AnimationDescriptor, AnimationKey, AnimationSprite, SpriteAnimationExt,
};
use bevy::prelude::*;
use bevy::sprite::{MaterialMesh2dBundle, Mesh2dHandle};
use bevy::sprite::Mesh2dHandle;
use bevy::utils::{HashMap, HashSet};
use itertools::Itertools;
use ryot_tiled::prelude::*;
Expand Down Expand Up @@ -152,27 +152,21 @@ pub(crate) fn load_sprite_texture(
Some(texture)
}

/// A system that ensures that all entities with an GameObjectId have a SpriteMaterial mesh bundle.
pub(crate) fn initialize_sprite_material(
pub(crate) fn initialize_elevation(
mut commands: Commands,
query: Query<(Entity, Has<Elevation>), (With<GameObjectId>, Without<Handle<SpriteMaterial>>)>,
#[cfg(feature = "debug")] q_debug: Query<
(Entity, &Layer),
(With<GameObjectId>, Without<Handle<SpriteMaterial>>),
>,
query: Query<Entity, (With<GameObjectId>, Without<Elevation>)>,
) {
query.iter().for_each(|(entity, has_elevation)| {
commands.entity(entity).insert((
MaterialMesh2dBundle::<SpriteMaterial>::default(),
SpriteLayout::default(),
));

if !has_elevation {
commands.entity(entity).insert(Elevation::default());
}
query.iter().for_each(|entity| {
commands.entity(entity).insert(Elevation::default());
});
}

#[cfg(feature = "debug")]
/// A system that ensures that all entities with an GameObjectId have a SpriteMaterial mesh bundle.
#[cfg(feature = "debug")]
pub(crate) fn debug_sprites(
mut commands: Commands,
q_debug: Query<(Entity, &Layer), (With<GameObjectId>, Without<Handle<SpriteMaterial>>)>,
) {
q_debug.iter().for_each(|(entity, layer)| {
commands.entity(entity).with_children(|builder| {
builder.spawn((
Expand Down
7 changes: 1 addition & 6 deletions crates/ryot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
#[cfg(feature = "bevy")]
pub mod bevy_ryot;

pub mod sprites;

pub use sprites::*;

pub mod prelude {
pub use crate::bevy_ryot::position::*;
#[cfg(feature = "bevy")]
pub use crate::bevy_ryot::*;
pub use crate::position::*;
pub use crate::sprites::*;
pub use ryot_internal::prelude::*;
}
8 changes: 8 additions & 0 deletions crates/ryot_asset_loading/src/atlas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use bevy_asset::Handle;
use bevy_asset_loader::asset_collection::AssetCollection;
use bevy_ecs::prelude::Resource;
use bevy_sprite::TextureAtlasLayout;

pub trait AtlasLayoutsAsset: Resource + AssetCollection + Send + Sync + 'static {
fn atlas_layouts(&self) -> &Vec<Handle<TextureAtlasLayout>>;
}
5 changes: 2 additions & 3 deletions crates/ryot_asset_loading/src/catalog.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use bevy_asset::{Asset, Assets, Handle};
use bevy_asset_loader::asset_collection::AssetCollection;
use bevy_ecs::change_detection::ResMut;
use bevy_ecs::prelude::{Res, Resource};
use bevy_ecs::prelude::Res;
use bevy_reflect::TypePath;
use bevy_utils::tracing::debug;
use ryot_content::prelude::SpriteSheetDataSet;

pub trait CatalogAsset: Resource + AssetCollection + Send + Sync + 'static {
pub trait CatalogAsset: crate::RyotAsset {
fn catalog_content(&self) -> &Handle<Catalog>;
}

Expand Down
8 changes: 8 additions & 0 deletions crates/ryot_asset_loading/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#![feature(trait_alias)]

use bevy_asset_loader::asset_collection::AssetCollection;
use bevy_ecs::prelude::Resource;

pub mod atlas;
pub mod catalog;
pub mod plugins;
pub mod sprites;
pub mod visual_elements;

pub trait RyotAsset = Resource + AssetCollection + Send + Sync + 'static;

pub mod prelude {
pub use crate::{
atlas::AtlasLayoutsAsset,
catalog::{prepare_sprite_sheets, Catalog, CatalogAsset},
content_plugin,
plugins::{BaseContentPlugin, MetaContentPlugin, VisualContentPlugin},
Expand Down
1 change: 0 additions & 1 deletion crates/ryot_asset_loading/src/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use bevy_asset_loader::prelude::{
use bevy_common_assets::json::JsonAssetPlugin;
use bevy_ecs::prelude::{IntoSystemConfigs, OnEnter};
use ryot_content::prelude::*;
use ryot_sprites::atlas::AtlasLayoutsAsset;
use ryot_tiled::prelude::TilePosition;
use std::marker::PhantomData;

Expand Down
1 change: 1 addition & 0 deletions crates/ryot_asset_loading/src/sprites.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::prelude::AtlasLayoutsAsset;
use bevy_asset::Assets;
use bevy_ecs::change_detection::{Res, ResMut};
use bevy_math::prelude::Rectangle;
Expand Down
4 changes: 1 addition & 3 deletions crates/ryot_asset_loading/src/visual_elements.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use bevy_asset::Assets;
use bevy_asset_loader::prelude::AssetCollection;
use bevy_ecs::change_detection::{Res, ResMut};
use bevy_ecs::prelude::Resource;
use bevy_utils::tracing::debug;
use ryot_content::prelude::VisualElements;

pub trait VisualElementsAsset: Resource + AssetCollection + Send + Sync + 'static {
pub trait VisualElementsAsset: crate::RyotAsset {
fn visual_elements(&self) -> &bevy_asset::Handle<VisualElements>;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod sprite_layout_test;
mod sprite_sheet_tests;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::*;
use crate::prelude::SpriteLayout;
use glam::UVec2;
use rstest::rstest;
use serde_json::to_string;
Expand Down
File renamed without changes.
26 changes: 2 additions & 24 deletions crates/ryot_sprites/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,31 @@ categories = ["game-development", "game-engines", "games"]
rustdoc-args = ["-Zunstable-options", "--cfg", "docsrs"]
all-features = true

[features]
default = ["bevy"]

bevy = [
"dep:bevy_app",
"dep:bevy_asset",
"dep:bevy_ecs",
"dep:bevy_reflect",
"dep:bevy_render",
"dep:bevy_sprite",
"dep:bevy_utils",
"dep:bevy_asset_loader",
]

[dependencies.bevy_app]
version = "0.13"
optional = true

[dependencies.bevy_asset]
version = "0.13"
optional = true

[dependencies.bevy_ecs]
version = "0.13"
features = ["bevy_reflect"]
optional = true

[dependencies.bevy_reflect]
version = "0.13"
features = ["bevy"]
optional = true

[dependencies.bevy_render]
version = "0.13"
optional = true

[dependencies.bevy_sprite]
version = "0.13"
optional = true

[dependencies.bevy_utils]
version = "0.13"
optional = true

[dependencies.bevy_asset_loader]
version = "0.20.1"
optional = true

[dependencies]
ryot_core = { path = "../ryot_core", version = "0.2.0" }
Expand All @@ -77,10 +55,10 @@ serde.workspace = true
serde_json.workspace = true
strum.workspace = true
thiserror.workspace = true
rand.workspace = true

[dev-dependencies]
quickcheck.workspace = true
quickcheck_macros.workspace = true
rstest.workspace = true
time-test.workspace = true
rand.workspace = true
time-test.workspace = true
7 changes: 7 additions & 0 deletions crates/ryot_sprites/src/animations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use bevy_ecs::prelude::*;

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum AnimationSystems {
Initialize,
Update,
}
6 changes: 0 additions & 6 deletions crates/ryot_sprites/src/atlas.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
use bevy_asset::Handle;
use bevy_asset_loader::asset_collection::AssetCollection;
use bevy_ecs::prelude::Resource;
use bevy_sprite::TextureAtlasLayout;
use derive_more::{Deref, DerefMut};

pub trait AtlasLayoutsAsset: Resource + AssetCollection + Send + Sync + 'static {
fn atlas_layouts(&self) -> &Vec<Handle<TextureAtlasLayout>>;
}

#[derive(Debug, Default, Clone, Resource, Deref, DerefMut)]
pub struct TextureAtlasLayouts(Vec<TextureAtlasLayout>);
31 changes: 10 additions & 21 deletions crates/ryot_sprites/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[cfg(feature = "bevy")]
use bevy_ecs::prelude::SystemSet;

pub mod animations;
pub mod atlas;
#[cfg(feature = "bevy")]
pub mod material;
#[cfg(feature = "bevy")]
pub mod meshes;
#[cfg(feature = "bevy")]
pub mod params;
pub mod plugins;

pub static SPRITE_SHEET_FOLDER: &str = "sprite-sheets";
Expand All @@ -13,33 +13,22 @@ pub fn get_decompressed_file_name(file_name: &str) -> String {
file_name.replace(".bmp.lzma", ".png")
}

#[derive(Debug, Hash, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "bevy", derive(bevy_ecs::prelude::SystemSet))]
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum SpriteSystems {
Load,
Initialize,
Update,
}

#[derive(Debug, Hash, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "bevy", derive(bevy_ecs::prelude::SystemSet))]
pub enum AnimationSystems {
Initialize,
Update,
}

pub mod prelude {
pub use crate::{get_decompressed_file_name, SPRITE_SHEET_FOLDER};

#[cfg(feature = "bevy")]
pub use crate::{
atlas::{AtlasLayoutsAsset, TextureAtlasLayouts},
animations::AnimationSystems,
atlas::TextureAtlasLayouts,
get_decompressed_file_name,
material::SpriteMaterial,
meshes::{RectMeshes, SpriteMeshes},
params::{SpriteOutline, SpriteParams},
plugins::RyotSpritePlugin,
AnimationSystems, SpriteSystems,
SpriteSystems, SPRITE_SHEET_FOLDER,
};
}

#[cfg(test)]
mod tests;
Loading

0 comments on commit d597661

Please sign in to comment.