Bastien Sevajol 3 gadus atpakaļ
vecāks
revīzija
de24cb92f5
4 mainītis faili ar 58 papildinājumiem un 19 dzēšanām
  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 Parādīt failu

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

+ 6 - 0
src/behavior/order.rs Parādīt failu

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

+ 5 - 0
src/scene/item.rs Parādīt failu

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

+ 45 - 19
src/scene/main.rs Parādīt failu

@@ -8,6 +8,7 @@ use ggez::timer::check_update_time;
8 8
 use ggez::{event, graphics, input, Context, GameResult};
9 9
 
10 10
 use crate::behavior::ItemBehavior;
11
+use crate::behavior::order::Order;
11 12
 use crate::config::{
12 13
     ANIMATE_EACH, DEBUG, DEFAULT_SELECTED_SQUARE_SIDE, DEFAULT_SELECTED_SQUARE_SIDE_HALF,
13 14
     DISPLAY_OFFSET_BY, DISPLAY_OFFSET_BY_SPEED, MAX_FRAME_I, META_EACH, PHYSICS_EACH,
@@ -112,7 +113,7 @@ impl MainState {
112 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 117
         self.scene_items
117 118
             .get_mut(index)
118 119
             .expect(SCENE_ITEMS_CHANGE_ERR_MSG)
@@ -164,7 +165,9 @@ impl MainState {
164 165
             // TODO: Add order to scene_item
165 166
             match scene_item_prepare_order {
166 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 171
                     // TODO: remove this code when used in right place
169 172
                     let angle = f32::atan2(
170 173
                         scene_position.y - scene_item.position.y,
@@ -238,11 +241,11 @@ impl MainState {
238 241
         // Scene items movements
239 242
         for scene_item in self.scene_items.iter_mut() {
240 243
             match scene_item.state.current_behavior {
241
-                ItemBehavior::Walking(_vector) => {
244
+                ItemBehavior::Walking(move_vector) => {
242 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,23 +281,46 @@ impl MainState {
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