preload sounds

This commit is contained in:
artem 2024-03-23 22:18:12 +03:00
parent feab10f958
commit 3460fc16e3
13 changed files with 778 additions and 649 deletions

10
Cargo.lock generated
View File

@ -1574,6 +1574,9 @@ name = "lazy_static"
version = "1.4.0" version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
dependencies = [
"spin",
]
[[package]] [[package]]
name = "lazycell" name = "lazycell"
@ -2112,6 +2115,7 @@ name = "platformer"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"fyrox", "fyrox",
"lazy_static",
] ]
[[package]] [[package]]
@ -2647,6 +2651,12 @@ dependencies = [
"smallvec", "smallvec",
] ]
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]] [[package]]
name = "strength_reduce" name = "strength_reduce"
version = "0.2.4" version = "0.2.4"

View File

@ -12,10 +12,10 @@ export interface InitOutput {
readonly __wbindgen_malloc: (a: number, b: number) => number; readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_export_2: WebAssembly.Table; readonly __wbindgen_export_2: WebAssembly.Table;
readonly wasm_bindgen__convert__closures__invoke1_mut__h7977a475161f720f: (a: number, b: number, c: number) => void; readonly wasm_bindgen__convert__closures__invoke1_mut__h1174b3f5d0f364f0: (a: number, b: number, c: number) => void;
readonly wasm_bindgen__convert__closures__invoke0_mut__h9cfe64ae8a53b273: (a: number, b: number) => void; readonly wasm_bindgen__convert__closures__invoke0_mut__he7eec17026fa9bbe: (a: number, b: number) => void;
readonly wasm_bindgen__convert__closures__invoke2_mut__hba3c5a1b4df3b13d: (a: number, b: number, c: number, d: number) => void; readonly wasm_bindgen__convert__closures__invoke2_mut__h87cce4bf568ddd8d: (a: number, b: number, c: number, d: number) => void;
readonly wasm_bindgen__convert__closures__invoke0_mut__hc321ab21773f28ba: (a: number, b: number) => void; readonly wasm_bindgen__convert__closures__invoke0_mut__h0ec95cd6d7bcd320: (a: number, b: number) => void;
readonly wasm_bindgen__convert__closures__invoke1_mut__h0f1d8e7262983e79: (a: number, b: number, c: number) => void; readonly wasm_bindgen__convert__closures__invoke1_mut__h0f1d8e7262983e79: (a: number, b: number, c: number) => void;
readonly wasm_bindgen__convert__closures__invoke0_mut__h79d5b69c906304b3: (a: number, b: number) => void; readonly wasm_bindgen__convert__closures__invoke0_mut__h79d5b69c906304b3: (a: number, b: number) => void;
readonly _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h4d122b4606e1c898: (a: number, b: number, c: number) => void; readonly _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h4d122b4606e1c898: (a: number, b: number, c: number) => void;

View File

@ -222,19 +222,19 @@ function makeMutClosure(arg0, arg1, dtor, f) {
return real; return real;
} }
function __wbg_adapter_32(arg0, arg1, arg2) { function __wbg_adapter_32(arg0, arg1, arg2) {
wasm.wasm_bindgen__convert__closures__invoke1_mut__h7977a475161f720f(arg0, arg1, addHeapObject(arg2)); wasm.wasm_bindgen__convert__closures__invoke1_mut__h1174b3f5d0f364f0(arg0, arg1, addHeapObject(arg2));
} }
function __wbg_adapter_39(arg0, arg1) { function __wbg_adapter_35(arg0, arg1) {
wasm.wasm_bindgen__convert__closures__invoke0_mut__h9cfe64ae8a53b273(arg0, arg1); wasm.wasm_bindgen__convert__closures__invoke0_mut__he7eec17026fa9bbe(arg0, arg1);
} }
function __wbg_adapter_44(arg0, arg1, arg2, arg3) { function __wbg_adapter_44(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__hba3c5a1b4df3b13d(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3)); wasm.wasm_bindgen__convert__closures__invoke2_mut__h87cce4bf568ddd8d(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
} }
function __wbg_adapter_49(arg0, arg1) { function __wbg_adapter_49(arg0, arg1) {
wasm.wasm_bindgen__convert__closures__invoke0_mut__hc321ab21773f28ba(arg0, arg1); wasm.wasm_bindgen__convert__closures__invoke0_mut__h0ec95cd6d7bcd320(arg0, arg1);
} }
function __wbg_adapter_52(arg0, arg1, arg2) { function __wbg_adapter_52(arg0, arg1, arg2) {
@ -1607,68 +1607,68 @@ function __wbg_get_imports() {
const ret = wasm.memory; const ret = wasm.memory;
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper2774 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper11046 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2170, __wbg_adapter_32); const ret = makeMutClosure(arg0, arg1, 9777, __wbg_adapter_32);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper2775 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper11047 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2170, __wbg_adapter_32); const ret = makeMutClosure(arg0, arg1, 9777, __wbg_adapter_35);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper2776 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper11048 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2170, __wbg_adapter_32); const ret = makeMutClosure(arg0, arg1, 9777, __wbg_adapter_32);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper2777 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper11049 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2170, __wbg_adapter_39); const ret = makeMutClosure(arg0, arg1, 9777, __wbg_adapter_32);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper2778 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper11050 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2170, __wbg_adapter_32); const ret = makeMutClosure(arg0, arg1, 9777, __wbg_adapter_32);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper2779 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper11051 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2170, __wbg_adapter_44); const ret = makeMutClosure(arg0, arg1, 9777, __wbg_adapter_44);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper2780 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper11053 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2170, __wbg_adapter_32); const ret = makeMutClosure(arg0, arg1, 9777, __wbg_adapter_32);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper42133 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper42107 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36612, __wbg_adapter_49); const ret = makeMutClosure(arg0, arg1, 36555, __wbg_adapter_49);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper43449 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper43423 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 37245, __wbg_adapter_52); const ret = makeMutClosure(arg0, arg1, 37188, __wbg_adapter_52);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper43450 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper43424 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 37245, __wbg_adapter_52); const ret = makeMutClosure(arg0, arg1, 37188, __wbg_adapter_52);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper43451 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper43425 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 37245, __wbg_adapter_52); const ret = makeMutClosure(arg0, arg1, 37188, __wbg_adapter_52);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper43452 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper43426 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 37245, __wbg_adapter_52); const ret = makeMutClosure(arg0, arg1, 37188, __wbg_adapter_52);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper43453 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper43427 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 37245, __wbg_adapter_61); const ret = makeMutClosure(arg0, arg1, 37188, __wbg_adapter_61);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper43454 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper43428 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 37245, __wbg_adapter_52); const ret = makeMutClosure(arg0, arg1, 37188, __wbg_adapter_52);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper43455 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper43429 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 37245, __wbg_adapter_52); const ret = makeMutClosure(arg0, arg1, 37188, __wbg_adapter_52);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper45632 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper45615 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 37723, __wbg_adapter_68); const ret = makeMutClosure(arg0, arg1, 37673, __wbg_adapter_68);
return addHeapObject(ret); return addHeapObject(ret);
}; };

