Przeglądaj źródła

sprite_info in object

Bastien Sevajol 3 lat temu
rodzic
commit
49dfb7998a
1 zmienionych plików z 8 dodań i 9 usunięć
  1. 8 9
      src/main.rs

+ 8 - 9
src/main.rs Wyświetl plik

@@ -8,17 +8,17 @@ use std::env;
8 8
 use std::path;
9 9
 
10 10
 struct SpriteInfo {
11
-    source: graphics::Rect,
11
+    start: na::Point2<f32>,
12 12
     tile_count: u16,
13 13
     relative_tile_width: f32,
14 14
     relative_tile_height: f32,
15 15
 }
16 16
 
17 17
 impl SpriteInfo {
18
-    pub fn new(source: graphics::Rect, tile_count: u16) -> Self {
18
+    pub fn new(start: na::Point2<f32>, tile_count: u16) -> Self {
19 19
         let relative_tile_width: f32 = 1.0 / tile_count as f32;
20 20
         Self {
21
-            source,
21
+            start,
22 22
             tile_count,
23 23
             relative_tile_width,
24 24
             relative_tile_height: 1.0,
@@ -27,7 +27,7 @@ impl SpriteInfo {
27 27
 
28 28
     pub fn from_type(type_: &SpriteType) -> Self {
29 29
         match type_ {
30
-            SpriteType::WalkingSoldier => Self::new(graphics::Rect::new(0.0, 0.0, 128.0, 24.0), 7),
30
+            SpriteType::WalkingSoldier => Self::new(na::Point2::new(0.0, 0.0), 7),
31 31
         }
32 32
     }
33 33
 }
@@ -50,14 +50,14 @@ fn sprite_batch_part_from_sprite_info(
50 50
 }
51 51
 
52 52
 struct SceneItem {
53
-    current_sprite_type: SpriteType,
53
+    sprite_info: SpriteInfo,
54 54
     position: na::Point2<f32>,
55 55
 }
56 56
 
57 57
 impl SceneItem {
58
-    pub fn new(current_sprite_type: SpriteType, position: na::Point2<f32>) -> Self {
58
+    pub fn new(sprite_type: SpriteType, position: na::Point2<f32>) -> Self {
59 59
         Self {
60
-            current_sprite_type,
60
+            sprite_info: SpriteInfo::from_type(&sprite_type),
61 61
             position,
62 62
         }
63 63
     }
@@ -107,8 +107,7 @@ impl event::EventHandler for MainState {
107 107
         graphics::clear(ctx, graphics::BLACK);
108 108
 
109 109
         for scene_item in self.scene_items.iter() {
110
-            let sprite_info = SpriteInfo::from_type(&scene_item.current_sprite_type);
111
-            let sprite_batch_part = sprite_batch_part_from_sprite_info(&sprite_info, self.i)
110
+            let sprite_batch_part = sprite_batch_part_from_sprite_info(&scene_item.sprite_info, self.i)
112 111
                 .dest(scene_item.position.clone());
113 112
             self.scene_items_sprite_batch.add(sprite_batch_part);
114 113
         }