浏览代码

reorganize code

Bastien Sevajol 3 年前
父节点
当前提交
95d6f50e8a
共有 6 个文件被更改,包括 151 次插入78 次删除
  1. 36 0
      src/config.rs
  2. 3 20
      src/main.rs
  3. 2 1
      src/physics/util.rs
  4. 2 3
      src/scene/item.rs
  5. 106 53
      src/scene/main.rs
  6. 2 1
      src/ui/mod.rs

+ 36 - 0
src/config.rs 查看文件

@@ -0,0 +1,36 @@
1
+// execute update code 60x per seconds
2
+pub const TARGET_FPS: u32 = 60;
3
+// execute meta code each 20 frames
4
+pub const META_EACH: u32 = 20;
5
+// execute physics code each 10 frames
6
+pub const PHYSICS_EACH: u32 = 10;
7
+// execute animate code each 30 frames
8
+pub const ANIMATE_EACH: u32 = 60;
9
+// change sprite animation tile 30 frames
10
+pub const SPRITE_EACH: u32 = 10;
11
+// max of frame_i used to calculate ticks
12
+pub const MAX_FRAME_I: u32 = 4294967295;
13
+// pixel offset by tick when player move screen display
14
+pub const DISPLAY_OFFSET_BY: f32 = 3.0;
15
+// pixel offset by tick when player move screen display with speed
16
+pub const DISPLAY_OFFSET_BY_SPEED: f32 = 10.0;
17
+// Width of sprite sheet
18
+pub const SCENE_ITEMS_SPRITE_SHEET_WIDTH: f32 = 800.0;
19
+// Height of sprite sheet
20
+pub const SCENE_ITEMS_SPRITE_SHEET_HEIGHT: f32 = 600.0;
21
+// Width of sprite sheet
22
+pub const UI_SPRITE_SHEET_WIDTH: f32 = 800.0;
23
+// Height of sprite sheet
24
+pub const UI_SPRITE_SHEET_HEIGHT: f32 = 600.0;
25
+// Width of one grid tile
26
+pub const GRID_TILE_WIDTH: f32 = 5.0;
27
+// Height of one grid tile
28
+pub const GRID_TILE_HEIGHT: f32 = 5.0;
29
+//
30
+pub const DEFAULT_SELECTED_SQUARE_SIDE: f32 = 14.0;
31
+//
32
+pub const DEFAULT_SELECTED_SQUARE_SIDE_HALF: f32 = DEFAULT_SELECTED_SQUARE_SIDE / 2.0;
33
+//
34
+pub const SCENE_ITEMS_CHANGE_ERR_MSG: &str = "scene_items content change !";
35
+//
36
+pub const DEBUG: bool = true;

+ 3 - 20
src/main.rs 查看文件

@@ -1,12 +1,13 @@
1 1
 use std::env;
2 2
 use std::path;
3 3
 
4
-use scene::main::MainState;
5
-
6 4
 use ggez::{event, GameResult};
7 5
 use glam::Vec2;
8 6
 
7
+use scene::main::MainState;
8
+
9 9
 mod behavior;
10
+mod config;
10 11
 mod physics;
11 12
 mod scene;
12 13
 mod ui;
@@ -17,24 +18,6 @@ type Offset = Vec2;
17 18
 type ScenePoint = Vec2;
18 19
 type Vector2 = Vec2;
19 20
 
