This commit is contained in:
artem 2024-03-09 15:21:51 +03:00
parent 82831eaa20
commit cbc11a3997
19 changed files with 651 additions and 532 deletions

Binary file not shown.

BIN
assets/data/sounds/menu.ogg Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
assets/data/sounds/win.ogg Normal file

Binary file not shown.

Binary file not shown.

View File

@ -1607,68 +1607,68 @@ function __wbg_get_imports() {
const ret = wasm.memory;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper5083 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5140, __wbg_adapter_32);
imports.wbg.__wbindgen_closure_wrapper5127 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5178, __wbg_adapter_32);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper5084 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5140, __wbg_adapter_32);
imports.wbg.__wbindgen_closure_wrapper5128 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5178, __wbg_adapter_32);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper5085 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5140, __wbg_adapter_32);
imports.wbg.__wbindgen_closure_wrapper5129 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5178, __wbg_adapter_32);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper5086 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5140, __wbg_adapter_32);
imports.wbg.__wbindgen_closure_wrapper5130 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5178, __wbg_adapter_32);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper5087 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5140, __wbg_adapter_41);
imports.wbg.__wbindgen_closure_wrapper5131 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5178, __wbg_adapter_41);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper5088 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5140, __wbg_adapter_44);
imports.wbg.__wbindgen_closure_wrapper5132 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5178, __wbg_adapter_44);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper5089 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5140, __wbg_adapter_32);
imports.wbg.__wbindgen_closure_wrapper5133 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 5178, __wbg_adapter_32);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper41469 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36204, __wbg_adapter_49);
imports.wbg.__wbindgen_closure_wrapper41513 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36242, __wbg_adapter_49);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper42724 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36823, __wbg_adapter_52);
imports.wbg.__wbindgen_closure_wrapper42768 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36861, __wbg_adapter_52);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper42725 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36823, __wbg_adapter_55);
imports.wbg.__wbindgen_closure_wrapper42769 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36861, __wbg_adapter_55);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper42726 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36823, __wbg_adapter_55);
imports.wbg.__wbindgen_closure_wrapper42770 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36861, __wbg_adapter_55);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper42727 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36823, __wbg_adapter_55);
imports.wbg.__wbindgen_closure_wrapper42771 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36861, __wbg_adapter_55);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper42728 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36823, __wbg_adapter_55);
imports.wbg.__wbindgen_closure_wrapper42772 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36861, __wbg_adapter_55);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper42729 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36823, __wbg_adapter_55);
imports.wbg.__wbindgen_closure_wrapper42773 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36861, __wbg_adapter_55);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper42730 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36823, __wbg_adapter_55);
imports.wbg.__wbindgen_closure_wrapper42774 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 36861, __wbg_adapter_55);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper45025 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 37316, __wbg_adapter_68);
imports.wbg.__wbindgen_closure_wrapper45069 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 37354, __wbg_adapter_68);
return addHeapObject(ret);
};

View File

