Skip to content

Commit

Permalink
Merge pull request #15 from SecretPocketCat/sfx
Browse files Browse the repository at this point in the history
Sfx
  • Loading branch information
SecretPocketCat authored Apr 10, 2023
2 parents fcaea74 + 911c393 commit 286c61e
Show file tree
Hide file tree
Showing 39 changed files with 162 additions and 82 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ on:
env:
# heads-up: this value is used as a pattern in an sed command as a workaround for a trunk issue
# if you use special characters, take a look at the 'Make paths relative' step in the 'build-web' job
GAME_EXECUTABLE_NAME: sonarscape
GAME_OSX_APP_NAME: sonarscape
GAME_EXECUTABLE_NAME: ranos
GAME_OSX_APP_NAME: ranos

permissions:
contents: write
Expand Down
12 changes: 6 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"cargo": {
"args": [
"build",
"--bin=sonarscape",
"--package=sonarscape",
"--bin=ranos",
"--package=ranos",
"--features",
"dev"
],
"filter": {
"name": "sonarscape",
"name": "ranos",
"kind": "bin"
}
},
Expand All @@ -47,13 +47,13 @@
"args": [
"test",
"--no-run",
"--bin=sonarscape",
"--package=sonarscape",
"--bin=ranos",
"--package=ranos",
"--features",
"dev"
],
"filter": {
"name": "sonarscape",
"name": "ranos",
"kind": "bin"
}
},
Expand Down
56 changes: 28 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "sonarscape"
name = "ranos"
version = "0.1.0"
publish = false
edition = "2021"
Expand Down
Binary file removed assets/audio/flying.ogg
Binary file not shown.
Binary file added assets/audio/sfx/drone.ogg
Binary file not shown.
Binary file added assets/audio/sfx/echo1.ogg
Binary file not shown.
Binary file added assets/audio/sfx/echo2.ogg
Binary file not shown.
Binary file added assets/audio/sfx/enemy_alert1.ogg
Binary file not shown.
Binary file added assets/audio/sfx/enemy_alert2.ogg
Binary file not shown.
Binary file added assets/audio/sfx/enemy_death1.ogg
Binary file not shown.
Binary file added assets/audio/sfx/enemy_death2.ogg
Binary file not shown.
Binary file added assets/audio/sfx/level_cleared.ogg
Binary file not shown.
Binary file added assets/audio/sfx/player_death1.ogg
Binary file not shown.
Binary file added assets/audio/sfx/player_death2.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click1.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click10.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click11.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click12.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click13.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click2.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click3.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click4.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click5.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click6.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click7.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click8.ogg
Binary file not shown.
Binary file removed assets/audio/sfx/ui/click9.ogg
Binary file not shown.
8 changes: 4 additions & 4 deletions build/macos/src/Game.app/Contents/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>sonarscape</string> <!-- ToDo: this should be the same as your OSX app name in the release flow -->
<string>ranos</string> <!-- ToDo: this should be the same as your OSX app name in the release flow -->
<key>CFBundleExecutable</key>
<string>sonarscape</string> <!-- ToDo: this should be the same as your executable name in the release flow -->
<string>ranos</string> <!-- ToDo: this should be the same as your executable name in the release flow -->
<key>CFBundleIconFile</key>
<string>AppIcon.icns</string>
<key>CFBundleIdentifier</key>
<string>spc.sonarscape</string> <!-- ToDo -->
<string>spc.ranos</string> <!-- ToDo -->
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>sonarscape</string> <!-- ToDo -->
<string>ranos</string> <!-- ToDo -->
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<title>sonarscape</title>
<title>ranos</title>
<!-- ToDo -->
<link data-trunk rel="copy-dir" href="assets" />
<link data-trunk rel="copy-dir" href="credits" />
Expand Down
42 changes: 27 additions & 15 deletions src/assets/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,36 @@ use bevy_kira_audio::AudioSource;

use super::add_dynamic_assets;

#[derive(AssetCollection, Resource)]
pub struct AudioAssets {
#[asset(path = "audio/flying.ogg")]
pub flying: Handle<AudioSource>,
}

