Pārlūkot izejas kodu

add display offset

Bastien Sevajol 3 gadus atpakaļ
vecāks
revīzija
112d05bb6c
1 mainītis faili ar 45 papildinājumiem un 7 dzēšanām
  1. 45 7
      src/main.rs

+ 45 - 7
src/main.rs Parādīt failu

@@ -1,9 +1,10 @@
1 1
 use ggez;
2
-use ggez::event;
2
+use ggez::event::KeyCode;
3 3
 use ggez::graphics;
4 4
 use ggez::graphics::{DrawMode, MeshBuilder};
5 5
 use ggez::nalgebra as na;
6 6
 use ggez::timer::check_update_time;
7
+use ggez::{event, input};
7 8
 use ggez::{Context, GameResult};
8 9
 use glam::Vec2;
9 10
 use std::env;
@@ -17,7 +18,8 @@ const PHYSICS_EACH: u32 = 10; // execute physics code each 10 frames
17 18
 const ANIMATE_EACH: u32 = 60; // execute animate code each 30 frames
18 19
 const SPRITE_EACH: u32 = 10; // change sprite animation tile 30 frames
19 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 23
 const SPRITE_SHEET_WIDTH: f32 = 800.0;
22 24
 const SPRITE_SHEET_HEIGHT: f32 = 600.0;
23 25
 
@@ -145,6 +147,7 @@ struct MainState {
145 147
     map_batch: graphics::spritebatch::SpriteBatch,
146 148
     scene_items: Vec<SceneItem>,
147 149
     physics_events: Vec<PhysicEvent>,
150
+    display_offset: na::Point2<f32>,
148 151
 }
149 152
 
150 153
 impl MainState {
@@ -176,10 +179,33 @@ impl MainState {
176 179
             map_batch,
177 180
             scene_items,
178 181
             physics_events: vec![],
182
+            display_offset: na::Point2::new(0.0, 0.0),
179 183
         };
180 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 209
     // TODO: manage errors
184 210
     fn physics(&mut self) {
185 211
         // Scene items movements
@@ -247,11 +273,20 @@ impl MainState {
247 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 285
 impl event::EventHandler for MainState {
253 286
     fn update(&mut self, ctx: &mut Context) -> GameResult {
254 287
         while check_update_time(ctx, TARGET_FPS) {
288
+            self.inputs(ctx);
289
+
255 290
             // TODO: meta: calculer par ex qui voit qui (soldat voit un ennemi: ajouter l'event a vu
256 291
             // ennemi, dans animate il se mettra a tirer)
257 292
             let tick_sprite = self.frame_i % SPRITE_EACH == 0;
@@ -313,25 +348,28 @@ impl event::EventHandler for MainState {
313 348
         }
314 349
         self.map_batch.add(
315 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 355
         let mesh = mesh_builder.build(ctx)?;
321 356
         graphics::draw(
322 357
             ctx,
323 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 362
         graphics::draw(
327 363
             ctx,
328 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 368
         graphics::draw(
332 369
             ctx,
333 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 375
         self.sprite_sheet_batch.clear();