4 Incheckningar d51496dc1d ... 0693d0cc79

Upphovsman SHA1 Meddelande Datum
  Bastien Sevajol 0693d0cc79 move 3 år sedan
  Bastien Sevajol de24cb92f5 move 3 år sedan
  Bastien Sevajol df060d5a1c clean code and print angle when click move 3 år sedan
  Bastien Sevajol c9a65474ac start order move code 3 år sedan
6 ändrade filer med 135 tillägg och 54 borttagningar
  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 Visa fil

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

+ 6 - 0
src/behavior/order.rs Visa fil

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

+ 0 - 1
src/main.rs Visa fil

@@ -12,7 +12,6 @@ mod physics;
12 12
 mod scene;
13 13
 mod ui;
14 14
 
15
-type Point2 = Vec2;
16 15
 type WindowPoint = Vec2;
17 16
 type Offset = Vec2;
18 17
 type ScenePoint = Vec2;

+ 11 - 4
src/scene/item.rs Visa fil

@@ -1,11 +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
-use crate::{Offset, Point2, ScenePoint};
9
+use crate::{Offset, ScenePoint};
9 10
 
10 11
 pub struct SceneItemSpriteInfo {
11 12
     pub relative_start_y: f32,
@@ -61,6 +62,9 @@ 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>,
67
+    pub display_angle: f32,
64 68
 }
65 69
 
66 70
 impl SceneItem {
@@ -72,6 +76,9 @@ impl SceneItem {
72 76
             state,
73 77
             meta_events: vec![],
74 78
             current_frame: 0,
79
+            current_order: None,
80
+            next_order: None,
81
+            display_angle: 0.0,
75 82
         }
76 83
     }
77 84
 
@@ -96,7 +103,7 @@ impl SceneItem {
96 103
                 sprite_info.relative_tile_width,
97 104
                 sprite_info.relative_tile_height,
98 105
             ))
99
-            .rotation(90.0f32.to_radians())
106
+            .rotation(self.display_angle)
100 107
             .offset(Offset::new(0.5, 0.5))
101 108
     }
102 109
 
@@ -104,8 +111,8 @@ impl SceneItem {
104 111
         // Here some logical about state, nature (soldier, tank, ...) and current behavior to
105 112
         // determine sprite type
106 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 116
             ItemBehavior::Standing(_) => SpriteType::StandingSoldier,
110 117
         }
111 118
     }

+ 109 - 42
src/scene/main.rs Visa fil

@@ -7,6 +7,7 @@ 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::order::Order;
10 11
 use crate::behavior::ItemBehavior;
11 12
 use crate::config::{
12 13
     ANIMATE_EACH, DEBUG, DEFAULT_SELECTED_SQUARE_SIDE, DEFAULT_SELECTED_SQUARE_SIDE_HALF,
@@ -21,6 +22,7 @@ use crate::scene::item::{ItemState, SceneItem, SceneItemType};
21 22
 use crate::ui::scene_item_menu::SceneItemMenuItem;
22 23
 use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
23 24
 use crate::{Offset, ScenePoint, WindowPoint};
25
+use std::f32::consts::FRAC_PI_2;
24 26
 
25 27
 pub struct MainState {
26 28
     // time
@@ -61,16 +63,16 @@ impl MainState {
61 63
         let mut scene_items = vec![];
62 64
         for x in 0..1 {
63 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 72
                 scene_items.push(SceneItem::new(
71 73
                     SceneItemType::Soldier,
72 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,6 +107,18 @@ impl MainState {
105 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 122
     fn inputs(&mut self, ctx: &Context) {
109 123
         let display_offset_by =
110 124
             if input::keyboard::is_mod_active(ctx, input::keyboard::KeyMods::SHIFT) {
@@ -149,6 +163,13 @@ impl MainState {
149 163
 
150 164
         if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
151 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 173
             self.scene_item_prepare_order = None;
153 174
         }
154 175
 
@@ -157,10 +178,7 @@ impl MainState {
157 178
             let window_menu_point =
158 179
                 window_point_from_scene_point(&scene_menu_point, &self.display_offset);
159 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 182
             if window_click_point.x >= window_menu_point.x
165 183
                 && window_click_point.x <= window_menu_point.x + menu_sprite_info.width
166 184
                 && window_click_point.y >= window_menu_point.y
@@ -197,10 +215,7 @@ impl MainState {
197 215
             self.get_first_scene_item_for_scene_point(&scene_right_click_point)
198 216
         {
199 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 219
                 self.scene_item_menu = Some((scene_item_usize, scene_item.position))
205 220
             }
206 221
         }
@@ -219,9 +234,12 @@ impl MainState {
219 234
         // Scene items movements
220 235
         for scene_item in self.scene_items.iter_mut() {
221 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 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 243
                     scene_item.grid_position =
226 244
                         util::grid_position_from_scene_point(&scene_item.position);
227 245
                 }
@@ -251,31 +269,83 @@ impl MainState {
251 269
         // TODO: ici il faut reflechir a comment organiser les comportements
252 270
 
253 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,7 +470,7 @@ impl MainState {
400 470
         mut mesh_builder: MeshBuilder,
401 471
     ) -> GameResult<MeshBuilder> {
402 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 474
             mesh_builder.rectangle(
405 475
                 DrawMode::Stroke(StrokeOptions::default()),
406 476
                 graphics::Rect::new(
@@ -449,10 +519,7 @@ impl MainState {
449 519
         if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
450 520
             match scene_item_prepare_order {
451 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 523
                     mesh_builder.line(
457 524
                         &vec![
458 525
                             scene_item.position.clone(),
@@ -521,9 +588,9 @@ impl event::EventHandler for MainState {
521 588
         graphics::clear(ctx, graphics::BLACK);
522 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 595
         scene_mesh_builder = self.update_mesh_builder_with_debug(scene_mesh_builder)?;
529 596
         scene_mesh_builder = self.update_mesh_builder_with_selected_items(scene_mesh_builder)?;
@@ -547,7 +614,7 @@ impl event::EventHandler for MainState {
547 614
 
548 615
         graphics::present(ctx)?;
549 616
 
550
-        println!("FPS: {}", ggez::timer::fps(ctx));
617
+        // println!("FPS: {}", ggez::timer::fps(ctx));
551 618
         Ok(())
552 619
     }
553 620
 

+ 4 - 4
src/ui/mod.rs Visa fil

@@ -3,7 +3,7 @@ use ggez::graphics;
3 3
 use crate::config::{UI_SPRITE_SHEET_HEIGHT, UI_SPRITE_SHEET_WIDTH};
4 4
 use crate::scene::item::SceneItem;
5 5
 use crate::ui::scene_item_menu::SceneItemMenuItem;
6
-use crate::{Point2, WindowPoint};
6
+use crate::WindowPoint;
7 7
 
8 8
 pub mod scene_item_menu;
9 9
 
@@ -45,9 +45,9 @@ impl UiSpriteInfo {
45 45
 
46 46
     pub fn which_item_clicked(
47 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 51
     ) -> Option<SceneItemMenuItem> {
52 52
         Some(SceneItemMenuItem::Move)
53 53
     }