Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bevy 0.4 objects loading support #41

Open
wants to merge 7 commits into
base: bevy-0.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "bevy_tiled_prototype"
description = "A plugin for rendering tiled maps."
version = "0.1.1"
authors = ["John Mitchell"]
version = "0.2.1"
authors = ["John Mitchell", "Daniel Taub<dmtaub@gmail.com>"]
homepage = "https://github.com/StarArawn/bevy_tiled"
repository = "https://github.com/StarArawn/bevy_tiled"
license-file = "LICENSE"
Expand All @@ -18,5 +18,5 @@ anyhow = "1.0"
bevy = "0.4.0"
bevy_reflect = "0.4.0"
glam = "0.11.2"
tiled = "0.9"
# tiled = { git = "https://github.com/mattyhall/rs-tiled" }
#tiled = "0.9"
tiled = { git = "https://github.com/mattyhall/rs-tiled", branch = "master" }
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,38 @@ A plugin for rendering tiled maps. Specfically maps from the tiled editor which
https://www.mapeditor.org/

Feel free to use this code as a reference for your own custom tile mapping solution as well.
## Object Layer Support

Object layers are now supported. They will be skipped if not visible. Individual objects that are invisible
will be spawned with is_visible set to false. You may pass into the configuration object:

debug_config: DebugConfig { enabled: true, material: None }

to show a color mesh for objects that have no tile sprite. This is only supported for rects and points (small squares) at this time.

Objects within tiles are not currently supported.

## Events

ObjectReadyEvent fires when an object has been spawned.

It has:

pub map_handle: Handle<Map>,

and ObjectReadyEvent includes the entity for the object itself

## Hot reload

Limited support for hot reload is provided. Old entities are removed based on the asset handles (for now).

asset_server.watch_for_changes().expect("watch for changes failed");

Then when you save your map, it should update in the application.

## Top-needed features

* support for iso maps
* support for objects in tiles
* support for embedded images

2 changes: 1 addition & 1 deletion assets/ortho-map.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
eJzVl9lNw0AURZ8ltgr4YquAjliaYPl8DdAChCVABRB2KkBhpwK2sKQC4FgiShQ8tseMx/aVjmRLY+fO3JnnFxGRGZiVamkBFos2EaOBQGQw6N6PFmcltcbwOx50vU+neKYztsb1OmzAZq4uo9XxbjO2wfUBHMJRjt5c6RyacAlXcF2oGzu9wCu0YCohpwcPftJqFdZ+r9uGMf1nvWjtwX7P/UiEN5vzUiZRW5Qao9QarUHRftKI2qLUGKXWaCOjZ9/zprYoNUapNdrM+Hsu5m0jaotSY5Rao62M73Ax70d4gme4yOgjSb01tH/eu/947xd8izvfS7Ds6F2+VZP8e5oVx+8rY0/D90dN385QPnuapN7DZnxvT1O2HiCNqtYD2GaXt3aot9tQh62K9CJn+DyFEziuiOd7fN7BLdyUxDN5C7kL+Yf74I8+8fkB7/AGQ+zdYU/7t21YI/IWchfyD/dBoibwO1nwmSNvIXch/3AfxCopE18ibyF3If9wH8TKNhOfMq2nTSZ5yXSeTOvZn0nU83Mw78Bb1P/tUKbzlHY9k87jD20Li3Y=
</data>
</layer>
<objectgroup id="3" name="Objects" visible="0">
<objectgroup id="3" name="Objects">
<object id="1" name="maggots" type="location" x="435" y="74" width="155" height="99">
<properties>
<property name="spawncount" type="int" value="5"/>
Expand Down
43 changes: 43 additions & 0 deletions examples/ortho_objects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use bevy::prelude::*;
use bevy_tiled_prototype::{DebugConfig, Object, TiledMapCenter};

// this example demonstrates debugging objects

const SCALE: f32 = 2.0;

fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_plugin(bevy_tiled_prototype::TiledMapPlugin)
.add_system(bevy::input::system::exit_on_esc_system.system())
.add_system(toggle_debug.system())
.add_startup_system(setup.system())
.run();
}

fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
commands
.spawn(bevy_tiled_prototype::TiledMapComponents {
map_asset: asset_server.load("ortho-map.tmx"),
center: TiledMapCenter(true),
origin: Transform::from_scale(Vec3::new(SCALE, SCALE, 1.0)),
debug_config: DebugConfig {
enabled: true,
material: None,

},
..Default::default()
})
.spawn(Camera2dBundle::default());
}

fn toggle_debug(
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<&mut Visible, With<Object>>,
) {
for mut visible in query.iter_mut() {
if keyboard_input.just_released(KeyCode::Space) {
visible.is_visible = !visible.is_visible;
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl Plugin for TiledMapPlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_asset::<map::Map>()
.init_asset_loader::<loader::TiledMapLoader>()
.add_event::<ObjectReadyEvent>()
.add_system(process_loaded_tile_maps.system());

let resources = app.resources();
Expand Down
17 changes: 11 additions & 6 deletions src/loader.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::map::Map;
use anyhow::Result;
use bevy::{
asset::{AssetLoader, LoadContext, LoadedAsset},
utils::BoxedFuture,
};
use bevy::{asset::{AssetLoader, AssetPath, LoadContext, LoadedAsset}, utils::BoxedFuture};

#[derive(Default)]
pub struct TiledMapLoader;
Expand All @@ -29,8 +26,16 @@ impl AssetLoader for TiledMapLoader {
) -> BoxedFuture<'a, Result<(), anyhow::Error>> {
Box::pin(async move {
let path = load_context.path();
let map = Map::try_from_bytes(path, bytes.into())?;
load_context.set_default_asset(LoadedAsset::new(map));
let mut map = Map::try_from_bytes(path, bytes.into())?;
let dependencies = map.asset_dependencies.drain(..)
.map(|image_path| {
// add tileset to dependencies
AssetPath::new(image_path, None)
}).collect();
let loaded_asset = LoadedAsset::new(map);
load_context.set_default_asset(
loaded_asset.with_dependencies(dependencies)
);
Ok(())
})
}
Expand Down
Loading