Browse Source

frame position by item

Bastien Sevajol 3 years ago
parent
commit
6147d2ed19
1 changed files with 41 additions and 19 deletions
  1. 41 19
      src/main.rs

+ 41 - 19
src/main.rs View File

7
 use std::env;
7
 use std::env;
8
 use std::path;
8
 use std::path;
9
 
9
 
10
+const TARGET_ANIMATION_FPS: u32 = 10;
11
+
10
 struct SpriteInfo {
12
 struct SpriteInfo {
11
-    start: na::Point2<f32>,
13
+    start_y: f32,
12
     tile_count: u16,
14
     tile_count: u16,
13
     relative_tile_width: f32,
15
     relative_tile_width: f32,
14
     relative_tile_height: f32,
16
     relative_tile_height: f32,
17
+    current_frame: u16,
15
 }
18
 }
16
 
19
 
17
 impl SpriteInfo {
20
 impl SpriteInfo {
18
-    pub fn new(start: na::Point2<f32>, tile_count: u16) -> Self {
21
+    pub fn new(start_y: f32, tile_count: u16) -> Self {
22
+        // FIXME BS NOW: en fait ca ce base sur la hauteur complète de l'image
19
         let relative_tile_width: f32 = 1.0 / tile_count as f32;
23
         let relative_tile_width: f32 = 1.0 / tile_count as f32;
20
         Self {
24
         Self {
21
-            start,
25
+            start_y,
22
             tile_count,
26
             tile_count,
23
             relative_tile_width,
27
             relative_tile_width,
24
             relative_tile_height: 1.0,
28
             relative_tile_height: 1.0,
29
+            current_frame: 0,
25
         }
30
         }
26
     }
31
     }
27
 
32
 
28
     pub fn from_type(type_: &SpriteType) -> Self {
33
     pub fn from_type(type_: &SpriteType) -> Self {
29
         match type_ {
34
         match type_ {
30
-            SpriteType::WalkingSoldier => Self::new(na::Point2::new(0.0, 0.0), 7),
35
+            SpriteType::WalkingSoldier => Self::new(0.0, 7),
36
+            SpriteType::JumpingSoldier => Self::new(24.0, 2),
31
         }
37
         }
32
     }
38
     }
33
 }
39
 }
34
 
40
 
35
 enum SpriteType {
41
 enum SpriteType {
36
     WalkingSoldier,
42
     WalkingSoldier,
43
+    JumpingSoldier,
37
 }
44
 }
38
 
45
 
39
 fn sprite_batch_part_from_sprite_info(
46
 fn sprite_batch_part_from_sprite_info(
40
     sprite_info: &SpriteInfo,
47
     sprite_info: &SpriteInfo,
41
-    frame_i: u32,
42
 ) -> graphics::DrawParam {
48
 ) -> graphics::DrawParam {
43
     let src = graphics::Rect::new(
49
     let src = graphics::Rect::new(
44
-        frame_i as f32 * sprite_info.relative_tile_width,
45
-        0.0,
50
+        sprite_info.current_frame as f32 * sprite_info.relative_tile_width,
51
+        sprite_info.start_y,
46
         sprite_info.relative_tile_width,
52
         sprite_info.relative_tile_width,
47
         sprite_info.relative_tile_height,
53
         sprite_info.relative_tile_height,
48
     );
54
     );
61
             position,
67
             position,
62
         }
68
         }
63
     }
69
     }
70
+
71
+    pub fn tick_frame(&mut self) {
72
+        self.sprite_info.current_frame += 1;
73
+        if self.sprite_info.current_frame >= self.sprite_info.tile_count {
74
+            self.sprite_info.current_frame = 0;
75
+        }
76
+    }
77
+}
78
+
79
+enum Message {
80
+
64
 }
81
 }
65
 
82
 
66
 struct MainState {
83
 struct MainState {
67
     scene_items_sprite_batch: graphics::spritebatch::SpriteBatch,
84
     scene_items_sprite_batch: graphics::spritebatch::SpriteBatch,
68
     scene_items: Vec<SceneItem>,
85
     scene_items: Vec<SceneItem>,
69
-    i: u32,
70
 }
86
 }
71
 
87
 
72
 impl MainState {
88
 impl MainState {
75
         let batch = graphics::spritebatch::SpriteBatch::new(image);
91
         let batch = graphics::spritebatch::SpriteBatch::new(image);
76
 
92
 
77
         let mut scene_items = vec![];
93
         let mut scene_items = vec![];
78
-        for x in 0..10 {
79
-            for y in 0..10 {
94
+        for x in 0..1 {
95
+            for y in 0..4 {
96
+                let type_ = if y % 2 == 0 {
97
+                    SpriteType::WalkingSoldier
98
+                } else {
99
+                    SpriteType::JumpingSoldier
100
+                };
101
+
80
                 scene_items.push(SceneItem::new(
102
                 scene_items.push(SceneItem::new(
81
                     SpriteType::WalkingSoldier,
103
                     SpriteType::WalkingSoldier,
82
                     na::Point2::new(x as f32 * 24.0, y as f32 * 24.0),
104
                     na::Point2::new(x as f32 * 24.0, y as f32 * 24.0),
87
         let s = MainState {
109
         let s = MainState {
88
             scene_items_sprite_batch: batch,
110
             scene_items_sprite_batch: batch,
89
             scene_items,
111
             scene_items,
90
-            i: 0,
91
         };
112
         };
92
         Ok(s)
113
         Ok(s)
93
     }
114
     }
94
 }
115
 }
116
+
95
 impl event::EventHandler for MainState {
117
 impl event::EventHandler for MainState {
96
     fn update(&mut self, ctx: &mut Context) -> GameResult {
118
     fn update(&mut self, ctx: &mut Context) -> GameResult {
97
-        if check_update_time(ctx, 5) {
98
-            self.i += 1;
99
-        }
100
-        if self.i > 6 {
101
-            self.i = 0;
119
+        while check_update_time(ctx, TARGET_ANIMATION_FPS) {
120
+            for scene_item in self.scene_items.iter_mut() {
121
+                scene_item.tick_frame();
122
+            }
102
         }
123
         }
124
+
103
         Ok(())
125
         Ok(())
104
     }
126
     }
105
 
127
 
107
         graphics::clear(ctx, graphics::BLACK);
129
         graphics::clear(ctx, graphics::BLACK);
108
 
130
 
109
         for scene_item in self.scene_items.iter() {
131
         for scene_item in self.scene_items.iter() {
110
-            let sprite_batch_part = sprite_batch_part_from_sprite_info(&scene_item.sprite_info, self.i)
132
+            let sprite_batch_part = sprite_batch_part_from_sprite_info(&scene_item.sprite_info)
111
                 .dest(scene_item.position.clone());
133
                 .dest(scene_item.position.clone());
112
             self.scene_items_sprite_batch.add(sprite_batch_part);
134
             self.scene_items_sprite_batch.add(sprite_batch_part);
113
         }
135
         }
115
             ctx,
137
             ctx,
116
             &self.scene_items_sprite_batch,
138
             &self.scene_items_sprite_batch,
117
             graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
139
             graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
118
-        );
140
+        )?;
119
 
141
 
120
         self.scene_items_sprite_batch.clear();
142
         self.scene_items_sprite_batch.clear();
121
-        graphics::present(ctx);
143
+        graphics::present(ctx)?;
122
 
144
 
123
         println!("FPS: {}", ggez::timer::fps(ctx));
145
         println!("FPS: {}", ggez::timer::fps(ctx));
124
         Ok(())
146
         Ok(())