Bastien Sevajol 3 years ago
parent
commit
b5f352a086
7 changed files with 65 additions and 27 deletions
  1. 18 12
      src/behavior/animate.rs
  2. 7 1
      src/config.rs
  3. 1 0
      src/main.rs
  4. 2 2
      src/scene/item.rs
  5. 24 10
      src/scene/main.rs
  6. 2 2
      src/ui/mod.rs
  7. 11 0
      src/util.rs

+ 18 - 12
src/behavior/animate.rs View File

@@ -2,6 +2,7 @@ use crate::behavior::order::Order;
2 2
 use crate::behavior::ItemBehavior;
3 3
 use crate::config::MOVE_TO_REACHED_WHEN_DISTANCE_INFERIOR_AT;
4 4
 use crate::scene::item::{ItemState, SceneItem, SceneItemModifier};
5
+use crate::util::velocity_for_behavior;
5 6
 use std::f32::consts::FRAC_PI_2;
6 7
 
7 8
 pub fn digest_next_order(scene_item: &SceneItem) -> Vec<SceneItemModifier> {
@@ -11,14 +12,10 @@ pub fn digest_next_order(scene_item: &SceneItem) -> Vec<SceneItemModifier> {
11 12
     if let Some(next_order) = &scene_item.next_order {
12 13
         match next_order {
13 14
             Order::MoveTo(_) => {
14
-                scene_item_modifiers.push(SceneItemModifier::SwitchToCurrentOrder);
15
-            }
16
-            Order::MoveFastTo(_) => {
17
-                scene_item_modifiers.push(SceneItemModifier::SwitchToCurrentOrder)
18
-            }
19
-            Order::HideTo(_) => {
20
-                scene_item_modifiers.push(SceneItemModifier::SwitchToCurrentOrder)
15
+                scene_item_modifiers.push(SceneItemModifier::SwitchToNextOrder);
21 16
             }
17
+            Order::MoveFastTo(_) => scene_item_modifiers.push(SceneItemModifier::SwitchToNextOrder),
18
+            Order::HideTo(_) => scene_item_modifiers.push(SceneItemModifier::SwitchToNextOrder),
22 19
         }
23 20
     }
24 21
 
@@ -41,12 +38,14 @@ pub fn digest_current_order(scene_item: &SceneItem) -> Vec<SceneItemModifier> {
41 38
                 // FIXME BS NOW: Change order only if it is not the current order
42 39
                 scene_item_modifiers.push(SceneItemModifier::ChangeState(ItemState::new(
43 40
                     ItemBehavior::MoveFastTo(*move_to_scene_point),
44
-                )))}
41
+                )))
42
+            }
45 43
             Order::HideTo(move_to_scene_point) => {
46 44
                 // FIXME BS NOW: Change order only if it is not the current order
47 45
                 scene_item_modifiers.push(SceneItemModifier::ChangeState(ItemState::new(
48 46
                     ItemBehavior::HideTo(*move_to_scene_point),
49
-                )))}
47
+                )))
48
+            }
50 49
         }
51 50
     }
52 51
 
