Skip to content

Commit

Permalink
upgrade to bevy 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
dnsserver committed Aug 16, 2022
1 parent bb9ff9c commit e5baff6
Show file tree
Hide file tree
Showing 11 changed files with 590 additions and 446 deletions.
898 changes: 511 additions & 387 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ opt-level = 1
opt-level = 3

[dependencies]
bevy = { version = "0.7", features = [
bevy = { version = "0.8", features = [
"dynamic",
"bevy_gilrs",
"bevy_winit",
Expand All @@ -20,6 +20,6 @@ bevy = { version = "0.7", features = [
"vorbis",
"x11",
"filesystem_watcher" ] }
bevy_kira_audio = { version = "0.9", features = ["ogg", "wav"] }
bevy-inspector-egui = "0.10"
bevy_kira_audio = { version = "0.12", features = ["ogg", "wav"] }
bevy-inspector-egui = "0.12"
rand = "*"
6 changes: 3 additions & 3 deletions src/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn spawn_ascii_text(
));
}
commands
.spawn()
.spawn_bundle(VisibilityBundle::default())
.insert(Name::new(format!("Text - {}", to_print)))
.insert(Transform {
translation: left_center,
Expand Down Expand Up @@ -192,7 +192,7 @@ pub fn spawn_nine_slice(
));

commands
.spawn()
.spawn_bundle(VisibilityBundle::default())
.insert(NineSlice)
.insert(Name::new("NineSpriteBox"))
//Needs transforms for parent heirarchy system to work
Expand All @@ -209,7 +209,7 @@ fn load_ascii(
) {
let image = assets.load("Ascii.png");
let atlas =
TextureAtlas::from_grid_with_padding(image, Vec2::splat(9.0), 16, 16, Vec2::splat(2.0));
TextureAtlas::from_grid_with_padding(image, Vec2::splat(9.0), 16, 16, Vec2::splat(2.0), Vec2::ZERO);

let atlas_handle = texture_atlases.add(atlas);

Expand Down
76 changes: 48 additions & 28 deletions src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
use bevy::prelude::*;
use bevy_kira_audio::{Audio, AudioChannel, AudioPlugin, AudioSource};
use bevy_kira_audio::{AudioChannel, AudioPlugin, AudioSource, AudioApp, AudioControl};

use crate::combat::{CombatState, FightEvent};
use crate::GameState;

pub struct GameAudioPlugin;

struct Background;
struct Effects;
struct Combat;
pub struct AudioState {
bgm_handle: Handle<AudioSource>,
combat_handle: Handle<AudioSource>,
hit_handle: Handle<AudioSource>,
reward_handle: Handle<AudioSource>,

bgm_channel: AudioChannel,
combat_channel: AudioChannel,
sfx_channel: AudioChannel,
volume: f32,
volume: f64,
}

impl Plugin for GameAudioPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(AudioPlugin)
.add_audio_channel::<Background>()
.add_audio_channel::<Effects>()
.add_audio_channel::<Combat>()
.add_startup_system_to_stage(StartupStage::PreStartup, load_audio)
.add_system_set(SystemSet::on_enter(GameState::Combat).with_system(start_combat_music))
.add_system_set(
Expand All @@ -32,36 +34,40 @@ impl Plugin for GameAudioPlugin {
.add_startup_system(start_bgm_music);
}
}
fn play_reward_sfx(audio: Res<Audio>, audio_state: Res<AudioState>) {
audio.play_in_channel(audio_state.reward_handle.clone(), &audio_state.sfx_channel);
fn play_reward_sfx(audio: Res<AudioChannel<Effects>>, audio_state: Res<AudioState>) {
audio.play(audio_state.reward_handle.clone());
}

fn play_hit_sfx(
audio: Res<Audio>,
audio: Res<AudioChannel<Effects>>,
audio_state: Res<AudioState>,
mut fight_event: EventReader<FightEvent>,
) {
if fight_event.iter().count() > 0 {
audio.play_in_channel(audio_state.hit_handle.clone(), &audio_state.sfx_channel);
audio.play(audio_state.hit_handle.clone());
}
}

fn resume_bgm_music(audio: Res<Audio>, audio_state: Res<AudioState>) {
audio.stop_channel(&audio_state.combat_channel);
audio.resume_channel(&audio_state.bgm_channel);
fn resume_bgm_music(
bg_audio: Res<AudioChannel<Background>>,
combat_audio: Res<AudioChannel<Combat>>,
) {
combat_audio.stop();
bg_audio.resume();
}

fn start_combat_music(audio: Res<Audio>, audio_state: Res<AudioState>) {
audio.pause_channel(&audio_state.bgm_channel);
audio.play_looped_in_channel(
audio_state.combat_handle.clone(),
&audio_state.combat_channel,
);
fn start_combat_music(
bg_audio: Res<AudioChannel<Background>>,
combat_audio: Res<AudioChannel<Combat>>,
audio_state: Res<AudioState>
) {
bg_audio.pause();
combat_audio.play(audio_state.combat_handle.clone()).looped();
}

fn volume_control(
keyboard: Res<Input<KeyCode>>,
audio: Res<Audio>,
bg_audio: Res<AudioChannel<Background>>,
mut audio_state: ResMut<AudioState>,
) {
if keyboard.just_pressed(KeyCode::Up) {
Expand All @@ -71,36 +77,50 @@ fn volume_control(
audio_state.volume -= 0.10;
}
audio_state.volume = audio_state.volume.clamp(0.0, 1.0);
audio.set_volume_in_channel(audio_state.volume, &audio_state.bgm_channel);
bg_audio.set_volume(audio_state.volume);
}

fn start_bgm_music(audio: Res<Audio>, audio_state: Res<AudioState>) {
audio.play_looped_in_channel(audio_state.bgm_handle.clone(), &audio_state.bgm_channel);
fn start_bgm_music(
bg_audio: Res<AudioChannel<Background>>,
audio_state: Res<AudioState>
) {
bg_audio.play(audio_state.bgm_handle.clone()).looped();
}

fn load_audio(mut commands: Commands, audio: Res<Audio>, assets: Res<AssetServer>) {
fn load_audio(
mut commands: Commands,
bg_audio: Res<AudioChannel<Background>>,
fx_audio: Res<AudioChannel<Effects>>,
cb_audio: Res<AudioChannel<Combat>>,
assets: Res<AssetServer>
) {
let bgm_handle = assets.load("bip-bop.ogg");
let combat_handle = assets.load("ganxta.ogg");
let hit_handle = assets.load("hit.wav");
let reward_handle = assets.load("reward.wav");


/*
let bgm_channel = AudioChannel::new("bgm".to_string());
let combat_channel = AudioChannel::new("combat".to_string());
let sfx_channel = AudioChannel::new("sfx".to_string());
let volume = 0.5;
*/
let volume = 0.01;

bg_audio.set_volume(volume);
fx_audio.set_volume(volume);
cb_audio.set_volume(volume);
/*
audio.set_volume_in_channel(volume, &bgm_channel);
audio.set_volume_in_channel(volume, &combat_channel);
audio.set_volume_in_channel(volume, &sfx_channel);
*/

commands.insert_resource(AudioState {
bgm_handle: bgm_handle,
combat_handle: combat_handle,
hit_handle: hit_handle,
reward_handle: reward_handle,
bgm_channel,
combat_channel,
sfx_channel,
volume,
});
}
6 changes: 3 additions & 3 deletions src/combat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::{prelude::*, render::camera::Camera2d};
use bevy::prelude::*;
use bevy_inspector_egui::Inspectable;

use crate::{
Expand Down Expand Up @@ -288,9 +288,9 @@ fn spawn_combat_button(
let fight_text = spawn_ascii_text(commands, ascii, text, Vec3::new(x_offset, 0.0, 0.0));

commands
.spawn()
.spawn_bundle(VisibilityBundle::default())
.insert(Transform {
translation: translation,
translation,
..Default::default()
})
.insert(GlobalTransform::default())
Expand Down
2 changes: 1 addition & 1 deletion src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl GraphicsPlugin {
) {
let image = assets.load("characters.png");
let atlas =
TextureAtlas::from_grid_with_padding(image, Vec2::splat(16.0), 12, 8, Vec2::splat(2.0));
TextureAtlas::from_grid_with_padding(image, Vec2::splat(16.0), 12, 8, Vec2::splat(2.0), Vec2::ZERO);
let atlas_handle = texture_atlases.add(atlas);

let columns = 12;
Expand Down
28 changes: 14 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ fn main() {
.insert_resource(ClearColor(CLEAR))
.insert_resource(WindowDescriptor {
width: height * RESOLUTION,
height: height,
height,
title: "Bevy Tutorial".to_string(),
present_mode: PresentMode::Fifo,
resizable: false,
resizable: true,
..Default::default()
})
.add_plugins(DefaultPlugins)
Expand All @@ -64,17 +64,17 @@ fn main() {
}

fn spawn_camera(mut commands: Commands) {
let mut camera = OrthographicCameraBundle::new_2d();

//Set the camera to have normalized coordinates of y values -1 to 1
camera.orthographic_projection.top = 1.0;
camera.orthographic_projection.bottom = -1.0;

camera.orthographic_projection.right = 1.0 * RESOLUTION;
camera.orthographic_projection.left = -1.0 * RESOLUTION;

//Force the camera to use our settings
camera.orthographic_projection.scaling_mode = ScalingMode::None;

let camera = Camera2dBundle{
projection: OrthographicProjection{
top: 1.0,
bottom: -1.0,
right: 1.0 * RESOLUTION,
left: -1.0 * RESOLUTION,
scaling_mode: ScalingMode::None,
scale: 1.,
..default()
},
..default()
};
commands.spawn_bundle(camera);
}
2 changes: 1 addition & 1 deletion src/npc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::{prelude::*, render::camera::Camera2d};
use bevy::prelude::*;

use crate::{
ascii::{spawn_ascii_sprite, spawn_ascii_text, spawn_nine_slice, AsciiSheet, NineSliceIndices},
Expand Down
2 changes: 1 addition & 1 deletion src/player.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::{
prelude::*,
render::{camera::Camera2d},
//render::{camera::Camera2d},
sprite::collide_aabb::collide,
};
use bevy_inspector_egui::Inspectable;
Expand Down
8 changes: 4 additions & 4 deletions src/start_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ fn setup_menu(mut commands: Commands, assets: Res<AssetServer>) {
button_pressed: assets.load("button_pressed.png"),
};

commands.spawn_bundle(UiCameraBundle::default());
//commands.spawn_bundle(UiCameraBundle::default());
commands
.spawn_bundle(ButtonBundle {
style: Style {
align_self: AlignSelf::Center,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
size: Size::new(Val::Percent(20.0), Val::Percent(10.0)),
margin: Rect::all(Val::Auto),
margin: UiRect::all(Val::Auto),
..Default::default()
},
color: Color::NONE.into(),
Expand All @@ -93,14 +93,14 @@ fn setup_menu(mut commands: Commands, assets: Res<AssetServer>) {
.insert(FocusPolicy::Pass)
.with_children(|parent| {
parent.spawn_bundle(TextBundle {
text: Text::with_section(
text: Text::from_section(
"Start Game",
TextStyle {
font: ui_assets.font.clone(),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
Default::default(),
//Default::default(),
),
focus_policy: FocusPolicy::Pass,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn create_simple_map(mut commands: Commands, ascii: Res<AsciiSheet>) {
}

commands
.spawn()
.spawn_bundle(VisibilityBundle::default())
.insert(Map)
.insert(Name::new("Map"))
.insert(Transform::default())
Expand Down

0 comments on commit e5baff6

Please sign in to comment.