Skip to content

Commit

Permalink
WIP: Dungeon example
Browse files Browse the repository at this point in the history
  • Loading branch information
jgayfer committed May 28, 2024
1 parent 8358c91 commit 21f331a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ bevy = { version = "0.13", default-features = false, features = [
"bevy_core_pipeline",
"bevy_winit",
"bevy_sprite",
"png",
"x11"
] }

Expand Down
Binary file added assets/dungeon_tiles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions examples/dungeon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use bevy::prelude::*;
use bevy_light_2d::prelude::*;

fn main() {
App::new()
.add_plugins((DefaultPlugins, Light2dPlugin))
.init_resource::<DungeonTileset>()
.add_systems(Startup, setup_camera)
.add_systems(Startup, (setup_dungeon_tileset, spawn_walls).chain())
.run()
}

#[derive(Resource, Default)]
struct DungeonTileset {
layout: Handle<TextureAtlasLayout>,
texture: Handle<Image>,
}

fn setup_camera(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}

fn spawn_walls(commands: Commands, tileset: Res<DungeonTileset>) {
spawn_from_atlas(
commands,
Vec3::splat(0.0),
TOP_WALL_A,
tileset.layout.clone(),
tileset.texture.clone(),
);
}

fn spawn_from_atlas(
mut commands: Commands,
translation: Vec3,
sprite_index: usize,
atlas_handle: Handle<TextureAtlasLayout>,
texture: Handle<Image>,
) {
commands.spawn(SpriteSheetBundle {
transform: Transform {
translation,
..default()
},
texture,
atlas: TextureAtlas {
index: sprite_index,
layout: atlas_handle,
},
..default()
});
}

fn setup_dungeon_tileset(
asset_server: Res<AssetServer>,
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
mut dungeon_tileset: ResMut<DungeonTileset>,
) {
dungeon_tileset.texture = asset_server.load("dungeon_tiles.png");
dungeon_tileset.layout = texture_atlas_layouts.add(TextureAtlasLayout::from_grid(
Vec2::new(16.0, 16.0),
10,
10,
None,
None,
));
}

const TOP_WALL_A: usize = 1;
const TOP_WALL_B: usize = 2;
const TOP_WALL_C: usize = 3;
const TOP_WALL_D: usize = 4;

0 comments on commit 21f331a

Please sign in to comment.