Bladeren bron

use offset

Bastien Sevajol 3 jaren geleden
bovenliggende
commit
3c5ca3fc99
3 gewijzigde bestanden met toevoegingen van 30 en 23 verwijderingen
  1. BIN
      resources/sprite_sheet.png
  2. BIN
      resources/sprite_sheet.xcf
  3. 30 23
      src/main.rs

BIN
resources/sprite_sheet.png Bestand weergeven


BIN
resources/sprite_sheet.xcf Bestand weergeven


+ 30 - 23
src/main.rs Bestand weergeven

@@ -1,6 +1,7 @@
1 1
 use ggez;
2 2
 use ggez::event;
3 3
 use ggez::graphics;
4
+use ggez::graphics::{DrawMode, MeshBuilder};
4 5
 use ggez::nalgebra as na;
5 6
 use ggez::timer::check_update_time;
6 7
 use ggez::{Context, GameResult};
@@ -33,8 +34,6 @@ struct SpriteInfo {
33 34
     tile_count: u16,
34 35
     tile_width: f32,
35 36
     tile_height: f32,
36
-    half_tile_width: f32,
37
-    half_tile_height: f32,
38 37
 }
39 38
 
40 39
 impl SpriteInfo {
@@ -53,19 +52,8 @@ impl SpriteInfo {
53 52
             tile_count,
54 53
             tile_width,
55 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 59
 enum SpriteType {
@@ -129,12 +117,17 @@ impl SceneItem {
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 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,7 +161,7 @@ impl MainState {
168 161
                 };
169 162
 
170 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 165
                     ItemState::new(current_behavior),
173 166
                 ));
174 167
             }
@@ -255,8 +248,7 @@ impl MainState {
255 248
 impl event::EventHandler for MainState {
256 249
     fn update(&mut self, ctx: &mut Context) -> GameResult {
257 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 252
             // ennemi, dans animate il se mettra a tirer)
261 253
             let tick_sprite = self.frame_i % SPRITE_EACH == 0;
262 254
             let tick_animate = self.frame_i % ANIMATE_EACH == 0;
@@ -299,19 +291,34 @@ impl event::EventHandler for MainState {
299 291
     fn draw(&mut self, ctx: &mut Context) -> GameResult {
300 292
         graphics::clear(ctx, graphics::BLACK);
301 293
 
294
+        let mut mesh_builder = MeshBuilder::new();
295
+
302 296
         for scene_item in self.scene_items.iter() {
303 297
             self.scene_items_sprite_batch.add(
304 298
                 scene_item
305
-                    .sprite_info()
306 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 312
         graphics::draw(
311 313
             ctx,
312 314
             &self.scene_items_sprite_batch,
313 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 323
         self.scene_items_sprite_batch.clear();
317 324
         graphics::present(ctx)?;