View File

@ -5,10 +5,10 @@ export function main(): void;
export function __wbindgen_malloc(a: number, b: number): number; export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number; export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
export const __wbindgen_export_2: WebAssembly.Table; export const __wbindgen_export_2: WebAssembly.Table;
export function wasm_bindgen__convert__closures__invoke1_mut__h7977a475161f720f(a: number, b: number, c: number): void; export function wasm_bindgen__convert__closures__invoke1_mut__h1174b3f5d0f364f0(a: number, b: number, c: number): void;
export function wasm_bindgen__convert__closures__invoke0_mut__h9cfe64ae8a53b273(a: number, b: number): void; export function wasm_bindgen__convert__closures__invoke0_mut__he7eec17026fa9bbe(a: number, b: number): void;
export function wasm_bindgen__convert__closures__invoke2_mut__hba3c5a1b4df3b13d(a: number, b: number, c: number, d: number): void; export function wasm_bindgen__convert__closures__invoke2_mut__h87cce4bf568ddd8d(a: number, b: number, c: number, d: number): void;
export function wasm_bindgen__convert__closures__invoke0_mut__hc321ab21773f28ba(a: number, b: number): void; export function wasm_bindgen__convert__closures__invoke0_mut__h0ec95cd6d7bcd320(a: number, b: number): void;
export function wasm_bindgen__convert__closures__invoke1_mut__h0f1d8e7262983e79(a: number, b: number, c: number): void; export function wasm_bindgen__convert__closures__invoke1_mut__h0f1d8e7262983e79(a: number, b: number, c: number): void;
export function wasm_bindgen__convert__closures__invoke0_mut__h79d5b69c906304b3(a: number, b: number): void; export function wasm_bindgen__convert__closures__invoke0_mut__h79d5b69c906304b3(a: number, b: number): void;
export function _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h4d122b4606e1c898(a: number, b: number, c: number): void; export function _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h4d122b4606e1c898(a: number, b: number, c: number): void;

