File tree 2 files changed +31
-11
lines changed
2 files changed +31
-11
lines changed Original file line number Diff line number Diff line change
1
+ use std:: sync:: mpsc:: Sender ;
2
+
1
3
pub struct BotContainer {
2
4
is_active : bool ,
3
5
bot : Box < dyn Bot > ,
@@ -20,23 +22,26 @@ pub trait Actor {
20
22
fn update ( & mut self ) ;
21
23
}
22
24
23
- #[ derive( Default ) ]
25
+ // #[derive(Default)]
24
26
#[ allow( dead_code) ]
25
27
pub struct Game {
26
28
// width: f32,
27
29
// height: f32,
28
30
size : Size ,
29
31
actors : Vec < Box < dyn Actor > > ,
30
32
bots_container : Vec < BotContainer > ,
33
+ sender : Sender < String > ,
31
34
}
32
35
33
36
impl Game {
34
- // pub fn new() -> Self {
35
- // Game {
36
- // actors: Vec::new(),
37
- // size:Size::default()
38
- // }
39
- // }
37
+ pub fn new ( sender : Sender < String > ) -> Self {
38
+ Game {
39
+ actors : Vec :: new ( ) ,
40
+ size : Size :: default ( ) ,
41
+ bots_container : Vec :: new ( ) ,
42
+ sender,
43
+ }
44
+ }
40
45
pub fn add_actor ( & mut self , actor : Box < dyn Actor > ) {
41
46
self . actors . push ( actor) ;
42
47
}
@@ -55,6 +60,9 @@ impl Game {
55
60
}
56
61
pub fn apply ( & self ) {
57
62
for container in & self . bots_container {
63
+ self . sender
64
+ . send ( "A simple message..." . to_string ( ) )
65
+ . expect ( "Error in sending message into channel" ) ;
58
66
if container. is_active {
59
67
container. bot . apply ( ) ;
60
68
}
Original file line number Diff line number Diff line change 1
1
use std:: {
2
+ sync:: mpsc:: channel,
2
3
thread:: { self , sleep} ,
3
4
time:: Duration ,
4
5
} ;
@@ -8,19 +9,30 @@ use framework::*;
8
9
mod framework;
9
10
10
11
fn main ( ) {
11
- // let mut game = Game::new();
12
- let mut game = Game :: default ( ) ;
12
+ let ( transmitter, receiver) = channel :: < String > ( ) ;
13
+
14
+ let mut game = Game :: new ( transmitter. clone ( ) ) ;
15
+ // let mut game = Game::default();
13
16
let super_mario = Player :: new ( 1 , "Super Mario" . to_string ( ) ) ;
14
17
let mushroom = Mushroom :: new ( 2 , 10 ) ;
15
18
game. add_actor ( Box :: new ( super_mario) ) ;
16
19
game. add_actor ( Box :: new ( mushroom) ) ;
17
20
18
21
let mega_mind = MindController :: default ( ) ;
19
22
game. add_bot ( Box :: new ( mega_mind) ) ;
20
-
21
23
game. add_bot ( Box :: new ( Confuser :: default ( ) ) ) ;
22
24
25
+ // GameEngine::run(&game);
26
+
23
27
game. apply ( ) ;
28
+ game. draw ( ) ;
29
+ game. update ( ) ;
30
+
31
+ // drop(transmitter);
32
+ // Not: Sürekli dinlemede kalınacağı için loop döngüsü hiçbir zaman başlamaz.
33
+ for r in receiver {
34
+ println ! ( "{}" , r) ;
35
+ }
24
36
25
37
loop {
26
38
sleep ( Duration :: from_secs ( 2 ) ) ;
@@ -35,7 +47,7 @@ struct MindController {}
35
47
36
48
impl Bot for MindController {
37
49
fn apply ( & self ) {
38
- thread:: spawn ( || {
50
+ thread:: spawn ( move || {
39
51
loop {
40
52
println ! ( "\t Applying simulation...{:?}" , thread:: current( ) . id( ) ) ;
41
53
sleep ( Duration :: from_secs ( 5 ) ) ;
You can’t perform that action at this time.
0 commit comments