Bastien Sevajol 3 years ago
parent
commit
0693d0cc79
4 changed files with 64 additions and 37 deletions
  1. 3 3
      src/behavior/mod.rs
  2. 1 1
      src/behavior/order.rs
  3. 6 4
      src/scene/item.rs
  4. 54 29
      src/scene/main.rs

+ 3 - 3
src/behavior/mod.rs View File

@@ -1,9 +1,9 @@
1 1
 pub mod order;
2 2
 
3
-use crate::Vector2;
3
+use crate::ScenePoint;
4 4
 
5 5
 pub enum ItemBehavior {
6 6
     Standing(u32), // since
7
-    Crawling,
8
-    Walking(Vector2),
7
+    CrawlingTo(ScenePoint),
8
+    WalkingTo(ScenePoint),
9 9
 }

+ 1 - 1
src/behavior/order.rs View File

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

+ 6 - 4
src/scene/item.rs View File

@@ -1,12 +1,12 @@
1 1
 use ggez::graphics;
2 2
 
3
+use crate::behavior::order::Order;
3 4
 use crate::behavior::ItemBehavior;
4 5
 use crate::config::{SCENE_ITEMS_SPRITE_SHEET_HEIGHT, SCENE_ITEMS_SPRITE_SHEET_WIDTH};
5 6
 use crate::physics::GridPosition;
6 7
 use crate::physics::{util, MetaEvent};
7 8
 use crate::scene::SpriteType;
8 9
 use crate::{Offset, ScenePoint};
9
-use crate::behavior::order::Order;
10 10
 
11 11
 pub struct SceneItemSpriteInfo {
12 12
     pub relative_start_y: f32,
@@ -64,6 +64,7 @@ pub struct SceneItem {
64 64
     pub current_frame: u16,
65 65
     pub current_order: Option<Order>,
66 66
     pub next_order: Option<Order>,
67
+    pub display_angle: f32,
67 68
 }
68 69
 
69 70
 impl SceneItem {
@@ -77,6 +78,7 @@ impl SceneItem {
77 78
             current_frame: 0,
78 79
             current_order: None,
79 80
             next_order: None,
81
+            display_angle: 0.0,
80 82
         }
81 83
     }
82 84
 
@@ -101,7 +103,7 @@ impl SceneItem {
101 103
                 sprite_info.relative_tile_width,
102 104
                 sprite_info.relative_tile_height,
103 105
             ))
104
-            .rotation(90.0f32.to_radians())
106
+            .rotation(self.display_angle)
105 107
             .offset(Offset::new(0.5, 0.5))
106 108
     }
107 109
 
@@ -109,8 +111,8 @@ impl SceneItem {
109 111
         // Here some logical about state, nature (soldier, tank, ...) and current behavior to
110 112
         // determine sprite type
111 113
         match self.state.current_behavior {
112
-            ItemBehavior::Crawling => SpriteType::CrawlingSoldier,
113
-            ItemBehavior::Walking(_) => SpriteType::WalkingSoldier,
114
+            ItemBehavior::CrawlingTo(_) => SpriteType::CrawlingSoldier,
115
+            ItemBehavior::WalkingTo(_) => SpriteType::WalkingSoldier,
114 116
             ItemBehavior::Standing(_) => SpriteType::StandingSoldier,
115 117
         }
116 118
     }

+ 54 - 29
src/scene/main.rs View File

@@ -7,8 +7,8 @@ use ggez::input::keyboard::KeyCode;
7 7
 use ggez::timer::check_update_time;
8 8
 use ggez::{event, graphics, input, Context, GameResult};
9 9
 
10
-use crate::behavior::ItemBehavior;
11 10
 use crate::behavior::order::Order;
