4 Commits d51496dc1d ... 0693d0cc79

Author SHA1 Message Date
  Bastien Sevajol 0693d0cc79 move 3 years ago
  Bastien Sevajol de24cb92f5 move 3 years ago
  Bastien Sevajol df060d5a1c clean code and print angle when click move 3 years ago
  Bastien Sevajol c9a65474ac start order move code 3 years ago
6 changed files with 135 additions and 54 deletions
  1. 5 3
      src/behavior/mod.rs
  2. 6 0
      src/behavior/order.rs
  3. 0 1
      src/main.rs
  4. 11 4
      src/scene/item.rs
  5. 109 42
      src/scene/main.rs
  6. 4 4
      src/ui/mod.rs

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

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

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

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

+ 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;

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

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

+ 109 - 42
src/scene/main.rs View File

7
 use ggez::timer::check_update_time;
7
 use ggez::timer::check_update_time;
8
 use ggez::{event, graphics, input, Context, GameResult};
8
 use ggez::{event, graphics, input, Context, GameResult};
9
 
9
 
10
+use crate::behavior::order::Order;
10
 use crate::behavior::ItemBehavior;
11
 use crate::behavior::ItemBehavior;
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,
21
 use crate::ui::scene_item_menu::SceneItemMenuItem;
22
 use crate::ui::scene_item_menu::SceneItemMenuItem;
22
 use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
23
 use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
23
 use crate::{Offset, ScenePoint, WindowPoint};
24
 use crate::{Offset, ScenePoint, WindowPoint};
25
+use std::f32::consts::FRAC_PI_2;
24
 
26
 
25
 pub struct MainState {
27
 pub struct MainState {
26
     // time
28
     // time
61
         let mut scene_items = vec![];
63
         let mut scene_items = vec![];
62
         for x in 0..1 {
64
         for x in 0..1 {
63
             for y in 0..4 {
65
             for y in 0..4 {
64
-                let current_behavior = if y % 2 == 0 {
65
-                    ItemBehavior::Walking(util::vec_from_angle(90.0))
66
-                } else {
67
-                    ItemBehavior::Crawling
68
-                };
66
+                // let current_behavior = if y % 2 == 0 {
67
+                //     ItemBehavior::WalkingTo(util::vec_from_angle(90.0))
68
+                // } else {
69
+                //     ItemBehavior::CrawlingTo()
70
+                // };
69
 
71
 
70
                 scene_items.push(SceneItem::new(
72
                 scene_items.push(SceneItem::new(
71
                     SceneItemType::Soldier,
73
                     SceneItemType::Soldier,
72
                     ScenePoint::new((x as f32 * 24.0) + 100.0, (y as f32 * 24.0) + 100.0),
74
                     ScenePoint::new((x as f32 * 24.0) + 100.0, (y as f32 * 24.0) + 100.0),
73
-                    ItemState::new(current_behavior),
75
+                    ItemState::new(ItemBehavior::Standing(0)),
74
                 ));
76
                 ));
75
             }
77
             }
76
         }
78
         }
105
         Ok(main_state)
107
         Ok(main_state)
106
     }
108
     }
107
 
109
 
