Browse Source

grid position computed with map infos

Bastien Sevajol 3 years ago
parent
commit
41dfbb6dc7
4 changed files with 21 additions and 17 deletions
  1. 2 0
      src/map/mod.rs
  2. 4 4
      src/physics/util.rs
  3. 3 2
      src/scene/item.rs
  4. 12 11
      src/scene/main.rs

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

68
     pub tiled_map: TiledMap,
68
     pub tiled_map: TiledMap,
69
     pub background_image: TiledImage,
69
     pub background_image: TiledImage,
70
     pub terrain_image: TiledImage,
70
     pub terrain_image: TiledImage,
71
+    pub terrain_tileset: Tileset,
71
     pub tiles: HashMap<(u32, u32), Tile>,
72
     pub tiles: HashMap<(u32, u32), Tile>,
72
 }
73
 }
73
 
74
 
170
             tiled_map: tiled_map.clone(),
171
             tiled_map: tiled_map.clone(),
171
             background_image: background_image.clone(),
172
             background_image: background_image.clone(),
172
             terrain_image,
173
             terrain_image,
174
+            terrain_tileset,
173
             tiles,
175
             tiles,
174
         })
176
         })
175
     }
177
     }

+ 4 - 4
src/physics/util.rs View File

1
-use crate::config::{GRID_TILE_HEIGHT, GRID_TILE_WIDTH};
1
+use crate::map::Map;
2
 use crate::physics::GridPosition;
2
 use crate::physics::GridPosition;
3
 use crate::{ScenePoint, WindowPoint};
3
 use crate::{ScenePoint, WindowPoint};
4
 
4
 
5
-pub fn grid_position_from_scene_point(position: &ScenePoint) -> GridPosition {
5
+pub fn grid_position_from_scene_point(position: &ScenePoint, map: &Map) -> GridPosition {
6
     GridPosition::new(
6
     GridPosition::new(
7
-        (position.x / GRID_TILE_WIDTH) as i32,
8
-        (position.y / GRID_TILE_HEIGHT) as i32,
7
+        (position.x / map.terrain_tileset.tile_width as f32) as i32,
8
+        (position.y / map.terrain_tileset.tile_height as f32) as i32,
9
     )
9
     )
10
 }
10
 }
11
 
11
 

+ 3 - 2
src/scene/item.rs View File

3
 use crate::behavior::order::Order;
3
 use crate::behavior::order::Order;
4
 use crate::behavior::ItemBehavior;
4
 use crate::behavior::ItemBehavior;
5
 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};
6
+use crate::map::Map;
6
 use crate::physics::GridPosition;
7
 use crate::physics::GridPosition;
7
 use crate::physics::{util, MetaEvent};
8
 use crate::physics::{util, MetaEvent};
8
 use crate::scene::SpriteType;
9
 use crate::scene::SpriteType;
70
 }
71
 }
71
 
72
 
72
 impl SceneItem {
73
 impl SceneItem {
73
-    pub fn new(type_: SceneItemType, position: ScenePoint, state: ItemState) -> Self {
74
+    pub fn new(type_: SceneItemType, position: ScenePoint, state: ItemState, map: &Map) -> Self {
74
         Self {
75
         Self {
75
             type_,
76
             type_,
76
             position: position.clone(),
77
             position: position.clone(),
77
-            grid_position: util::grid_position_from_scene_point(&position.clone()),
78
+            grid_position: util::grid_position_from_scene_point(&position.clone(), map),
78
             state,
79
             state,
79
             meta_events: vec![],
80
             meta_events: vec![],
80
             current_frame: 0.0,
81
             current_frame: 0.0,

+ 12 - 11
src/scene/main.rs View File

123
                     SceneItemType::Soldier,
123
                     SceneItemType::Soldier,
124
                     ScenePoint::new((x as f32 * 24.0) + 100.0, (y as f32 * 24.0) + 100.0),
124
                     ScenePoint::new((x as f32 * 24.0) + 100.0, (y as f32 * 24.0) + 100.0),
125
                     ItemState::new(ItemBehavior::Standing),
125
                     ItemState::new(ItemBehavior::Standing),
126
+                    &map,
126
                 ));
127
                 ));
127
             }
128
             }
128
         }
129
         }
129
 
130
 
131
+        let mut scene_items_by_grid_position: HashMap<GridPosition, Vec<usize>> = HashMap::new();
132
+        for (i, scene_item) in scene_items.iter().enumerate() {
133
+            let grid_position = util::grid_position_from_scene_point(&scene_item.position, &map);
134
+            scene_items_by_grid_position
135
+                .entry(grid_position)
136
+                .or_default()
137
+                .push(i);
138
+        }
139
+
130
         let mut main_state = MainState {
140
         let mut main_state = MainState {
131
             frame_i: 0,
141
             frame_i: 0,
132
             start: Instant::now(),
142
             start: Instant::now(),
139
             ui_batch,
149
             ui_batch,
140
             terrain_batch,
150
             terrain_batch,
141
             scene_items,
151
             scene_items,
142
-            scene_items_by_grid_position: HashMap::new(),
152
+            scene_items_by_grid_position,
143
             physics_events: vec![],
153
             physics_events: vec![],
144
             last_key_consumed: HashMap::new(),
154
             last_key_consumed: HashMap::new(),
145
             left_click_down: None,
155
             left_click_down: None,
151
             scene_item_prepare_order: None,
161
             scene_item_prepare_order: None,
152
         };
162
         };
153
 
163
 
154
-        for (i, scene_item) in main_state.scene_items.iter().enumerate() {
155
-            let grid_position = util::grid_position_from_scene_point(&scene_item.position);
156
-            main_state
157
-                .scene_items_by_grid_position
158
-                .entry(grid_position)
159
-                .or_default()
160
-                .push(i);
161
-        }
162
-
163
         Ok(main_state)
164
         Ok(main_state)
164
     }
165
     }
165
 
166
 
346
                     scene_item.position.x += move_vector.x;
347
                     scene_item.position.x += move_vector.x;
347
                     scene_item.position.y += move_vector.y;
348
                     scene_item.position.y += move_vector.y;
348
                     scene_item.grid_position =
349
                     scene_item.grid_position =
349
-                        util::grid_position_from_scene_point(&scene_item.position);
350
+                        util::grid_position_from_scene_point(&scene_item.position, &self.map);
350
                 }
351
                 }
351
             }
352
             }
352
         }
353
         }