Skip to content

Commit

Permalink
Handle non-unit enums (#58)
Browse files Browse the repository at this point in the history
*  Restrict Tiled exports only to Components and Resources #50 

*  Support non-unit enums user properties #49 

* fix: prevent infinite loop in case an asset fails to load

* feat: improve how we handle default values for user properties

---------

Co-authored-by: Niashi <[email protected]>
  • Loading branch information
adrien-bon and Niashi24 authored Dec 14, 2024
1 parent b9bf394 commit 9fe428e
Show file tree
Hide file tree
Showing 10 changed files with 4,707 additions and 3,413 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ A [migration guide](https://adrien-bon.github.io/bevy_ecs_tiled/migrations/v0_5.

- Update to Bevy 0.15
- Support Tiled image layers (#16)
- Add support for non-unit enums user properties (#49)
- Better handling for user properties default values: you should now be able to load
properties without having to fully define them

### Changed

- Remove `map_initial_transform` and `map_initial_visibility` from `TiledMapSettings`, use required components instead
- Simplify map reload mechanisms and update corresponding example
- Only export custom properties that are related to either a Component, a Bundle or a Resource (#50)
This should reduce the clutter when adding a new property

### Bugfixes

- Do not skip last frame for animated tiles (#61)
- Make sure all custom properties we export have the 'useAs property' flag set (and only this one)
Not having this flag was preventing to use some some of the properties
- Prevent infinite loop in case of an asset loading error

## v0.4.2

Expand Down
103 changes: 100 additions & 3 deletions assets/hex_map_pointy_top_odd.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.11.0" orientation="hexagonal" renderorder="right-down" width="20" height="20" tilewidth="16" tileheight="16" infinite="0" hexsidelength="8" staggeraxis="y" staggerindex="odd" nextlayerid="5" nextobjectid="5">
<map version="1.10" tiledversion="1.11.0" orientation="hexagonal" renderorder="right-down" width="20" height="20" tilewidth="16" tileheight="16" infinite="0" hexsidelength="8" staggeraxis="y" staggerindex="odd" nextlayerid="5" nextobjectid="14">
<tileset firstgid="1" source="drjamgo_hex_16x16.tsx"/>
<imagelayer id="4" name="Image Layer 1" offsetx="134.27" offsety="-26.9557">
<image source="bevy_icon.png" width="256" height="256"/>
Expand Down Expand Up @@ -31,15 +31,112 @@
<objectgroup id="3" name="Object Layer ">
<object id="1" name="Player spawn" x="98.0258" y="124.343" width="11.1064" height="7.24328">
<properties>
<property name="SpawnInfos" type="class"/>
<property name="SpawnInfos" type="class" propertytype="properties_basic::SpawnInfos">
<properties>
<property name="ty" type="class" propertytype="properties_basic::SpawnType">
<properties>
<property name=":variant" propertytype="properties_basic::SpawnType:::Variant" value="Player"/>
<property name="Friendly" type="class" propertytype="properties_basic::SpawnType::Friendly">
<properties>
<property name="0" type="int" value="12"/>
</properties>
</property>
<property name="Player" type="class" propertytype="properties_basic::SpawnType::Player">
<properties>
<property name="0" type="int" value="14"/>
<property name="color" type="color" value="#ff82d4e7"/>
<property name="id" type="int" value="23"/>
</properties>
</property>
</properties>
</property>
</properties>
</property>
</properties>
</object>
<object id="4" name="Enemy spawn" x="180.358" y="147.522" width="9.89915" height="9.65771">
<properties>
<property name="SpawnInfos" type="class"/>
<property name="SpawnInfos" type="class" propertytype="properties_basic::SpawnInfos">
<properties>
<property name="ty" type="class" propertytype="properties_basic::SpawnType">
<properties>
<property name=":variant" propertytype="properties_basic::SpawnType:::Variant" value="Enemy"/>
<property name="Enemy" type="class" propertytype="properties_basic::SpawnType::Enemy">
<properties>
<property name="0" type="color" value="#ffec0527"/>
</properties>
</property>
<property name="Player" type="class" propertytype="properties_basic::SpawnType::Player">
<properties>
<property name="id" type="int" value="24"/>
</properties>
</property>
</properties>
</property>
</properties>
</property>
</properties>
<ellipse/>
</object>
<object id="5" name="Friendly village #1" x="177" y="123.375">
<properties>
<property name="SpawnInfos" type="class" propertytype="properties_basic::SpawnInfos">
<properties>
<property name="ty" type="class" propertytype="properties_basic::SpawnType">
<properties>
<property name=":variant" propertytype="properties_basic::SpawnType:::Variant" value="Friendly"/>
<property name="Friendly" type="class" propertytype="properties_basic::SpawnType::Friendly">
<properties>
<property name="0" type="int" value="45"/>
</properties>
</property>
<property name="Player" type="class" propertytype="properties_basic::SpawnType::Player">
<properties>
<property name="id" type="int" value="123"/>
</properties>
</property>
</properties>
</property>
</properties>
</property>
</properties>
<point/>
</object>
<object id="6" name="Friendly village #2" x="144.5" y="148.125">
<properties>
<property name="SpawnInfos" type="class" propertytype="properties_basic::SpawnInfos">
<properties>
<property name="ty" type="class" propertytype="properties_basic::SpawnType">
<properties>
<property name=":variant" propertytype="properties_basic::SpawnType:::Variant" value="Friendly"/>
</properties>
</property>
</properties>
</property>
</properties>
<point/>
</object>
<object id="7" name="Unknown spawn" x="105.5" y="152.5">
<properties>
<property name="SpawnInfos" type="class" propertytype="properties_basic::SpawnInfos"/>
</properties>
<point/>
</object>
<object id="11" x="145.75" y="5.75">
<properties>
<property name="Text2d" type="class" propertytype="bevy_text::text2d::Text2d">
<properties>
<property name="0" value="This Text2d comes directly from Tiled !"/>
</properties>
</property>
<property name="TextColor" type="class" propertytype="bevy_text::text::TextColor">
<properties>
<property name="0" type="color" value="#ffff1d21"/>
</properties>
</property>
</properties>
<point/>
</object>
</objectgroup>
<layer id="2" name="BuildingsLayer" width="20" height="20">
<data encoding="csv">
Expand Down
13 changes: 7 additions & 6 deletions book/src/guides/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ In addition to this guide, there is also a [dedicated example](https://github.co
Your Tiled map, layer, tile or object will be represented by a Bevy `Entity`.
So, it makes sense that if you want to add custom properties to them, these properties should either be a `Component` or a `Bundle`.

Also, Tiled custom properties use Bevy `Reflect` mechanism.
So, in order to be usable in Tiled, your custom types must be "Reflectable".
Tiled custom properties use Bevy `Reflect` mechanism.
In order to be usable in Tiled, your custom types must be "Reflectable".
To do, these types must derive the `Reflect` trait and get registered with Bevy.

```rust, no_run
Expand Down Expand Up @@ -56,13 +56,13 @@ fn main() {
}
```

And that's all !
And that's all for the code part !

Note that in the above example, our custom type also derive the `Default` trait.
It is particulary useful to do so: if you don't, you would have to fill all the fields of your custom type when you use it in Tiled.

Finally, note that you can also add `Resource` to your map.
They won't be attached to a particular entity and as such are only allowed on Tiled maps.
Since `Resource`s are not attached to a particular entity and they are shared accros your app, we chose to restrict their usage only as Tiled map properties.

## Add custom properties to your map

Expand All @@ -87,5 +87,6 @@ Note that it concerns all the types that derive the `Reflect` trait: there can b
![view-custom-types](images/properties_custom-type.png)

You can now add them to different elements of your map, like tiles objects, layers or the map itself.
For more information on how to do add custom properties, see the [official TIled documentation](https://doc.mapeditor.org/en/stable/manual/custom-properties/).
You should only add properties imported from Bevy: adding ones that you created only in Tiled will not be loaded.
For more information on how to do add custom properties, see the [official Tiled documentation](https://doc.mapeditor.org/en/stable/manual/custom-properties/).

You should only add properties imported from Bevy: adding ones that you created only in Tiled will not be loaded in Bevy if they do not contain actual Bevy `Component`s.
5 changes: 4 additions & 1 deletion book/src/migrations/v0_5.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
## Overview

Version 0.5 updates the crate for Bevy v0.15.
It notably takes advantage of the new `required_component` feature to simplify the crate API.

It also takes advantage of the new `required_component` feature to simplify the crate API.

Also, this version provide some fixes related to user properties.
If you are using them, you should reload your export file.

## Bevy v0.15 update

Expand Down
Loading

0 comments on commit 9fe428e

Please sign in to comment.