Skip to content

Commit

Permalink
WIP update to Bevy 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Sep 26, 2023
1 parent 317ba99 commit 04dc71d
Show file tree
Hide file tree
Showing 13 changed files with 2,087 additions and 1,429 deletions.
3,118 changes: 1,887 additions & 1,231 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ publish = false
lto = true
codegen-units = 1

[features]
default = [
"bevy/bevy_winit",
"bevy/render",
"bevy/png",
"bevy/x11"
]
#[features]
#default = [
# "bevy/bevy_winit",
# "bevy/render",
# "bevy/png",
# "bevy/x11"
#]

[dependencies]
bevy = { version = "0.7", default-features = false}
bevy_kira_audio = { version = "0.9" }
bevy_asset_loader = { version = "0.10" }
bevy_prototype_lyon = { version = "0.5" }
bevy = { version = "0.11" }
bevy_kira_audio = { version = "0.17" }
bevy_asset_loader = { version = "0.17" }
bevy_prototype_lyon = { version = "0.9" }
rand = { version = "0.8" }

[target.'cfg(target_os = "linux")'.dependencies]
winit = { version = "0.25", features=["x11"]}
winit = { version = "0.28", features=["x11"]}

[build-dependencies]
embed-resource = "1.4"
32 changes: 20 additions & 12 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,36 @@ use crate::loading::AudioAssets;
use crate::towers::TowerShot;
use crate::AppState;
use bevy::prelude::*;
use bevy_kira_audio::{Audio, AudioChannel, AudioPlugin};
use bevy_kira_audio::{Audio, AudioApp, AudioChannel, AudioControl, AudioPlugin};

pub struct InternalAudioPlugin;

impl Plugin for InternalAudioPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(AudioPlugin)
.add_system_set(SystemSet::on_enter(AppState::Menu).with_system(start_audio))
.add_system_set(
SystemSet::on_update(AppState::InGame)
.with_system(tower_shots)
.with_system(enemy_breach),
app.add_plugins(AudioPlugin)
.add_audio_channel::<BackgroundAudio>()
.add_systems(OnEnter(AppState::Menu), start_audio)
.add_systems(
Update,
(tower_shots, enemy_breach).run_if(in_state(AppState::InGame)),
)
.add_system_set(SystemSet::on_exit(AppState::InGame).with_system(stop_audio));
.add_systems(OnExit(AppState::InGame), stop_audio);
}
}