11
+use crate::behavior::ItemBehavior;
12 12
 use crate::config::{
13 13
     ANIMATE_EACH, DEBUG, DEFAULT_SELECTED_SQUARE_SIDE, DEFAULT_SELECTED_SQUARE_SIDE_HALF,
14 14
     DISPLAY_OFFSET_BY, DISPLAY_OFFSET_BY_SPEED, MAX_FRAME_I, META_EACH, PHYSICS_EACH,
@@ -63,16 +63,16 @@ impl MainState {
63 63
         let mut scene_items = vec![];
64 64
         for x in 0..1 {
65 65
             for y in 0..4 {
66
-                let current_behavior = if y % 2 == 0 {
67
-                    ItemBehavior::Walking(util::vec_from_angle(90.0))
68
-                } else {
69
-                    ItemBehavior::Crawling
70
-                };
66
+                // let current_behavior = if y % 2 == 0 {
67
+                //     ItemBehavior::WalkingTo(util::vec_from_angle(90.0))
68
+                // } else {
69
+                //     ItemBehavior::CrawlingTo()
70
+                // };
71 71
 
72 72
                 scene_items.push(SceneItem::new(
73 73
                     SceneItemType::Soldier,
74 74
                     ScenePoint::new((x as f32 * 24.0) + 100.0, (y as f32 * 24.0) + 100.0),
75
-                    ItemState::new(current_behavior),
75
+                    ItemState::new(ItemBehavior::Standing(0)),
76 76
                 ));
77 77
             }
78 78
         }
@@ -167,13 +167,6 @@ impl MainState {
167 167
                 SceneItemPrepareOrder::Move(scene_item_usize) => {
168 168
                     let mut scene_item = self.get_scene_item_mut(*scene_item_usize);
169 169
                     scene_item.next_order = Some(Order::MoveTo(scene_position));
170
-
171
-                    // TODO: remove this code when used in right place
172
-                    let angle = f32::atan2(
173
-                        scene_position.y - scene_item.position.y,
174
-                        scene_position.x - scene_item.position.x,
175
-                    ) + FRAC_PI_2;
176
-                    println!("{:?}", angle);
177 170
                 }
178 171
             }
179 172
 
@@ -241,11 +234,14 @@ impl MainState {
241 234
         // Scene items movements
242 235
         for scene_item in self.scene_items.iter_mut() {
243 236
             match scene_item.state.current_behavior {
244
-                ItemBehavior::Walking(move_vector) => {
237
+                ItemBehavior::WalkingTo(scene_point) => {
238
+                    // FIXME BS NOW: velocity
239
+                    let move_vector = (scene_point - scene_item.position).normalize() * 1.0;
245 240
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
246 241
                     scene_item.position.x += move_vector.x;
247 242
                     scene_item.position.y += move_vector.y;
248
-                    scene_item.grid_position = util::grid_position_from_scene_point(&scene_item.position);
243
+                    scene_item.grid_position =
244
+                        util::grid_position_from_scene_point(&scene_item.position);
249 245
                 }
250 246
                 _ => {}
251 247
             }
@@ -273,13 +269,13 @@ impl MainState {
273 269
         // TODO: ici il faut reflechir a comment organiser les comportements
274 270
 
275 271
         for scene_item in self.scene_items.iter_mut() {
276
-            for meta_event in &scene_item.meta_events {
277
-                match meta_event {
278
-                    MetaEvent::FeelExplosion => {
279
-                        scene_item.state = ItemState::new(ItemBehavior::Standing(self.frame_i));
280
-                    }
281
-                }
282
-            }
272
+            // for meta_event in &scene_item.meta_events {
273
+            //     match meta_event {
274
+            //         MetaEvent::FeelExplosion => {
275
+            //             scene_item.state = ItemState::new(ItemBehavior::Standing(self.frame_i));
276
+            //         }
277
+            //     }
278
+            // }
283 279
 
284 280
             // match scene_item.state.current_behavior {
285 281
             //     ItemBehavior::Crawling => {
@@ -300,13 +296,10 @@ impl MainState {
300 296
             scene_item.meta_events.drain(..);
301 297
 
302 298
             if let Some(next_order) = &scene_item.next_order {
303
-                // Compute here if it possible (fear, compatible with current order, etc)
299
+                // TODO: Compute here if it possible (fear, compatible with current order, etc)
304 300
                 match next_order {
305 301
                     Order::MoveTo(move_to_scene_point) => {
306 302
                         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));
310 303
                     }
311 304
                 }
312 305
                 scene_item.next_order = None;
@@ -316,11 +309,43 @@ impl MainState {
316 309
             if let Some(current_order) = &scene_item.current_order {
317 310
                 match current_order {
318 311
                     Order::MoveTo(move_to_scene_point) => {
319
-                        let move_vector = (*move_to_scene_point - scene_item.position).normalize() * 1.0;
320
-                        println!("{:?}", move_vector);
312
+                        let change_to_walk = match scene_item.state.current_behavior {
313
+                            ItemBehavior::Standing(_) => true,
314
+                            ItemBehavior::CrawlingTo(_) => true,
315
+                            ItemBehavior::WalkingTo(_) => false,
316
+                        };
317
+
318
+                        if change_to_walk {
319
+                            scene_item.state =
320
+                                ItemState::new(ItemBehavior::WalkingTo(*move_to_scene_point));
321
+                        }
321 322
                     }
322 323
                 }
323 324
             }
325
+
326
+            match scene_item.state.current_behavior {
327
+                ItemBehavior::Standing(_) => {}
328
+                ItemBehavior::CrawlingTo(scene_point) => {
329
+                    let angle = f32::atan2(
330
+                        scene_point.y - scene_item.position.y,
331
+                        scene_point.x - scene_item.position.x,
332
+                    ) + FRAC_PI_2;
333
+                    scene_item.display_angle = angle;
334
+
335
+                    let move_vector = (scene_point - scene_item.position).normalize() * 1.0;
336
+                    println!("{:?}", move_vector);
337
+                }
338
+                ItemBehavior::WalkingTo(scene_point) => {
339
+                    let angle = f32::atan2(
340
+                        scene_point.y - scene_item.position.y,
341
+                        scene_point.x - scene_item.position.x,
342
+                    ) + FRAC_PI_2;
343
+                    scene_item.display_angle = angle;
344
+
345
+                    let move_vector = (scene_point - scene_item.position).normalize() * 1.0;
346
+                    println!("{:?}", move_vector);
347
+                }
348
+            }
324 349
         }
325 350
     }
326 351