20
-const TARGET_FPS: u32 = 60; // execute update code 60x per seconds
21
-const META_EACH: u32 = 20; // execute meta code each 20 frames
22
-const PHYSICS_EACH: u32 = 10; // execute physics code each 10 frames
23
-const ANIMATE_EACH: u32 = 60; // execute animate code each 30 frames
24
-const SPRITE_EACH: u32 = 10; // change sprite animation tile 30 frames
25
-const MAX_FRAME_I: u32 = 4294967295; // max of frame_i used to calculate ticks
26
-const DISPLAY_OFFSET_BY: f32 = 3.0; // pixel offset by tick when player move screen display
27
-const DISPLAY_OFFSET_BY_SPEED: f32 = 10.0; // pixel offset by tick when player move screen display with speed
28
-const SCENE_ITEMS_SPRITE_SHEET_WIDTH: f32 = 800.0; // Width of sprite sheet
29
-const SCENE_ITEMS_SPRITE_SHEET_HEIGHT: f32 = 600.0; // Height of sprite sheet
30
-const UI_SPRITE_SHEET_WIDTH: f32 = 800.0; // Width of sprite sheet
31
-const UI_SPRITE_SHEET_HEIGHT: f32 = 600.0; // Height of sprite sheet
32
-const GRID_TILE_WIDTH: f32 = 5.0; // Width of one grid tile
33
-const GRID_TILE_HEIGHT: f32 = 5.0; // Height of one grid tile
34
-const DEFAULT_SELECTED_SQUARE_SIDE: f32 = 14.0;
35
-const DEFAULT_SELECTED_SQUARE_SIDE_HALF: f32 = DEFAULT_SELECTED_SQUARE_SIDE / 2.0;
36
-const SCENE_ITEMS_CHANGE_ERR_MSG: &str = "scene_items content change !";
37
-
38 21
 pub fn main() -> GameResult {
39 22
     let resource_dir = if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
40 23
         let mut path = path::PathBuf::from(manifest_dir);

+ 2 - 1
src/physics/util.rs 查看文件

@@ -1,5 +1,6 @@
1
+use crate::config::{GRID_TILE_HEIGHT, GRID_TILE_WIDTH};
1 2
 use crate::physics::GridPosition;
2
-use crate::{ScenePoint, Vector2, WindowPoint, GRID_TILE_HEIGHT, GRID_TILE_WIDTH};
3
+use crate::{ScenePoint, Vector2, WindowPoint};
3 4
 
4 5
 pub fn vec_from_angle(angle: f32) -> Vector2 {
5 6
     let vx = angle.sin();

+ 2 - 3
src/scene/item.rs 查看文件

@@ -1,12 +1,11 @@
1 1
 use ggez::graphics;
2 2
 
3 3
 use crate::behavior::ItemBehavior;
4
+use crate::config::{SCENE_ITEMS_SPRITE_SHEET_HEIGHT, SCENE_ITEMS_SPRITE_SHEET_WIDTH};
4 5
 use crate::physics::GridPosition;
5 6
 use crate::physics::{util, MetaEvent};
6 7
 use crate::scene::SpriteType;
7
-use crate::{
8
-    Offset, Point2, ScenePoint, SCENE_ITEMS_SPRITE_SHEET_HEIGHT, SCENE_ITEMS_SPRITE_SHEET_WIDTH,
9
-};
8
+use crate::{Offset, Point2, ScenePoint};
10 9
 
11 10
 pub struct SceneItemSpriteInfo {
12 11
     pub relative_start_y: f32,

+ 106 - 53
src/scene/main.rs 查看文件

@@ -3,10 +3,16 @@ use std::collections::HashMap;
3 3
 
4 4
 use ggez::event::MouseButton;
5 5
 use ggez::graphics::{DrawMode, MeshBuilder, StrokeOptions};
6
+use ggez::input::keyboard::KeyCode;
6 7
 use ggez::timer::check_update_time;
7 8
 use ggez::{event, graphics, input, Context, GameResult};
8 9
 
9 10
 use crate::behavior::ItemBehavior;
11
+use crate::config::{
12
+    ANIMATE_EACH, DEBUG, DEFAULT_SELECTED_SQUARE_SIDE, DEFAULT_SELECTED_SQUARE_SIDE_HALF,
13
+    DISPLAY_OFFSET_BY, DISPLAY_OFFSET_BY_SPEED, MAX_FRAME_I, META_EACH, PHYSICS_EACH,
14
+    SCENE_ITEMS_CHANGE_ERR_MSG, SPRITE_EACH, TARGET_FPS,
15
+};
10 16
 use crate::physics::util::scene_point_from_window_point;
11 17
 use crate::physics::util::window_point_from_scene_point;
12 18
 use crate::physics::GridPosition;
@@ -14,12 +20,7 @@ use crate::physics::{util, MetaEvent, PhysicEvent};
14 20
 use crate::scene::item::{ItemState, SceneItem, SceneItemType};
15 21
 use crate::ui::scene_item_menu::SceneItemMenuItem;
16 22
 use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
17
-use crate::{
18
-    Offset, ScenePoint, WindowPoint, ANIMATE_EACH, DEFAULT_SELECTED_SQUARE_SIDE,
19
-    DEFAULT_SELECTED_SQUARE_SIDE_HALF, DISPLAY_OFFSET_BY, DISPLAY_OFFSET_BY_SPEED, MAX_FRAME_I,
20
-    META_EACH, PHYSICS_EACH, SCENE_ITEMS_CHANGE_ERR_MSG, SPRITE_EACH, TARGET_FPS,
21
-};
22
-use ggez::input::keyboard::KeyCode;
23
+use crate::{Offset, ScenePoint, WindowPoint};
23 24
 
24 25
 pub struct MainState {
25 26
     // time
@@ -315,6 +316,83 @@ impl MainState {
315 316
 
316 317
         selection
317 318
     }
319
+
320
+    fn build_scene_items_draw_params(&self) -> Vec<graphics::DrawParam> {
321
+        let mut draw_params = vec![];
322
+
323
+        for scene_item in self.scene_items.iter() {
324
+            draw_params.push(
325
+                scene_item
326
+                    .as_draw_param(scene_item.current_frame as f32)
327
+                    .dest(scene_item.position.clone()),
328
+            )
329
+        }
330
+
331
+        draw_params
332
+    }
333
+
334
+    fn update_mesh_builder_with_scene_items_positions(
335
+        &self,
336
+        mut mesh_builder: MeshBuilder,
337
+    ) -> GameResult<MeshBuilder> {
338
+        for scene_item in self.scene_items.iter() {
339
+            mesh_builder.circle(
340
+                DrawMode::fill(),
341
+                scene_item.position.clone(),
342
+                2.0,
343
+                2.0,
344
+                graphics::WHITE,
345
+            )?;
346
+        }
347
+
348
+        GameResult::Ok(mesh_builder)
349
+    }
350
+
351
+    fn update_mesh_builder_with_selected_items(
352
+        &self,
353
+        mut mesh_builder: MeshBuilder,
354
+    ) -> GameResult<MeshBuilder> {
355
+        for i in &self.selected_scene_items {
356
+            let selected_scene_item = self.scene_items.get(*i).expect(SCENE_ITEMS_CHANGE_ERR_MSG);
357
+            mesh_builder.rectangle(
358
+                DrawMode::Stroke(StrokeOptions::default()),
359
+                graphics::Rect::new(
360
+                    selected_scene_item.position.x - DEFAULT_SELECTED_SQUARE_SIDE_HALF,
361
+                    selected_scene_item.position.y - DEFAULT_SELECTED_SQUARE_SIDE_HALF,
362
+                    DEFAULT_SELECTED_SQUARE_SIDE,
363
+                    DEFAULT_SELECTED_SQUARE_SIDE,
364
+                ),
365
+                graphics::GREEN,
366
+            )?;
367
+        }
368
+
369
+        GameResult::Ok(mesh_builder)
370
+    }
371
+
372
+    fn update_mesh_builder_with_selection_area(
373
+        &self,
374
+        mut mesh_builder: MeshBuilder,
375
+        window_left_click_down_point: WindowPoint,
376
+    ) -> 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
+            )?;
392
+        }
393
+
394
+        GameResult::Ok(mesh_builder)
395
+    }
318 396
 }
