skullball/rust/src/game/commands.rs

75 lines
2.2 KiB
Rust

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<CommandLinePanel>) {
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<CommandLinePanel>) {
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"
}
}