View File

@ -5,3 +5,4 @@ edition = "2021"
[dependencies] [dependencies]
fyrox = {workspace = true} fyrox = {workspace = true}
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }

View File

@ -48,6 +48,7 @@ const ATTACK_SPEED: f32 = 1.0;
const SUPER_DAMAGE: f32 = 30.0; const SUPER_DAMAGE: f32 = 30.0;
const SUPER_ATTACK_SPEED: f32 = 0.3; const SUPER_ATTACK_SPEED: f32 = 0.3;
const MOAN_TIME: f32 = 3.1; const MOAN_TIME: f32 = 3.1;
const PAUSE_TO_NEXT_LEVEL: f32 = 1.0;
#[derive(Visit, Reflect, Debug, Clone, Default)] #[derive(Visit, Reflect, Debug, Clone, Default)]
pub struct Archer { pub struct Archer {
@ -209,7 +210,7 @@ impl Archer {
return; return;
} }
self.fight = false; self.fight = false;
player_script.take_damage(&damage); player_script.take_damage(&damage, ctx.elapsed_time);
} }
fn get_player_position(&self, context: &mut ScriptContext) -> Vector3<f32> { fn get_player_position(&self, context: &mut ScriptContext) -> Vector3<f32> {
@ -502,7 +503,7 @@ impl ScriptTrait for Archer {
if self.intersections_box(&mut context.scene.graph) { if self.intersections_box(&mut context.scene.graph) {
self.dead = true; self.dead = true;
let game = context.plugins.get_mut::<Game>(); let game = context.plugins.get_mut::<Game>();
game.next_level(); game.next_level(context.elapsed_time + PAUSE_TO_NEXT_LEVEL);
return; return;
} }
self.set_fight(context); self.set_fight(context);

View File

@ -46,6 +46,7 @@ const SPEED: f32 = 3.1;
const DAMAGE: f32 = 100.0; const DAMAGE: f32 = 100.0;
const ATTACK_SPEED: f32 = 0.1; const ATTACK_SPEED: f32 = 0.1;
const MOAN_TIME: f32 = 3.1; const MOAN_TIME: f32 = 3.1;
const PAUSE_TO_NEXT_LEVEL: f32 = 1.0;
#[derive(Visit, Reflect, Debug, Clone, Default)] #[derive(Visit, Reflect, Debug, Clone, Default)]
pub struct Swordman { pub struct Swordman {
@ -207,7 +208,7 @@ impl Swordman {
return; return;
} }
self.block = false; self.block = false;
player_script.take_damage(&self.attack_damage); player_script.take_damage(&self.attack_damage, ctx.elapsed_time);
} }
fn get_player_position(&self, context: &mut ScriptContext) -> Vector3<f32> { fn get_player_position(&self, context: &mut ScriptContext) -> Vector3<f32> {
@ -500,7 +501,7 @@ impl ScriptTrait for Swordman {
if self.intersections_box(&mut context.scene.graph) { if self.intersections_box(&mut context.scene.graph) {
self.dead = true; self.dead = true;
let game = context.plugins.get_mut::<Game>(); let game = context.plugins.get_mut::<Game>();
game.next_level(); game.next_level(context.elapsed_time + PAUSE_TO_NEXT_LEVEL);
return; return;
} }
self.set_fight(context); self.set_fight(context);

View File

@ -1,4 +1,5 @@
//! Game project. #[macro_use]
extern crate lazy_static;
use fyrox::core::color::Color; use fyrox::core::color::Color;
use fyrox::graph::SceneGraph; use fyrox::graph::SceneGraph;
use fyrox::gui::Thickness; use fyrox::gui::Thickness;
@ -67,6 +68,8 @@ pub struct Game {
level: u32, level: u32,
need_change_level: bool, need_change_level: bool,
need_show_menu: bool, need_show_menu: bool,
show_menu_time: f32,
change_level_time: f32,
} }
impl Game { impl Game {
@ -104,14 +107,18 @@ impl Game {
level: START_LEVEL, level: START_LEVEL,
need_change_level: false, need_change_level: false,
need_show_menu: false, need_show_menu: false,
show_menu_time: 0.0,
change_level_time: 0.0,
} }
} }
pub fn next_level(&mut self) { pub fn next_level(&mut self, change_level_time: f32) {
self.need_change_level = true; self.need_change_level = true;
self.change_level_time= change_level_time;
} }
pub fn show_menu(&mut self) { pub fn show_menu(&mut self, show_menu_time: f32) {
self.show_menu_time = show_menu_time;
self.need_show_menu = true; self.need_show_menu = true;
} }
} }
@ -134,13 +141,16 @@ impl Plugin for Game {
} }
fn update(&mut self, context: &mut PluginContext) { fn update(&mut self, context: &mut PluginContext) {
if self.need_change_level { if self.need_change_level && self.change_level_time < context.elapsed_time {
self.need_change_level = false; self.need_change_level = false;
self.level += 1; self.level += 1;
context.async_scene_loader.request("data/scene.rgs"); context.async_scene_loader.request("data/scene.rgs");
} }
if self.need_show_menu {
//println!("{} {} ", context.elapsed_time, self.show_menu_time);
if self.need_show_menu && self.show_menu_time < context.elapsed_time {
self.need_show_menu = false; self.need_show_menu = false;
self.show_menu_time = 0.0;
context context
.user_interface .user_interface
.send_message(WidgetMessage::visibility( .send_message(WidgetMessage::visibility(

View File

@ -6,7 +6,7 @@ pub enum ScreenSizeMessage {
}, },
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum SoundMessage { pub enum SoundMessage {
PlayerWalk, PlayerWalk,
PlayerJump, PlayerJump,
@ -20,3 +20,4 @@ pub enum SoundMessage {
SkeletonMoan, SkeletonMoan,
Bones, Bones,
} }

View File

@ -36,6 +36,7 @@ use fyrox::{
const JUMP_HEIGHT: f32 = 6.0; const JUMP_HEIGHT: f32 = 6.0;
const SPEED: f32 = 2.9; const SPEED: f32 = 2.9;
const ONE_SEC: f32 = 1.0;
#[derive(Visit, Reflect, Debug, Default, Clone)] #[derive(Visit, Reflect, Debug, Default, Clone)]
pub struct Player { pub struct Player {
@ -61,6 +62,9 @@ pub struct Player {
// Health Bar // Health Bar
max_health: f32, max_health: f32,
health: f32, health: f32,
#[reflect(hidden)]
#[visit(skip)]
time_dead: f32,
health_bar_background: Handle<Node>, health_bar_background: Handle<Node>,
health_bar_progress: Handle<Node>, health_bar_progress: Handle<Node>,
} }
@ -165,11 +169,14 @@ impl Player {
self.health_bar_progress = health_bar_progress; self.health_bar_progress = health_bar_progress;
} }
pub fn take_damage(&mut self, damage: &f32) { pub fn take_damage(&mut self, damage: &f32, time_damage: f32) {
if self.health == 0.0 { if self.health == 0.0 {
return; return;
} }
self.health -= damage; self.health -= damage;
if self.health <= 0.0 {
self.time_dead = time_damage;
}
} }
pub fn get_health(&self) -> f32 { pub fn get_health(&self) -> f32 {
@ -443,9 +450,9 @@ impl Player {
} }
health_bar_transform.set_position(Vector3::new(nex_x, d.y, d.z)); health_bar_transform.set_position(Vector3::new(nex_x, d.y, d.z));
} }
if self.health <= 0.0 { if self.health <= 0.0 && self.time_dead + ONE_SEC >= context.elapsed_time {
let game = context.plugins.get_mut::<Game>(); let game = context.plugins.get_mut::<Game>();
game.show_menu(); game.show_menu(context.elapsed_time + 3.0);
context.message_sender.send_global(SoundMessage::Lose); context.message_sender.send_global(SoundMessage::Lose);
} }
} }

View File

@ -14,6 +14,7 @@ use fyrox::{
}, },
script::{ScriptContext, ScriptMessageContext, ScriptMessagePayload, ScriptTrait}, script::{ScriptContext, ScriptMessageContext, ScriptMessagePayload, ScriptTrait},
}; };
use std::collections::HashMap;
#[derive(Visit, Reflect, Debug, Default, Clone)] #[derive(Visit, Reflect, Debug, Default, Clone)]
pub struct Sound { pub struct Sound {
@ -30,6 +31,27 @@ pub struct Sound {
win: Handle<Node>, win: Handle<Node>,
} }
lazy_static! {
static ref SOUNDS_NAMES: HashMap<SoundMessage, String> = {
let mut m = HashMap::new();
m.insert(SoundMessage::PlayerWalk, "player_walk.ogg".to_owned());
m.insert(SoundMessage::PlayerJump, "distance-jump-sound.ogg".to_owned());
m.insert(SoundMessage::EnemyWalk, "skeleton_walk.ogg".to_owned());
m.insert(SoundMessage::PlayerAttack, "player_sword.ogg".to_owned());
m.insert(SoundMessage::SkeletonAttack, "skeleton_sword.ogg".to_owned());
m.insert(SoundMessage::ArcherBowAttack, "bow-arrow.ogg".to_owned());
m.insert(SoundMessage::ArcherKnifeAttack, "knife.ogg".to_owned());
m.insert(SoundMessage::Lose, "lose.ogg".to_owned());
m.insert(SoundMessage::Win, "win.ogg".to_owned());
m.insert(SoundMessage::SkeletonMoan, "skeleton-woke-up.ogg".to_owned());
m.insert(SoundMessage::Bones, "the-bone-of-the-skeleton-moves.ogg".to_owned());
m
};
}
const FAST_SPEED: f64 = 2.0; const FAST_SPEED: f64 = 2.0;
const NORMAL_SPEED: f64 = 1.0; const NORMAL_SPEED: f64 = 1.0;
const LOW_GAIN: f32 = 0.1; const LOW_GAIN: f32 = 0.1;
@ -43,9 +65,16 @@ impl Sound {
RigidBodyBuilder::new(BaseBuilder::new().with_script(sound)).build(graph); RigidBodyBuilder::new(BaseBuilder::new().with_script(sound)).build(graph);
} }
fn new_sound(&self, ctx: &mut ScriptMessageContext, name: &str, pitch: f64, gain: f32) -> Handle<Node> { fn new_sound(
&self,
ctx: &mut ScriptMessageContext,
sound_type: SoundMessage,
pitch: f64,
gain: f32,
) -> Handle<Node> {
let name = SOUNDS_NAMES.get(&sound_type).unwrap().clone();
let mut path = "assets/data/sounds/".to_owned(); let mut path = "assets/data/sounds/".to_owned();
path.push_str(name); path.push_str(&name);
let walk_sound = ctx.resource_manager.request::<SoundBuffer>(path); let walk_sound = ctx.resource_manager.request::<SoundBuffer>(path);
return SoundBuilder::new(BaseBuilder::new()) return SoundBuilder::new(BaseBuilder::new())
.with_buffer(Some(walk_sound)) .with_buffer(Some(walk_sound))
@ -56,7 +85,14 @@ impl Sound {
.build(&mut ctx.scene.graph); .build(&mut ctx.scene.graph);
} }
fn play_sound(&self, ctx: &mut ScriptMessageContext, name: &str, handle: Handle<Node>, pitch: f64, gain: f32) -> Option<Handle<Node>> { fn play_sound(
&self,
ctx: &mut ScriptMessageContext,
sound_type: SoundMessage,
handle: Handle<Node>,
pitch: f64,
gain: f32,
) -> Option<Handle<Node>> {
let sound_body = ctx.scene.graph.try_get_mut(handle); let sound_body = ctx.scene.graph.try_get_mut(handle);
if let Some(sound) = sound_body { if let Some(sound) = sound_body {
if sound.as_sound().status() == Status::Stopped { if sound.as_sound().status() == Status::Stopped {
@ -64,7 +100,7 @@ impl Sound {
} }
return None; return None;
} }
return Some(self.new_sound(ctx, name, pitch, gain)); return Some(self.new_sound(ctx, sound_type, pitch, gain));
} }
} }
@ -72,10 +108,14 @@ impl_component_provider!(Sound,);
impl ScriptTrait for Sound { impl ScriptTrait for Sound {
// Called once at initialization. // Called once at initialization.
fn on_init(&mut self, _context: &mut ScriptContext) { fn on_init(&mut self, context: &mut ScriptContext) {
// context for (_, val) in SOUNDS_NAMES.iter() {
// .resource_manager let mut path = "assets/data/sounds/".to_owned();
// .request::<SoundBuffer>("/mnt/23154027-2dd3-43ea-93f1-02a8df7a3c5a/projects/fyrox/platformer/assets/data/sounds/player_walk.ogg"); path.push_str(val);
context
.resource_manager
.request::<SoundBuffer>(path);
}
} }
// Put start logic - it is called when every other script is already initialized. // Put start logic - it is called when every other script is already initialized.
@ -91,57 +131,115 @@ impl ScriptTrait for Sound {
ctx: &mut ScriptMessageContext, ctx: &mut ScriptMessageContext,
) { ) {
if let Some(SoundMessage::PlayerWalk) = message.downcast_ref::<SoundMessage>() { if let Some(SoundMessage::PlayerWalk) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "player_walk.ogg", self.player_walk, FAST_SPEED, LOW_GAIN) { if let Some(sound) = self.play_sound(
ctx,
SoundMessage::PlayerWalk,
self.player_walk,
FAST_SPEED,
LOW_GAIN,
) {
self.player_walk = sound; self.player_walk = sound;
} }
} }
if let Some(SoundMessage::EnemyWalk) = message.downcast_ref::<SoundMessage>() { if let Some(SoundMessage::EnemyWalk) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "skeleton_walk.ogg", self.skeleton_walk, FAST_SPEED, LOW_GAIN) { if let Some(sound) = self.play_sound(
ctx,
SoundMessage::EnemyWalk,
self.skeleton_walk,
FAST_SPEED,
LOW_GAIN,
) {
self.skeleton_walk = sound; self.skeleton_walk = sound;
} }
} }
if let Some(SoundMessage::PlayerAttack) = message.downcast_ref::<SoundMessage>() { if let Some(SoundMessage::PlayerAttack) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "player_sword.ogg", self.player_attack, FAST_SPEED, LOW_GAIN) { if let Some(sound) = self.play_sound(
ctx,
SoundMessage::PlayerAttack,
self.player_attack,
FAST_SPEED,
LOW_GAIN,
) {
self.player_attack = sound; self.player_attack = sound;
} }
} }
if let Some(SoundMessage::SkeletonAttack) = message.downcast_ref::<SoundMessage>() { if let Some(SoundMessage::SkeletonAttack) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "skeleton_sword.ogg", self.skeleton_attack, FAST_SPEED, LOW_GAIN) { if let Some(sound) = self.play_sound(
ctx,
SoundMessage::SkeletonAttack,
self.skeleton_attack,
FAST_SPEED,
LOW_GAIN,
) {
self.skeleton_attack = sound; self.skeleton_attack = sound;
} }
} }
if let Some(SoundMessage::ArcherBowAttack) = message.downcast_ref::<SoundMessage>() { if let Some(SoundMessage::ArcherBowAttack) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "bow-arrow.ogg", self.archer_bow_attack, FAST_SPEED, LOW_GAIN) { if let Some(sound) = self.play_sound(
ctx,
SoundMessage::ArcherBowAttack,
self.archer_bow_attack,
FAST_SPEED,
LOW_GAIN,
) {
self.archer_bow_attack = sound; self.archer_bow_attack = sound;
} }
} }
if let Some(SoundMessage::ArcherKnifeAttack) = message.downcast_ref::<SoundMessage>() { if let Some(SoundMessage::ArcherKnifeAttack) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "skeleton_sword.ogg", self.archer_knife_attack, FAST_SPEED, LOW_GAIN) { if let Some(sound) = self.play_sound(
ctx,
SoundMessage::ArcherKnifeAttack,
self.archer_knife_attack,
FAST_SPEED,
LOW_GAIN,
) {
self.archer_knife_attack = sound; self.archer_knife_attack = sound;
} }
} }
if let Some(SoundMessage::Lose) = message.downcast_ref::<SoundMessage>() { if let Some(SoundMessage::Lose) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "lose.ogg", self.lose, NORMAL_SPEED, NORMAL_GAIN) { if let Some(sound) =
self.play_sound(ctx, SoundMessage::Lose, self.lose, NORMAL_SPEED, NORMAL_GAIN)
{
self.lose = sound; self.lose = sound;
} }
} }
if let Some(SoundMessage::PlayerJump) = message.downcast_ref::<SoundMessage>() { if let Some(SoundMessage::PlayerJump) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "distance-jump-sound.ogg", self.player_jump, FAST_SPEED, LOW_GAIN) { if let Some(sound) = self.play_sound(
ctx,
SoundMessage::PlayerJump,
self.player_jump,
FAST_SPEED,
LOW_GAIN,
) {
self.player_jump = sound; self.player_jump = sound;
} }
} }
if let Some(SoundMessage::Win) = message.downcast_ref::<SoundMessage>() { if let Some(SoundMessage::Win) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "win.ogg", self.win, NORMAL_SPEED, NORMAL_GAIN) { if let Some(sound) =
self.play_sound(ctx, SoundMessage::Win, self.win, NORMAL_SPEED, NORMAL_GAIN)
{
self.win = sound; self.win = sound;
} }
} }
if let Some(SoundMessage::SkeletonMoan) = message.downcast_ref::<SoundMessage>() { if let Some(SoundMessage::SkeletonMoan) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "skeleton-woke-up.ogg", self.skeleton_moan, FAST_SPEED, LOW_GAIN) { if let Some(sound) = self.play_sound(
ctx,
SoundMessage::SkeletonMoan,
self.skeleton_moan,
FAST_SPEED,
LOW_GAIN,
) {
self.skeleton_moan = sound; self.skeleton_moan = sound;
} }
} }
if let Some(SoundMessage::Bones) = message.downcast_ref::<SoundMessage>() { if let Some(SoundMessage::Bones) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "the-bone-of-the-skeleton-moves.ogg", self.archer_moan, NORMAL_SPEED, LOW_GAIN) { if let Some(sound) = self.play_sound(
ctx,
SoundMessage::Bones,
self.archer_moan,
NORMAL_SPEED,
LOW_GAIN,
) {
self.archer_moan = sound; self.archer_moan = sound;
} }
} }

File diff suppressed because it is too large Load Diff