#[derive(AssetCollection, Resource)]
pub struct SfxAssets {
#[asset(key = "click", collection(typed))]
pub click: Vec<Handle<AudioSource>>,
// todo: fix echo
#[asset(key = "echo", collection(typed))]
pub echo: Vec<Handle<AudioSource>>,

#[asset(key = "player_death", collection(typed))]
pub player_death: Vec<Handle<AudioSource>>,

#[asset(key = "enemy_alert", collection(typed))]
pub enemy_alert: Vec<Handle<AudioSource>>,

#[asset(key = "enemy_death", collection(typed))]
pub enemy_death: Vec<Handle<AudioSource>>,

#[asset(path = "audio/sfx/level_cleared.ogg")]
pub level_cleared: Handle<AudioSource>,

#[asset(path = "audio/sfx/drone.ogg")]
pub drone: Handle<AudioSource>,
}

pub(super) fn setup_sfx_assets(mut dynamic_assets: ResMut<DynamicAssets>) {
add_dynamic_assets(
&mut dynamic_assets,
"click",
"audio/sfx/ui/click",
"ogg",
1..=13,
);
for key in ["echo", "player_death", "enemy_alert", "enemy_death"] {
add_dynamic_assets(
&mut dynamic_assets,
key,
&format!("audio/sfx/{}", key),
"ogg",
1..=2,
);
}
}
1 change: 0 additions & 1 deletion src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub fn assets_plugin(app: &mut App) {
.add_loading_state(LoadingState::new(AppState::Loading))
.add_collection_to_loading_state::<_, fonts::FontAssets>(AppState::Loading)
.add_collection_to_loading_state::<_, audio::SfxAssets>(AppState::Loading)
.add_collection_to_loading_state::<_, audio::AudioAssets>(AppState::Loading)
.add_collection_to_loading_state::<_, textures::TextureAssets>(AppState::Loading)
.add_startup_system(audio::setup_sfx_assets)
.add_plugin(ProgressPlugin::new(AppState::Loading))
Expand Down
75 changes: 62 additions & 13 deletions src/audio/sfx.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,84 @@
use std::time::Duration;

use bevy::input::mouse::MouseButtonInput;
use bevy::{ecs::system::Resource, prelude::*};
use bevy_kira_audio::prelude::*;
use rand::seq::SliceRandom;
use rand::*;

use crate::assets::audio::SfxAssets;
use crate::echolocation::echolocation::EcholocationEv;
use crate::enemy::EnemyEv;
use crate::io::save::VolumeSettings;
use crate::player::player::PlayerEv;
use crate::state::AppState;

// todo: use sfx + master volume
pub(super) fn sfx_plugin(app: &mut bevy::prelude::App) {
app.add_audio_channel::<SfxChannel>()
.add_system(
play_sfx_on_evt::<MouseButtonInput>
.add_systems(
(
play_sfx_on_evt::<EcholocationEv>,
play_sfx_on_evt::<PlayerEv>,
play_sfx_on_evt::<EnemyEv>,
)
.in_base_set(CoreSet::PostUpdate)
.run_if(resource_exists::<SfxAssets>()),
.distributive_run_if(resource_exists::<SfxAssets>()),
)
.add_system(set_sfx_volume.run_if(resource_changed::<VolumeSettings>()));
.add_system(set_sfx_volume.run_if(resource_changed::<VolumeSettings>()))
.add_system(play_drone.in_schedule(OnExit(AppState::Loading)));
}

#[derive(Resource)]
struct SfxChannel;

pub trait SfxEv {
fn get_volume(&self) -> f64;
fn get_sfx_handles(sfx: &SfxAssets) -> &[Handle<AudioSource>];
fn get_sfx_handles(&self, sfx: &SfxAssets) -> Vec<Handle<AudioSource>>;
fn skip(&self) -> bool {
false
}
}

impl SfxEv for MouseButtonInput {
impl SfxEv for EcholocationEv {
fn get_volume(&self) -> f64 {
// 0.25.lerp(&1.0, &self.strength)
0.2
1.0
}

fn get_sfx_handles(sfx: &SfxAssets) -> &[Handle<AudioSource>] {
&sfx.click
fn get_sfx_handles(&self, sfx: &SfxAssets) -> Vec<Handle<AudioSource>> {
sfx.echo.clone()
}
}

fn skip(&self) -> bool {
!self.state.is_pressed()
impl SfxEv for PlayerEv {
fn get_volume(&self) -> f64 {
match self {
PlayerEv::ClearedLevel => 0.5,
PlayerEv::Died => 0.9,
}
}

fn get_sfx_handles(&self, sfx: &SfxAssets) -> Vec<Handle<AudioSource>> {
match self {
PlayerEv::ClearedLevel => vec![sfx.level_cleared.clone()],
PlayerEv::Died => sfx.player_death.clone(),
}
}
}

impl SfxEv for EnemyEv {
fn get_volume(&self) -> f64 {
match self {
EnemyEv::Killed => 0.85,
EnemyEv::Alarmed => 0.3,
}
}

fn get_sfx_handles(&self, sfx: &SfxAssets) -> Vec<Handle<AudioSource>> {
match self {
EnemyEv::Killed => sfx.enemy_death.clone(),
EnemyEv::Alarmed => sfx.enemy_alert.clone(),
}
}
}

Expand All @@ -57,7 +95,7 @@ fn play_sfx_on_evt<TEvt: Event + SfxEv>(
continue;
}

if let Some(sfx_handle) = TEvt::get_sfx_handles(&sfx).choose(&mut rand) {
if let Some(sfx_handle) = ev.get_sfx_handles(&sfx).choose(&mut rand) {
audio
.play(sfx_handle.clone())
.with_volume(ev.get_volume() * volume.get_sfx_volume());
Expand All @@ -68,3 +106,14 @@ fn play_sfx_on_evt<TEvt: Event + SfxEv>(
fn set_sfx_volume(audio: Res<AudioChannel<SfxChannel>>, volume: Res<VolumeSettings>) {
audio.set_volume(volume.get_sfx_volume());
}

fn play_drone(audio: Res<Audio>, assets: Res<SfxAssets>) {
audio
.play(assets.drone.clone())
.loop_from(1.5)
.fade_in(AudioTween::new(
Duration::from_secs(2),
AudioEasing::OutPowi(2),
))
.with_volume(0.2);
}
5 changes: 5 additions & 0 deletions src/echolocation/echolocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct EcholocationHitEv {
pub hit_e: Entity,
}

pub struct EcholocationEv;

pub struct Echolocate;

pub(super) fn echolocate(
Expand All @@ -42,11 +44,14 @@ pub(super) fn echolocate(
Without<Cooldown<Echolocate>>,
>,
textures: Res<TextureAssets>,
mut ev_w: EventWriter<EcholocationEv>,
) {
for (player_e, _, player_t) in input_q
.iter()
.filter(|(_, input, ..)| input.just_pressed(PlayerAction::Echo))
{
ev_w.send(EcholocationEv);

// cooldown
cmd.entity(player_e)
.try_insert(Cooldown::<Echolocate>::new(1.2));
Expand Down
5 changes: 4 additions & 1 deletion src/echolocation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use bevy::prelude::*;
use crate::{assets::textures::TextureAssets, state::UnpausedGame};

use self::{
echolocation::{echo_hit, echolocate, flash_on_echolocation, EcholocationHitEv},
echolocation::{
echo_hit, echolocate, flash_on_echolocation, EcholocationEv, EcholocationHitEv,
},
wave::spawn_wave,
};

Expand All @@ -12,6 +14,7 @@ pub mod wave;

pub fn echo_plugin(app: &mut App) {
app.add_event::<EcholocationHitEv>()
.add_event::<EcholocationEv>()
.add_systems((echolocate, echo_hit).in_set(UnpausedGame))
.add_system(flash_on_echolocation)
.add_system(spawn_wave.run_if(resource_exists::<TextureAssets>()));
Expand Down
Loading

0 comments on commit 286c61e

Please sign in to comment.