Skip to content

Commit

Permalink
rect fill for tile map
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Jul 23, 2024
1 parent 304b30b commit 75de751
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions fyrox-impl/src/scene/tilemap/brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
},
scene::{debug::SceneDrawingContext, tilemap::tileset::TileDefinitionHandle},
};
use fyrox_core::math::Rect;
use std::{
any::Any,
error::Error,
Expand Down Expand Up @@ -118,6 +119,20 @@ pub struct TileMapBrush {
}

impl TileMapBrush {
/// Returns bounding rectangle of the tile map brush in grid coordinates.
#[inline]
pub fn bounding_rect(&self) -> Rect<i32> {
let mut min = Vector2::repeat(i32::MAX);
let mut max = Vector2::repeat(i32::MIN);

for tile in self.tiles.iter() {
min = tile.local_position.inf(&min);
max = tile.local_position.sup(&max);
}

Rect::from_points(min, max)
}

/// Draw brush outline to the scene drawing context.
pub fn draw_outline(
&self,
Expand Down
23 changes: 23 additions & 0 deletions fyrox-impl/src/scene/tilemap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,29 @@ impl TileMap {
}
}
}

/// Fills the given rectangle using the specified brush.
#[inline]
pub fn rect_fill(&mut self, rect: Rect<i32>, brush: &TileMapBrush) {
let brush_rect = brush.bounding_rect();
for y in
(rect.position.y..(rect.position.y + rect.size.y)).step_by(brush_rect.size.y as usize)
{
for x in (rect.position.x..(rect.position.x + rect.size.x))
.step_by(brush_rect.size.x as usize)
{
for brush_tile in brush.tiles.iter() {
let position = Vector2::new(x, y) + brush_tile.local_position;
if rect.contains(position) {
self.insert_tile(Tile {
position,
definition_handle: brush_tile.definition_handle,
});
}
}
}
}
}
}

impl Default for TileMap {
Expand Down

0 comments on commit 75de751

Please sign in to comment.