Browse Source

use open combat tiles

Bastien Sevajol 3 years ago
parent
commit
aa3c410ea9
3 changed files with 37 additions and 35 deletions
  1. BIN
      resources/sprite_sheet.png
  2. BIN
      resources/sprite_sheet.xcf
  3. 37 35
      src/main.rs

BIN
resources/sprite_sheet.png View File


BIN
resources/sprite_sheet.xcf View File


+ 37 - 35
src/main.rs View File

17
 const SPRITE_EACH: u32 = 10; // change sprite animation tile 30 frames
17
 const SPRITE_EACH: u32 = 10; // change sprite animation tile 30 frames
18
 const MAX_FRAME_I: u32 = 4294967295; // max of frame_i used to calculate ticks
18
 const MAX_FRAME_I: u32 = 4294967295; // max of frame_i used to calculate ticks
19
 
19
 
20
-const SPRITE_SHEET_WIDTH: f32 = 168.0;
21
-const SPRITE_SHEET_HEIGHT: f32 = 72.0;
20
+const SPRITE_SHEET_WIDTH: f32 = 800.0;
21
+const SPRITE_SHEET_HEIGHT: f32 = 600.0;
22
 
22
 
23
 fn vec_from_angle(angle: f32) -> Vector2 {
23
 fn vec_from_angle(angle: f32) -> Vector2 {
24
     let vx = angle.sin();
24
     let vx = angle.sin();
31
     relative_tile_width: f32,
31
     relative_tile_width: f32,
32
     relative_tile_height: f32,
32
     relative_tile_height: f32,
33
     tile_count: u16,
33
     tile_count: u16,
34
+    tile_width: f32,
35
+    tile_height: f32,
36
+    half_tile_width: f32,
37
+    half_tile_height: f32,
34
 }
38
 }
35
 
39
 
36
 impl SpriteInfo {
40
 impl SpriteInfo {
37
-    pub fn new(
38
-        relative_start_y: f32,
39
-        relative_tile_width: f32,
40
-        relative_tile_height: f32,
41
-        tile_count: u16,
42
-    ) -> Self {
43
-        Self {
44
-            relative_start_y,
45
-            relative_tile_width,
46
-            relative_tile_height,
47
-            tile_count,
48
-        }
49
-    }
50
-
51
     // TODO: ask on rust community if this is performant, or how to make it static
41
     // TODO: ask on rust community if this is performant, or how to make it static
52
     pub fn from_type(type_: &SpriteType) -> Self {
42
     pub fn from_type(type_: &SpriteType) -> Self {
53
         let (start_y, tile_width, tile_height, tile_count) = match type_ {
43
         let (start_y, tile_width, tile_height, tile_count) = match type_ {
54
-            SpriteType::WalkingSoldier => (0.0, 24.0, 24.0, 7),
55
-            SpriteType::JumpingSoldier => (24.0, 24.0, 24.0, 2),
56
-            SpriteType::StandingSoldier => (48.0, 24.0, 24.0, 1),
44
+            SpriteType::WalkingSoldier => (12.0, 12.0, 12.0, 8),
45
+            SpriteType::CrawlingSoldier => (26.0, 26.0, 26.0, 8),
46
+            SpriteType::StandingSoldier => (0.0, 12.0, 12.0, 1),
57
         };
47
         };
58
 
48
 
59
         Self {
49
         Self {
61
             relative_tile_width: tile_width / SPRITE_SHEET_WIDTH,
51
             relative_tile_width: tile_width / SPRITE_SHEET_WIDTH,
62
             relative_tile_height: tile_height / SPRITE_SHEET_HEIGHT,
52
             relative_tile_height: tile_height / SPRITE_SHEET_HEIGHT,
63
             tile_count,
53
             tile_count,
54
+            tile_width,
55
+            tile_height,
56
+            half_tile_width: tile_width / 2.0,
57
+            half_tile_height: tile_height / 2.0,
64
         }
58
         }
65
     }
59
     }
66
 
60
 
76
 
70
 
77
 enum SpriteType {
71
 enum SpriteType {
78
     WalkingSoldier,
72
     WalkingSoldier,
79
-    JumpingSoldier,
73
+    CrawlingSoldier,
80
     StandingSoldier,
74
     StandingSoldier,
81
 }
75
 }
82
 
76
 
83
 enum ItemBehavior {
77
 enum ItemBehavior {
84
     Standing(u32), // since
78
     Standing(u32), // since
85
-    Jumping,
86
-    Running(Vector2),
79
+    Crawling,
80
+    Walking(Vector2),
87
 }
81
 }
88
 
82
 
89
 struct ItemState {
83
 struct ItemState {
98
     pub fn sprite_type(&self) -> SpriteType {
92
     pub fn sprite_type(&self) -> SpriteType {
99
         // Here some logical about state and current behavior to determine sprite type
93
         // Here some logical about state and current behavior to determine sprite type
100
         match self.current_behavior {
94
         match self.current_behavior {
101
-            ItemBehavior::Jumping => SpriteType::JumpingSoldier,
102
-            ItemBehavior::Running(_) => SpriteType::WalkingSoldier,
95
+            ItemBehavior::Crawling => SpriteType::CrawlingSoldier,
96
+            ItemBehavior::Walking(_) => SpriteType::WalkingSoldier,
103
             ItemBehavior::Standing(_) => SpriteType::StandingSoldier,
97
             ItemBehavior::Standing(_) => SpriteType::StandingSoldier,
104
         }
98
         }
105
     }
99
     }
134
             self.current_frame = 0;
128
             self.current_frame = 0;
135
         }
129
         }
136
     }
130
     }
