Skip to content

Commit

Permalink
Upgrade to Bevy 0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
rparrett committed Mar 7, 2024
1 parent dbeab4d commit 13c163a
Show file tree
Hide file tree
Showing 18 changed files with 1,117 additions and 892 deletions.
1,109 changes: 679 additions & 430 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = { version = "0.12", default-features = false, features = [
bevy = { version = "0.13", default-features = false, features = [
"bevy_asset",
"bevy_audio",
"bevy_winit",
Expand All @@ -23,12 +23,11 @@ bevy = { version = "0.12", default-features = false, features = [
"webgl2",
] }

bevy_asset_loader = { version = "0.18", features = [
"2d",
"standard_dynamic_assets",
bevy_asset_loader = { version = "0.20" }
bevy_common_assets = { version = "0.10", features = ["ron"] }
bevy_ecs_tilemap = { git = "https://github.com/rparrett/bevy_ecs_tilemap", branch = "bevy13", features = [
"atlas",
] }
bevy_common_assets = { version = "0.8", features = ["ron"] }
bevy_ecs_tilemap = { version = "0.12", features = ["atlas"] }

tiled = { version = "0.11", default-features = false }
serde = { version = "1", features = ["derive"] }
Expand Down
8 changes: 8 additions & 0 deletions assets/atlas/crab.atlas.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(
path: "textures/enemies/crab.png",
tile_size: Vec2(32., 32.),
columns: 8,
rows: 10,
padding: None,
offset: None
)
8 changes: 8 additions & 0 deletions assets/atlas/deathknight.atlas.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(
path: "textures/enemies/deathknight.png",
tile_size: Vec2(42., 42.),
columns: 5,
rows: 9,
padding: None,
offset: None
)
8 changes: 8 additions & 0 deletions assets/atlas/skeleton.atlas.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(
path: "textures/enemies/skeleton.png",
tile_size: Vec2(48., 48.),
columns: 4,
rows: 9,
padding: None,
offset: None
)
8 changes: 8 additions & 0 deletions assets/atlas/skeleton2.atlas.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(
path: "textures/enemies/skeleton2.png",
tile_size: Vec2(48., 48.),
columns: 4,
rows: 9,
padding: None,
offset: None
)
8 changes: 8 additions & 0 deletions assets/atlas/snake.atlas.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(
path: "textures/enemies/snake.png",
tile_size: Vec2(28., 28.),
columns: 6,
rows: 9,
padding: None,
offset: None
)
37 changes: 0 additions & 37 deletions assets/enemy_atlas.assets.ron

This file was deleted.

61 changes: 61 additions & 0 deletions src/atlas_loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use bevy::{
asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext},
prelude::*,
utils::BoxedFuture,
};
use serde::Deserialize;

#[derive(Asset, TypePath, Deserialize)]
struct AtlasImageDescriptor {
path: String,
tile_size: Vec2,
columns: usize,
rows: usize,
padding: Option<Vec2>,
offset: Option<Vec2>,
}

#[derive(Asset, TypePath)]
pub struct AtlasImage {
pub image: Handle<Image>,
pub layout: Handle<TextureAtlasLayout>,
}

pub struct AtlasImageLoader;

impl AssetLoader for AtlasImageLoader {
type Asset = AtlasImage;
type Settings = ();
type Error = anyhow::Error;
fn load<'a>(
&'a self,
reader: &'a mut Reader,
_settings: &'a (),
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
let desc = ron::de::from_bytes::<AtlasImageDescriptor>(&bytes)?;

let layout = TextureAtlasLayout::from_grid(
desc.tile_size,
desc.columns,
desc.rows,
desc.padding,
desc.offset,
);

let layout_handle = load_context.add_labeled_asset("layout".to_string(), layout);

Ok(AtlasImage {
image: load_context.load(desc.path),
layout: layout_handle,
})
})
}

fn extensions(&self) -> &[&str] {
&["atlas.ron"]
}
}
13 changes: 8 additions & 5 deletions src/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ fn animate(
time: Res<Time>,
mut query: Query<(
&mut AnimationTimer,
&mut TextureAtlasSprite,
&mut TextureAtlas,
&mut Sprite,
&EnemyKind,
&Direction,
&AnimationState,
Expand All @@ -259,7 +260,9 @@ fn animate(
anim_handles: Res<EnemyAnimationHandles>,
anim_data_assets: Res<Assets<AnimationData>>,
) {
for (mut timer, mut sprite, kind, direction, anim_state, mut tick) in query.iter_mut() {
for (mut timer, mut atlas, mut sprite, kind, direction, anim_state, mut tick) in
query.iter_mut()
{
timer.0.tick(time.delta());
if !timer.0.just_finished() {
continue;
Expand Down Expand Up @@ -332,13 +335,13 @@ fn animate(

tick.0 += 1;
if tick.0 % modulus == 0 {
sprite.index += 1;
atlas.index += 1;
}

let end = start + length - 1;

if !(start..=end).contains(&sprite.index) {
sprite.index = start
if !(start..=end).contains(&atlas.index) {
atlas.index = start
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/game_over.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn spawn_game_over(
color: if lost { Color::RED } else { Color::WHITE },
},
)
.with_alignment(TextAlignment::Center),
.with_justify(JustifyText::Center),
..default()
});
});
Expand Down
47 changes: 22 additions & 25 deletions src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@ use bevy::prelude::*;

use bevy_asset_loader::prelude::*;

use crate::{data::AnimationData, map::TiledMap, GameData, TaipoState};
use crate::{atlas_loader::AtlasImage, data::AnimationData, map::TiledMap, GameData, TaipoState};

pub struct LoadingPlugin;

impl Plugin for LoadingPlugin {
fn build(&self, app: &mut App) {
app.add_loading_state(
LoadingState::new(TaipoState::Load).continue_to_state(TaipoState::MainMenu),
);
app.add_collection_to_loading_state::<_, TextureHandles>(TaipoState::Load);
app.add_collection_to_loading_state::<_, UiTextureHandles>(TaipoState::Load);
app.add_collection_to_loading_state::<_, EnemyAtlasHandles>(TaipoState::Load);
app.add_collection_to_loading_state::<_, EnemyAnimationHandles>(TaipoState::Load);
app.add_collection_to_loading_state::<_, GameDataHandles>(TaipoState::Load);
app.add_collection_to_loading_state::<_, FontHandles>(TaipoState::Load);
app.add_collection_to_loading_state::<_, LevelHandles>(TaipoState::Load);
app.add_collection_to_loading_state::<_, AudioHandles>(TaipoState::Load);
app.add_dynamic_collection_to_loading_state::<_, StandardDynamicAssetCollection>(
TaipoState::Load,
"enemy_atlas.assets.ron",
LoadingState::new(TaipoState::Load)
.load_collection::<TextureHandles>()
.load_collection::<UiTextureHandles>()
.load_collection::<EnemyAtlasHandles>()
.load_collection::<EnemyAnimationHandles>()
.load_collection::<GameDataHandles>()
.load_collection::<FontHandles>()
.load_collection::<LevelHandles>()
.load_collection::<AudioHandles>()
.continue_to_state(TaipoState::MainMenu),
);
}
}
Expand Down Expand Up @@ -82,19 +79,19 @@ pub struct LevelHandles {

#[derive(AssetCollection, Resource)]
pub struct EnemyAtlasHandles {
#[asset(key = "crab")]
crab: Handle<TextureAtlas>,
#[asset(key = "deathknight")]
deathknight: Handle<TextureAtlas>,
#[asset(key = "skeleton")]
skeleton: Handle<TextureAtlas>,
#[asset(key = "skeleton2")]
skeleton2: Handle<TextureAtlas>,
#[asset(key = "snake")]
snake: Handle<TextureAtlas>,
#[asset(path = "atlas/crab.atlas.ron")]
crab: Handle<AtlasImage>,
#[asset(path = "atlas/deathknight.atlas.ron")]
deathknight: Handle<AtlasImage>,
#[asset(path = "atlas/skeleton.atlas.ron")]
skeleton: Handle<AtlasImage>,
#[asset(path = "atlas/skeleton2.atlas.ron")]
skeleton2: Handle<AtlasImage>,
#[asset(path = "atlas/snake.atlas.ron")]
snake: Handle<AtlasImage>,
}
impl EnemyAtlasHandles {
pub fn by_key(&self, key: &str) -> Handle<TextureAtlas> {
pub fn by_key(&self, key: &str) -> Handle<AtlasImage> {
match key {
"crab" => self.crab.clone(),
"deathknight" => self.deathknight.clone(),
Expand Down
Loading

0 comments on commit 13c163a

Please sign in to comment.