This commit is contained in:
artem 2024-02-24 11:19:18 +03:00
parent 21e2d1819a
commit 26991b0d90
5 changed files with 596 additions and 510 deletions

BIN
data/cd2f1-36d91_sunday.ttf Normal file

Binary file not shown.

Binary file not shown.

View File

@ -1,11 +1,10 @@
use fyrox::{ use fyrox::{
core::{ core::{
algebra::{Point2, Vector2, Vector3}, algebra::{Point2, Vector2, Vector3}, impl_component_provider, pool::Handle, reflect::prelude::*, uuid_provider, visitor::prelude::*
impl_component_provider, },
pool::Handle, gui::{
reflect::prelude::*, message::MessageDirection,
uuid_provider, widget::WidgetMessage,
visitor::prelude::*,
}, },
scene::{ scene::{
animation::spritesheet::SpriteSheetAnimation, animation::spritesheet::SpriteSheetAnimation,
@ -177,19 +176,24 @@ impl Enemy {
return Some(buffer); return Some(buffer);
} }
fn attack_player(&mut self, graph: &mut Graph) { fn attack_player(&mut self, ctx: &mut ScriptContext) {
if self.win { if self.win {
return; return;
} }
if !self.fight { if !self.fight {
return; return;
} }
match graph.try_get_script_of_mut::<Player>(self.player_handle) { match ctx.scene.graph.try_get_script_of_mut::<Player>(self.player_handle) {
Some(script) => { Some(script) => {
if script.get_health() <= 0.0 { if script.get_health() <= 0.0 {
self.fight = false; self.fight = false;
self.block = false; self.block = false;
self.win = true; self.win = true;
ctx.user_interface.send_message(WidgetMessage::visibility(
ctx.user_interface.root(),
MessageDirection::ToWidget,
true,
));
return; return;
} }
self.block = false; self.block = false;
@ -346,7 +350,7 @@ impl Enemy {
} }
self.attack_timer += context.dt; self.attack_timer += context.dt;
if self.attack_timer >= self.attack_speed { if self.attack_timer >= self.attack_speed {
self.attack_player(&mut context.scene.graph); self.attack_player(context);
self.attack_timer = 0.0; self.attack_timer = 0.0;
return; return;
} }

View File

@ -4,25 +4,27 @@ use fyrox::{
core::{log::Log, pool::Handle}, core::{log::Log, pool::Handle},
engine::GraphicsContext, engine::GraphicsContext,
event::{Event, WindowEvent}, event::{Event, WindowEvent},
gui::{message::UiMessage, UserInterface}, gui::{
button::ButtonMessage,
message::{MessageDirection, UiMessage},
widget::WidgetMessage,
UiNode, UserInterface,
},
plugin::{Plugin, PluginConstructor, PluginContext, PluginRegistrationContext}, plugin::{Plugin, PluginConstructor, PluginContext, PluginRegistrationContext},
scene::{ scene::{graph::Graph, Scene},
graph::Graph, Scene
}
}; };
use std::path::Path; use std::path::Path;
mod map;
mod player;
mod enemy; mod enemy;
mod enemy_spawn; mod enemy_spawn;
mod map;
mod msg; mod msg;
mod player;
use map::build_map;
use player::Player;
use enemy::Enemy; use enemy::Enemy;
use enemy_spawn::EnemySpawn; use enemy_spawn::EnemySpawn;
use map::build_map;
use msg::Message; use msg::Message;
use player::Player;
pub struct GameConstructor; pub struct GameConstructor;
@ -41,24 +43,30 @@ impl PluginConstructor for GameConstructor {
pub struct Game { pub struct Game {
scene: Handle<Scene>, scene: Handle<Scene>,
new_game: Handle<UiNode>,
exit: Handle<UiNode>,
} }
impl Game { impl Game {
pub fn new(scene_path: Option<&str>, context: PluginContext) -> Self { pub fn new(_scene_path: Option<&str>, ctx: PluginContext) -> Self {
context ctx.task_pool.spawn_plugin_task(
.async_scene_loader UserInterface::load_from_file("data/menu.ui", ctx.resource_manager.clone()),
.request(scene_path.unwrap_or("data/scene.rgs"));
context.task_pool.spawn_plugin_task(
UserInterface::load_from_file("data/menu.ui", context.resource_manager.clone()),
|result, game: &mut Game, ctx| match result { |result, game: &mut Game, ctx| match result {
Ok(menu) => { Ok(menu) => {
*ctx.user_interface = menu; *ctx.user_interface = menu;
game.new_game = ctx.user_interface.find_by_name_down_from_root("NewGame");
game.exit = ctx.user_interface.find_by_name_down_from_root("Exit");
} }
Err(e) => Log::err(format!("Unable to load main menu! Reason: {:?}", e)), Err(e) => Log::err(format!("Unable to load main menu! Reason: {:?}", e)),
// let ui = ctx.user_interface.clone();
// *ui = result.unwrap();
}, },
); );
Self { Self {
scene: Handle::NONE, scene: Handle::NONE,
new_game: Handle::NONE,
exit: Handle::NONE,
} }
} }
} }
@ -79,7 +87,7 @@ impl Plugin for Game {
for script_scene in &context.script_processor.scripted_scenes { for script_scene in &context.script_processor.scripted_scenes {
script_scene.message_sender.send_global(Message::Resize { script_scene.message_sender.send_global(Message::Resize {
width: window_size.width, width: window_size.width,
height: window_size.height height: window_size.height,
}); });
} }
} }
@ -89,15 +97,29 @@ impl Plugin for Game {
for script_scene in &context.script_processor.scripted_scenes { for script_scene in &context.script_processor.scripted_scenes {
script_scene.message_sender.send_global(Message::Resize { script_scene.message_sender.send_global(Message::Resize {
width: size.width, width: size.width,
height: size.height height: size.height,
}); });
} }
} }
} }
} }
fn on_ui_message(&mut self, _context: &mut PluginContext, _message: &UiMessage) { fn on_ui_message(&mut self, ctx: &mut PluginContext, message: &UiMessage) {
// Handle UI events here. if let Some(ButtonMessage::Click) = message.data() {
if message.destination() == self.new_game {
ctx.user_interface.send_message(WidgetMessage::visibility(
ctx.user_interface.root(),
MessageDirection::ToWidget,
false,
));
ctx.async_scene_loader
.request("data/scene.rgs");
} else if message.destination() == self.exit {
if let Some(window_target) = ctx.window_target {
window_target.exit();
}
}
}
} }
fn on_scene_begin_loading(&mut self, _path: &Path, ctx: &mut PluginContext) { fn on_scene_begin_loading(&mut self, _path: &Path, ctx: &mut PluginContext) {

File diff suppressed because it is too large Load Diff