fn start_audio(audio_assets: Res<AudioAssets>, audio: Res<Audio>) {
let background_channel = AudioChannel::new("background".to_owned());
#[derive(Resource)]
struct BackgroundAudio;

fn start_audio(
audio_assets: Res<AudioAssets>,
background_channel: Res<AudioChannel<BackgroundAudio>>,
audio: Res<Audio>,
) {
audio.set_volume(0.15);
audio.set_volume_in_channel(0.15, &background_channel);
audio.play_looped_in_channel(audio_assets.background.clone(), &background_channel);
background_channel.set_volume(0.15);
background_channel
.play(audio_assets.background.clone())
.looped();
}

fn stop_audio(audio: Res<Audio>) {
Expand Down
12 changes: 7 additions & 5 deletions src/bullets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ pub struct BulletPlugin;

impl Plugin for BulletPlugin {
fn build(&self, app: &mut App) {
app.add_system_set(
SystemSet::on_update(AppState::InGame)
.with_system(update_bullets.label(EnemyLabels::Damage)),
app.add_systems(
Update,
update_bullets
.label(EnemyLabels::Damage)
.run_if(in_state(AppState::InGame)),
)
.add_system_set(SystemSet::on_exit(AppState::InGame).with_system(break_down_bullets));
.add_systems(OnExit(AppState::InGame), break_down_bullets);
}
}

Expand Down Expand Up @@ -58,7 +60,7 @@ fn update_bullets(

pub fn spawn_bullet(commands: &mut Commands, bullet: Bullet, translation: Vec3) -> Entity {
commands
.spawn_bundle(GeometryBuilder::build_as(
.spawn(GeometryBuilder::build_as(
&Circle {
radius: 3.,
center: Vec2::splat(0.),
Expand Down
50 changes: 23 additions & 27 deletions src/enemies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rand::prelude::*;
use crate::map::{Coordinate, Map};
use crate::puzzle::CurrentPiece;
use crate::ui::GameState;
use crate::{AppState, OicanaStage, ENEMY_Z};
use crate::{AppState, ENEMY_Z};

pub struct EnemiesPlugin;

Expand All @@ -18,44 +18,39 @@ impl Plugin for EnemiesPlugin {
last_spawn: Instant::now(),
})
.add_event::<EnemyBreach>()
.add_stage_after(
CoreStage::Update,
OicanaStage::EnemyRemoval,
SystemStage::parallel(),
.add_systems(
PostUpdate,
remove_enemies.run_if(in_state(AppState::InGame)),
)
.add_state_to_stage(OicanaStage::EnemyRemoval, AppState::Loading)
.add_system_set_to_stage(
OicanaStage::EnemyRemoval,
SystemSet::on_update(AppState::InGame).with_system(remove_enemies),
.add_systems(
Update,
(
update_enemy_colors
.label(EnemyLabels::UpdateColor)
.after(EnemyLabels::Damage),
spawn_enemies.before(EnemyLabels::UpdateColor),
update_tamable_enemies.before(EnemyLabels::UpdateColor),
move_enemies
.label(EnemyLabels::Move)
.before(EnemyLabels::Damage),
)
.run_if(in_state(AppState::InGame)),
)
.add_system_set(
SystemSet::on_update(AppState::InGame)
.with_system(
update_enemy_colors
.label(EnemyLabels::UpdateColor)
.after(EnemyLabels::Damage),
)
.with_system(spawn_enemies.before(EnemyLabels::UpdateColor))
.with_system(update_tamable_enemies.before(EnemyLabels::UpdateColor))
.with_system(
move_enemies
.label(EnemyLabels::Move)
.before(EnemyLabels::Damage),
),
)
.add_system_set(SystemSet::on_exit(AppState::InGame).with_system(break_down_enemies));
.add_systems(OnExit(AppState::InGame), break_down_enemies);
}
}

#[derive(SystemLabel, Clone, Hash, Debug, Eq, PartialEq)]
#[derive(SystemSet, Clone, Hash, Debug, Eq, PartialEq)]
pub enum EnemyLabels {
UpdateColor,
Damage,
Move,
}

#[derive(Event)]
pub struct EnemyBreach;

#[derive(Resource)]
struct WaveState {
pub last_spawn: Instant,
}
Expand All @@ -79,6 +74,7 @@ pub struct Health {
pub value: i32,
}

#[derive(Resource)]
pub struct Trees {
pub coordinates: Vec<Coordinate>,
}
Expand Down Expand Up @@ -187,7 +183,7 @@ fn create_enemy(
travelled: 0.,
};
commands
.spawn_bundle(form.build_bundle(
.spawn(form.build_bundle(
Transform::from_translation(Vec3::new(map.spawn.x, map.spawn.y, ENEMY_Z)),
enemy.get_color(health),
Some(enemy.get_color(health)),
Expand Down
38 changes: 18 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,35 @@ pub const PUZZLE_Z: f32 = 2.;
pub const ENEMY_Z: f32 = 3.;
pub const BULLET_Z: f32 = 4.;

#[derive(Clone, Eq, PartialEq, Debug, Hash)]
#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, States)]
pub enum AppState {
Restart,
InGame,
#[default]
Loading,
Menu,
}

#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
pub enum OicanaStage {
EnemyRemoval,
}

impl Plugin for GamePlugin {
fn build(&self, app: &mut App) {
app.insert_resource(ClearColor(Color::BLACK))
.add_state(AppState::Loading)
.add_plugin(LoadingPlugin)
.add_plugin(ShapePlugin)
.add_plugin(MenuPlugin)
.add_plugin(MapPlugin)
.add_plugin(EnemiesPlugin)
.add_plugin(TowersPlugin)
.add_plugin(BulletPlugin)
.add_plugin(UiPlugin)
.add_plugin(PuzzlePlugin)
.add_plugin(InternalAudioPlugin);
app.add_system_set(SystemSet::on_enter(AppState::Restart).with_system(switch_to_game));
.add_state::<AppState>()
.add_plugins((
LoadingPlugin,
ShapePlugin,
MenuPlugin,
MapPlugin,
EnemiesPlugin,
TowersPlugin,
BulletPlugin,
UiPlugin,
PuzzlePlugin,
InternalAudioPlugin,
));
app.add_systems(OnEnter(AppState::Restart), switch_to_game);
}
}

fn switch_to_game(mut state: ResMut<State<AppState>>) {
state.set(AppState::InGame).unwrap();
fn switch_to_game(mut state: ResMut<NextState<AppState>>) {
state.set(AppState::InGame);
}
20 changes: 10 additions & 10 deletions src/loading.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use crate::map::Tile;
use crate::AppState;
use bevy::prelude::*;
use bevy_asset_loader::{AssetCollection, AssetLoader};
use bevy_asset_loader::prelude::*;
use bevy_kira_audio::AudioSource;

pub struct LoadingPlugin;

impl Plugin for LoadingPlugin {
fn build(&self, app: &mut App) {
AssetLoader::new(AppState::Loading)
.continue_to_state(AppState::Menu)
.with_collection::<FontAssets>()
.with_collection::<AudioAssets>()
.with_collection::<TextureAssets>()
.build(app);
app.add_loading_state(
LoadingState::new(AppState::Loading).continue_to_state(AppState::Menu),
)
.add_collection_to_loading_state::<_, FontAssets>(AppState::Loading)
.add_collection_to_loading_state::<_, AudioAssets>(AppState::Loading)
.add_collection_to_loading_state::<_, TextureAssets>(AppState::Loading);
}
}

#[derive(AssetCollection)]
#[derive(AssetCollection, Resource)]
pub struct FontAssets {
#[asset(path = "fonts/FiraSans-Bold.ttf")]
pub fira_sans: Handle<Font>,
}

#[derive(AssetCollection)]
#[derive(AssetCollection, Resource)]
pub struct AudioAssets {
#[asset(path = "sounds/background.ogg")]
pub background: Handle<AudioSource>,
Expand All @@ -33,7 +33,7 @@ pub struct AudioAssets {
pub enemy_breach: Handle<AudioSource>,
}

#[derive(AssetCollection)]
#[derive(AssetCollection, Resource)]
pub struct TextureAssets {
#[asset(path = "textures/blank.png")]
pub blank: Handle<Image>,
Expand Down
20 changes: 11 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use bevy::prelude::*;
use bevy::window::WindowResolution;
use oicana::GamePlugin;

#[bevy_main]
fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.insert_resource(WindowDescriptor {
width: 800.,
height: 600.,
title: "Oicana".to_string(),
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_plugin(GamePlugin)
.insert_resource(Msaa::Sample4)
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
resolution: WindowResolution::new(800., 600.),
title: "Oicana".to_string(),
..default()
}),
..default()
}))
.add_plugins(GamePlugin)
.run();
}
14 changes: 5 additions & 9 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ impl Plugin for MapPlugin {
let map = Map::load_map();
app.insert_resource(map.gather_trees())
.insert_resource(map)
.add_system_set(
SystemSet::on_enter(AppState::Menu)
.with_system(render_map)
.with_system(setup_camera),
);
.add_systems(OnEnter(AppState::Menu), (render_map, setup_camera));
}
}

Expand Down Expand Up @@ -47,7 +43,7 @@ impl Coordinate {
}
}

#[derive(Debug)]
#[derive(Debug, Resource)]
pub struct Map {
pub height: usize,
pub width: usize,
Expand Down Expand Up @@ -193,18 +189,18 @@ impl Map {
}

fn setup_camera(mut commands: Commands, map: Res<Map>) {
let mut camera_bundle = OrthographicCameraBundle::new_2d();
let mut camera_bundle = Camera2dBundle::default();
camera_bundle.transform.translation.x = (map.width as f32 / 2. - 0.5) * map.tile_size;
camera_bundle.transform.translation.y = (map.height as f32 / 2. - 0.5) * map.tile_size;
commands.spawn_bundle(camera_bundle);
commands.spawn(camera_bundle);
}

fn render_map(mut commands: Commands, map: Res<Map>, texture_assets: Res<TextureAssets>) {
for row in 0..map.height {
for column in 0..map.width {
let tile = &map.tiles[row][column];
commands
.spawn_bundle(SpriteBundle {
.spawn(SpriteBundle {
texture: texture_assets.get_handle_for_tile(tile).clone(),
transform: Transform::from_translation(Vec3::new(
column as f32 * map.tile_size,
Expand Down
Loading

0 comments on commit 04dc71d

Please sign in to comment.