@@ -59,6 +58,7 @@ pub fn digest_current_behavior(scene_item: &SceneItem) -> Vec<SceneItemModifier>
59 58
     match scene_item.state.current_behavior {
60 59
         ItemBehavior::Standing => {}
61 60
         ItemBehavior::MoveTo(going_to_scene_point)
61
+        | ItemBehavior::MoveFastTo(going_to_scene_point)
62 62
         | ItemBehavior::HideTo(going_to_scene_point) => {
63 63
             // Note: angle computed by adding FRAC_PI_2 because sprites are north oriented
64 64
             scene_item_modifiers.push(SceneItemModifier::ChangeDisplayAngle(
@@ -70,15 +70,21 @@ pub fn digest_current_behavior(scene_item: &SceneItem) -> Vec<SceneItemModifier>
70 70
 
71 71
             // Check if scene_point reached
72 72
             let distance = going_to_scene_point.distance(scene_item.position);
73
-            if distance < MOVE_TO_REACHED_WHEN_DISTANCE_INFERIOR_AT {
73
+            let velocity = velocity_for_behavior(&scene_item.state.current_behavior)
74
+                .expect("must have velocity here");
75
+            if distance < MOVE_TO_REACHED_WHEN_DISTANCE_INFERIOR_AT * velocity {
74 76
                 scene_item_modifiers.push(SceneItemModifier::ChangeState(ItemState::new(
75 77
                     ItemBehavior::Standing,
76 78
                 )));
79
+
80
+                // Test if reached destination is from an order. If it is, switch to next order.
77 81
                 if let Some(current_order) = &scene_item.current_order {
78 82
                     match current_order {
79
-                        Order::MoveTo(move_to_scene_point) => {
83
+                        Order::MoveTo(move_to_scene_point)
84
+                        | Order::MoveFastTo(move_to_scene_point)
85
+                        | Order::HideTo(move_to_scene_point) => {
80 86
                             if *move_to_scene_point == going_to_scene_point {
81
-                                scene_item_modifiers.push(SceneItemModifier::SwitchToCurrentOrder);
87
+                                scene_item_modifiers.push(SceneItemModifier::SwitchToNextOrder);
82 88
                             }
83 89
                         }
84 90
                     }

+ 7 - 1
src/config.rs View File

@@ -35,4 +35,10 @@ pub const SCENE_ITEMS_CHANGE_ERR_MSG: &str = "scene_items content change !";
35 35
 //
36 36
 pub const DEBUG: bool = true;
37 37
 // Distance from move target point to consider reached
38
-pub const MOVE_TO_REACHED_WHEN_DISTANCE_INFERIOR_AT: f32 = 1.0;
38
+pub const MOVE_TO_REACHED_WHEN_DISTANCE_INFERIOR_AT: f32 = 3.0;
39
+// Velocity of move vector
40
+pub const MOVE_VELOCITY: f32 = 1.0;
41
+// Velocity of move fast vector
42
+pub const MOVE_FAST_VELOCITY: f32 = 2.0;
43
+// Velocity of move hide vector
44
+pub const MOVE_HIDE_VELOCITY: f32 = 0.5;

+ 1 - 0
src/main.rs View File

@@ -11,6 +11,7 @@ mod config;
11 11
 mod physics;
12 12
 mod scene;
13 13
 mod ui;
14
+mod util;
14 15
 
15 16
 type WindowPoint = Vec2;
16 17
 type Offset = Vec2;

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

@@ -120,7 +120,7 @@ impl SceneItem {
120 120
 }
121 121
 
122 122
 pub enum SceneItemModifier {
123
-    SwitchToCurrentOrder,
123
+    SwitchToNextOrder,
124 124
     ChangeDisplayAngle(f32),
125 125
     ChangeState(ItemState),
126 126
 }
@@ -128,7 +128,7 @@ pub enum SceneItemModifier {
128 128
 pub fn apply_scene_item_modifier(scene_item: &mut SceneItem, modifiers: Vec<SceneItemModifier>) {
129 129
     for modifier in modifiers {
130 130
         match modifier {
131
-            SceneItemModifier::SwitchToCurrentOrder => {
131
+            SceneItemModifier::SwitchToNextOrder => {
132 132
                 let next_order = scene_item.next_order.clone();
133 133
                 scene_item.current_order = next_order;
134 134
                 scene_item.next_order = None;

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

@@ -13,9 +13,9 @@ use crate::behavior::order::Order;
13 13
 use crate::behavior::ItemBehavior;
14 14
 use crate::config::{
15 15
     ANIMATE_EACH, DEBUG, DEFAULT_SELECTED_SQUARE_SIDE, DEFAULT_SELECTED_SQUARE_SIDE_HALF,
16
-    DISPLAY_OFFSET_BY, DISPLAY_OFFSET_BY_SPEED, MAX_FRAME_I, META_EACH,
17
-    MOVE_TO_REACHED_WHEN_DISTANCE_INFERIOR_AT, PHYSICS_EACH, SCENE_ITEMS_CHANGE_ERR_MSG,
18
-    SPRITE_EACH, TARGET_FPS,
16
+    DISPLAY_OFFSET_BY, DISPLAY_OFFSET_BY_SPEED, MAX_FRAME_I, META_EACH, MOVE_FAST_VELOCITY,
17
+    MOVE_HIDE_VELOCITY, MOVE_TO_REACHED_WHEN_DISTANCE_INFERIOR_AT, MOVE_VELOCITY, PHYSICS_EACH,
18
+    SCENE_ITEMS_CHANGE_ERR_MSG, SPRITE_EACH, TARGET_FPS,
19 19
 };
20 20
 use crate::physics::util::scene_point_from_window_point;
21 21
 use crate::physics::util::window_point_from_scene_point;
@@ -27,6 +27,7 @@ use crate::scene::item::{
27 27
 use crate::ui::vertical_menu::{vertical_menu_sprite_info, VerticalMenuSpriteInfo};
28 28
 use crate::ui::MenuItem;
29 29
 use crate::ui::{SceneItemPrepareOrder, UiComponent, UserEvent};
30
+use crate::util::velocity_for_behavior;
30 31
 use crate::{Offset, ScenePoint, WindowPoint};
31 32
 
32 33
 pub struct MainState {
@@ -185,7 +186,8 @@ impl MainState {
185 186
                 }
186 187
                 SceneItemPrepareOrder::Hide(scene_item_usize) => {
187 188
                     let mut scene_item = self.get_scene_item_mut(*scene_item_usize);
188
-                    scene_item.next_order = Some(Order::HideTo(scene_click_point));}
189
+                    scene_item.next_order = Some(Order::HideTo(scene_click_point));
190
+                }
189 191
             }
190 192
 
191 193
             self.scene_item_prepare_order = None;
@@ -255,16 +257,20 @@ impl MainState {
255 257
         // Scene items movements
256 258
         for scene_item in self.scene_items.iter_mut() {
257 259
             match scene_item.state.current_behavior {
258
-                ItemBehavior::MoveTo(scene_point) => {
259
-                    // FIXME BS NOW: velocity
260
-                    let move_vector = (scene_point - scene_item.position).normalize() * 1.0;
260
+                ItemBehavior::Standing => {}
261
+                ItemBehavior::MoveTo(move_to_scene_point)
262
+                | ItemBehavior::MoveFastTo(move_to_scene_point)
263
+                | ItemBehavior::HideTo(move_to_scene_point) => {
264
+                    let velocity = velocity_for_behavior(&scene_item.state.current_behavior)
265
+                        .expect("must have velocity here");
266
+                    let move_vector =
267
+                        (move_to_scene_point - scene_item.position).normalize() * velocity;
261 268
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
262 269
                     scene_item.position.x += move_vector.x;
263 270
                     scene_item.position.y += move_vector.y;
264 271
                     scene_item.grid_position =
265 272
                         util::grid_position_from_scene_point(&scene_item.position);
266 273
                 }
267
-                _ => {}
268 274
             }
269 275
         }
270 276
 
@@ -463,7 +469,15 @@ impl MainState {
463 469
     ) -> GameResult<MeshBuilder> {
464 470
         if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
465 471
             match scene_item_prepare_order {
466
-                SceneItemPrepareOrder::Move(scene_item_usize) => {
472
+                SceneItemPrepareOrder::Move(scene_item_usize)
473
+                | SceneItemPrepareOrder::MoveFast(scene_item_usize)
474
+                | SceneItemPrepareOrder::Hide(scene_item_usize) => {
475
+                    let color = match &scene_item_prepare_order {
476
+                        SceneItemPrepareOrder::Move(_) => graphics::BLUE,
477
+                        SceneItemPrepareOrder::MoveFast(_) => graphics::MAGENTA,
478
+                        SceneItemPrepareOrder::Hide(_) => graphics::YELLOW,
479
+                    };
480
+
467 481
                     let scene_item = self.get_scene_item(*scene_item_usize);
468 482
                     mesh_builder.line(
469 483
                         &vec![
@@ -474,7 +488,7 @@ impl MainState {
474 488
                             ),
475 489
                         ],
476 490
                         2.0,
477
-                        graphics::WHITE,
491
+                        color,
478 492
                     )?;
479 493
                 }
480 494
             }

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

@@ -22,9 +22,9 @@ pub enum UserEvent {
22 22
 }
23 23
 
24 24
 pub enum SceneItemPrepareOrder {
25
-    Move(usize), // scene_item usize
25
+    Move(usize),     // scene_item usize
26 26
     MoveFast(usize), // scene_item usize
27
-    Hide(usize), // scene_item usize
27
+    Hide(usize),     // scene_item usize
28 28
 }
29 29
 
30 30
 #[derive(Clone)]

+ 11 - 0
src/util.rs View File

@@ -0,0 +1,11 @@
1
+use crate::behavior::ItemBehavior;
2
+use crate::config::{MOVE_FAST_VELOCITY, MOVE_HIDE_VELOCITY, MOVE_VELOCITY};
3
+
4
+pub fn velocity_for_behavior(behavior: &ItemBehavior) -> Option<f32> {
5
+    match behavior {
6
+        ItemBehavior::MoveTo(_) => Some(MOVE_VELOCITY),
7
+        ItemBehavior::MoveFastTo(_) => Some(MOVE_FAST_VELOCITY),
8
+        ItemBehavior::HideTo(_) => Some(MOVE_HIDE_VELOCITY),
9
+        _ => None,
10
+    }
11
+}