Przeglądaj źródła

reorganize code

Bastien Sevajol 3 lat temu
rodzic
commit
af2bf149b0
1 zmienionych plików z 119 dodań i 131 usunięć
  1. 119 131
      src/scene/main.rs

+ 119 - 131
src/scene/main.rs Wyświetl plik

@@ -317,31 +317,78 @@ impl MainState {
317 317
         selection
318 318
     }
319 319
 
320
-    fn build_scene_items_draw_params(&self) -> Vec<graphics::DrawParam> {
321
-        let mut draw_params = vec![];
322
-
320
+    fn generate_scene_item_sprites(&mut self) -> GameResult {
323 321
         for scene_item in self.scene_items.iter() {
324
-            draw_params.push(
322
+            self.sprite_sheet_batch.add(
325 323
                 scene_item
326 324
                     .as_draw_param(scene_item.current_frame as f32)
327 325
                     .dest(scene_item.position.clone()),
328
-            )
326
+            );
329 327
         }
330 328
 
331
-        draw_params
329
+        Ok(())
330
+    }
331
+
332
+    fn generate_scene_item_menu_sprites(&mut self) -> GameResult {
333
+        if let Some((_, scene_point)) = self.scene_item_menu {
334
+            self.ui_batch.add(
335
+                UiSpriteInfo::from_type(UiItem::SceneItemMenu)
336
+                    .as_draw_param()
337
+                    .dest(scene_point),
338
+            );
339
+        }
340
+
341
+        Ok(())
342
+    }
343
+
344
+    fn generate_map_sprites(&mut self) -> GameResult {
345
+        self.map_batch.add(
346
+            graphics::DrawParam::new()
347
+                .src(graphics::Rect::new(0.0, 0.0, 1.0, 1.0))
348
+                .dest(ScenePoint::new(0.0, 0.0)),
349
+        );
350
+
351
+        Ok(())
332 352
     }
333 353
 
334
-    fn update_mesh_builder_with_scene_items_positions(
354
+    fn update_mesh_builder_with_debug(
335 355
         &self,
336 356
         mut mesh_builder: MeshBuilder,
337 357
     ) -> GameResult<MeshBuilder> {
338
-        for scene_item in self.scene_items.iter() {
358
+        if DEBUG {
359
+            // Draw circle on each scene item position
360
+            for scene_item in self.scene_items.iter() {
361
+                mesh_builder.circle(
362
+                    DrawMode::fill(),
363
+                    scene_item.position.clone(),
364
+                    2.0,
365
+                    2.0,
366
+                    graphics::WHITE,
367
+                )?;
368
+            }
369
+
370
+            // Draw circle where left click down
371
+            if let Some(window_left_click_down_point) = self.left_click_down {
372
+                let scene_left_click_down_point = scene_point_from_window_point(
373
+                    &window_left_click_down_point,
374
+                    &self.display_offset,
375
+                );
376
+                mesh_builder.circle(
377
+                    DrawMode::fill(),
378
+                    scene_left_click_down_point,
379
+                    2.0,
380
+                    2.0,
381
+                    graphics::YELLOW,
382
+                )?;
383
+            }
384
+
385
+            // Draw circle at cursor position
339 386
             mesh_builder.circle(
340 387
                 DrawMode::fill(),
341
-                scene_item.position.clone(),
388
+                scene_point_from_window_point(&self.current_cursor_position, &self.display_offset),
342 389
                 2.0,
343 390
                 2.0,
344
-                graphics::WHITE,
391
+                graphics::BLUE,
345 392
             )?;
346 393
         }
347 394
 
@@ -372,23 +419,53 @@ impl MainState {
372 419
     fn update_mesh_builder_with_selection_area(
373 420
         &self,
374 421
         mut mesh_builder: MeshBuilder,
375
-        window_left_click_down_point: WindowPoint,
376 422
     ) -> GameResult<MeshBuilder> {
377
-        let scene_left_click_down_point =
378
-            scene_point_from_window_point(&window_left_click_down_point, &self.display_offset);
379
-        let scene_current_cursor_position =
380
-            scene_point_from_window_point(&self.current_cursor_position, &self.display_offset);
381
-        if scene_left_click_down_point != scene_current_cursor_position {
382
-            mesh_builder.rectangle(
383
-                DrawMode::stroke(1.0),
384
-                graphics::Rect::new(
385
-                    scene_left_click_down_point.x,
386
-                    scene_left_click_down_point.y,
387
-                    scene_current_cursor_position.x - scene_left_click_down_point.x,
388
-                    scene_current_cursor_position.y - scene_left_click_down_point.y,
389
-                ),
390
-                graphics::GREEN,
391
-            )?;
423
+        if let Some(window_left_click_down_point) = self.left_click_down {
424
+            let scene_left_click_down_point =
425
+                scene_point_from_window_point(&window_left_click_down_point, &self.display_offset);
426
+            let scene_current_cursor_position =
427
+                scene_point_from_window_point(&self.current_cursor_position, &self.display_offset);
428
+            if scene_left_click_down_point != scene_current_cursor_position {
429
+                mesh_builder.rectangle(
430
+                    DrawMode::stroke(1.0),
431
+                    graphics::Rect::new(
432
+                        scene_left_click_down_point.x,
433
+                        scene_left_click_down_point.y,
434
+                        scene_current_cursor_position.x - scene_left_click_down_point.x,
435
+                        scene_current_cursor_position.y - scene_left_click_down_point.y,
436
+                    ),
437
+                    graphics::GREEN,
438
+                )?;
439
+            }
440
+        }
441
+
442
+        GameResult::Ok(mesh_builder)
443
+    }
444
+
445
+    fn update_mesh_builder_with_prepare_order(
446
+        &self,
447
+        mut mesh_builder: MeshBuilder,
448
+    ) -> GameResult<MeshBuilder> {
449
+        if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
450
+            match scene_item_prepare_order {
451
+                SceneItemPrepareOrder::Move(scene_item_usize) => {
452
+                    let scene_item = self
453
+                        .scene_items
454
+                        .get(*scene_item_usize)
455
+                        .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
456
+                    mesh_builder.line(
457
+                        &vec![
458
+                            scene_item.position.clone(),
459
+                            scene_point_from_window_point(
460
+                                &self.current_cursor_position,
461
+                                &self.display_offset,
462
+                            ),
463
+                        ],
464
+                        2.0,
465
+                        graphics::WHITE,
466
+                    )?;
467
+                }
468
+            }
392 469
         }
393 470
 
394 471
         GameResult::Ok(mesh_builder)
@@ -444,119 +521,30 @@ impl event::EventHandler for MainState {
444 521
         graphics::clear(ctx, graphics::BLACK);
445 522
         let mut scene_mesh_builder = MeshBuilder::new();
446 523
 
447
-        for sprite in self.build_scene_items_draw_params() {
448
-            self.sprite_sheet_batch.add(sprite);
449
-        }
450
-
451
-        if DEBUG {
452
-            scene_mesh_builder =
453
-                self.update_mesh_builder_with_scene_items_positions(scene_mesh_builder)?;
454
-        }
524
+        self.generate_scene_item_sprites();
525
+        self.generate_scene_item_menu_sprites();
526
+        self.generate_map_sprites();
455 527
 
528
+        scene_mesh_builder = self.update_mesh_builder_with_debug(scene_mesh_builder)?;
456 529
         scene_mesh_builder = self.update_mesh_builder_with_selected_items(scene_mesh_builder)?;
457
-
458
-        if let Some(window_left_click_down_point) = self.left_click_down {
459
-            scene_mesh_builder = self.update_mesh_builder_with_selection_area(
460
-                scene_mesh_builder,
461
-                window_left_click_down_point,
462
-            )?;
463
-
464
-            if DEBUG {
465
-                let scene_left_click_down_point = scene_point_from_window_point(
466
-                    &window_left_click_down_point,
467
-                    &self.display_offset,
468
-                );
469
-                scene_mesh_builder.circle(
470
-                    DrawMode::fill(),
471
-                    scene_left_click_down_point,
472
-                    2.0,
473
-                    2.0,
474
-                    graphics::YELLOW,
475
-                )?;
476
-            }
477
-        }
478
-
479
-        scene_mesh_builder.circle(
480
-            DrawMode::fill(),
481
-            scene_point_from_window_point(&self.current_cursor_position, &self.display_offset),
482
-            2.0,
483
-            2.0,
484
-            graphics::BLUE,
485
-        )?;
486
-
487
-        if let Some((_, scene_point)) = self.scene_item_menu {
488
-            self.ui_batch.add(
489
-                UiSpriteInfo::from_type(UiItem::SceneItemMenu)
490
-                    .as_draw_param()
491
-                    .dest(scene_point),
492
-            );
493
-        }
494
-
495
-        if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
496
-            match scene_item_prepare_order {
497
-                SceneItemPrepareOrder::Move(scene_item_usize) => {
498
-                    let scene_item = self
499
-                        .scene_items
500
-                        .get(*scene_item_usize)
501
-                        .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
502
-                    scene_mesh_builder.line(
503
-                        &vec![
504
-                            scene_item.position.clone(),
505
-                            scene_point_from_window_point(
506
-                                &self.current_cursor_position,
507
-                                &self.display_offset,
508
-                            ),
509
-                        ],
510
-                        2.0,
511
-                        graphics::WHITE,
512
-                    )?;
513
-                }
514
-            }
515
-        }
516
-
517
-        self.map_batch.add(
518
-            graphics::DrawParam::new()
519
-                .src(graphics::Rect::new(0.0, 0.0, 1.0, 1.0))
520
-                .dest(ScenePoint::new(0.0, 0.0)),
521
-        );
530
+        scene_mesh_builder = self.update_mesh_builder_with_selection_area(scene_mesh_builder)?;
531
+        scene_mesh_builder = self.update_mesh_builder_with_prepare_order(scene_mesh_builder)?;
522 532
 
523 533
         let scene_mesh = scene_mesh_builder.build(ctx)?;
524
-        graphics::draw(
525
-            ctx,
526
-            &self.map_batch,
527
-            graphics::DrawParam::new().dest(window_point_from_scene_point(
528
-                &ScenePoint::new(0.0, 0.0),
529
-                &self.display_offset,
530
-            )),
531
-        )?;
532
-        graphics::draw(
533
-            ctx,
534
-            &self.sprite_sheet_batch,
535
-            graphics::DrawParam::new().dest(window_point_from_scene_point(
536
-                &ScenePoint::new(0.0, 0.0),
537
-                &self.display_offset,
538
-            )),
539
-        )?;
540
-        graphics::draw(
541
-            ctx,
542
-            &scene_mesh,
543
-            graphics::DrawParam::new().dest(window_point_from_scene_point(
544
-                &ScenePoint::new(0.0, 0.0),
545
-                &self.display_offset,
546
-            )),
547
-        )?;
548
-        graphics::draw(
549
-            ctx,
550
-            &self.ui_batch,
551
-            graphics::DrawParam::new().dest(window_point_from_scene_point(
552
-                &ScenePoint::new(0.0, 0.0),
553
-                &self.display_offset,
554
-            )),
555
-        )?;
534
+        let window_draw_param = graphics::DrawParam::new().dest(window_point_from_scene_point(
535
+            &ScenePoint::new(0.0, 0.0),
536
+            &self.display_offset,
537
+        ));
538
+
539
+        graphics::draw(ctx, &self.map_batch, window_draw_param)?;
540
+        graphics::draw(ctx, &self.sprite_sheet_batch, window_draw_param)?;
541
+        graphics::draw(ctx, &scene_mesh, window_draw_param)?;
542
+        graphics::draw(ctx, &self.ui_batch, window_draw_param)?;
556 543
 
557 544
         self.sprite_sheet_batch.clear();
558 545
         self.map_batch.clear();
559 546
         self.ui_batch.clear();
547
+
560 548
         graphics::present(ctx)?;
561 549
 
562 550
         println!("FPS: {}", ggez::timer::fps(ctx));