File tree 2 files changed +68
-19
lines changed
2 files changed +68
-19
lines changed Original file line number Diff line number Diff line change
1
+ #[ allow( dead_code) ]
2
+ pub trait Actor {
3
+ fn draw ( & self ) ;
4
+ fn update ( & mut self ) ;
5
+ }
6
+
7
+ #[ derive( Default ) ]
8
+ pub struct Game {
9
+ // width: f32,
10
+ // height: f32,
11
+ size : Size ,
12
+ actors : Vec < Box < dyn Actor > > ,
13
+ }
14
+
15
+ impl Game {
16
+ // pub fn new() -> Self {
17
+ // Game {
18
+ // actors: Vec::new(),
19
+ // size:Size::default()
20
+ // }
21
+ // }
22
+ pub fn add_actor ( & mut self , actor : Box < dyn Actor > ) {
23
+ self . actors . push ( actor) ;
24
+ }
25
+ pub fn draw ( & self ) {
26
+ for actor in & self . actors {
27
+ actor. draw ( ) ;
28
+ }
29
+ }
30
+ pub fn update ( & mut self ) {
31
+ for actor in & mut self . actors {
32
+ actor. update ( ) ;
33
+ }
34
+ }
35
+ }
36
+
37
+ // #[derive(Default)]
38
+ #[ allow( dead_code) ]
39
+ pub struct Size {
40
+ width : f32 ,
41
+ height : f32 ,
42
+ }
43
+
44
+ impl Default for Size {
45
+ fn default ( ) -> Self {
46
+ Size {
47
+ width : 1280_f32 ,
48
+ height : 960_f32 ,
49
+ }
50
+ }
51
+ }
Original file line number Diff line number Diff line change
1
+ use std:: { thread:: sleep, time:: Duration } ;
2
+
3
+ use framework:: * ;
4
+
5
+ mod framework;
6
+
1
7
fn main ( ) {
2
- let mut game = Game :: new ( ) ;
8
+ // let mut game = Game::new();
9
+ let mut game = Game :: default ( ) ;
3
10
let super_mario = Player :: new ( 1 , "Super Mario" . to_string ( ) ) ;
4
11
let mushroom = Mushroom :: new ( 2 , 10 ) ;
5
12
game. add_actor ( Box :: new ( super_mario) ) ;
6
13
game. add_actor ( Box :: new ( mushroom) ) ;
14
+
15
+ loop {
16
+ sleep ( Duration :: from_secs ( 2 ) ) ;
17
+ game. draw ( ) ;
18
+ game. update ( ) ;
19
+ // state check
20
+ }
7
21
}
8
22
23
+ #[ allow( dead_code) ]
9
24
struct Player {
10
25
id : u32 ,
11
26
name : String ,
@@ -25,6 +40,7 @@ impl Actor for Player {
25
40
}
26
41
}
27
42
43
+ #[ allow( dead_code) ]
28
44
struct Mushroom {
29
45
id : u32 ,
30
46
strength : u8 ,
@@ -44,21 +60,3 @@ impl Actor for Mushroom {
44
60
println ! ( "Calculation strength" ) ;
45
61
}
46
62
}
47
-
48
- trait Actor {
49
- fn draw ( & self ) ;
50
- fn update ( & mut self ) ;
51
- }
52
-
53
- struct Game {
54
- actors : Vec < Box < dyn Actor > > ,
55
- }
56
-
57
- impl Game {
58
- pub fn new ( ) -> Self {
59
- Game { actors : Vec :: new ( ) }
60
- }
61
- pub fn add_actor ( & mut self , actor : Box < dyn Actor > ) {
62
- self . actors . push ( actor) ;
63
- }
64
- }
You can’t perform that action at this time.
0 commit comments