Skip to content

Commit

Permalink
finish MVP of embedded objects!
Browse files Browse the repository at this point in the history
rename and despawn recursive
  • Loading branch information
dmtaub committed Apr 29, 2021
1 parent 2a28c67 commit af25dca
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Individual objects that are invisible will be spawned with is_visible set to fal
### Example

To see objects and debugging in action, run the `ortho_debug` example which will enable debug viewing of objects.
Use the spacebar to toggle objects.
Use the spacebar to toggle objects. Rects within tiles are also shown.

```sh
# Runs the debug/objects example
Expand Down
249 changes: 249 additions & 0 deletions assets/ortho-debug.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.5" tiledversion="1.5.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="48">
<tileset firstgid="1" name="outdoor" tilewidth="16" tileheight="16" tilecount="288" columns="24">
<image source="ortho.png" width="384" height="192"/>
<tile id="6" probability="0.1"/>
<tile id="27" probability="0.05"/>
<tile id="28" probability="0.05"/>
<tile id="30" probability="0.1"/>
<tile id="51" probability="0.05"/>
<tile id="52" probability="0.05"/>
<tile id="54" probability="0.1"/>
<tile id="75" probability="0.05"/>
<tile id="76" probability="0.05"/>
<tile id="78" probability="0.1"/>
<tile id="82" probability="0.1"/>
<tile id="83" probability="0.1"/>
<tile id="99" probability="0.05"/>
<tile id="102" probability="0.1"/>
<tile id="106" probability="0.1"/>
<tile id="107" probability="0.1"/>
<tile id="126" probability="0.1"/>
<tile id="281">
<objectgroup draworder="index" id="2">
<object id="1" x="3.9375" y="6.0625" width="6" height="2.8125"/>
</objectgroup>
</tile>
<tile id="282">
<objectgroup draworder="index" id="3">
<object id="2" x="2.75" y="3.125" width="12.125" height="5.875"/>
</objectgroup>
</tile>
<wangsets>
<wangset name="Terrains" type="corner" tile="-1">
<wangcolor name="Grass" color="#ff0000" tile="150" probability="1"/>
<wangcolor name="Dirt" color="#00ff00" tile="100" probability="1"/>
<wangcolor name="Dark Dirt" color="#0000ff" tile="34" probability="1"/>
<wangcolor name="Water" color="#ff7700" tile="171" probability="1"/>
<wangtile tileid="0" wangid="0,1,0,2,0,1,0,1"/>
<wangtile tileid="1" wangid="0,1,0,2,0,2,0,1"/>
<wangtile tileid="2" wangid="0,1,0,2,0,2,0,1"/>
<wangtile tileid="3" wangid="0,1,0,2,0,2,0,1"/>
<wangtile tileid="4" wangid="0,1,0,2,0,2,0,1"/>
<wangtile tileid="5" wangid="0,1,0,1,0,2,0,1"/>
<wangtile tileid="6" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="7" wangid="0,2,0,3,0,2,0,2"/>
<wangtile tileid="8" wangid="0,2,0,3,0,3,0,2"/>
<wangtile tileid="9" wangid="0,2,0,3,0,3,0,2"/>
<wangtile tileid="10" wangid="0,2,0,3,0,3,0,2"/>
<wangtile tileid="11" wangid="0,2,0,3,0,3,0,2"/>
<wangtile tileid="12" wangid="0,2,0,2,0,3,0,2"/>
<wangtile tileid="13" wangid="0,1,0,3,0,1,0,1"/>
<wangtile tileid="14" wangid="0,1,0,3,0,3,0,1"/>
<wangtile tileid="15" wangid="0,1,0,3,0,3,0,1"/>
<wangtile tileid="16" wangid="0,1,0,3,0,3,0,1"/>
<wangtile tileid="17" wangid="0,1,0,3,0,3,0,1"/>
<wangtile tileid="18" wangid="0,1,0,1,0,3,0,1"/>
<wangtile tileid="24" wangid="0,2,0,2,0,1,0,1"/>
<wangtile tileid="25" wangid="0,2,0,1,0,2,0,2"/>
<wangtile tileid="26" wangid="0,2,0,2,0,1,0,2"/>
<wangtile tileid="27" wangid="0,2,0,2,0,2,0,2"/>
<wangtile tileid="28" wangid="0,2,0,2,0,2,0,2"/>
<wangtile tileid="29" wangid="0,1,0,1,0,2,0,2"/>
<wangtile tileid="30" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="31" wangid="0,3,0,3,0,2,0,2"/>
<wangtile tileid="32" wangid="0,3,0,2,0,3,0,3"/>
<wangtile tileid="33" wangid="0,3,0,3,0,2,0,3"/>
<wangtile tileid="34" wangid="0,3,0,3,0,3,0,3"/>
<wangtile tileid="35" wangid="0,3,0,3,0,3,0,3"/>
<wangtile tileid="36" wangid="0,2,0,2,0,3,0,3"/>
<wangtile tileid="37" wangid="0,3,0,3,0,1,0,1"/>
<wangtile tileid="38" wangid="0,3,0,1,0,3,0,3"/>
<wangtile tileid="39" wangid="0,3,0,3,0,1,0,3"/>
<wangtile tileid="40" wangid="0,3,0,3,0,3,0,3"/>
<wangtile tileid="42" wangid="0,1,0,1,0,3,0,3"/>
<wangtile tileid="48" wangid="0,2,0,2,0,1,0,1"/>
<wangtile tileid="49" wangid="0,1,0,2,0,2,0,2"/>
<wangtile tileid="50" wangid="0,2,0,2,0,2,0,1"/>
<wangtile tileid="51" wangid="0,2,0,2,0,2,0,2"/>
<wangtile tileid="52" wangid="0,2,0,2,0,2,0,2"/>
<wangtile tileid="53" wangid="0,1,0,1,0,2,0,2"/>
<wangtile tileid="54" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="55" wangid="0,3,0,3,0,2,0,2"/>
<wangtile tileid="56" wangid="0,2,0,3,0,3,0,3"/>
<wangtile tileid="57" wangid="0,3,0,3,0,3,0,2"/>
<wangtile tileid="58" wangid="0,3,0,3,0,3,0,3"/>
<wangtile tileid="59" wangid="0,3,0,3,0,3,0,3"/>
<wangtile tileid="60" wangid="0,2,0,2,0,3,0,3"/>
<wangtile tileid="61" wangid="0,3,0,3,0,1,0,1"/>
<wangtile tileid="62" wangid="0,1,0,3,0,3,0,3"/>
<wangtile tileid="63" wangid="0,3,0,3,0,3,0,1"/>
<wangtile tileid="66" wangid="0,1,0,1,0,3,0,3"/>
<wangtile tileid="72" wangid="0,2,0,2,0,1,0,1"/>
<wangtile tileid="73" wangid="0,2,0,1,0,2,0,1"/>
<wangtile tileid="74" wangid="0,1,0,2,0,1,0,2"/>
<wangtile tileid="75" wangid="0,2,0,2,0,2,0,2"/>
<wangtile tileid="76" wangid="0,2,0,2,0,2,0,2"/>
<wangtile tileid="77" wangid="0,1,0,1,0,2,0,2"/>
<wangtile tileid="78" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="79" wangid="0,3,0,3,0,2,0,2"/>
<wangtile tileid="80" wangid="0,3,0,2,0,3,0,2"/>
<wangtile tileid="81" wangid="0,2,0,3,0,2,0,3"/>
<wangtile tileid="82" wangid="0,3,0,3,0,3,0,3"/>
<wangtile tileid="83" wangid="0,3,0,3,0,3,0,3"/>
<wangtile tileid="84" wangid="0,2,0,2,0,3,0,3"/>
<wangtile tileid="85" wangid="0,3,0,3,0,1,0,1"/>
<wangtile tileid="86" wangid="0,3,0,1,0,3,0,1"/>
<wangtile tileid="87" wangid="0,1,0,3,0,1,0,3"/>
<wangtile tileid="90" wangid="0,1,0,1,0,3,0,3"/>
<wangtile tileid="96" wangid="0,2,0,2,0,1,0,1"/>
<wangtile tileid="97" wangid="0,1,0,2,0,1,0,2"/>
<wangtile tileid="98" wangid="0,2,0,1,0,2,0,1"/>
<wangtile tileid="99" wangid="0,2,0,2,0,2,0,2"/>
<wangtile tileid="100" wangid="0,2,0,2,0,2,0,2"/>
<wangtile tileid="101" wangid="0,1,0,1,0,2,0,2"/>
<wangtile tileid="102" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="103" wangid="0,3,0,3,0,2,0,2"/>
<wangtile tileid="104" wangid="0,2,0,3,0,2,0,3"/>
<wangtile tileid="105" wangid="0,3,0,2,0,3,0,2"/>
<wangtile tileid="106" wangid="0,3,0,3,0,3,0,3"/>
<wangtile tileid="107" wangid="0,3,0,3,0,3,0,3"/>
<wangtile tileid="108" wangid="0,2,0,2,0,3,0,3"/>
<wangtile tileid="109" wangid="0,3,0,3,0,1,0,1"/>
<wangtile tileid="110" wangid="0,1,0,3,0,1,0,3"/>
<wangtile tileid="111" wangid="0,3,0,1,0,3,0,1"/>
<wangtile tileid="114" wangid="0,1,0,1,0,3,0,3"/>
<wangtile tileid="120" wangid="0,2,0,1,0,1,0,1"/>
<wangtile tileid="121" wangid="0,2,0,1,0,1,0,2"/>
<wangtile tileid="122" wangid="0,2,0,1,0,1,0,2"/>
<wangtile tileid="123" wangid="0,2,0,1,0,1,0,2"/>
<wangtile tileid="124" wangid="0,2,0,1,0,1,0,2"/>
<wangtile tileid="125" wangid="0,1,0,1,0,1,0,2"/>
<wangtile tileid="126" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="127" wangid="0,3,0,2,0,2,0,2"/>
<wangtile tileid="128" wangid="0,3,0,2,0,2,0,3"/>
<wangtile tileid="129" wangid="0,3,0,2,0,2,0,3"/>
<wangtile tileid="130" wangid="0,3,0,2,0,2,0,3"/>
<wangtile tileid="131" wangid="0,3,0,2,0,2,0,3"/>
<wangtile tileid="132" wangid="0,2,0,2,0,2,0,3"/>
<wangtile tileid="133" wangid="0,3,0,1,0,1,0,1"/>
<wangtile tileid="134" wangid="0,3,0,1,0,1,0,3"/>
<wangtile tileid="135" wangid="0,3,0,1,0,1,0,3"/>
<wangtile tileid="136" wangid="0,3,0,1,0,1,0,3"/>
<wangtile tileid="137" wangid="0,3,0,1,0,1,0,3"/>
<wangtile tileid="138" wangid="0,1,0,1,0,1,0,3"/>
<wangtile tileid="144" wangid="0,1,0,4,0,1,0,1"/>
<wangtile tileid="145" wangid="0,1,0,4,0,4,0,1"/>
<wangtile tileid="146" wangid="0,1,0,4,0,4,0,1"/>
<wangtile tileid="147" wangid="0,1,0,4,0,4,0,1"/>
<wangtile tileid="148" wangid="0,1,0,4,0,4,0,1"/>
<wangtile tileid="149" wangid="0,1,0,1,0,4,0,1"/>
<wangtile tileid="150" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="151" wangid="0,2,0,4,0,2,0,2"/>
<wangtile tileid="152" wangid="0,2,0,4,0,4,0,2"/>
<wangtile tileid="153" wangid="0,2,0,4,0,4,0,2"/>
<wangtile tileid="154" wangid="0,2,0,4,0,4,0,2"/>
<wangtile tileid="155" wangid="0,2,0,4,0,4,0,2"/>
<wangtile tileid="156" wangid="0,2,0,2,0,4,0,2"/>
<wangtile tileid="168" wangid="0,4,0,4,0,1,0,1"/>
<wangtile tileid="169" wangid="0,4,0,1,0,4,0,4"/>
<wangtile tileid="170" wangid="0,4,0,4,0,1,0,4"/>
<wangtile tileid="171" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="172" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="173" wangid="0,1,0,1,0,4,0,4"/>
<wangtile tileid="174" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="175" wangid="0,4,0,4,0,2,0,2"/>
<wangtile tileid="176" wangid="0,4,0,2,0,4,0,4"/>
<wangtile tileid="177" wangid="0,4,0,4,0,2,0,4"/>
<wangtile tileid="178" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="179" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="180" wangid="0,2,0,2,0,4,0,4"/>
<wangtile tileid="192" wangid="0,4,0,4,0,1,0,1"/>
<wangtile tileid="193" wangid="0,1,0,4,0,4,0,4"/>
<wangtile tileid="194" wangid="0,4,0,4,0,4,0,1"/>
<wangtile tileid="195" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="196" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="197" wangid="0,1,0,1,0,4,0,4"/>
<wangtile tileid="198" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="199" wangid="0,4,0,4,0,2,0,2"/>
<wangtile tileid="200" wangid="0,2,0,4,0,4,0,4"/>
<wangtile tileid="201" wangid="0,4,0,4,0,4,0,2"/>
<wangtile tileid="202" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="203" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="204" wangid="0,2,0,2,0,4,0,4"/>
<wangtile tileid="216" wangid="0,4,0,4,0,1,0,1"/>
<wangtile tileid="217" wangid="0,4,0,1,0,4,0,1"/>
<wangtile tileid="218" wangid="0,1,0,4,0,1,0,4"/>
<wangtile tileid="219" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="220" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="221" wangid="0,1,0,1,0,4,0,4"/>
<wangtile tileid="222" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="223" wangid="0,4,0,4,0,2,0,2"/>
<wangtile tileid="224" wangid="0,4,0,2,0,4,0,2"/>
<wangtile tileid="225" wangid="0,2,0,4,0,2,0,4"/>
<wangtile tileid="226" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="227" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="228" wangid="0,2,0,2,0,4,0,4"/>
<wangtile tileid="240" wangid="0,4,0,4,0,1,0,1"/>
<wangtile tileid="241" wangid="0,1,0,4,0,1,0,4"/>
<wangtile tileid="242" wangid="0,4,0,1,0,4,0,1"/>
<wangtile tileid="243" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="244" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="245" wangid="0,1,0,1,0,4,0,4"/>
<wangtile tileid="246" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="247" wangid="0,4,0,4,0,2,0,2"/>
<wangtile tileid="248" wangid="0,2,0,4,0,2,0,4"/>
<wangtile tileid="249" wangid="0,4,0,2,0,4,0,2"/>
<wangtile tileid="250" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="251" wangid="0,4,0,4,0,4,0,4"/>
<wangtile tileid="252" wangid="0,2,0,2,0,4,0,4"/>
<wangtile tileid="264" wangid="0,4,0,1,0,1,0,1"/>
<wangtile tileid="265" wangid="0,4,0,1,0,1,0,4"/>
<wangtile tileid="266" wangid="0,4,0,1,0,1,0,4"/>
<wangtile tileid="267" wangid="0,4,0,1,0,1,0,4"/>
<wangtile tileid="268" wangid="0,4,0,1,0,1,0,4"/>
<wangtile tileid="269" wangid="0,1,0,1,0,1,0,4"/>
<wangtile tileid="270" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="271" wangid="0,4,0,2,0,2,0,2"/>
<wangtile tileid="272" wangid="0,4,0,2,0,2,0,4"/>
<wangtile tileid="273" wangid="0,4,0,2,0,2,0,4"/>
<wangtile tileid="274" wangid="0,4,0,2,0,2,0,4"/>
<wangtile tileid="275" wangid="0,4,0,2,0,2,0,4"/>
<wangtile tileid="276" wangid="0,2,0,2,0,2,0,4"/>
</wangset>
</wangsets>
</tileset>
<tileset firstgid="289" name="tall_trees" tilewidth="54" tileheight="72" tilecount="2" columns="2">
<image source="../../twodina/assets/maps/trees.png" width="108" height="72"/>
<tile id="0">
<objectgroup draworder="index" id="2">
<object id="1" x="14.25" y="21.75" width="26.375" height="49.625"/>
</objectgroup>
</tile>
</tileset>
<layer id="1" name="Ground" width="10" height="10">
<data encoding="base64" compression="zlib">
eJxtjkEOAUEURL9gtlYWEhwCcQcbd7DoQzgJEc4wcQgZQ9yAjMQZxEIk3s+U5C8sXqpnqrqqKzPrNMwKFLFM37nVrGBptZ/L66EjaEEz3CmU83OFLiDBGnYwlfey2vezb46Vu8NH3VnYixnvOimXQtY7ZzDR/yM8odR20n77T9cb9tBlaIDO5Sf1+buucA59fRiGnm3oOojfG71vE3zveYgbXELuC/OiMJ0=
</data>
</layer>
<objectgroup id="3" name="Objects" opacity="0.9">
<object id="40" gid="289" x="106.333" y="96" width="54" height="72"/>
<object id="41" gid="290" x="11" y="65.3333" width="54" height="72"/>
<object id="42" gid="283" x="2.66667" y="142.667" width="16" height="48.3333"/>
<object id="43" gid="283" x="72.6667" y="133.333" width="16" height="16"/>
<object id="44" gid="283" x="86.5" y="160" width="70.1667" height="20.1667"/>
<object id="45" gid="289" x="40.75" y="87.5" width="72" height="72"/>
<object id="46" gid="283" x="108.333" y="21.3333" width="16" height="16"/>
<object id="47" gid="283" x="42" y="102.667" width="16" height="16"/>
</objectgroup>
</map>
2 changes: 1 addition & 1 deletion examples/ortho_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(bevy_tiled_prototype::TiledMapBundle {
map_asset: asset_server.load("ortho-map.tmx"),
map_asset: asset_server.load("ortho-debug.tmx"),
center: TiledMapCenter(true),
origin: Transform::from_scale(Vec3::new(SCALE, SCALE, 1.0)),
debug_config: DebugConfig {
Expand Down
4 changes: 2 additions & 2 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ pub fn process_loaded_tile_maps(
// println!("Despawning previously-created mesh for this chunk");
for entity in entities.iter() {
// println!("calling despawn on {:?}", entity);
commands.entity(*entity).despawn();
commands.entity(*entity).despawn_recursive();
}
});
let mut chunk_entities: Vec<Entity> = Default::default();
Expand Down Expand Up @@ -414,7 +414,7 @@ pub fn process_loaded_tile_maps(
// println!("Despawning previously-created object sprite");
for entity in entities.iter() {
// println!("calling despawn on {:?}", entity);
commands.entity(*entity).despawn();
commands.entity(*entity).despawn_recursive();
}
});
}
Expand Down
20 changes: 11 additions & 9 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct ObjectGroup {
impl ObjectGroup {
pub fn new_with_tile_ids(
inner: &tiled::ObjectGroup,
tile_gids: &HashMap<u32, u32>,
tileset_gid_by_gid: &HashMap<u32, u32>,
) -> ObjectGroup {
// println!("grp {}", inner.name.to_string());
ObjectGroup {
Expand All @@ -23,7 +23,7 @@ impl ObjectGroup {
objects: inner
.objects
.iter()
.map(|obj| Object::new_with_tile_ids(obj, tile_gids))
.map(|obj| Object::new_with_tile_ids(obj, tileset_gid_by_gid))
.collect(),
}
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl Object {
let dimensions = self
.dimensions()
.expect("Don't know how to handle object without dimensions");

let mut new_entity_commands = if let Some(texture_atlas) = texture_atlas {
let sprite_index = self.sprite_index.expect("missing sprite index");
let tileset_gid = self.tileset_gid.expect("missing tileset");
Expand All @@ -169,7 +169,7 @@ impl Object {
let object_tile = object_tileset.and_then(|ts| ts
.tiles.iter().find(|&tile| tile.id + ts.first_gid == self.gid)
);

// use object dimensions and tile size to determine extra scale to apply for tile objects
let tile_scale = if let Some(size) = object_tile_size {
Some((dimensions / size).extend(1.0))
Expand All @@ -191,6 +191,7 @@ impl Object {
..Default::default()
});
// spawn embedded objects as children
let tile_size = object_tile_size.expect("child object needs parent to have a size");
object_tile.map(|tile| {
entity_commands.with_children(|builder| {
//builder.spawn
Expand All @@ -199,11 +200,12 @@ impl Object {
let marker_object = Object::new(obj);

let mut embedded_object_transform = Transform::from_scale(Vec3::splat(1.0));
embedded_object_transform.translation =
Vec3::new(obj.x, -obj.y, 0.00005) +
-Vec3::new(self.size.x, -self.size.y, 0.01) / 2.0 / tile_scale.unwrap_or(Vec3::splat(1.0)) +
Vec3::new(obj.width, -obj.height, 0.01) / 2.0;

embedded_object_transform.translation = Vec3::new(
obj.x -(tile_size.x - obj.width) / 2.0, //(self.size.y / tile_scale.unwrap_or(Vec3::ONE).y - obj.height) / 2.0 - obj.y,
-obj.y + (tile_size.y - obj.height) / 2.0, //(self.size.y / tile_scale.unwrap_or(Vec3::ONE).y - obj.height) / 2.0 - obj.y,
0.0001
);
//println!("{:?},{:?}", tile_scale.unwrap(), embedded_object_transform.translation );
let size = marker_object.dimensions().expect("embedded object needs dimension");

builder.spawn_bundle(
Expand Down

0 comments on commit af25dca

Please sign in to comment.