Bläddra i källkod

frame position by item

Bastien Sevajol 3 år sedan
förälder
incheckning
6147d2ed19
1 ändrade filer med 41 tillägg och 19 borttagningar
  1. 41 19
      src/main.rs

+ 41 - 19
src/main.rs Visa fil

@@ -7,42 +7,48 @@ use ggez::{Context, GameResult};
7 7
 use std::env;
8 8
 use std::path;
9 9
 
10
+const TARGET_ANIMATION_FPS: u32 = 10;
11
+
10 12
 struct SpriteInfo {
11
-    start: na::Point2<f32>,
13
+    start_y: f32,
12 14
     tile_count: u16,
13 15
     relative_tile_width: f32,
14 16
     relative_tile_height: f32,
17
+    current_frame: u16,
15 18
 }
16 19
 
17 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 23
         let relative_tile_width: f32 = 1.0 / tile_count as f32;
20 24
         Self {
21
-            start,
25
+            start_y,
22 26
             tile_count,
23 27
             relative_tile_width,
24 28
             relative_tile_height: 1.0,
29
+            current_frame: 0,
25 30
         }
26 31
     }
27 32
 
28 33
     pub fn from_type(type_: &SpriteType) -> Self {
29 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 41
 enum SpriteType {
36 42
     WalkingSoldier,
43
+    JumpingSoldier,
37 44
 }
38 45
 
39 46
 fn sprite_batch_part_from_sprite_info(
40 47
     sprite_info: &SpriteInfo,
41
-    frame_i: u32,
42 48
 ) -> graphics::DrawParam {
43 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 52
         sprite_info.relative_tile_width,
47 53
         sprite_info.relative_tile_height,
48 54
     );
@@ -61,12 +67,22 @@ impl SceneItem {
61 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 83
 struct MainState {
67 84
     scene_items_sprite_batch: graphics::spritebatch::SpriteBatch,
68 85
     scene_items: Vec<SceneItem>,
69
-    i: u32,
70 86
 }
71 87
 
72 88
 impl MainState {
@@ -75,8 +91,14 @@ impl MainState {
75 91
         let batch = graphics::spritebatch::SpriteBatch::new(image);
76 92
 
77 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 102
                 scene_items.push(SceneItem::new(
81 103
                     SpriteType::WalkingSoldier,
82 104
                     na::Point2::new(x as f32 * 24.0, y as f32 * 24.0),
@@ -87,19 +109,19 @@ impl MainState {
87 109
         let s = MainState {
88 110
             scene_items_sprite_batch: batch,
89 111
             scene_items,
90
-            i: 0,
91 112
         };
92 113
         Ok(s)
93 114
     }
94 115
 }
116
+
95 117
 impl event::EventHandler for MainState {
96 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 125
         Ok(())
104 126
     }
105 127
 
@@ -107,7 +129,7 @@ impl event::EventHandler for MainState {
107 129
         graphics::clear(ctx, graphics::BLACK);
108 130
 
109 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 133
                 .dest(scene_item.position.clone());
112 134
             self.scene_items_sprite_batch.add(sprite_batch_part);
113 135
         }
@@ -115,10 +137,10 @@ impl event::EventHandler for MainState {
115 137
             ctx,
116 138
             &self.scene_items_sprite_batch,
117 139
             graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
118
-        );
140
+        )?;
119 141
 
120 142
         self.scene_items_sprite_batch.clear();
121
-        graphics::present(ctx);
143
+        graphics::present(ctx)?;
122 144
 
123 145
         println!("FPS: {}", ggez::timer::fps(ctx));
124 146
         Ok(())