Ver código fonte

use open combat tiles

Bastien Sevajol 3 anos atrás
pai
commit
aa3c410ea9
3 arquivos alterados com 37 adições e 35 exclusões
  1. BIN
      resources/sprite_sheet.png
  2. BIN
      resources/sprite_sheet.xcf
  3. 37 35
      src/main.rs

BIN
resources/sprite_sheet.png Ver arquivo


BIN
resources/sprite_sheet.xcf Ver arquivo


+ 37 - 35
src/main.rs Ver arquivo

@@ -17,8 +17,8 @@ const ANIMATE_EACH: u32 = 60; // execute animate code each 30 frames
17 17
 const SPRITE_EACH: u32 = 10; // change sprite animation tile 30 frames
18 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 23
 fn vec_from_angle(angle: f32) -> Vector2 {
24 24
     let vx = angle.sin();
@@ -31,29 +31,19 @@ struct SpriteInfo {
31 31
     relative_tile_width: f32,
32 32
     relative_tile_height: f32,
33 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 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 41
     // TODO: ask on rust community if this is performant, or how to make it static
52 42
     pub fn from_type(type_: &SpriteType) -> Self {
53 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 49
         Self {
@@ -61,6 +51,10 @@ impl SpriteInfo {
61 51
             relative_tile_width: tile_width / SPRITE_SHEET_WIDTH,
62 52
             relative_tile_height: tile_height / SPRITE_SHEET_HEIGHT,
63 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,14 +70,14 @@ impl SpriteInfo {
76 70
 
77 71
 enum SpriteType {
78 72
     WalkingSoldier,
79
-    JumpingSoldier,
73
+    CrawlingSoldier,
80 74
     StandingSoldier,
81 75
 }
82 76
 
83 77
 enum ItemBehavior {
84 78
     Standing(u32), // since
85
-    Jumping,
86
-    Running(Vector2),
79
+    Crawling,
80
+    Walking(Vector2),
87 81
 }
88 82
 
89 83
 struct ItemState {
@@ -98,8 +92,8 @@ impl ItemState {
98 92
     pub fn sprite_type(&self) -> SpriteType {
99 93
         // Here some logical about state and current behavior to determine sprite type
100 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 97
             ItemBehavior::Standing(_) => SpriteType::StandingSoldier,
104 98
         }
105 99
     }
@@ -134,6 +128,14 @@ impl SceneItem {
134 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 141
 enum PhysicEvent {
@@ -153,16 +155,16 @@ struct MainState {
153 155
 
154 156
 impl MainState {
155 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 159
         let batch = graphics::spritebatch::SpriteBatch::new(image);
158 160
 
159 161
         let mut scene_items = vec![];
160 162
         for x in 0..1 {
161 163
             for y in 0..4 {
162 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 166
                 } else {
165
-                    ItemBehavior::Jumping
167
+                    ItemBehavior::Crawling
166 168
                 };
167 169
 
168 170
                 scene_items.push(SceneItem::new(
@@ -186,7 +188,7 @@ impl MainState {
186 188
         // Scene items movements
187 189
         for scene_item in self.scene_items.iter_mut() {
188 190
             match scene_item.state.current_behavior {
189
-                ItemBehavior::Running(vector) => {
191
+                ItemBehavior::Walking(vector) => {
190 192
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
191 193
                     scene_item.position.x += 1.0;
192 194
                 }
@@ -195,7 +197,7 @@ impl MainState {
195 197
         }
196 198
 
197 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 201
             self.physics_events.push(PhysicEvent::Explosion);
200 202
         }
201 203
     }
@@ -225,16 +227,16 @@ impl MainState {
225 227
             }
226 228
 
227 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 236
                 ItemBehavior::Standing(since) => {
235 237
                     if self.frame_i - since >= 120 {
236 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,7 +304,7 @@ impl event::EventHandler for MainState {
302 304
                 scene_item
303 305
                     .sprite_info()
304 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 310
         graphics::draw(