Skip to content

Commit

Permalink
Draw in render world progress 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldom-SE committed Jul 25, 2024
1 parent ee1fe66 commit a17c991
Show file tree
Hide file tree
Showing 10 changed files with 757 additions and 622 deletions.
47 changes: 30 additions & 17 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::time::Duration;

use bevy::render::extract_resource::{ExtractResource, ExtractResourcePlugin};
use bevy::utils::Instant;

use crate::position::Spatial;
Expand All @@ -13,22 +14,23 @@ use crate::{
};

pub(crate) fn plug(app: &mut App) {
app.configure_sets(
PostUpdate,
PxSet::FinishAnimations
.after(PxSet::LoadAssets)
.before(PxSet::Draw),
)
.add_systems(
PostUpdate,
(
finish_animations::<PxSprite>,
finish_animations::<PxFilter>,
finish_animations::<PxTypeface>,
finish_animations::<PxTileset>,
app.add_plugins(ExtractResourcePlugin::<LastUpdate>::default())
.configure_sets(
PostUpdate,
PxSet::FinishAnimations
.after(PxSet::LoadAssets)
.before(PxSet::Draw),
)
.in_set(PxSet::FinishAnimations),
);
.add_systems(
PostUpdate,
(
finish_animations::<PxSprite>,
finish_animations::<PxFilter>,
finish_animations::<PxTypeface>,
finish_animations::<PxTileset>,
)
.in_set(PxSet::FinishAnimations),
);
}

/// Direction the animation plays
Expand Down Expand Up @@ -289,6 +291,17 @@ pub(crate) fn draw_spatial<'a, A: Animation + Spatial>(
draw_animation(spatial, param, &mut image, animation, filters);
}

#[derive(Resource)]
pub(crate) struct LastUpdate(pub(crate) Instant);

impl ExtractResource for LastUpdate {
type Source = Time<Real>;

fn extract_resource(source: &Time<Real>) -> Self {
Self(source.last_update().unwrap_or_else(|| source.startup()))
}
}

pub(crate) fn copy_animation_params(
params: Option<(
&PxAnimationDirection,
Expand All @@ -297,7 +310,7 @@ pub(crate) fn copy_animation_params(
&PxAnimationFrameTransition,
&PxAnimationStart,
)>,
time: &Time<Real>,
last_update: Instant,
) -> Option<(
PxAnimationDirection,
PxAnimationDuration,
Expand All @@ -312,7 +325,7 @@ pub(crate) fn copy_animation_params(
duration,
on_finish,
frame_transition,
time.last_update().unwrap_or_else(|| time.startup()) - start,
last_update - start,
)
},
)
Expand Down
7 changes: 5 additions & 2 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use bevy::render::extract_resource::{ExtractResource, ExtractResourcePlugin};

use crate::prelude::*;

pub(crate) fn plug(app: &mut App) {
app.init_resource::<PxCamera>();
app.add_plugins(ExtractResourcePlugin::<PxCamera>::default())
.init_resource::<PxCamera>();
}

/// Resource that represents the camera's position
#[derive(Clone, Copy, Debug, Default, Deref, DerefMut, Resource)]
#[derive(ExtractResource, Resource, Deref, DerefMut, Clone, Copy, Default, Debug)]
pub struct PxCamera(pub IVec2);

/// Determines whether the entity is locked to the camera
Expand Down
2 changes: 1 addition & 1 deletion src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) fn plug(app: &mut App) {
PostUpdate,
(
change_cursor.before(PxSet::DrawCursor),
draw_cursor.in_set(PxSet::DrawCursor),
// draw_cursor.in_set(PxSet::DrawCursor),
),
);
}
Expand Down
26 changes: 23 additions & 3 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::time::Duration;
use anyhow::{Error, Result};
use bevy::{
asset::{io::Reader, AssetLoader, LoadContext},
render::texture::{ImageLoader, ImageLoaderSettings},
render::{
render_asset::{PrepareAssetError, RenderAsset, RenderAssetPlugin, RenderAssetUsages},
texture::{ImageLoader, ImageLoaderSettings},
},
};

