Skip to content

Commit

Permalink
Merge pull request #9 from NiklasEi/update-to-bevy-0-11
Browse files Browse the repository at this point in the history
Update to bevy 0 11
  • Loading branch information
NiklasEi authored Dec 22, 2024
2 parents 317ba99 + 8a46bdb commit 8049828
Show file tree
Hide file tree
Showing 14 changed files with 2,140 additions and 1,490 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"
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ All enemies are geometrical forms. In the beginning they are all grey, but when

As in every TD game, you can lose by letting too many enemies through to your base.

## Development

Run the game locally with `cargo run` or in the browser with `trunk serve`.

## Credits

See [CREDITS.md](credits/CREDITS.md)
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
27 changes: 16 additions & 11 deletions src/bullets.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::enemies::{Enemy, EnemyLabels, Health, Tameable};
use crate::enemies::{Enemy, EnemySet, Health, Tameable};
use crate::AppState;
use bevy::prelude::*;
use bevy_prototype_lyon::prelude::*;
Expand All @@ -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
.in_set(EnemySet::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,13 +60,16 @@ fn update_bullets(

pub fn spawn_bullet(commands: &mut Commands, bullet: Bullet, translation: Vec3) -> Entity {
commands
.spawn_bundle(GeometryBuilder::build_as(
&Circle {
radius: 3.,
center: Vec2::splat(0.),
.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&Circle {
radius: 3.,
center: Vec2::splat(0.),
}),
transform: Transform::from_translation(translation),
..default()
},
DrawMode::Fill(FillMode::color(Color::BLACK)),
Transform::from_translation(translation),
Fill::color(Color::BLACK),
))
.insert(bullet)
.id()
Expand Down
79 changes: 34 additions & 45 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,37 @@ 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
.in_set(EnemySet::UpdateColor)
.after(EnemySet::Damage),
spawn_enemies.before(EnemySet::UpdateColor),
update_tamable_enemies.before(EnemySet::UpdateColor),
move_enemies.in_set(EnemySet::Move).before(EnemySet::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)]
pub enum EnemyLabels {
#[derive(SystemSet, Clone, Hash, Debug, Eq, PartialEq)]
pub enum EnemySet {
UpdateColor,
Damage,
Move,
}

#[derive(Event)]
pub struct EnemyBreach;

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

#[derive(Resource)]
pub struct Trees {
pub coordinates: Vec<Coordinate>,
}
Expand Down Expand Up @@ -187,7 +181,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 All @@ -209,7 +203,7 @@ impl EnemyForm {
transform: Transform,
outline_color: Color,
fill_color: Option<Color>,
) -> ShapeBundle {
) -> impl Bundle {
let shape = shapes::RegularPolygon {
sides: match self {
EnemyForm::Circle => 5,
Expand All @@ -220,13 +214,14 @@ impl EnemyForm {
..shapes::RegularPolygon::default()
};

GeometryBuilder::build_as(
&shape,
DrawMode::Outlined {
fill_mode: FillMode::color(fill_color.unwrap_or(Color::NONE)),
outline_mode: StrokeMode::new(outline_color, 2.0),
(
ShapeBundle {
path: GeometryBuilder::build_as(&shape),
transform,
..default()
},
transform,
Fill::color(fill_color.unwrap_or(Color::NONE)),
Stroke::new(outline_color, 2.0),
)
}
}
Expand Down Expand Up @@ -294,20 +289,14 @@ fn move_enemies(
}

fn update_enemy_colors(
mut damaged_enemies: Query<(&mut DrawMode, &Health, &Enemy), Changed<Health>>,
mut damaged_enemies: Query<(&mut Fill, &mut Stroke, &Health, &Enemy), Changed<Health>>,
) {
for (mut draw_mode, health, enemy) in damaged_enemies.iter_mut() {
for (mut fill, mut stroke, health, enemy) in damaged_enemies.iter_mut() {
if health.value == enemy.colored_health {
continue;
}
if let DrawMode::Outlined {
ref mut fill_mode,
ref mut outline_mode,
} = *draw_mode
{
fill_mode.color = enemy.get_color(health.value);
outline_mode.color = enemy.get_color(health.value);
}
fill.color = enemy.get_color(health.value);
stroke.color = enemy.get_color(health.value);
}
}

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
Loading

0 comments on commit 8049828

Please sign in to comment.