110
+    fn get_scene_item(&self, index: usize) -> &SceneItem {
111
+        self.scene_items
112
+            .get(index)
113
+            .expect(SCENE_ITEMS_CHANGE_ERR_MSG)
114
+    }
115
+
116
+    fn get_scene_item_mut(&mut self, index: usize) -> &mut SceneItem {
117
+        self.scene_items
118
+            .get_mut(index)
119
+            .expect(SCENE_ITEMS_CHANGE_ERR_MSG)
120
+    }
121
+
108
     fn inputs(&mut self, ctx: &Context) {
122
     fn inputs(&mut self, ctx: &Context) {
109
         let display_offset_by =
123
         let display_offset_by =
110
             if input::keyboard::is_mod_active(ctx, input::keyboard::KeyMods::SHIFT) {
124
             if input::keyboard::is_mod_active(ctx, input::keyboard::KeyMods::SHIFT) {
149
 
163
 
150
         if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
164
         if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
151
             // TODO: Add order to scene_item
165
             // TODO: Add order to scene_item
166
+            match scene_item_prepare_order {
167
+                SceneItemPrepareOrder::Move(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
+                }
171
+            }
172
+
152
             self.scene_item_prepare_order = None;
173
             self.scene_item_prepare_order = None;
153
         }
174
         }
154
 
175
 
157
             let window_menu_point =
178
             let window_menu_point =
158
                 window_point_from_scene_point(&scene_menu_point, &self.display_offset);
179
                 window_point_from_scene_point(&scene_menu_point, &self.display_offset);
159
             let menu_sprite_info = UiSpriteInfo::from_type(UiItem::SceneItemMenu);
180
             let menu_sprite_info = UiSpriteInfo::from_type(UiItem::SceneItemMenu);
160
-            let scene_item = self
161
-                .scene_items
162
-                .get(scene_item_usize)
163
-                .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
181
+            let scene_item = self.get_scene_item(scene_item_usize);
164
             if window_click_point.x >= window_menu_point.x
182
             if window_click_point.x >= window_menu_point.x
165
                 && window_click_point.x <= window_menu_point.x + menu_sprite_info.width
183
                 && window_click_point.x <= window_menu_point.x + menu_sprite_info.width
166
                 && window_click_point.y >= window_menu_point.y
184
                 && window_click_point.y >= window_menu_point.y
197
             self.get_first_scene_item_for_scene_point(&scene_right_click_point)
215
             self.get_first_scene_item_for_scene_point(&scene_right_click_point)
198
         {
216
         {
199
             if self.selected_scene_items.contains(&scene_item_usize) {
217
             if self.selected_scene_items.contains(&scene_item_usize) {
200
-                let scene_item = self
201
-                    .scene_items
202
-                    .get(scene_item_usize)
203
-                    .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
218
+                let scene_item = self.get_scene_item(scene_item_usize);
204
                 self.scene_item_menu = Some((scene_item_usize, scene_item.position))
219
                 self.scene_item_menu = Some((scene_item_usize, scene_item.position))
205
             }
220
             }
206
         }
221
         }
219
         // Scene items movements
234
         // Scene items movements
220
         for scene_item in self.scene_items.iter_mut() {
235
         for scene_item in self.scene_items.iter_mut() {
221
             match scene_item.state.current_behavior {
236
             match scene_item.state.current_behavior {
222
-                ItemBehavior::Walking(vector) => {
237
+                ItemBehavior::WalkingTo(scene_point) => {
238
+                    // FIXME BS NOW: velocity
239
+                    let move_vector = (scene_point - scene_item.position).normalize() * 1.0;
223
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
240
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
224
-                    scene_item.position.x += 1.0;
241
+                    scene_item.position.x += move_vector.x;
242
+                    scene_item.position.y += move_vector.y;
225
                     scene_item.grid_position =
243
                     scene_item.grid_position =
226
                         util::grid_position_from_scene_point(&scene_item.position);
244
                         util::grid_position_from_scene_point(&scene_item.position);
227
                 }
245
                 }
251
         // TODO: ici il faut reflechir a comment organiser les comportements
269
         // TODO: ici il faut reflechir a comment organiser les comportements
252
 
270
 
253
         for scene_item in self.scene_items.iter_mut() {
271
         for scene_item in self.scene_items.iter_mut() {
254
-            for meta_event in &scene_item.meta_events {
255
-                match meta_event {
256
-                    MetaEvent::FeelExplosion => {
257
-                        scene_item.state = ItemState::new(ItemBehavior::Standing(self.frame_i));
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
+            // }
279
+
280
+            // match scene_item.state.current_behavior {
281
+            //     ItemBehavior::Crawling => {
282
+            //         scene_item.state =
283
+            //             ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
284
+            //     }
285
+            //     ItemBehavior::Walking(_) => {
286
+            //         scene_item.state = ItemState::new(ItemBehavior::Crawling);
287
+            //     }
288
+            //     ItemBehavior::Standing(since) => {
289
+            //         if self.frame_i - since >= 120 {
290
+            //             scene_item.state =
291
+            //                 ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
292
+            //         }
293
+            //     }
294
+            // }
295
+
296
+            scene_item.meta_events.drain(..);
297
+
298
+            if let Some(next_order) = &scene_item.next_order {
299
+                // TODO: Compute here if it possible (fear, compatible with current order, etc)
300
+                match next_order {
301
+                    Order::MoveTo(move_to_scene_point) => {
302
+                        scene_item.current_order = Some(Order::MoveTo(*move_to_scene_point));
258
                     }
303
                     }
259
                 }
304
                 }
305
+                scene_item.next_order = None;
260
             }
306
             }
261
 
307
 
262
-            match scene_item.state.current_behavior {
263
-                ItemBehavior::Crawling => {
264
-                    scene_item.state =
265
-                        ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
266
-                }
267
-                ItemBehavior::Walking(_) => {
268
-                    scene_item.state = ItemState::new(ItemBehavior::Crawling);
269
-                }
270
-                ItemBehavior::Standing(since) => {
271
-                    if self.frame_i - since >= 120 {
272
-                        scene_item.state =
273
-                            ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
308
+            // FIXME BS NOW: stop move when move is accomplished; warn: recompute move_vector here
309
+            if let Some(current_order) = &scene_item.current_order {
310
+                match current_order {
311
+                    Order::MoveTo(move_to_scene_point) => {
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
+                        }
274
                     }
322
                     }
275
                 }
323
                 }
276
             }
324
             }
277
 
325
 
278
-            scene_item.meta_events.drain(..);
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
+            }
279
         }
349
         }
280
     }
350
     }
281
 
351
 
400
         mut mesh_builder: MeshBuilder,
470
         mut mesh_builder: MeshBuilder,
401
     ) -> GameResult<MeshBuilder> {
471
     ) -> GameResult<MeshBuilder> {
402
         for i in &self.selected_scene_items {
472
         for i in &self.selected_scene_items {
403
-            let selected_scene_item = self.scene_items.get(*i).expect(SCENE_ITEMS_CHANGE_ERR_MSG);
473
+            let selected_scene_item = self.get_scene_item(*i);
404
             mesh_builder.rectangle(
474
             mesh_builder.rectangle(
405
                 DrawMode::Stroke(StrokeOptions::default()),
475
                 DrawMode::Stroke(StrokeOptions::default()),
406
                 graphics::Rect::new(
476
                 graphics::Rect::new(
449
         if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
519
         if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
450
             match scene_item_prepare_order {
520
             match scene_item_prepare_order {
451
                 SceneItemPrepareOrder::Move(scene_item_usize) => {
521
                 SceneItemPrepareOrder::Move(scene_item_usize) => {
452
-                    let scene_item = self
453
-                        .scene_items
454
-                        .get(*scene_item_usize)
455
-                        .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
522
+                    let scene_item = self.get_scene_item(*scene_item_usize);
456
                     mesh_builder.line(
523
                     mesh_builder.line(
457
                         &vec![
524
                         &vec![
458
                             scene_item.position.clone(),
525
                             scene_item.position.clone(),
521
         graphics::clear(ctx, graphics::BLACK);
588
         graphics::clear(ctx, graphics::BLACK);
522
         let mut scene_mesh_builder = MeshBuilder::new();
589
         let mut scene_mesh_builder = MeshBuilder::new();
523
 
590
 
524
-        self.generate_scene_item_sprites();
525
-        self.generate_scene_item_menu_sprites();
526
-        self.generate_map_sprites();
591
+        self.generate_scene_item_sprites()?;
592
+        self.generate_scene_item_menu_sprites()?;
593
+        self.generate_map_sprites()?;
527
 
594
 
528
         scene_mesh_builder = self.update_mesh_builder_with_debug(scene_mesh_builder)?;
595
         scene_mesh_builder = self.update_mesh_builder_with_debug(scene_mesh_builder)?;
529
         scene_mesh_builder = self.update_mesh_builder_with_selected_items(scene_mesh_builder)?;
596
         scene_mesh_builder = self.update_mesh_builder_with_selected_items(scene_mesh_builder)?;
547
 
614
 
548
         graphics::present(ctx)?;
615
         graphics::present(ctx)?;
549
 
616
 
550
-        println!("FPS: {}", ggez::timer::fps(ctx));
617
+        // println!("FPS: {}", ggez::timer::fps(ctx));
551
         Ok(())
618
         Ok(())
552
     }
619
     }
553
 
620
 

+ 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
     }