Explorar el Código

clean code and print angle when click move

Bastien Sevajol hace 3 años
padre
commit
df060d5a1c
Se han modificado 4 ficheros con 29 adiciones y 22 borrados
  1. 0 1
      src/main.rs
  2. 1 1
      src/scene/item.rs
  3. 24 16
      src/scene/main.rs
  4. 4 4
      src/ui/mod.rs

+ 0 - 1
src/main.rs Ver fichero

@@ -12,7 +12,6 @@ mod physics;
12 12
 mod scene;
13 13
 mod ui;
14 14
 
15
-type Point2 = Vec2;
16 15
 type WindowPoint = Vec2;
17 16
 type Offset = Vec2;
18 17
 type ScenePoint = Vec2;

+ 1 - 1
src/scene/item.rs Ver fichero

@@ -5,7 +5,7 @@ use crate::config::{SCENE_ITEMS_SPRITE_SHEET_HEIGHT, SCENE_ITEMS_SPRITE_SHEET_WI
5 5
 use crate::physics::GridPosition;
6 6
 use crate::physics::{util, MetaEvent};
7 7
 use crate::scene::SpriteType;
8
-use crate::{Offset, Point2, ScenePoint};
8
+use crate::{Offset, ScenePoint};
9 9
 
10 10
 pub struct SceneItemSpriteInfo {
11 11
     pub relative_start_y: f32,

+ 24 - 16
src/scene/main.rs Ver fichero

@@ -21,6 +21,7 @@ use crate::scene::item::{ItemState, SceneItem, SceneItemType};
21 21
 use crate::ui::scene_item_menu::SceneItemMenuItem;
22 22
 use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
23 23
 use crate::{Offset, ScenePoint, WindowPoint};
24
+use std::f32::consts::FRAC_PI_2;
24 25
 
25 26
 pub struct MainState {
26 27
     // time
@@ -105,6 +106,18 @@ impl MainState {
105 106
         Ok(main_state)
106 107
     }
107 108
 
109
+    fn get_scene_item(&self, index: usize) -> &SceneItem {
110
+        self.scene_items
111
+            .get(index)
112
+            .expect(SCENE_ITEMS_CHANGE_ERR_MSG)
113
+    }
114
+
115
+    fn get_scene_item_mut(&mut self, index: usize) -> &SceneItem {
116
+        self.scene_items
117
+            .get_mut(index)
118
+            .expect(SCENE_ITEMS_CHANGE_ERR_MSG)
119
+    }
120
+
108 121
     fn inputs(&mut self, ctx: &Context) {
109 122
         let display_offset_by =
110 123
             if input::keyboard::is_mod_active(ctx, input::keyboard::KeyMods::SHIFT) {
@@ -151,8 +164,12 @@ impl MainState {
151 164
             // TODO: Add order to scene_item
152 165
             match scene_item_prepare_order {
153 166
                 SceneItemPrepareOrder::Move(scene_item_usize) => {
154
-                    let scene_item = self.scene_items.get_mut(*scene_item_usize).expect(SCENE_ITEMS_CHANGE_ERR_MSG);
155
-                    let angle = f32::atan2(scene_position.y - scene_item.position.y, scene_position.x - scene_item.position.x) + f32::consts::FRAC_PI_2;
167
+                    let scene_item = self.get_scene_item(*scene_item_usize);
168
+                    // TODO: remove this code when used in right place
169
+                    let angle = f32::atan2(
170
+                        scene_position.y - scene_item.position.y,
171
+                        scene_position.x - scene_item.position.x,
172
+                    ) + FRAC_PI_2;
156 173
                     println!("{:?}", angle);
157 174
                 }
158 175
             }
@@ -165,10 +182,7 @@ impl MainState {
165 182
             let window_menu_point =
166 183
                 window_point_from_scene_point(&scene_menu_point, &self.display_offset);
167 184
             let menu_sprite_info = UiSpriteInfo::from_type(UiItem::SceneItemMenu);
168
-            let scene_item = self
169
-                .scene_items
170
-                .get(scene_item_usize)
171
-                .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
185
+            let scene_item = self.get_scene_item(scene_item_usize);
172 186
             if window_click_point.x >= window_menu_point.x
173 187
                 && window_click_point.x <= window_menu_point.x + menu_sprite_info.width
174 188
                 && window_click_point.y >= window_menu_point.y
@@ -205,10 +219,7 @@ impl MainState {
205 219
             self.get_first_scene_item_for_scene_point(&scene_right_click_point)
206 220
         {
207 221
             if self.selected_scene_items.contains(&scene_item_usize) {
208
-                let scene_item = self
209
-                    .scene_items
210
-                    .get(scene_item_usize)
211
-                    .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
222
+                let scene_item = self.get_scene_item(scene_item_usize);
212 223
                 self.scene_item_menu = Some((scene_item_usize, scene_item.position))
213 224
             }
214 225
         }
@@ -227,7 +238,7 @@ impl MainState {
227 238
         // Scene items movements
228 239
         for scene_item in self.scene_items.iter_mut() {
229 240
             match scene_item.state.current_behavior {
230
-                ItemBehavior::Walking(vector) => {
241
+                ItemBehavior::Walking(_vector) => {
231 242
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
232 243
                     scene_item.position.x += 1.0;
233 244
                     scene_item.grid_position =
@@ -408,7 +419,7 @@ impl MainState {
408 419
         mut mesh_builder: MeshBuilder,
409 420
     ) -> GameResult<MeshBuilder> {
410 421
         for i in &self.selected_scene_items {
411
-            let selected_scene_item = self.scene_items.get(*i).expect(SCENE_ITEMS_CHANGE_ERR_MSG);
422
+            let selected_scene_item = self.get_scene_item(*i);
412 423
             mesh_builder.rectangle(
413 424
                 DrawMode::Stroke(StrokeOptions::default()),
414 425
                 graphics::Rect::new(
@@ -457,10 +468,7 @@ impl MainState {
457 468
         if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
458 469
             match scene_item_prepare_order {
459 470
                 SceneItemPrepareOrder::Move(scene_item_usize) => {
460
-                    let scene_item = self
461
-                        .scene_items
462
-                        .get(*scene_item_usize)
463
-                        .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
471
+                    let scene_item = self.get_scene_item(*scene_item_usize);
464 472
                     mesh_builder.line(
465 473
                         &vec![
466 474
                             scene_item.position.clone(),

+ 4 - 4
src/ui/mod.rs Ver fichero

@@ -3,7 +3,7 @@ use ggez::graphics;
3 3
 use crate::config::{UI_SPRITE_SHEET_HEIGHT, UI_SPRITE_SHEET_WIDTH};
4 4
 use crate::scene::item::SceneItem;
5 5
 use crate::ui::scene_item_menu::SceneItemMenuItem;
6
-use crate::{Point2, WindowPoint};
6
+use crate::WindowPoint;
7 7
 
8 8
 pub mod scene_item_menu;
9 9
 
@@ -45,9 +45,9 @@ impl UiSpriteInfo {
45 45
 
46 46
     pub fn which_item_clicked(
47 47
         &self,
48
-        window_menu_point: WindowPoint,
49
-        window_click_point: WindowPoint,
50
-        scene_item: &SceneItem,
48
+        _window_menu_point: WindowPoint,
49
+        _window_click_point: WindowPoint,
50
+        _scene_item: &SceneItem,
51 51
     ) -> Option<SceneItemMenuItem> {
52 52
         Some(SceneItemMenuItem::Move)
53 53
     }