Bastien Sevajol 3 years ago
parent
commit
de24cb92f5
4 changed files with 58 additions and 19 deletions
  1. 2 0
      src/behavior/mod.rs
  2. 6 0
      src/behavior/order.rs
  3. 5 0
      src/scene/item.rs
  4. 45 19
      src/scene/main.rs

+ 2 - 0
src/behavior/mod.rs View File

1
+pub mod order;
2
+
1
 use crate::Vector2;
3
 use crate::Vector2;
2
 
4
 
3
 pub enum ItemBehavior {
5
 pub enum ItemBehavior {

+ 6 - 0
src/behavior/order.rs View File

1
+use crate::ScenePoint;
2
+
3
+#[derive(Clone)]
4
+pub enum Order {
5
+    MoveTo(ScenePoint)
6
+}

+ 5 - 0
src/scene/item.rs View File

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, ScenePoint};
8
 use crate::{Offset, ScenePoint};
9
+use crate::behavior::order::Order;
9
 
10
 
10
 pub struct SceneItemSpriteInfo {
11
 pub struct SceneItemSpriteInfo {
11
     pub relative_start_y: f32,
12
     pub relative_start_y: f32,
61
     pub state: ItemState,
62
     pub state: ItemState,
62
     pub meta_events: Vec<MetaEvent>,
63
     pub meta_events: Vec<MetaEvent>,
63
     pub current_frame: u16,
64
     pub current_frame: u16,
65
+    pub current_order: Option<Order>,
66
+    pub next_order: Option<Order>,
64
 }
67
 }
65
 
68
 
66
 impl SceneItem {
69
 impl SceneItem {
72
             state,
75
             state,
73
             meta_events: vec![],
76
             meta_events: vec![],
74
             current_frame: 0,
77
             current_frame: 0,
78
+            current_order: None,
79
+            next_order: None,
75
         }
80
         }
76
     }
81
     }
77
 
82
 

+ 45 - 19
src/scene/main.rs View File

8
 use ggez::{event, graphics, input, Context, GameResult};
8
 use ggez::{event, graphics, input, Context, GameResult};
9
 
9
 
10
 use crate::behavior::ItemBehavior;
10
 use crate::behavior::ItemBehavior;
11
+use crate::behavior::order::Order;
11
 use crate::config::{
12
 use crate::config::{
12
     ANIMATE_EACH, DEBUG, DEFAULT_SELECTED_SQUARE_SIDE, DEFAULT_SELECTED_SQUARE_SIDE_HALF,
13
     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
     DISPLAY_OFFSET_BY, DISPLAY_OFFSET_BY_SPEED, MAX_FRAME_I, META_EACH, PHYSICS_EACH,
112
             .expect(SCENE_ITEMS_CHANGE_ERR_MSG)
113
             .expect(SCENE_ITEMS_CHANGE_ERR_MSG)
113
     }
114
     }
114
 
115
 
115
-    fn get_scene_item_mut(&mut self, index: usize) -> &SceneItem {
116
+    fn get_scene_item_mut(&mut self, index: usize) -> &mut SceneItem {
116
         self.scene_items
117
         self.scene_items
117
             .get_mut(index)
118
             .get_mut(index)
118
             .expect(SCENE_ITEMS_CHANGE_ERR_MSG)
119
             .expect(SCENE_ITEMS_CHANGE_ERR_MSG)
164
             // TODO: Add order to scene_item
165
             // TODO: Add order to scene_item
165
             match scene_item_prepare_order {
166
             match scene_item_prepare_order {
166
                 SceneItemPrepareOrder::Move(scene_item_usize) => {
167
                 SceneItemPrepareOrder::Move(scene_item_usize) => {
167
-                    let scene_item = self.get_scene_item(*scene_item_usize);
168
+                    let mut scene_item = self.get_scene_item_mut(*scene_item_usize);
169
+                    scene_item.next_order = Some(Order::MoveTo(scene_position));
170
+
168
                     // TODO: remove this code when used in right place
171
                     // TODO: remove this code when used in right place
169
                     let angle = f32::atan2(
172
                     let angle = f32::atan2(
170
                         scene_position.y - scene_item.position.y,
173
                         scene_position.y - scene_item.position.y,
238
         // Scene items movements
241
         // Scene items movements
239
         for scene_item in self.scene_items.iter_mut() {
242
         for scene_item in self.scene_items.iter_mut() {
240
             match scene_item.state.current_behavior {
243
             match scene_item.state.current_behavior {
241
-                ItemBehavior::Walking(_vector) => {
244
+                ItemBehavior::Walking(move_vector) => {
242
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
245
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
243
-                    scene_item.position.x += 1.0;
244
-                    scene_item.grid_position =
245
-                        util::grid_position_from_scene_point(&scene_item.position);
246
+                    scene_item.position.x += move_vector.x;
247
+                    scene_item.position.y += move_vector.y;
248
+                    scene_item.grid_position = util::grid_position_from_scene_point(&scene_item.position);
246
                 }
249
                 }
247
                 _ => {}
250
                 _ => {}
248
             }
251
             }
278
                 }
281
                 }
279
             }
282
             }
280
 
283
 
281
-            match scene_item.state.current_behavior {
282
-                ItemBehavior::Crawling => {
283
-                    scene_item.state =
284
-                        ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
285
-                }
286
-                ItemBehavior::Walking(_) => {
287
-                    scene_item.state = ItemState::new(ItemBehavior::Crawling);
288
-                }
289
-                ItemBehavior::Standing(since) => {
290
-                    if self.frame_i - since >= 120 {
291
-                        scene_item.state =
292
-                            ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
284
+            // match scene_item.state.current_behavior {
285
+            //     ItemBehavior::Crawling => {
286
+            //         scene_item.state =
287
+            //             ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
288
+            //     }
289
+            //     ItemBehavior::Walking(_) => {
290
+            //         scene_item.state = ItemState::new(ItemBehavior::Crawling);
291
+            //     }
292
+            //     ItemBehavior::Standing(since) => {
293
+            //         if self.frame_i - since >= 120 {
294
+            //             scene_item.state =
295
+            //                 ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
296
+            //         }
297
+            //     }
298
+            // }
299
+
300
+            scene_item.meta_events.drain(..);
301
+
302
+            if let Some(next_order) = &scene_item.next_order {
303
+                // Compute here if it possible (fear, compatible with current order, etc)
304
+                match next_order {
305
+                    Order::MoveTo(move_to_scene_point) => {
306
+                        scene_item.current_order = Some(Order::MoveTo(*move_to_scene_point));
307
+                        // FIXME BS NOW: velocity
308
+                        let move_vector = (*move_to_scene_point - scene_item.position).normalize() * 1.0;
309
+                        scene_item.state = ItemState::new(ItemBehavior::Walking(move_vector));
293
                     }
310
                     }
294
                 }
311
                 }
312
+                scene_item.next_order = None;
295
             }
313
             }
296
 
314
 
297
-            scene_item.meta_events.drain(..);
315
+            // FIXME BS NOW: stop move when move is accomplished; warn: recompute move_vector here
316
+            if let Some(current_order) = &scene_item.current_order {
317
+                match current_order {
318
+                    Order::MoveTo(move_to_scene_point) => {
319
+                        let move_vector = (*move_to_scene_point - scene_item.position).normalize() * 1.0;
320
+                        println!("{:?}", move_vector);
321
+                    }
322
+                }
323
+            }
298
         }
324
         }
299
     }
325
     }
300
 
326