Browse Source

clean code and print angle when click move

Bastien Sevajol 3 years ago
parent
commit
df060d5a1c
4 changed files with 29 additions and 22 deletions
  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 View File

12
 mod scene;
12
 mod scene;
13
 mod ui;
13
 mod ui;
14
 
14
 
15
-type Point2 = Vec2;
16
 type WindowPoint = Vec2;
15
 type WindowPoint = Vec2;
17
 type Offset = Vec2;
16
 type Offset = Vec2;
18
 type ScenePoint = Vec2;
17
 type ScenePoint = Vec2;

+ 1 - 1
src/scene/item.rs View File

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

+ 24 - 16
src/scene/main.rs View File

21
 use crate::ui::scene_item_menu::SceneItemMenuItem;
21
 use crate::ui::scene_item_menu::SceneItemMenuItem;
22
 use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
22
 use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
23
 use crate::{Offset, ScenePoint, WindowPoint};
23
 use crate::{Offset, ScenePoint, WindowPoint};
24
+use std::f32::consts::FRAC_PI_2;
24
 
25
 
25
 pub struct MainState {
26
 pub struct MainState {
26
     // time
27
     // time
105
         Ok(main_state)
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
     fn inputs(&mut self, ctx: &Context) {
121
     fn inputs(&mut self, ctx: &Context) {
109
         let display_offset_by =
122
         let display_offset_by =
110
             if input::keyboard::is_mod_active(ctx, input::keyboard::KeyMods::SHIFT) {
123
             if input::keyboard::is_mod_active(ctx, input::keyboard::KeyMods::SHIFT) {
151
             // TODO: Add order to scene_item
164
             // TODO: Add order to scene_item
152
             match scene_item_prepare_order {
165
             match scene_item_prepare_order {
153
                 SceneItemPrepareOrder::Move(scene_item_usize) => {
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
                     println!("{:?}", angle);
173
                     println!("{:?}", angle);
157
                 }
174
                 }
158
             }
175
             }
165
             let window_menu_point =
182
             let window_menu_point =
166
                 window_point_from_scene_point(&scene_menu_point, &self.display_offset);
183
                 window_point_from_scene_point(&scene_menu_point, &self.display_offset);
167
             let menu_sprite_info = UiSpriteInfo::from_type(UiItem::SceneItemMenu);
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
             if window_click_point.x >= window_menu_point.x
186
             if window_click_point.x >= window_menu_point.x
173
                 && window_click_point.x <= window_menu_point.x + menu_sprite_info.width
187
                 && window_click_point.x <= window_menu_point.x + menu_sprite_info.width
174
                 && window_click_point.y >= window_menu_point.y
188
                 && window_click_point.y >= window_menu_point.y
205
             self.get_first_scene_item_for_scene_point(&scene_right_click_point)
219
             self.get_first_scene_item_for_scene_point(&scene_right_click_point)
206
         {
220
         {
207
             if self.selected_scene_items.contains(&scene_item_usize) {
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
                 self.scene_item_menu = Some((scene_item_usize, scene_item.position))
223
                 self.scene_item_menu = Some((scene_item_usize, scene_item.position))
213
             }
224
             }
214
         }
225
         }
227
         // Scene items movements
238
         // Scene items movements
228
         for scene_item in self.scene_items.iter_mut() {
239
         for scene_item in self.scene_items.iter_mut() {
229
             match scene_item.state.current_behavior {
240
             match scene_item.state.current_behavior {
230
-                ItemBehavior::Walking(vector) => {
241
+                ItemBehavior::Walking(_vector) => {
231
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
242
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
232
                     scene_item.position.x += 1.0;
243
                     scene_item.position.x += 1.0;
233
                     scene_item.grid_position =
244
                     scene_item.grid_position =
408
         mut mesh_builder: MeshBuilder,
419
         mut mesh_builder: MeshBuilder,
409
     ) -> GameResult<MeshBuilder> {
420
     ) -> GameResult<MeshBuilder> {
410
         for i in &self.selected_scene_items {
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
             mesh_builder.rectangle(
423
             mesh_builder.rectangle(
413
                 DrawMode::Stroke(StrokeOptions::default()),
424
                 DrawMode::Stroke(StrokeOptions::default()),
414
                 graphics::Rect::new(
425
                 graphics::Rect::new(
457
         if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
468
         if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
458
             match scene_item_prepare_order {
469
             match scene_item_prepare_order {
459
                 SceneItemPrepareOrder::Move(scene_item_usize) => {
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
                     mesh_builder.line(
472
                     mesh_builder.line(
465
                         &vec![
473
                         &vec![
466
                             scene_item.position.clone(),
474
                             scene_item.position.clone(),

+ 4 - 4
src/ui/mod.rs View File

3
 use crate::config::{UI_SPRITE_SHEET_HEIGHT, UI_SPRITE_SHEET_WIDTH};
3
 use crate::config::{UI_SPRITE_SHEET_HEIGHT, UI_SPRITE_SHEET_WIDTH};
4
 use crate::scene::item::SceneItem;
4
 use crate::scene::item::SceneItem;
5
 use crate::ui::scene_item_menu::SceneItemMenuItem;
5
 use crate::ui::scene_item_menu::SceneItemMenuItem;
6
-use crate::{Point2, WindowPoint};
6
+use crate::WindowPoint;
7
 
7
 
8
 pub mod scene_item_menu;
8
 pub mod scene_item_menu;
9
 
9
 
45
 
45
 
46
     pub fn which_item_clicked(
46
     pub fn which_item_clicked(
47
         &self,
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
     ) -> Option<SceneItemMenuItem> {
51
     ) -> Option<SceneItemMenuItem> {
52
         Some(SceneItemMenuItem::Move)
52
         Some(SceneItemMenuItem::Move)
53
     }
53
     }