Skip to content

Commit

Permalink
Bevy v0.13.1 (#114)
Browse files Browse the repository at this point in the history
Issue:
==============
N/A

What was done:
==============
* Updated Bevy to v0.13.1 ([Migration
Guide](https://bevyengine.org/learn/migration-guides/0-12-to-0-13/)).
* Updated bevy_embedded_assets to v0.10.2.
* Refactored code to migrate to the new version.
* Updated Noise crate to v0.9.0.
  • Loading branch information
mnmaita authored Apr 1, 2024
1 parent ec1cdbf commit 6aae792
Show file tree
Hide file tree
Showing 15 changed files with 784 additions and 582 deletions.
1,139 changes: 662 additions & 477 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ rust-version = "1.74.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = { version = "0.12.1", default-features = false, features = [
bevy = { version = "0.13.1", default-features = false, features = [
"bevy_asset",
"bevy_audio",
"bevy_core_pipeline",
Expand All @@ -25,12 +25,12 @@ bevy = { version = "0.12.1", default-features = false, features = [
"webgl2",
"x11",
] }
bevy_rapier2d = { version = "0.24.0", features = ["debug-render-2d"] }
bevy_particle_systems = "0.11.2"
noise = "0.8.2"
bevy_rapier2d = { version = "0.25.0", features = ["debug-render-2d"] }
bevy_particle_systems = "0.12.0"
noise = "0.9.0"
rand = "0.8.5"
pathfinding = "4.8.2"
bevy_embedded_assets = "0.9.1"
pathfinding = "4.9.1"
bevy_embedded_assets = "0.10.2"
lazy_static = "1.4.0"


Expand Down
13 changes: 13 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html lang="en">

<head>
<meta charset="UTF-8" />
<style>
canvas {
width: 100%;
height: 100%;
}
</style>
</head>

</html>
6 changes: 1 addition & 5 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ impl AnimationTimer {

fn animate_sprite(
time: Res<Time>,
mut query: Query<(
&AnimationIndices,
&mut AnimationTimer,
&mut TextureAtlasSprite,
)>,
mut query: Query<(&AnimationIndices, &mut AnimationTimer, &mut TextureAtlas)>,
) {
for (indices, mut timer, mut sprite) in &mut query {
if timer.tick(time.delta()).just_finished() {
Expand Down
8 changes: 3 additions & 5 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::{core_pipeline::clear_color::ClearColorConfig, prelude::*, render::view::RenderLayers};
use bevy::{prelude::*, render::view::RenderLayers};

use crate::{
game::Player,
Expand Down Expand Up @@ -29,7 +29,7 @@ impl Plugin for CameraPlugin {
PostUpdate,
(
(
update_camera.run_if(any_with_component::<Player>()),
update_camera.run_if(any_with_component::<Player>),
constrain_camera_position_to_level,
)
.chain(),
Expand Down Expand Up @@ -74,12 +74,10 @@ impl SubLayerCameraBundle {
Self {
camera_2d: Camera2dBundle {
camera: Camera {
clear_color: ClearColorConfig::None,
order: layer as isize,
..default()
},
camera_2d: Camera2d {
clear_color: ClearColorConfig::None,
},
..default()
},
render_layers: RenderLayers::layer(layer),
Expand Down
2 changes: 1 addition & 1 deletion src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn draw_camera_constraints(
}

fn draw_mouse_direction(
mouse_input: ResMut<Input<MouseButton>>,
mouse_input: ResMut<ButtonInput<MouseButton>>,
cursor_world_position_checker: CursorWorldPositionChecker,
query: Query<&Transform, With<Player>>,
mut gizmos: Gizmos,
Expand Down
49 changes: 27 additions & 22 deletions src/game/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,35 +121,27 @@ pub enum Behavior {
}

#[derive(Resource)]
pub struct TextureArcherAtlasHandle(Handle<TextureAtlas>);
pub struct TextureArcherAtlasHandle(Handle<TextureAtlasLayout>);

#[derive(Resource)]
pub struct TextureAxeAtlasHandle(Handle<TextureAtlas>);
pub struct TextureAxeAtlasHandle(Handle<TextureAtlasLayout>);

fn load_atlas_handlers(mut commands: Commands, asset_server: Res<AssetServer>) {
let texture_archer = asset_server
.get_handle("textures/enemy_archer.png")
.unwrap_or_default();

let texture_atlas_archer =
TextureAtlas::from_grid(texture_archer, Vec2::new(72., 78.), 16, 8, None, None);
TextureAtlasLayout::from_grid(Vec2::new(72., 78.), 16, 8, None, None);
let texture_atlas_handle_archer = asset_server.add(texture_atlas_archer);

commands.insert_resource(TextureArcherAtlasHandle(texture_atlas_handle_archer));

let texture_axe = asset_server
.get_handle("textures/enemy_axe.png")
.unwrap_or_default();

let texture_atlas_axe =
TextureAtlas::from_grid(texture_axe, Vec2::new(72., 78.), 16, 8, None, None);
let texture_atlas_axe = TextureAtlasLayout::from_grid(Vec2::new(72., 78.), 16, 8, None, None);
let texture_atlas_handle_axe = asset_server.add(texture_atlas_axe);

commands.insert_resource(TextureAxeAtlasHandle(texture_atlas_handle_axe));
}

fn spawn_enemies(
mut commands: Commands,
asset_server: Res<AssetServer>,
time: Res<Time>,
mut enemy_spawn_timer: ResMut<EnemySpawnTimer>,
mut enemy_spawn_counter: ResMut<EnemySpawnCounter>,
Expand All @@ -173,10 +165,20 @@ fn spawn_enemies(
let translation = tile_transform.translation.truncate().extend(1.);

//pick a random texture atlas handle between archer and axe
let texture_atlas_handle = if rng.gen_bool(0.5) {
texture_archer_atlas_handle.0.clone()
let (texture_atlas_handle, texture) = if rng.gen_bool(0.5) {
(
texture_archer_atlas_handle.0.clone(),
asset_server
.get_handle("textures/enemy_archer.png")
.unwrap_or_default(),
)
} else {
texture_axeman_atlas_handle.0.clone()
(
texture_axeman_atlas_handle.0.clone(),
asset_server
.get_handle("textures/enemy_axe.png")
.unwrap_or_default(),
)
};

let mut enemy_entity_commands = commands.spawn(EnemyBundle {
Expand All @@ -193,8 +195,11 @@ fn spawn_enemies(
animation_timer: AnimationTimer::from_seconds(0.2),
sprite_orientation: SpriteAnimation::RunLeft,
sprite: SpriteSheetBundle {
sprite: TextureAtlasSprite::new(4),
texture_atlas: texture_atlas_handle.clone(),
atlas: TextureAtlas {
layout: texture_atlas_handle,
index: 4,
},
texture,
transform: Transform::from_translation(translation),
..default()
},
Expand Down Expand Up @@ -224,7 +229,7 @@ fn handle_enemy_behavior(
&Behavior,
&mut SpriteAnimation,
&mut AnimationIndices,
&mut TextureAtlasSprite,
&mut TextureAtlas,
),
With<Enemy>,
>,
Expand All @@ -239,7 +244,7 @@ fn handle_enemy_behavior(
enemy_behavior,
mut sprite_orientation,
mut animation_indices,
mut sprite_index,
mut texture_atlas,
) in &mut enemy_query
{
match enemy_behavior {
Expand Down Expand Up @@ -294,7 +299,7 @@ fn handle_enemy_behavior(
if old_sprite_orientation != *sprite_orientation {
if let Some(value) = SPRITE_ANIMATION_INDEX_MAP.get(&sprite_orientation) {
*animation_indices = AnimationIndices::new(value[0], value[1]);
*sprite_index = TextureAtlasSprite::new(value[0]);
texture_atlas.index = value[0];
};
}
}
Expand Down Expand Up @@ -330,7 +335,7 @@ fn handle_enemy_attacks(
emitter,
enemy_position,
800.,
))
));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/game/fire_breath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ fn spawn_fire_breath(
let fire_texture = asset_server
.get_handle("textures/fire_anim.png")
.unwrap_or_default();

let texture_atlas_fire =
TextureAtlas::from_grid(fire_texture, Vec2::new(40., 40.), 2, 1, None, None);
let texture_atlas_handle_fire = asset_server.add(texture_atlas_fire);
let texture_atlas_layout_fire =
TextureAtlasLayout::from_grid(Vec2::new(40., 40.), 2, 1, None, None);
let texture_atlas_handle_fire = asset_server.add(texture_atlas_layout_fire);
let animated_index: AtlasIndex = AtlasIndex::Animated(AnimatedIndex {
indices: vec![0, 1],
time_step: 0.2,
Expand All @@ -94,6 +93,7 @@ fn spawn_fire_breath(
texture: ParticleTexture::TextureAtlas {
atlas: texture_atlas_handle_fire.clone(),
index: animated_index.clone(),
texture: fire_texture.clone(),
},
spawn_rate_per_second: 5.0.into(),
initial_speed: JitteredValue::jittered(3.0, -1.0..1.0),
Expand Down
7 changes: 2 additions & 5 deletions src/game/game_over.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use bevy::{
audio::{Volume, VolumeLevel},
prelude::*,
};
use bevy::{audio::Volume, prelude::*};

use crate::{audio::PlayMusicEvent, entity_cleanup, playing, AppState};

Expand Down Expand Up @@ -187,7 +184,7 @@ fn play_background_music(mut play_music_event_writer: EventWriter<PlayMusicEvent
play_music_event_writer.send(PlayMusicEvent::new(
"theme3.ogg",
Some(PlaybackSettings {
volume: Volume::Absolute(VolumeLevel::new(0.25)),
volume: Volume::new(0.25),
..default()
}),
None,
Expand Down
40 changes: 19 additions & 21 deletions src/game/level.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use bevy::{
audio::{Volume, VolumeLevel},
ecs::system::SystemParam,
prelude::*,
render::view::RenderLayers,
sprite::Anchor,
audio::Volume, ecs::system::SystemParam, prelude::*, render::view::RenderLayers, sprite::Anchor,
};
use bevy_rapier2d::prelude::*;
use noise::{NoiseFn, Perlin};
Expand Down Expand Up @@ -60,19 +56,13 @@ impl Plugin for LevelPlugin {
}

fn generate_tilemaps(mut commands: Commands, asset_server: Res<AssetServer>) {
let tileset_ground_texture = asset_server
.get_handle("textures/tileset_ground.png")
.unwrap_or_default();
let tileset_objects_texture = asset_server
.get_handle("textures/tileset_objects.png")
.unwrap_or_default();
let tileset_ground_texture_atlas =
TextureAtlas::from_grid(tileset_ground_texture, TILE_SIZE, 16, 18, None, None);
let tileset_ground_texture_atlas_layout =
TextureAtlasLayout::from_grid(TILE_SIZE, 16, 18, None, None);
let tileset_objects_texture_atlas =
TextureAtlas::from_grid(tileset_objects_texture, TILE_SIZE, 38, 14, None, None);
TextureAtlasLayout::from_grid(TILE_SIZE, 38, 14, None, None);

commands.insert_resource(TilesetGroundTextureAtlasHandle(
asset_server.add(tileset_ground_texture_atlas),
asset_server.add(tileset_ground_texture_atlas_layout),
));
commands.insert_resource(TilesetObjectsTextureAtlasHandle(
asset_server.add(tileset_objects_texture_atlas),
Expand Down Expand Up @@ -111,9 +101,14 @@ fn generate_level_matrix(mut commands: Commands) {

fn spawn_level_tiles(
mut commands: Commands,
asset_server: Res<AssetServer>,
level_matrix: Res<LevelMatrix>,
tileset_ground_texture_atlas_handle: Res<TilesetGroundTextureAtlasHandle>,
tileset_ground_texture_atlas_layout_handle: Res<TilesetGroundTextureAtlasHandle>,
) {
let tileset_ground_texture = asset_server
.get_handle("textures/tileset_ground.png")
.unwrap_or_default();

for ((x, y), tile) in level_matrix.0.items() {
let tile = *tile;
let position = translate_grid_position_to_world_space(&(x, y));
Expand All @@ -122,8 +117,11 @@ fn spawn_level_tiles(
let mut tile_entity = commands.spawn(TileBundle {
render_layers: RenderLayers::layer(RenderLayer::Background.into()),
sprite: SpriteSheetBundle {
sprite: TextureAtlasSprite::new(tile.into()),
texture_atlas: tileset_ground_texture_atlas_handle.clone(),
atlas: TextureAtlas {
layout: tileset_ground_texture_atlas_layout_handle.0.clone(),
index: tile.into(),
},
texture: tileset_ground_texture.clone(),
transform,
..default()
},
Expand Down Expand Up @@ -349,18 +347,18 @@ fn play_background_music(mut play_music_event_writer: EventWriter<PlayMusicEvent
"theme2.ogg",
Some(PlaybackSettings {
mode: bevy::audio::PlaybackMode::Loop,
volume: Volume::Absolute(VolumeLevel::new(0.25)),
volume: Volume::new(0.25),
..default()
}),
None,
));
}

#[derive(Resource, Deref)]
pub struct TilesetGroundTextureAtlasHandle(Handle<TextureAtlas>);
pub struct TilesetGroundTextureAtlasHandle(Handle<TextureAtlasLayout>);

#[derive(Resource, Deref)]
pub struct TilesetObjectsTextureAtlasHandle(Handle<TextureAtlas>);
pub struct TilesetObjectsTextureAtlasHandle(Handle<TextureAtlasLayout>);

#[derive(Resource, Deref)]
pub struct LevelMatrix(Matrix<Tile>);
Expand Down
12 changes: 8 additions & 4 deletions src/game/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
let texture = asset_server
.get_handle("textures/dragon.png")
.unwrap_or_default();
let texture_atlas = TextureAtlas::from_grid(texture, Vec2::new(191., 161.), 12, 1, None, None);
let texture_atlas_handle = asset_server.add(texture_atlas);
let texture_atlas_layout =
TextureAtlasLayout::from_grid(Vec2::new(191., 161.), 12, 1, None, None);
let texture_atlas_layout_handle = asset_server.add(texture_atlas_layout);

let mut player_entity_commands = commands.spawn(PlayerBundle {
animation_indices: AnimationIndices::new(0, 2),
Expand All @@ -59,8 +60,11 @@ fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
render_layers: RenderLayers::layer(RenderLayer::Sky.into()),
speed: Speed(10.),
spritesheet: SpriteSheetBundle {
sprite: TextureAtlasSprite::new(0),
texture_atlas: texture_atlas_handle.clone(),
atlas: TextureAtlas {
layout: texture_atlas_layout_handle,
index: 0,
},
texture,
transform: Transform::from_translation(Vec2::ONE.extend(1.)),
..default()
},
Expand Down
Loading

0 comments on commit 6aae792

Please sign in to comment.