-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.rs
160 lines (143 loc) · 4.17 KB
/
command.rs
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
/**
* -----------------------------------------------------------------------------
* COMMAND PATTERN
*
* To execute, please run: cargo run --bin command
* To run tests, please run: 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:
*
* 1. Command : Trait that defines the common method
* 2. Concrete Command : Each struct that implements Command trait
* 3. Invoker : function that invokes the concrete command
* 4. Receiver : Function that performs actual work
* 5: Client : Part of a code that uses the above mechanism
*
* 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;
// 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 {
self.fire_rate
}
}
impl WeaponTrait for Sniper {
fn fire(&mut self) -> u8 {
(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: 20 };
let sniper = Sniper {
fire_rate: 10,
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 = 50;
let stdout = Term::stdout();
loop {
stdout.clear_screen().unwrap();
println!(
r#"
===============================================================================
[ 1 ]: Rifle [ 2 ]: Sniper [ F ]: Fire [ R ]: Reload [ Q ]: Quit
===============================================================================
[ {} ]: {}
"#,
if selected == 0 {
"︻デ═一"
} else {
"╾━╤デ╦︻"
},
if ammo > 0 {
"☢ ".repeat(ammo)
} else {
"!! NO AMMO !!".to_string()
}
);
if let Ok(character) = stdout.read_char() {
match character {
'1' => {
if selected != 0 {
selected = 0;
ammo = 50;
}
}
'2' => {
if selected != 1 {
selected = 1;
ammo = 50;
}
}
'f' => {
if ammo > 0 {
let command = commands.get(selected).clone().unwrap();
let bullets_fired = command.execute();
let a = (bullets_fired as f32 / 5f32).max(0.0) as usize;
if a < ammo {
ammo -= a;
} else {
ammo = 0;
}
}
}
'r' => {
ammo = 50;
}
_ => {
break;
}
}
}
}
}