diff --git a/rust/src/game/commands.rs b/rust/src/game/commands.rs new file mode 100644 index 0000000..d588ee0 --- /dev/null +++ b/rust/src/game/commands.rs @@ -0,0 +1,74 @@ +use godot::classes::class_macros::private::virtuals::Xrvrs::Gd; + +use crate::{ + game::Game, + ui::cli::{CliColor, Command, CommandLinePanel}, +}; + +pub fn register_commands(cli: &mut Gd) { + cli.bind_mut() + .register_command("respawn_ball".to_string(), Box::new(RespawnBallCommand)); + cli.bind_mut() + .register_command("players".to_string(), Box::new(ListPlayersCommand)); + cli.bind_mut() + .register_command("kill".to_string(), Box::new(KillCommand)); +} + +pub fn unregister_commands(cli: &mut Gd) { + cli.bind_mut() + .unregister_command("respawn_ball".to_string()); + cli.bind_mut().unregister_command("players".to_string()); + cli.bind_mut().unregister_command("kill".to_string()); +} + +pub struct RespawnBallCommand; +impl Command for RespawnBallCommand { + fn execute(&self, _: &mut CommandLinePanel, _: &[&str]) { + if let Some(game) = &mut Game::singleton() { + game.bind_mut().respawn_ball(); + } + } + + fn help(&self) -> &str { + "respawns the ball" + } +} + +pub struct ListPlayersCommand; +impl Command for ListPlayersCommand { + fn execute(&self, cli: &mut CommandLinePanel, _: &[&str]) { + if let Some(game) = &mut Game::singleton() { + for player in &game.bind().players { + cli.publish_message( + format!(" - {} ({})", player.data.name, player.id()), + CliColor::Info, + ); + } + } + } + + fn help(&self) -> &str { + "lists all players" + } +} + +pub struct KillCommand; +impl Command for KillCommand { + fn execute(&self, cli: &mut CommandLinePanel, args: &[&str]) { + if let Some(player_str) = args.first() { + if let Ok(player_id) = player_str.parse() { + if let Some(game) = &mut Game::singleton() { + game.bind_mut().kill_player(player_id); + } + } else { + cli.publish_message("Invalid player id".to_string(), CliColor::Error); + } + } else { + cli.publish_message("Usage: kill [player_id]".to_string(), CliColor::Error); + } + } + + fn help(&self) -> &str { + "kills given player" + } +} diff --git a/rust/src/game/mod.rs b/rust/src/game/mod.rs index 8271adf..e8300a0 100644 --- a/rust/src/game/mod.rs +++ b/rust/src/game/mod.rs @@ -10,7 +10,13 @@ use crate::{ }, bgm::{Music, MusicManager}, chat::{Chat, ChatMessage}, - game::opts::{GameOption, GameOptionValue, GameOptions}, + game::{ + commands::{ + KillCommand, ListPlayersCommand, RespawnBallCommand, register_commands, + unregister_commands, + }, + opts::{GameOption, GameOptionValue, GameOptions}, + }, map::Map, map_resource::MapResource, net::{ @@ -33,6 +39,7 @@ use crate::{ ui::cli::{CliColor, Command, CommandLinePanel}, }; +pub mod commands; pub mod opts; pub const GAME_MANAGER_GLOBAL: &str = "GameManagerGlobal"; @@ -155,58 +162,6 @@ impl GameManager { } } -pub struct RespawnBallCommand; -impl Command for RespawnBallCommand { - fn execute(&self, _: &mut CommandLinePanel, _: &[&str]) { - if let Some(game) = &mut Game::singleton() { - game.bind_mut().respawn_ball(); - } - } - - fn help(&self) -> &str { - "respawns the ball" - } -} - -pub struct ListPlayersCommand; -impl Command for ListPlayersCommand { - fn execute(&self, cli: &mut CommandLinePanel, _: &[&str]) { - if let Some(game) = &mut Game::singleton() { - for player in &game.bind().players { - cli.publish_message( - format!(" - {} ({})", player.data.name, player.id()), - CliColor::Info, - ); - } - } - } - - fn help(&self) -> &str { - "lists all players" - } -} - -pub struct KillCommand; -impl Command for KillCommand { - fn execute(&self, cli: &mut CommandLinePanel, args: &[&str]) { - if let Some(player_str) = args.first() { - if let Ok(player_id) = player_str.parse() { - if let Some(game) = &mut Game::singleton() { - game.bind_mut().kill_player(player_id); - } - } else { - cli.publish_message("Invalid player id".to_string(), CliColor::Error); - } - } else { - cli.publish_message("Usage: kill [player_id]".to_string(), CliColor::Error); - } - } - - fn help(&self) -> &str { - "kills given player" - } -} - #[derive(GodotClass)] #[class(base=Node, init)] pub struct Game { @@ -440,12 +395,7 @@ impl Game { peer.set_accepting_connections(false); let mut cli = CommandLinePanel::singleton(); - cli.bind_mut() - .register_command("respawn_ball".to_string(), Box::new(RespawnBallCommand)); - cli.bind_mut() - .register_command("players".to_string(), Box::new(ListPlayersCommand)); - cli.bind_mut() - .register_command("kill".to_string(), Box::new(KillCommand)); + register_commands(&mut cli); } } }); @@ -514,10 +464,7 @@ impl Game { }); let mut cli = CommandLinePanel::singleton(); - cli.bind_mut() - .unregister_command("respawn_ball".to_string()); - cli.bind_mut().unregister_command("players".to_string()); - cli.bind_mut().unregister_command("kill".to_string()); + unregister_commands(&mut cli); } pub fn change_option(&mut self, opt: GameOption, value: GameOptionValue) {