1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::app3d::{AppResource, DebugResource};
use crate::simulation::commands::{
    MetricsCommand, ObjectCommand, PauseCommand, SetSpeedupCommand, SimulationCommand,
};
use bevy::prelude::*;
use bevy_console::{
    AddConsoleCommand, ConsoleCommand, ConsoleConfiguration, ConsolePlugin, NamedCommand,
};
use clap::Parser;

pub fn add_console_to_app(app: &mut App) {
    app.add_plugins(ConsolePlugin);
    app.add_console_command::<DebugCommand, _>(debug_command);
    app.add_console_command::<PauseCommand, _>(pause_command);
    app.add_console_command::<SetSpeedupCommand, _>(set_speedup_command);
    app.add_console_command::<ObjectCommand, _>(object_command);
    app.add_console_command::<MetricsCommand, _>(metrics_command);
    app.insert_resource(ConsoleConfiguration {
        ..Default::default()
    });
}

/// Some debug configuration
#[derive(Parser, ConsoleCommand)]
#[command(name = "debug")]
struct DebugCommand {}

impl Resource for PauseCommand {}
impl NamedCommand for PauseCommand {
    fn name() -> &'static str {
        "pause"
    }
}

impl Resource for SetSpeedupCommand {}
impl NamedCommand for SetSpeedupCommand {
    fn name() -> &'static str {
        "speedup"
    }
}

impl Resource for ObjectCommand {}
impl NamedCommand for ObjectCommand {
    fn name() -> &'static str {
        "object"
    }
}

impl Resource for MetricsCommand {}
impl NamedCommand for MetricsCommand {
    fn name() -> &'static str {
        "metrics"
    }
}

fn debug_command(
    mut console_command: ConsoleCommand<DebugCommand>,
    mut debug_resource: ResMut<DebugResource>,
) {
    if let Some(Ok(_command)) = console_command.take() {
        debug_resource.show_train_target = !debug_resource.show_train_target;
        console_command.ok();
    }
}

fn pause_command(
    mut console_command: ConsoleCommand<PauseCommand>,
    mut app_resource: ResMut<AppResource>,
) {
    if let Some(Ok(command)) = console_command.take() {
        if let Some(simulation) = &app_resource.simulation.as_mut() {
            if let Ok(mut simulation) = simulation.write() {
                if let Some(info) = command.execute(&mut simulation) {
                    console_command.reply_ok(info);
                }
            }
        }
    }
}

fn set_speedup_command(
    mut console_command: ConsoleCommand<SetSpeedupCommand>,
    mut app_resource: ResMut<AppResource>,
) {
    if let Some(Ok(command)) = console_command.take() {
        if let Some(simulation) = &app_resource.simulation.as_mut() {
            if let Ok(mut simulation) = simulation.write() {
                if let Some(info) = command.execute(&mut simulation) {
                    console_command.reply_ok(info);
                }
            }
        }
    }
}

fn object_command(
    mut console_command: ConsoleCommand<ObjectCommand>,
    mut app_resource: ResMut<AppResource>,
) {
    if let Some(Ok(command)) = console_command.take() {
        if let Some(simulation) = &app_resource.simulation.as_mut() {
            if let Ok(mut simulation) = simulation.write() {
                if let Some(info) = command.execute(&mut simulation) {
                    console_command.reply(info);
                }
            }
        }
    }
}

fn metrics_command(
    mut console_command: ConsoleCommand<MetricsCommand>,
    mut app_resource: ResMut<AppResource>,
) {
    if let Some(Ok(command)) = console_command.take() {
        if let Some(simulation) = &app_resource.simulation.as_mut() {
            if let Ok(mut simulation) = simulation.write() {
                if let Some(info) = command.execute(&mut simulation) {
                    console_command.reply(info);
                }
            }
        }
    }
}