Skip to content

Commit

Permalink
physics + layers for tile map
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Jul 30, 2024
1 parent 9c88e45 commit 4f95419
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 32 deletions.
22 changes: 11 additions & 11 deletions src/code/snippets/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/code/snippets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
fyrox = { git = "https://github.com/FyroxEngine/Fyrox", rev = "c865306042407c686cf53c83c7d9c2471a460b3e" }
fyroxed_base = { git = "https://github.com/FyroxEngine/Fyrox", rev = "c865306042407c686cf53c83c7d9c2471a460b3e" }
fyrox = { git = "https://github.com/FyroxEngine/Fyrox", rev = "7b25f44d96e289abf36c3a616ba474bbe6d5f428" }
fyroxed_base = { git = "https://github.com/FyroxEngine/Fyrox", rev = "7b25f44d96e289abf36c3a616ba474bbe6d5f428" }
strum = "0.26.0"
strum_macros = "0.26.0"
14 changes: 1 addition & 13 deletions src/code/snippets/src/resource/custom.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use fyrox::asset::io::ResourceIo;
use fyrox::asset::loader::LoaderPayload;
use fyrox::asset::manager::ResourceManager;
use fyrox::asset::state::LoadError;
use fyrox::core::futures::executor::block_on;
use fyrox::plugin::{Plugin, PluginRegistrationContext};
use fyrox::{
asset::{
Expand All @@ -11,7 +9,6 @@ use fyrox::{
},
core::{
io::{self},
parking_lot::Mutex,
reflect::prelude::*,
type_traits::prelude::*,
uuid::Uuid,
Expand Down Expand Up @@ -115,16 +112,7 @@ fn main() {

// Register property editor.
editor.inspector.property_editors.insert(
ResourceFieldPropertyEditorDefinition::<CustomResource>::new(
Arc::new(Mutex::new(
|resource_manager: &ResourceManager, path: &Path| {
resource_manager
.try_request::<CustomResource>(path)
.map(block_on)
},
)),
editor.message_sender.clone(),
),
ResourceFieldPropertyEditorDefinition::<CustomResource>::new(editor.message_sender.clone()),
);

// ...
Expand Down
19 changes: 19 additions & 0 deletions src/code/snippets/src/scene/tilemap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use fyrox::scene::dim2::collider::{ColliderBuilder, ColliderShape, GeometrySource, TileMapShape};
use fyrox::scene::dim2::rigidbody::RigidBodyBuilder;
use fyrox::scene::rigidbody::RigidBodyType;
use fyrox::{
asset::untyped::ResourceKind,
core::{algebra::Vector2, color::Color, math::Rect, pool::Handle},
Expand Down Expand Up @@ -62,3 +65,19 @@ fn create_tile_map(graph: &mut Graph) -> Handle<Node> {
.build(graph)
}
// ANCHOR_END: create_tile_map

// ANCHOR: tile_map_physics
fn add_tile_map_physics(tile_map: Handle<Node>, graph: &mut Graph) {
// Create a new collider with tile map shape.
let collider = ColliderBuilder::new(BaseBuilder::new())
.with_shape(ColliderShape::TileMap(TileMapShape {
tile_map: GeometrySource(tile_map),
}))
.build(graph);

// Create a static rigid body with the tile map collider.
let rigid_body = RigidBodyBuilder::new(BaseBuilder::new().with_children([collider]))
.with_body_type(RigidBodyType::Static)
.build(graph);
}
// ANCHOR_END: tile_map_physics
Binary file added src/scene/tile_map_physics.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 26 additions & 6 deletions src/scene/tilemap.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,40 +89,60 @@ Select some tiles on the palette and start drawing:

![drawing](drawing.png)

## Drawing Tools

There are number of tools (apart from the drawing itself) that could be useful while editing tile maps.

## Erase
### Erase

![erase](erase.gif)

Erases tiles using the shape of the current brush, could be activated using `Shift` key or by clicking on the
button with eraser icon.

## Flood fill
### Flood fill

![flood fill](flood_fill.gif)

Fills a region with the same tile kind (or empty space) using random tiles from the current brush. Could
be activated using the button with paint bucket icon.

## Pick
### Pick

![pick](pick.gif)

Picks a rectangular region of tiles from the tile map itself and turns them into the current brush. Could be
activated using `Alt` key or by clicking the button with pipette icon.

## Rectangular fill
### Rectangular fill

![rect fill](rect_fill.gif)

Fills a rectangular region with the tiles from the current brush. It tiles the given region using the
tiles from current brush. Could be activated using `Ctrl` key of by clicking on the button with the tiles icon.

## Nine slice
### Nine slice

![nine slice](nine_slice.gif)

Fills a rectangular region using a 3x3 brush (the size limitation could be dropped in the future). The
corners of the brush will be placed at the corners of the selected region, the middle tiles between corners will be
duplicated from corner to corner. The center tile will be used to fill the rest of the rectangle.
duplicated from corner to corner. The center tile will be used to fill the rest of the rectangle.

## Physics

Tile maps supports physics for tiles, and it could be enabled by using special collider shape called `TileMap`. In code
it could be done something like this:

```rust
{{#include ../code/snippets/src/scene/tilemap.rs:tile_map_physics}}
```

In the editor it could be done by creating a static 2D rigid body with a 2D collider that has `TileMap` shape:

![tile map physics](tile_map_physics.png)

## Layers

Tile map does not support any layers on its own, but layers could be added very easy by simply creating another tile
map with its own tile set and shifting this new layer by Z axis towards camera on some small value.

0 comments on commit 4f95419

Please sign in to comment.