131
+
132
+    pub fn position_with_tile_decal(&self) -> na::Point2<f32> {
133
+        let sprite_info = self.sprite_info();
134
+        na::Point2::new(
135
+            self.position.x - sprite_info.half_tile_width,
136
+            self.position.y - sprite_info.half_tile_height,
137
+        )
138
+    }
137
 }
139
 }
138
 
140
 
139
 enum PhysicEvent {
141
 enum PhysicEvent {
153
 
155
 
154
 impl MainState {
156
 impl MainState {
155
     fn new(ctx: &mut Context) -> GameResult<MainState> {
157
     fn new(ctx: &mut Context) -> GameResult<MainState> {
156
-        let image = graphics::Image::new(ctx, "/test.png").unwrap();
158
+        let image = graphics::Image::new(ctx, "/sprite_sheet.png").unwrap();
157
         let batch = graphics::spritebatch::SpriteBatch::new(image);
159
         let batch = graphics::spritebatch::SpriteBatch::new(image);
158
 
160
 
159
         let mut scene_items = vec![];
161
         let mut scene_items = vec![];
160
         for x in 0..1 {
162
         for x in 0..1 {
161
             for y in 0..4 {
163
             for y in 0..4 {
162
                 let current_behavior = if y % 2 == 0 {
164
                 let current_behavior = if y % 2 == 0 {
163
-                    ItemBehavior::Running(vec_from_angle(90.0))
165
+                    ItemBehavior::Walking(vec_from_angle(90.0))
164
                 } else {
166
                 } else {
165
-                    ItemBehavior::Jumping
167
+                    ItemBehavior::Crawling
166
                 };
168
                 };
167
 
169
 
168
                 scene_items.push(SceneItem::new(
170
                 scene_items.push(SceneItem::new(
186
         // Scene items movements
188
         // Scene items movements
187
         for scene_item in self.scene_items.iter_mut() {
189
         for scene_item in self.scene_items.iter_mut() {
188
             match scene_item.state.current_behavior {
190
             match scene_item.state.current_behavior {
189
-                ItemBehavior::Running(vector) => {
191
+                ItemBehavior::Walking(vector) => {
190
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
192
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
191
                     scene_item.position.x += 1.0;
193
                     scene_item.position.x += 1.0;
192
                 }
194
                 }
195
         }
197
         }
196
 
198
 
197
         // (FAKE) Drop a bomb to motivate stop move
199
         // (FAKE) Drop a bomb to motivate stop move
198
-        if self.frame_i % 300 == 0 && self.frame_i != 0 {
200
+        if self.frame_i % 600 == 0 && self.frame_i != 0 {
199
             self.physics_events.push(PhysicEvent::Explosion);
201
             self.physics_events.push(PhysicEvent::Explosion);
200
         }
202
         }
201
     }
203
     }
225
             }
227
             }
226
 
228
 
227
             match scene_item.state.current_behavior {
229
             match scene_item.state.current_behavior {
228
-                ItemBehavior::Jumping => {
229
-                    scene_item.state = ItemState::new(ItemBehavior::Running(vec_from_angle(90.0)));
230
+                ItemBehavior::Crawling => {
231
+                    scene_item.state = ItemState::new(ItemBehavior::Walking(vec_from_angle(90.0)));
230
                 }
232
                 }
231
-                ItemBehavior::Running(_) => {
232
-                    scene_item.state = ItemState::new(ItemBehavior::Jumping);
233
+                ItemBehavior::Walking(_) => {
234
+                    scene_item.state = ItemState::new(ItemBehavior::Crawling);
233
                 }
235
                 }
234
                 ItemBehavior::Standing(since) => {
236
                 ItemBehavior::Standing(since) => {
235
                     if self.frame_i - since >= 120 {
237
                     if self.frame_i - since >= 120 {
236
                         scene_item.state =
238
                         scene_item.state =
237
-                            ItemState::new(ItemBehavior::Running(vec_from_angle(90.0)));
239
+                            ItemState::new(ItemBehavior::Walking(vec_from_angle(90.0)));
238
                     }
240
                     }
239
                 }
241
                 }
240
             }
242
             }
302
                 scene_item
304
                 scene_item
303
                     .sprite_info()
305
                     .sprite_info()
304
                     .as_draw_param(scene_item.current_frame as f32)
306
                     .as_draw_param(scene_item.current_frame as f32)
305
-                    .dest(scene_item.position.clone()),
307
+                    .dest(scene_item.position_with_tile_decal()),
306
             );
308
             );
307
         }
309
         }
308
         graphics::draw(
310
         graphics::draw(