File tree 2 files changed +42
-7
lines changed
2 files changed +42
-7
lines changed Original file line number Diff line number Diff line change
1
+ pub struct BotContainer {
2
+ is_active : bool ,
3
+ bot : Box < dyn Bot > ,
4
+ }
5
+ impl BotContainer {
6
+ pub fn new ( is_active : bool , bot : Box < dyn Bot > ) -> Self {
7
+ BotContainer { is_active, bot }
8
+ }
9
+ }
10
+
1
11
pub trait Bot {
2
12
fn apply ( & self ) {
3
13
println ! ( "Default AI movements" ) ;
@@ -17,7 +27,7 @@ pub struct Game {
17
27
// height: f32,
18
28
size : Size ,
19
29
actors : Vec < Box < dyn Actor > > ,
20
- bots : Vec < Box < dyn Bot > > ,
30
+ bots_container : Vec < BotContainer > ,
21
31
}
22
32
23
33
impl Game {
@@ -36,14 +46,18 @@ impl Game {
36
46
}
37
47
}
38
48
pub fn add_bot ( & mut self , bot : Box < dyn Bot > ) {
39
- self . bots . push ( bot) ;
49
+ self . bots_container . push ( BotContainer :: new ( true , bot) ) ;
40
50
}
41
51
pub fn update ( & mut self ) {
42
52
for actor in & mut self . actors {
43
53
actor. update ( ) ;
44
54
}
45
- for bot in & self . bots {
46
- bot. apply ( ) ;
55
+ }
56
+ pub fn apply ( & self ) {
57
+ for container in & self . bots_container {
58
+ if container. is_active {
59
+ container. bot . apply ( ) ;
60
+ }
47
61
}
48
62
}
49
63
}
Original file line number Diff line number Diff line change @@ -14,9 +14,14 @@ fn main() {
14
14
let mushroom = Mushroom :: new ( 2 , 10 ) ;
15
15
game. add_actor ( Box :: new ( super_mario) ) ;
16
16
game. add_actor ( Box :: new ( mushroom) ) ;
17
+
17
18
let mega_mind = MindController :: default ( ) ;
18
19
game. add_bot ( Box :: new ( mega_mind) ) ;
19
20
21
+ game. add_bot ( Box :: new ( Confuser :: default ( ) ) ) ;
22
+
23
+ game. apply ( ) ;
24
+
20
25
loop {
21
26
sleep ( Duration :: from_secs ( 2 ) ) ;
22
27
game. draw ( ) ;
@@ -26,20 +31,36 @@ fn main() {
26
31
}
27
32
28
33
#[ derive( Default ) ]
29
- pub struct MindController { }
34
+ struct MindController { }
30
35
31
36
impl Bot for MindController {
32
37
fn apply ( & self ) {
33
- //todo@buraksenyurt Buradaki thread'in bir kere açılmasını garanti et
34
38
thread:: spawn ( || {
35
39
loop {
36
- println ! ( "Applying simulation..." ) ;
40
+ println ! ( "\t Applying simulation...{:?}" , thread :: current ( ) . id ( ) ) ;
37
41
sleep ( Duration :: from_secs ( 5 ) ) ;
38
42
}
39
43
} ) ;
40
44
}
41
45
}
42
46
47
+ #[ derive( Default ) ]
48
+ struct Confuser { }
49
+
50
+ impl Bot for Confuser {
51
+ fn apply ( & self ) {
52
+ thread:: spawn ( || {
53
+ loop {
54
+ println ! (
55
+ "\t \t Brain confusing in progress...{:?}" ,
56
+ thread:: current( ) . id( )
57
+ ) ;
58
+ sleep ( Duration :: from_secs ( 1 ) ) ;
59
+ }
60
+ } ) ;
61
+ }
62
+ }
63
+
43
64
#[ allow( dead_code) ]
44
65
struct Player {
45
66
id : u32 ,
You can’t perform that action at this time.
0 commit comments