use crate::{
Expand All @@ -18,7 +21,8 @@ use crate::{
};

pub(crate) fn plug(app: &mut App) {
app.init_asset::<PxFilter>()
app.add_plugins(RenderAssetPlugin::<PxFilter>::default())
.init_asset::<PxFilter>()
.init_asset_loader::<PxFilterLoader>();
}

Expand Down Expand Up @@ -103,9 +107,25 @@ impl AssetLoader for PxFilterLoader {
/// from the top-left corner, moving rightwards, wrapping downwards when it gets to the edge
/// of the image. For examples, see the `assets/` directory in this repository. `fade_to_black.png`
/// is an animated filter.
#[derive(Asset, Reflect, Debug)]
#[derive(Asset, Clone, Reflect, Debug)]
pub struct PxFilter(pub(crate) PxImage<u8>);

impl RenderAsset for PxFilter {
type SourceAsset = Self;
type Param = ();

fn asset_usage(_: &Self) -> RenderAssetUsages {
RenderAssetUsages::RENDER_WORLD
}

fn prepare_asset(
source_asset: Self,
&mut (): &mut (),
) -> Result<Self, PrepareAssetError<Self>> {
Ok(source_asset)
}
}

impl Animation for PxFilter {
type Param = ();

Expand Down
2 changes: 1 addition & 1 deletion src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};

use crate::{math::RectExt, palette::Palette, pixel::Pixel, prelude::*};

#[derive(Serialize, Deserialize, Reflect, Debug)]
#[derive(Serialize, Deserialize, Clone, Reflect, Debug)]
pub(crate) struct PxImage<P: Pixel> {
image: Vec<P>,
width: usize,
Expand Down
27 changes: 24 additions & 3 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use std::mem::replace;
use anyhow::{Error, Result};
use bevy::{
asset::{io::Reader, AssetLoader, LoadContext},
render::texture::{ImageLoader, ImageLoaderSettings},
ecs::system::SystemParamItem,
render::{
render_asset::{PrepareAssetError, RenderAsset, RenderAssetPlugin, RenderAssetUsages},
texture::{ImageLoader, ImageLoaderSettings},
},
};
use serde::{Deserialize, Serialize};

Expand All @@ -16,7 +20,8 @@ use crate::{
};

pub(crate) fn plug(app: &mut App) {
app.init_asset::<PxTileset>()
app.add_plugins(RenderAssetPlugin::<PxTileset>::default())
.init_asset::<PxTileset>()
.init_asset_loader::<PxTilesetLoader>();
}

Expand Down Expand Up @@ -126,13 +131,29 @@ impl AssetLoader for PxTilesetLoader {
/// For animated tilesets, add additional frames to the right of tiles, marking the end
/// of an animation with a fully transparent tile or the end of the image.
/// See `assets/tileset/tileset.png` for an example.
#[derive(Asset, Reflect, Debug)]
#[derive(Asset, Clone, Reflect, Debug)]
pub struct PxTileset {
pub(crate) tileset: Vec<PxSprite>,
tile_size: UVec2,
max_frame_count: usize,
}

impl RenderAsset for PxTileset {
type SourceAsset = Self;
type Param = ();

fn asset_usage(_: &Self) -> RenderAssetUsages {
RenderAssetUsages::RENDER_WORLD
}

fn prepare_asset(
source_asset: Self,
&mut (): &mut (),
) -> Result<Self, PrepareAssetError<Self>> {
Ok(source_asset)
}
}

impl AnimationAsset for PxTileset {
fn max_frame_count(&self) -> usize {
self.max_frame_count
Expand Down
44 changes: 22 additions & 22 deletions src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,29 @@ pub(crate) fn plug(app: &mut App) {
update_position_to_sub.in_set(PxSet::UpdatePosToSubPos),
)
.chain(),
)
.add_systems(
PostUpdate,
(
align_to_screen!(
(&PxMap, &Handle<PxTileset>),
Res<Assets<PxTileset>>,
|(map, tileset), tilesets: &Res<Assets<PxTileset>>| {
Some((map, tilesets.get(tileset)?).frame_size())
}
),
align_to_screen!(&Handle<PxSprite>, Res<Assets<PxSprite>>, |sprite,
sprites: &Res<
Assets<PxSprite>,
>| {
Some(sprites.get(sprite)?.frame_size())
}),
align_to_screen!(&PxRect, (), |rect: &PxRect, &()| Some(rect.frame_size())),
#[cfg(feature = "line")]
align_to_screen!(&PxLine, (), |line: &PxLine, &()| Some(line.frame_size())),
)
.before(PxSet::Draw),
);
// .add_systems(
// PostUpdate,
// (
// align_to_screen!(
// (&PxMap, &Handle<PxTileset>),
// Res<Assets<PxTileset>>,
// |(map, tileset), tilesets: &Res<Assets<PxTileset>>| {
// Some((map, tilesets.get(tileset)?).frame_size())
// }
// ),
// align_to_screen!(&Handle<PxSprite>, Res<Assets<PxSprite>>, |sprite,
// sprites: &Res<
// Assets<PxSprite>,
// >| {
// Some(sprites.get(sprite)?.frame_size())
// }),
// align_to_screen!(&PxRect, (), |rect: &PxRect, &()| Some(rect.frame_size())),
// #[cfg(feature = "line")]
// align_to_screen!(&PxLine, (), |line: &PxLine, &()| Some(line.frame_size())),
// )
// .before(PxSet::Draw),
// );
}

pub(crate) trait Spatial {
Expand Down
Loading

0 comments on commit a17c991

Please sign in to comment.