@ -1,3 +1,5 @@
use crate::msg::SoundMessage;
use crate::Game;
use crate::Player;
use fyrox::graph::SceneGraph;
use fyrox::{
@ -22,7 +24,6 @@ use fyrox::{
},
script::{ScriptContext, ScriptTrait},
};
use crate::Game;
impl_component_provider!(Swordman,);
uuid_provider!(Swordman = "a5671d19-9f1a-4286-8486-add4ebaadaec");
@ -283,12 +284,12 @@ impl Swordman {
if dif_x < 0.0 {
dif_x *= -1.0;
}
if player_position.y > enemy_rigid_body.global_position().y && dif_x < 1.0 {
if player_position.y > enemy_rigid_body.global_position().y && dif_x < 1.0 {
if player_position.x > enemy_rigid_body.global_position().x {
return SPEED * -1.0;
return SPEED * -1.0;
}
if player_position.x < enemy_rigid_body.global_position().x {
return SPEED;
return SPEED;
}
}
if enemy_rigid_body.global_position().x < END_MAP_LEFT + 3.0 {
@ -346,6 +347,9 @@ impl Swordman {
if let Some(enemy_rigid_data) = enemy_rigid_body {
enemy_rigid_data.set_lin_vel(Vector2::new(x_speed, -1.0));
}
if x_speed != 0.0 {
context.message_sender.send_global(SoundMessage::EnemyWalk);
}
}
fn animate_choose(&mut self, context: &mut ScriptContext) {

View File

@ -18,19 +18,24 @@ use fyrox::{
},
keyboard::{KeyCode, PhysicalKey},
plugin::{Plugin, PluginConstructor, PluginContext, PluginRegistrationContext},
scene::{graph::Graph, Scene},
scene::{
graph::Graph,
Scene,
},
};
use std::path::Path;
mod enemies;
mod map;
mod msg;
mod player;
mod sound;
use enemies::archer::{enemy::Archer, spawn::ArcherSpawn};
use enemies::swordsman::{enemy::Swordman, spawn::SwordmanSpawn};
use map::build_map;
use msg::Message;
use msg::ScreenSizeMessage;
use player::Player;
use sound::Sound;
const START_LEVEL: u32 = 1;
pub struct GameConstructor;
@ -41,6 +46,7 @@ impl PluginConstructor for GameConstructor {
let script_constructors = &context.serialization_context.script_constructors;
script_constructors.add::<Swordman>("Enemy");
script_constructors.add::<Player>("Player");
script_constructors.add::<Sound>("Sound");
}
fn create_instance(&self, scene_path: Option<&str>, context: PluginContext) -> Box<dyn Plugin> {
@ -77,6 +83,7 @@ impl Game {
// *ui = result.unwrap();
},
);
Self {
level_node: Handle::NONE,
scene: Handle::NONE,
@ -144,7 +151,7 @@ impl Plugin for Game {
if let GraphicsContext::Initialized(ref graphics_context) = context.graphics_context {
let window_size = graphics_context.window.inner_size();
for script_scene in &context.script_processor.scripted_scenes {
script_scene.message_sender.send_global(Message::Resize {
script_scene.message_sender.send_global(ScreenSizeMessage::Resize {
width: window_size.width,
height: window_size.height,
});
@ -154,7 +161,7 @@ impl Plugin for Game {
if let Event::WindowEvent { event, .. } = event {
if let WindowEvent::Resized(size) = event {
for script_scene in &context.script_processor.scripted_scenes {
script_scene.message_sender.send_global(Message::Resize {
script_scene.message_sender.send_global(ScreenSizeMessage::Resize {
width: size.width,
height: size.height,
});
@ -185,13 +192,11 @@ impl Plugin for Game {
if self.scene.is_some() {
ctx.scenes.remove(self.scene);
}
ctx
.user_interface
.send_message(WidgetMessage::visibility(
self.level_node,
MessageDirection::ToWidget,
false,
));
ctx.user_interface.send_message(WidgetMessage::visibility(
self.level_node,
MessageDirection::ToWidget,
false,
));
}
fn on_scene_loaded(
@ -214,7 +219,9 @@ impl Plugin for Game {
let graph: &mut Graph = &mut context.scenes[self.scene].graph;
let resource_manager: &ResourceManager = &context.resource_manager;
build_map(graph, resource_manager, self.level);
Sound::new(graph);
if self.level == 1 {
SwordmanSpawn::new().spawn_enemy(graph, resource_manager, -5.0);
}
@ -223,7 +230,7 @@ impl Plugin for Game {
}
let ui_ctx = &mut context.user_interface.build_ctx();
self.level_node = GridBuilder::new(
self.level_node = GridBuilder::new(
WidgetBuilder::new().with_child(
TextBuilder::new(WidgetBuilder::new().with_foreground(Brush::Solid(Color::BLACK)))
.with_text(format!("Level: {}", self.level))

View File

@ -1,7 +1,14 @@
#[derive(Debug, Clone)]
pub enum Message {
pub enum ScreenSizeMessage {
Resize {
width: u32,
height: u32,
},
}
#[derive(Debug, Clone)]
pub enum SoundMessage {
PlayerWalk,
EnemyWalk,
PlayerAttack,
}

View File

@ -1,6 +1,10 @@
use std::collections::VecDeque;
use crate::msg::Message;
use crate::msg::ScreenSizeMessage;
use crate::msg::SoundMessage;
use crate::Archer;
use crate::Game;
use crate::Swordman;
use fyrox::graph::BaseSceneGraph;
use fyrox::{
core::{
@ -29,14 +33,11 @@ use fyrox::{
},
script::{ScriptContext, ScriptMessageContext, ScriptMessagePayload, ScriptTrait},
};
use crate::Game;
use crate::Swordman;
use crate::Archer;
const JUMP_HEIGHT: f32 = 6.0;
const SPEED: f32 = 2.9;
#[derive(Visit, Reflect, Debug, Clone, Default)]
#[derive(Visit, Reflect, Debug, Default, Clone)]
pub struct Player {
sprite: Handle<Node>,
move_left: bool,
@ -227,6 +228,9 @@ impl Player {
Some(rigid_data) => {
let x_speed = self.get_speed();
rigid_data.set_lin_vel(Vector2::new(x_speed, rigid_data.lin_vel().y));
if x_speed != 0.0 && !self.is_in_fly(rigid_data) {
context.message_sender.send_global(SoundMessage::PlayerWalk);
}
}
None => {}
}
@ -332,6 +336,7 @@ impl Player {
Some(node) => node,
None => return,
};
context.message_sender.send_global(SoundMessage::PlayerAttack);
let self_position = self_node.global_position();
// Cast a ray from *this* node in the direction of the player node
let mut buffer = Vec::<Intersection>::new();
@ -364,17 +369,11 @@ impl Player {
parent = Some(d.parent());
}
if let Some(d) = parent {
let enemy_script = context
.scene
.graph
.try_get_script_of_mut::<Swordman>(d);
let enemy_script = context.scene.graph.try_get_script_of_mut::<Swordman>(d);
if let Some(enemy) = enemy_script {
enemy.take_damage(&self.damage);
}
let enemy_script = context
.scene
.graph
.try_get_script_of_mut::<Archer>(d);
let enemy_script = context.scene.graph.try_get_script_of_mut::<Archer>(d);
if let Some(enemy) = enemy_script {
enemy.take_damage(&self.damage);
}
@ -431,7 +430,7 @@ impl Player {
// If the new scale is different from the current scale, update the scale
if health_bar_scale.x != new_health {
health_bar_transform.set_scale(Vector3::new(
new_health ,
new_health,
// Don't change the y or z scale
health_bar_scale.y,
health_bar_scale.z,
@ -440,16 +439,16 @@ impl Player {
let mut nex_x = 0.03;
if new_health < 0.7 {
nex_x = 0.05
}
}
if new_health < 0.5 {
nex_x = 0.07
}
}
if new_health < 0.3 {
nex_x = 0.08
}
}
if new_health < 0.2 {
nex_x = 0.1
}
}
health_bar_transform.set_position(Vector3::new(nex_x, d.y, d.z));
}
if self.health <= 0.0 {
@ -512,7 +511,7 @@ impl ScriptTrait for Player {
fn on_start(&mut self, context: &mut ScriptContext) {
context
.message_dispatcher
.subscribe_to::<Message>(context.handle);
.subscribe_to::<ScreenSizeMessage>(context.handle);
}
fn on_message(
@ -521,7 +520,7 @@ impl ScriptTrait for Player {
_ctx: &mut ScriptMessageContext,
) {
// React to message.
if let Some(Message::Resize { width, height }) = message.downcast_ref::<Message>() {
if let Some(ScreenSizeMessage::Resize { width, height }) = message.downcast_ref::<ScreenSizeMessage>() {
self.window_height = height.clone();
self.window_width = width.clone();
}

102
game/src/sound.rs Normal file
View File

@ -0,0 +1,102 @@
use crate::msg::SoundMessage;
use fyrox::{
core::{
impl_component_provider, pool::Handle, reflect::prelude::*, uuid_provider,
visitor::prelude::*,
},
event::Event,
scene::{
base::BaseBuilder,
dim2::rigidbody::RigidBodyBuilder,
graph::Graph,
node::Node,
sound::{SoundBuffer, SoundBuilder, Status},
},
script::{ScriptContext, ScriptMessageContext, ScriptMessagePayload, ScriptTrait},
};
#[derive(Visit, Reflect, Debug, Default, Clone)]
pub struct Sound {
player_walk: Handle<Node>,
skeleton_walk: Handle<Node>,
player_attack: Handle<Node>,
}
uuid_provider!(Sound = "cc671d19-9f1a-4286-8486-add4ebaadaec");
impl Sound {
pub fn new(graph: &mut Graph) {
let sound = Sound::default();
RigidBodyBuilder::new(BaseBuilder::new().with_script(sound)).build(graph);
}
fn new_sound(&self, ctx: &mut ScriptMessageContext, name: &str) -> Handle<Node> {
let mut path = "/mnt/23154027-2dd3-43ea-93f1-02a8df7a3c5a/projects/fyrox/platformer/assets/data/sounds/".to_owned();
path.push_str(name);
let walk_sound = ctx.resource_manager.request::<SoundBuffer>(path);
return SoundBuilder::new(BaseBuilder::new())
.with_buffer(Some(walk_sound))
.with_status(Status::Playing)
.with_play_once(true)
.with_gain(0.1)
.with_pitch(2.0)
.build(&mut ctx.scene.graph);
}
fn play_sound(&self, ctx: &mut ScriptMessageContext, name: &str, handle: Handle<Node>) -> Option<Handle<Node>> {
let sound_body = ctx.scene.graph.try_get_mut(handle);
if let Some(sound) = sound_body {
if sound.as_sound().status() == Status::Stopped {
sound.as_sound_mut().set_status(Status::Playing);
}
return None;
}
return Some(self.new_sound(ctx, name));
}
}
impl_component_provider!(Sound,);
impl ScriptTrait for Sound {
// Called once at initialization.
fn on_init(&mut self, _context: &mut ScriptContext) {
// context
// .resource_manager
// .request::<SoundBuffer>("/mnt/23154027-2dd3-43ea-93f1-02a8df7a3c5a/projects/fyrox/platformer/assets/data/sounds/player_walk.ogg");
}
// Put start logic - it is called when every other script is already initialized.
fn on_start(&mut self, context: &mut ScriptContext) {
context
.message_dispatcher
.subscribe_to::<SoundMessage>(context.handle);
}
fn on_message(
&mut self,
message: &mut dyn ScriptMessagePayload,
ctx: &mut ScriptMessageContext,
) {
if let Some(SoundMessage::PlayerWalk) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "player_walk.ogg", self.player_walk) {
self.player_walk = sound;
}
}
if let Some(SoundMessage::EnemyWalk) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "skeleton_walk.ogg", self.skeleton_walk) {
self.skeleton_walk = sound;
}
}
if let Some(SoundMessage::PlayerAttack) = message.downcast_ref::<SoundMessage>() {
if let Some(sound) = self.play_sound(ctx, "player_sword.ogg", self.player_attack) {
self.player_attack = sound;
}
}
}
// Called whenever there is an event from OS (mouse click, keypress, etc.)
fn on_os_event(&mut self, _event: &Event<()>, _context: &mut ScriptContext) {}
// Called every frame at fixed rate of 60 FPS.
fn on_update(&mut self, _context: &mut ScriptContext) {}
}

File diff suppressed because it is too large Load Diff