Ver código fonte

grid position computed with map infos

Bastien Sevajol 3 anos atrás
pai
commit
41dfbb6dc7
4 arquivos alterados com 21 adições e 17 exclusões
  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 Ver arquivo

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

+ 4 - 4
src/physics/util.rs Ver arquivo

@@ -1,11 +1,11 @@
1
-use crate::config::{GRID_TILE_HEIGHT, GRID_TILE_WIDTH};
1
+use crate::map::Map;
2 2
 use crate::physics::GridPosition;
3 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 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 Ver arquivo

@@ -3,6 +3,7 @@ use ggez::graphics;
3 3
 use crate::behavior::order::Order;
4 4
 use crate::behavior::ItemBehavior;
5 5
 use crate::config::{SCENE_ITEMS_SPRITE_SHEET_HEIGHT, SCENE_ITEMS_SPRITE_SHEET_WIDTH};
6
+use crate::map::Map;
6 7
 use crate::physics::GridPosition;
7 8
 use crate::physics::{util, MetaEvent};
8 9
 use crate::scene::SpriteType;
@@ -70,11 +71,11 @@ pub struct SceneItem {
70 71
 }
71 72
 
72 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 75
         Self {
75 76
             type_,
76 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 79
             state,
79 80
             meta_events: vec![],
80 81
             current_frame: 0.0,

+ 12 - 11
src/scene/main.rs Ver arquivo

@@ -123,10 +123,20 @@ impl MainState {
123 123
                     SceneItemType::Soldier,
124 124
                     ScenePoint::new((x as f32 * 24.0) + 100.0, (y as f32 * 24.0) + 100.0),
125 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 140
         let mut main_state = MainState {
131 141
             frame_i: 0,
132 142
             start: Instant::now(),
@@ -139,7 +149,7 @@ impl MainState {
139 149
             ui_batch,
140 150
             terrain_batch,
141 151
             scene_items,
142
-            scene_items_by_grid_position: HashMap::new(),
152
+            scene_items_by_grid_position,
143 153
             physics_events: vec![],
144 154
             last_key_consumed: HashMap::new(),
145 155
             left_click_down: None,
@@ -151,15 +161,6 @@ impl MainState {
151 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 164
         Ok(main_state)
164 165
     }
165 166
 
@@ -346,7 +347,7 @@ impl MainState {
346 347
                     scene_item.position.x += move_vector.x;
347 348
                     scene_item.position.y += move_vector.y;
348 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
         }