Browse Source

use offset

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

BIN
resources/sprite_sheet.png View File


BIN
resources/sprite_sheet.xcf View File


+ 30 - 23
src/main.rs View File

1
 use ggez;
1
 use ggez;
2
 use ggez::event;
2
 use ggez::event;
3
 use ggez::graphics;
3
 use ggez::graphics;
4
+use ggez::graphics::{DrawMode, MeshBuilder};
4
 use ggez::nalgebra as na;
5
 use ggez::nalgebra as na;
5
 use ggez::timer::check_update_time;
6
 use ggez::timer::check_update_time;
6
 use ggez::{Context, GameResult};
7
 use ggez::{Context, GameResult};
33
     tile_count: u16,
34
     tile_count: u16,
34
     tile_width: f32,
35
     tile_width: f32,
35
     tile_height: f32,
36
     tile_height: f32,
36
-    half_tile_width: f32,
37
-    half_tile_height: f32,
38
 }
37
 }
39
 
38
 
40
 impl SpriteInfo {
39
 impl SpriteInfo {
53
             tile_count,
52
             tile_count,
54
             tile_width,
53
             tile_width,
55
             tile_height,
54
             tile_height,
56
-            half_tile_width: tile_width / 2.0,
57
-            half_tile_height: tile_height / 2.0,
58
         }
55
         }
59
     }
56
     }
60
-
61
-    pub fn as_draw_param(&self, current_frame: f32) -> graphics::DrawParam {
62
-        graphics::DrawParam::new().src(graphics::Rect::new(
63
-            current_frame as f32 * self.relative_tile_width,
64
-            self.relative_start_y,
65
-            self.relative_tile_width,
66
-            self.relative_tile_height,
67
-        ))
68
-    }
69
 }
57
 }
70
 
58
 
71
 enum SpriteType {
59
 enum SpriteType {
129
         }
117
         }
130
     }
118
     }
131
 
119
 
132
-    pub fn position_with_tile_decal(&self) -> na::Point2<f32> {
120
+    pub fn as_draw_param(&self, current_frame: f32) -> graphics::DrawParam {
133
         let sprite_info = self.sprite_info();
121
         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
-        )
122
+        graphics::DrawParam::new()
123
+            .src(graphics::Rect::new(
124
+                current_frame as f32 * sprite_info.relative_tile_width,
125
+                sprite_info.relative_start_y,
126
+                sprite_info.relative_tile_width,
127
+                sprite_info.relative_tile_height,
128
+            ))
129
+            .rotation(90.0f32.to_radians())
130
+            .offset(na::Point2::new(0.5, 0.5))
138
     }
131
     }
139
 }
132
 }
140
 
133
 
168
                 };
161
                 };
169
 
162
 
170
                 scene_items.push(SceneItem::new(
163
                 scene_items.push(SceneItem::new(
171
-                    na::Point2::new(x as f32 * 24.0, y as f32 * 24.0),
164
+                    na::Point2::new((x as f32 * 24.0) + 100.0, (y as f32 * 24.0) + 100.0),
172
                     ItemState::new(current_behavior),
165
                     ItemState::new(current_behavior),
173
                 ));
166
                 ));
174
             }
167
             }
255
 impl event::EventHandler for MainState {
248
 impl event::EventHandler for MainState {
256
     fn update(&mut self, ctx: &mut Context) -> GameResult {
249
     fn update(&mut self, ctx: &mut Context) -> GameResult {
257
         while check_update_time(ctx, TARGET_FPS) {
250
         while check_update_time(ctx, TARGET_FPS) {
258
-            // FIXME: gérer ici la maj des physics, animate, meta etc
259
-            // meta: calculer par ex qui voit qui (soldat voit un ennemi: ajouter l'event a vu
251
+            // TODO: meta: calculer par ex qui voit qui (soldat voit un ennemi: ajouter l'event a vu
260
             // ennemi, dans animate il se mettra a tirer)
252
             // ennemi, dans animate il se mettra a tirer)
261
             let tick_sprite = self.frame_i % SPRITE_EACH == 0;
253
             let tick_sprite = self.frame_i % SPRITE_EACH == 0;
262
             let tick_animate = self.frame_i % ANIMATE_EACH == 0;
254
             let tick_animate = self.frame_i % ANIMATE_EACH == 0;
299
     fn draw(&mut self, ctx: &mut Context) -> GameResult {
291
     fn draw(&mut self, ctx: &mut Context) -> GameResult {
300
         graphics::clear(ctx, graphics::BLACK);
292
         graphics::clear(ctx, graphics::BLACK);
301
 
293
 
294
+        let mut mesh_builder = MeshBuilder::new();
295
+
302
         for scene_item in self.scene_items.iter() {
296
         for scene_item in self.scene_items.iter() {
303
             self.scene_items_sprite_batch.add(
297
             self.scene_items_sprite_batch.add(
304
                 scene_item
298
                 scene_item
305
-                    .sprite_info()
306
                     .as_draw_param(scene_item.current_frame as f32)
299
                     .as_draw_param(scene_item.current_frame as f32)
307
-                    .dest(scene_item.position_with_tile_decal()),
300
+                    .dest(scene_item.position.clone()),
301
+            );
302
+            mesh_builder.circle(
303
+                DrawMode::fill(),
304
+                scene_item.position.clone(),
305
+                2.0,
306
+                2.0,
307
+                graphics::WHITE,
308
             );
308
             );
309
         }
309
         }
310
+
311
+        let mesh = mesh_builder.build(ctx)?;
310
         graphics::draw(
312
         graphics::draw(
311
             ctx,
313
             ctx,
312
             &self.scene_items_sprite_batch,
314
             &self.scene_items_sprite_batch,
313
             graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
315
             graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
314
         )?;
316
         )?;
317
+        graphics::draw(
318
+            ctx,
319
+            &mesh,
320
+            graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
321
+        )?;
315
 
322
 
316
         self.scene_items_sprite_batch.clear();
323
         self.scene_items_sprite_batch.clear();
317
         graphics::present(ctx)?;
324
         graphics::present(ctx)?;