Browse Source

add display offset

Bastien Sevajol 3 years ago
parent
commit
112d05bb6c
1 changed files with 45 additions and 7 deletions
  1. 45 7
      src/main.rs

+ 45 - 7
src/main.rs View File

1
 use ggez;
1
 use ggez;
2
-use ggez::event;
2
+use ggez::event::KeyCode;
3
 use ggez::graphics;
3
 use ggez::graphics;
4
 use ggez::graphics::{DrawMode, MeshBuilder};
4
 use ggez::graphics::{DrawMode, MeshBuilder};
5
 use ggez::nalgebra as na;
5
 use ggez::nalgebra as na;
6
 use ggez::timer::check_update_time;
6
 use ggez::timer::check_update_time;
7
+use ggez::{event, input};
7
 use ggez::{Context, GameResult};
8
 use ggez::{Context, GameResult};
8
 use glam::Vec2;
9
 use glam::Vec2;
9
 use std::env;
10
 use std::env;
17
 const ANIMATE_EACH: u32 = 60; // execute animate code each 30 frames
18
 const ANIMATE_EACH: u32 = 60; // execute animate code each 30 frames
18
 const SPRITE_EACH: u32 = 10; // change sprite animation tile 30 frames
19
 const SPRITE_EACH: u32 = 10; // change sprite animation tile 30 frames
19
 const MAX_FRAME_I: u32 = 4294967295; // max of frame_i used to calculate ticks
20
 const MAX_FRAME_I: u32 = 4294967295; // max of frame_i used to calculate ticks
20
-
21
+const DISPLAY_OFFSET_BY: f32 = 3.0;
22
+const DISPLAY_OFFSET_BY_SPEED: f32 = 10.0;
21
 const SPRITE_SHEET_WIDTH: f32 = 800.0;
23
 const SPRITE_SHEET_WIDTH: f32 = 800.0;
22
 const SPRITE_SHEET_HEIGHT: f32 = 600.0;
24
 const SPRITE_SHEET_HEIGHT: f32 = 600.0;
23
 
25
 
145
     map_batch: graphics::spritebatch::SpriteBatch,
147
     map_batch: graphics::spritebatch::SpriteBatch,
146
     scene_items: Vec<SceneItem>,
148
     scene_items: Vec<SceneItem>,
147
     physics_events: Vec<PhysicEvent>,
149
     physics_events: Vec<PhysicEvent>,
150
+    display_offset: na::Point2<f32>,
148
 }
151
 }
149
 
152
 
150
 impl MainState {
153
 impl MainState {
176
             map_batch,
179
             map_batch,
177
             scene_items,
180
             scene_items,
178
             physics_events: vec![],
181
             physics_events: vec![],
182
+            display_offset: na::Point2::new(0.0, 0.0),
179
         };
183
         };
180
         Ok(s)
184
         Ok(s)
181
     }
185
     }
182
 
186
 
187
+    fn inputs(&mut self, ctx: &Context) {
188
+        let display_offset_by =
189
+            if input::keyboard::is_mod_active(ctx, input::keyboard::KeyMods::SHIFT) {
190
+                DISPLAY_OFFSET_BY_SPEED
191
+            } else {
192
+                DISPLAY_OFFSET_BY
193
+            };
194
+
195
+        if input::keyboard::is_key_pressed(ctx, KeyCode::Left) {
196
+            self.display_offset.x += display_offset_by;
197
+        }
198
+        if input::keyboard::is_key_pressed(ctx, KeyCode::Right) {
199
+            self.display_offset.x -= display_offset_by;
200
+        }
201
+        if input::keyboard::is_key_pressed(ctx, KeyCode::Up) {
202
+            self.display_offset.y += display_offset_by;
203
+        }
204
+        if input::keyboard::is_key_pressed(ctx, KeyCode::Down) {
205
+            self.display_offset.y -= display_offset_by;
206
+        }
207
+    }
208
+
183
     // TODO: manage errors
209
     // TODO: manage errors
184
     fn physics(&mut self) {
210
     fn physics(&mut self) {
185
         // Scene items movements
211
         // Scene items movements
247
             scene_item.tick_sprite();
273
             scene_item.tick_sprite();
248
         }
274
         }
249
     }
275
     }
276
+
277
+    fn position_with_display_offset(&self, position: &na::Point2<f32>) -> na::Point2<f32> {
278
+        na::Point2::new(
279
+            position.x + self.display_offset.x,
280
+            position.y + self.display_offset.y,
281
+        )
282
+    }
250
 }
283
 }
251
 
284
 
252
 impl event::EventHandler for MainState {
285
 impl event::EventHandler for MainState {
253
     fn update(&mut self, ctx: &mut Context) -> GameResult {
286
     fn update(&mut self, ctx: &mut Context) -> GameResult {
254
         while check_update_time(ctx, TARGET_FPS) {
287
         while check_update_time(ctx, TARGET_FPS) {
288
+            self.inputs(ctx);
289
+
255
             // TODO: meta: calculer par ex qui voit qui (soldat voit un ennemi: ajouter l'event a vu
290
             // TODO: meta: calculer par ex qui voit qui (soldat voit un ennemi: ajouter l'event a vu
256
             // ennemi, dans animate il se mettra a tirer)
291
             // ennemi, dans animate il se mettra a tirer)
257
             let tick_sprite = self.frame_i % SPRITE_EACH == 0;
292
             let tick_sprite = self.frame_i % SPRITE_EACH == 0;
313
         }
348
         }
314
         self.map_batch.add(
349
         self.map_batch.add(
315
             graphics::DrawParam::new()
350
             graphics::DrawParam::new()
316
-            .src(graphics::Rect::new(0.0, 0.0, 1.0, 1.0))
317
-            .dest(na::Point2::new(0.0, 0.0))
351
+                .src(graphics::Rect::new(0.0, 0.0, 1.0, 1.0))
352
+                .dest(na::Point2::new(0.0, 0.0)),
318
         );
353
         );
319
 
354
 
320
         let mesh = mesh_builder.build(ctx)?;
355
         let mesh = mesh_builder.build(ctx)?;
321
         graphics::draw(
356
         graphics::draw(
322
             ctx,
357
             ctx,
323
             &self.map_batch,
358
             &self.map_batch,
324
-            graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
359
+            graphics::DrawParam::new()
360
+                .dest(self.position_with_display_offset(&na::Point2::new(0.0, 0.0))),
325
         )?;
361
         )?;
326
         graphics::draw(
362
         graphics::draw(
327
             ctx,
363
             ctx,
328
             &self.sprite_sheet_batch,
364
             &self.sprite_sheet_batch,
329
-            graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
365
+            graphics::DrawParam::new()
366
+                .dest(self.position_with_display_offset(&na::Point2::new(0.0, 0.0))),
330
         )?;
367
         )?;
331
         graphics::draw(
368
         graphics::draw(
332
             ctx,
369
             ctx,
333
             &mesh,
370
             &mesh,
334
-            graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
371
+            graphics::DrawParam::new()
372
+                .dest(self.position_with_display_offset(&na::Point2::new(0.0, 0.0))),
335
         )?;
373
         )?;
336
 
374
 
337
         self.sprite_sheet_batch.clear();
375
         self.sprite_sheet_batch.clear();