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

New renderer #62

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
922 changes: 886 additions & 36 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ web = [

[dependencies]
anyhow = "1.0"
bevy = { version = "0.5", default-features = false }
bevy = { version = "0.5" }
bevy_ecs_tilemap = { git="https://github.com/StarArawn/bevy_ecs_tilemap", rev="50ec3c12c68079cf0d624e4dcade349351c3ae6c" }
log = "0.4"
tiled = { version = "0.9", default-features = false }

[dev-dependencies]
env_logger = "0.8"
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ https://www.mapeditor.org/

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

This plugin uses bevy_ecs_tilemap(https://github.com/StarArawn/bevy_ecs_tilemap) to render tiled data.

## Bevy Versions

The `main` branch of this repository targets Bevy 0.5. When using bevy_tiled, please make sure your version of Bevy matches the version referenced by this library. There are versions for 0.4 and 0.3 as well.
Expand Down Expand Up @@ -37,13 +39,16 @@ In these examples, you should be able to use the wasd keys to pan across the map
# Features
## Toplevel Entity Support

TODO: REWRITE THIS SECTION
For now, TiledMapBundle is just a configuration object. If you would like access to a toplevel entity that can be transformed, pass into the configuration:

parent_option: Some(entity)

Then, both chunks and objects will be inserted as children to this entity, which will be tagged with MapRoot. This API is likely to change, but we have an [example](/examples/parent_entity.rs) for how it currently works.
## Object Group Support

TODO: REWRITE THIS SECTION

Object Grous 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:

Expand Down Expand Up @@ -83,11 +88,13 @@ Then when you save your map, it should update in the application.

## WASM and bevy_webgl2

## TODO see if web still works? :/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rparrett if you have a chance to try it out -- I'd be interested to know whether this new render can be adapted for use on web...


Use `default-features=false, features=["web"]` in your project's `Cargo.toml`. Tiled maps using Zstd compression are not supported.

## Top-needed features

* better support for isometric maps
* support for embeded objects in tiles
* support for embedded images in Tmx files
* support for animations
* ~~support for animations~~ done
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still needed for objects

14 changes: 8 additions & 6 deletions examples/iso_main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use bevy::{prelude::*, render::camera::Camera};
use bevy_tiled_prototype::TiledMapCenter;
use bevy::{app::CoreStage::PreUpdate, prelude::*, render::{camera::Camera}};
use bevy_tiled_prototype::prelude::*;

mod utils;

fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_plugin(bevy_tiled_prototype::TiledMapPlugin)
.add_plugin(TiledMapPlugin)
.add_system(bevy::input::system::exit_on_esc_system.system())
.add_startup_system(setup.system())
.add_system(camera_movement.system())
// Needs to run before rendering to set texture atlas filter for new tiles- would be better to use states
.add_system_to_stage(PreUpdate, utils::texture_sampler::set_texture_filters_to_nearest.system())
.run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(bevy_tiled_prototype::TiledMapBundle {
commands.spawn_bundle(TiledMapBundle {
map_asset: asset_server.load("iso-map.tmx"),
center: TiledMapCenter(true),
origin: Transform::from_scale(Vec3::new(4.0, 4.0, 1.0)),
..Default::default()
});
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
Expand Down
38 changes: 0 additions & 38 deletions examples/ortho_debug.rs

This file was deleted.

33 changes: 11 additions & 22 deletions examples/ortho_main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
use bevy::{app::CoreStage::PreUpdate, prelude::*, render::camera::Camera};
use bevy_tiled_prototype::{MapReadyEvent, TiledMapCenter};
use bevy_tiled_prototype::prelude::*;

mod utils;

fn main() {
env_logger::Builder::from_default_env()
.filter_level(log::LevelFilter::Error)
.init();

App::build()
.add_plugins(DefaultPlugins)
.add_plugin(bevy_tiled_prototype::TiledMapPlugin)
.add_system(bevy::input::system::exit_on_esc_system.system())
.add_startup_system(setup.system())
.add_system(camera_movement.system())
// Needs to run before rendering to set texture atlas filter for new tiles- would be better to use states
.add_system_to_stage(PreUpdate, set_texture_filters_to_nearest.system())
.add_system_to_stage(PreUpdate, utils::texture_sampler::set_texture_filters_to_nearest.system())
.run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(bevy_tiled_prototype::TiledMapBundle {
asset_server.watch_for_changes().unwrap();

commands.spawn_bundle(TiledMapBundle {
map_asset: asset_server.load("ortho-map.tmx"),
center: TiledMapCenter(true),
origin: Transform::from_scale(Vec3::new(4.0, 4.0, 1.0)),
..Default::default()
});
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
Expand Down Expand Up @@ -61,20 +67,3 @@ fn camera_movement(
transform.translation += time.delta_seconds() * direction * 1000.;
}
}

// demo of https://github.com/StarArawn/bevy_tiled/issues/47#issuecomment-817126515
// Would be cleaner to put this in a separate AppState, transitioning out after textures loaded
fn set_texture_filters_to_nearest(
mut map_ready_events: EventReader<MapReadyEvent>,
mut textures: ResMut<Assets<Texture>>,
texture_atlases: Res<Assets<TextureAtlas>>,
) {
// quick and dirty, run this for all textures every time a map is created/modified
if map_ready_events.iter().count() > 0 {
for (_, atlas) in texture_atlases.iter() {
if let Some(texture) = textures.get_mut(atlas.texture.clone()) {
texture.sampler.min_filter = bevy::render::texture::FilterMode::Nearest;
}
}
}
}
14 changes: 8 additions & 6 deletions examples/parent_entity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_tiled_prototype::{MapRoot, TiledMapCenter};
use bevy_tiled_prototype::prelude::*;

// this example demonstrates moving the map mesh entities using
// the MapRoot marker on a passed-in parent element
Expand All @@ -11,6 +11,8 @@ struct MovementData {
transform: Transform,
}

struct MapRoot;

fn main() {
App::build()
.insert_resource(MovementData::default())
Expand All @@ -34,14 +36,14 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..Default::default()
},
))
.insert(MapRoot)
.id();

commands.spawn_bundle(bevy_tiled_prototype::TiledMapBundle {
map_asset: asset_server.load("ortho-map.tmx"),
parent_option: Some(parent),
center: TiledMapCenter(true),
origin: Transform::from_scale(Vec3::new(4.0, 4.0, 1.0)),
commands.entity(parent).with_children(|child_builder| {
child_builder.spawn_bundle(TiledMapBundle {
map_asset: asset_server.load("ortho-map.tmx"),
..Default::default()
});
});
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
}
Expand Down
1 change: 1 addition & 0 deletions examples/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod texture_sampler;
20 changes: 20 additions & 0 deletions examples/utils/texture_sampler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use bevy::{prelude::*, render::texture::FilterMode};

// demo of https://github.com/StarArawn/bevy_tiled/issues/47#issuecomment-817126515
// Would be cleaner to put this in a separate AppState, transitioning out after textures loaded
pub fn set_texture_filters_to_nearest(
mut texture_events: EventReader<AssetEvent<Texture>>,
mut textures: ResMut<Assets<Texture>>,
) {
// quick and dirty, run this for all textures every time a map is created/modified
for event in texture_events.iter() {
match event {
AssetEvent::Created { handle } => {
if let Some(mut texture) = textures.get_mut(handle){
texture.sampler.min_filter = FilterMode::Nearest;
}
Comment on lines +8 to +15
Copy link
Collaborator

@dmtaub dmtaub May 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going through all the trouble to do this in every example, should we consider enabling it as a feature or with a boolean flag in a config object?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have a feature that queries textures for the tiled map layers and updates them to use min_filter. I think having the user do this during a "loading" game state is probably better though.

}
_ => ()
}
}
}
38 changes: 38 additions & 0 deletions src/animation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::Tile;

/// Information specific to the current frame in the animation.
#[derive(Clone)]
pub struct Frame {
/// Tile id.
pub tile_id: u32,
/// Duration until next frame.
pub duration: f64,
}

/// Information about the tiles animation state.
pub struct Animation {
/// Frame info.
pub frames: Vec<Frame>,
/// The current frame.
pub current_frame: usize,
pub last_update: f64,
}

pub fn update(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

time: Res<Time>,
mut query: Query<(&mut Tile, &mut Animation)>,
) {
let current_time = time.seconds_since_startup();
for (mut tile, mut animation) in query.iter_mut() {
let frame = animation.frames[animation.current_frame].clone();
if (current_time - animation.last_update) > frame.duration {
animation.current_frame += 1;
tile.texture_index = frame.tile_id;
if animation.current_frame > animation.frames.len() - 1 {
animation.current_frame = 0;
}
animation.last_update = current_time;
}
}
}
Loading