-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommand.rs
More file actions
178 lines (157 loc) · 4.98 KB
/
command.rs
File metadata and controls
178 lines (157 loc) · 4.98 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//!
//! # Command Design Pattern
//!
//! To run/test, please run the following commands in your terminal
//!
//! ```sh
//! cargo run --bin command
//! ```
//!
//! ```sh
//! cargo test --bin command
//! ```
//!
//! Command pattern is a behavioral design pattern that turns a request into a
//! stand-alone object that contains all the information about the request.
//!
//! Key components in this pattern are as follows:
//!
//! | component | description |
//! | ---------------- | -------------------------------------------- |
//! | command | Trait that defines the common method |
//! | Concrete Command | Each struct that implements Command trait |
//! | Invoker | function that invokes the concrete command |
//! | Receiver | Function that performs actual work |
//! | Client | Part of a code that uses the above mechanism |
//!
use std::{thread::sleep, time::Duration};
// Example below uses Command pattern to fire a weapon in a shooting game in
// which firing command directly fires any weapon without knowing the detail
// about each element's fire procedure.
use console::Term;
const MAX_AMMO: usize = 30;
// Define Command
trait Command {
fn execute(&self) -> u8;
}
// Define Weapon Classes and traits
trait WeaponTrait {
fn fire(&mut self) -> u8; // it returns number of bullets fired
}
#[derive(Clone)]
struct AssultRifle {
fire_rate: u8,
}
#[derive(Clone)]
struct Sniper {
fire_rate: u8,
efficiency: u8,
}
impl WeaponTrait for AssultRifle {
fn fire(&mut self) -> u8 {
// sleep(Duration::from_millis((100 - self.fire_rate as u64) * 10));
self.fire_rate
}
}
impl WeaponTrait for Sniper {
fn fire(&mut self) -> u8 {
// sleep(Duration::from_millis((100 - self.fire_rate as u64) * 10));
(self.fire_rate as f32 * self.efficiency as f32 * 0.01) as u8
}
}
// Define Concrete Command classes and implementations
#[derive(Clone)]
struct FireAssultRifleCommand {
weapon: AssultRifle,
}
struct FireSniperCommand {
weapon: Sniper,
}
impl Command for FireAssultRifleCommand {
fn execute(&self) -> u8 {
self.weapon.clone().fire()
}
}
impl Command for FireSniperCommand {
fn execute(&self) -> u8 {
self.weapon.clone().fire()
}
}
fn main() {
let assult_rifle = AssultRifle { fire_rate: 90 };
let sniper = Sniper {
fire_rate: 30,
efficiency: 90,
};
let fire_assult_rifle_command = FireAssultRifleCommand {
weapon: assult_rifle,
};
let fire_sniper_command = FireSniperCommand { weapon: sniper };
let commands: Vec<Box<dyn Command>> = vec![
Box::new(fire_assult_rifle_command),
Box::new(fire_sniper_command),
];
let mut selected = 0;
let mut ammo = MAX_AMMO;
let stdout = Term::stdout();
loop {
stdout.clear_screen().unwrap();
let ammo_emoji = if selected == 0 { "🔫" } else { "🚀" };
println!(
r#"
===============================================================================
[ 1 ]: Rifle [ 2 ]: Sniper [ F ]: Fire [ R ]: Reload [ Q ]: Quit
===============================================================================
[{:03}] {}
-------------------------------------------------------------------------------
{}
"#,
ammo,
if ammo > 0 {
(ammo_emoji).repeat(ammo)
} else {
"!! NO AMMO !!".to_string()
},
if selected == 0 {
"[Rifle] ︻デ═一💥"
} else {
"[Sniper] 💥╾━╤デ╦︻"
},
);
if let Ok(character) = stdout.read_char() {
match character {
'1' => {
if selected != 0 {
selected = 0;
ammo = MAX_AMMO;
}
}
'2' => {
if selected != 1 {
selected = 1;
ammo = MAX_AMMO;
}
}
'f' => {
if ammo > 0 {
let command = commands.get(selected).clone().unwrap();
let fire_rate = command.execute();
if ammo > 0 {
ammo -= 1;
}
// it pauses the execution depending the firerate. 100 means no pause
// We are currently dealing with the rust std library without async, so the firing will not
// block the key press buffer, so if you repeatedly press f, it takes time to complete firing.
sleep(Duration::from_millis((100 - fire_rate as u64) * 10));
}
}
'r' => {
ammo = MAX_AMMO;
}
_ => {
break;
}
}
}
}
}