319 397
 
320 398
 impl event::EventHandler for MainState {
@@ -364,63 +442,38 @@ impl event::EventHandler for MainState {
364 442
 
365 443
     fn draw(&mut self, ctx: &mut Context) -> GameResult {
366 444
         graphics::clear(ctx, graphics::BLACK);
367
-
368 445
         let mut scene_mesh_builder = MeshBuilder::new();
369 446
 
370
-        for scene_item in self.scene_items.iter() {
371
-            self.sprite_sheet_batch.add(
372
-                scene_item
373
-                    .as_draw_param(scene_item.current_frame as f32)
374
-                    .dest(scene_item.position.clone()),
375
-            );
376
-            scene_mesh_builder.circle(
377
-                DrawMode::fill(),
378
-                scene_item.position.clone(),
379
-                2.0,
380
-                2.0,
381
-                graphics::WHITE,
382
-            )?;
447
+        for sprite in self.build_scene_items_draw_params() {
448
+            self.sprite_sheet_batch.add(sprite);
383 449
         }
384 450
 
385
-        for i in &self.selected_scene_items {
386
-            let selected_scene_item = self.scene_items.get(*i).expect(SCENE_ITEMS_CHANGE_ERR_MSG);
387
-            scene_mesh_builder.rectangle(
388
-                DrawMode::Stroke(StrokeOptions::default()),
389
-                graphics::Rect::new(
390
-                    selected_scene_item.position.x - DEFAULT_SELECTED_SQUARE_SIDE_HALF,
391
-                    selected_scene_item.position.y - DEFAULT_SELECTED_SQUARE_SIDE_HALF,
392
-                    DEFAULT_SELECTED_SQUARE_SIDE,
393
-                    DEFAULT_SELECTED_SQUARE_SIDE,
394
-                ),
395
-                graphics::GREEN,
396
-            )?;
451
+        if DEBUG {
452
+            scene_mesh_builder =
453
+                self.update_mesh_builder_with_scene_items_positions(scene_mesh_builder)?;
397 454
         }
398 455
 
456
+        scene_mesh_builder = self.update_mesh_builder_with_selected_items(scene_mesh_builder)?;
457
+
399 458
         if let Some(window_left_click_down_point) = self.left_click_down {
400
-            let scene_left_click_down_point =
401
-                scene_point_from_window_point(&window_left_click_down_point, &self.display_offset);
402
-            let scene_current_cursor_position =
403
-                scene_point_from_window_point(&self.current_cursor_position, &self.display_offset);
404
-            if scene_left_click_down_point != scene_current_cursor_position {
405
-                scene_mesh_builder.rectangle(
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(
406 470
                     DrawMode::fill(),
407
-                    graphics::Rect::new(
408
-                        scene_left_click_down_point.x,
409
-                        scene_left_click_down_point.y,
410
-                        scene_current_cursor_position.x - scene_left_click_down_point.x,
411
-                        scene_current_cursor_position.y - scene_left_click_down_point.y,
412
-                    ),
413
-                    graphics::GREEN,
471
+                    scene_left_click_down_point,
472
+                    2.0,
473
+                    2.0,
474
+                    graphics::YELLOW,
414 475
                 )?;
415 476
             }
416
-
417
-            scene_mesh_builder.circle(
418
-                DrawMode::fill(),
419
-                scene_left_click_down_point,
420
-                2.0,
421
-                2.0,
422
-                graphics::YELLOW,
423
-            )?;
424 477
         }
425 478
 
426 479
         scene_mesh_builder.circle(

+ 2 - 1
src/ui/mod.rs 查看文件

@@ -1,8 +1,9 @@
1 1
 use ggez::graphics;
2 2
 
3
+use crate::config::{UI_SPRITE_SHEET_HEIGHT, UI_SPRITE_SHEET_WIDTH};
3 4
 use crate::scene::item::SceneItem;
4 5
 use crate::ui::scene_item_menu::SceneItemMenuItem;
5
-use crate::{Point2, WindowPoint, UI_SPRITE_SHEET_HEIGHT, UI_SPRITE_SHEET_WIDTH};
6
+use crate::{Point2, WindowPoint};
6 7
 
7 8
 pub mod scene_item_menu;
8 9