Przeglądaj źródła

correct sprite proportions

Bastien Sevajol 3 lat temu
rodzic
commit
b147893dcd
2 zmienionych plików z 45 dodań i 23 usunięć
  1. BIN
      resources/test.png
  2. 45 23
      src/main.rs

BIN
resources/test.png Wyświetl plik


+ 45 - 23
src/main.rs Wyświetl plik

@@ -10,30 +10,45 @@ use std::path;
10 10
 const TARGET_ANIMATION_FPS: u32 = 10;
11 11
 
12 12
 struct SpriteInfo {
13
-    start_y: f32,
14
-    tile_count: u16,
13
+    relative_start_y: f32,
15 14
     relative_tile_width: f32,
16 15
     relative_tile_height: f32,
16
+    tile_count: u16,
17 17
     current_frame: u16,
18 18
 }
19 19
 
20 20
 impl SpriteInfo {
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
23
-        let relative_tile_width: f32 = 1.0 / tile_count as f32;
21
+    pub fn new(
22
+        relative_start_y: f32,
23
+        relative_tile_width: f32,
24
+        relative_tile_height: f32,
25
+        tile_count: u16,
26
+    ) -> Self {
24 27
         Self {
25
-            start_y,
26
-            tile_count,
28
+            relative_start_y,
27 29
             relative_tile_width,
28
-            relative_tile_height: 1.0,
30
+            relative_tile_height,
31
+            tile_count,
29 32
             current_frame: 0,
30 33
         }
31 34
     }
32 35
 
33
-    pub fn from_type(type_: &SpriteType) -> Self {
34
-        match type_ {
35
-            SpriteType::WalkingSoldier => Self::new(0.0, 7),
36
-            SpriteType::JumpingSoldier => Self::new(24.0, 2),
36
+    pub fn from_type(
37
+        type_: &SpriteType,
38
+        sprite_sheet_width: f32,
39
+        sprite_sheet_height: f32,
40
+    ) -> Self {
41
+        let (start_y, tile_width, tile_height, tile_count) = match type_ {
42
+            SpriteType::WalkingSoldier => (0.0, 24.0, 24.0, 7),
43
+            SpriteType::JumpingSoldier => (24.0, 24.0, 24.0, 2),
44
+        };
45
+
46
+        Self {
47
+            relative_start_y: start_y / sprite_sheet_height,
48
+            relative_tile_width: tile_width / sprite_sheet_width,
49
+            relative_tile_height: tile_height / sprite_sheet_height,
50
+            tile_count,
51
+            current_frame: 0,
37 52
         }
38 53
     }
39 54
 }
@@ -43,12 +58,10 @@ enum SpriteType {
43 58
     JumpingSoldier,
44 59
 }
45 60
 
46
-fn sprite_batch_part_from_sprite_info(
47
-    sprite_info: &SpriteInfo,
48
-) -> graphics::DrawParam {
61
+fn sprite_batch_part_from_sprite_info(sprite_info: &SpriteInfo) -> graphics::DrawParam {
49 62
     let src = graphics::Rect::new(
50 63
         sprite_info.current_frame as f32 * sprite_info.relative_tile_width,
51
-        sprite_info.start_y,
64
+        sprite_info.relative_start_y,
52 65
         sprite_info.relative_tile_width,
53 66
         sprite_info.relative_tile_height,
54 67
     );
@@ -61,9 +74,18 @@ struct SceneItem {
61 74
 }
62 75
 
63 76
 impl SceneItem {
64
-    pub fn new(sprite_type: SpriteType, position: na::Point2<f32>) -> Self {
77
+    pub fn new(
78
+        sprite_type: SpriteType,
79
+        position: na::Point2<f32>,
80
+        sprite_sheet_width: f32,
81
+        sprite_sheet_height: f32,
82
+    ) -> Self {
65 83
         Self {
66
-            sprite_info: SpriteInfo::from_type(&sprite_type),
84
+            sprite_info: SpriteInfo::from_type(
85
+                &sprite_type,
86
+                sprite_sheet_width,
87
+                sprite_sheet_height,
88
+            ),
67 89
             position,
68 90
         }
69 91
     }
@@ -76,9 +98,7 @@ impl SceneItem {
76 98
     }
77 99
 }
78 100
 
79
-enum Message {
80
-
81
-}
101
+enum Message {}
82 102
 
83 103
 struct MainState {
84 104
     scene_items_sprite_batch: graphics::spritebatch::SpriteBatch,
@@ -93,15 +113,17 @@ impl MainState {
93 113
         let mut scene_items = vec![];
94 114
         for x in 0..1 {
95 115
             for y in 0..4 {
96
-                let type_ = if y % 2 == 0 {
116
+                let sprite_type = if y % 2 == 0 {
97 117
                     SpriteType::WalkingSoldier
98 118
                 } else {
99 119
                     SpriteType::JumpingSoldier
100 120
                 };
101 121
 
102 122
                 scene_items.push(SceneItem::new(
103
-                    SpriteType::WalkingSoldier,
123
+                    sprite_type,
104 124
                     na::Point2::new(x as f32 * 24.0, y as f32 * 24.0),
125
+                    168.0,
126
+                    48.0,
105 127
                 ));
106 128
             }
107 129
         }