Skip to content

Commit

Permalink
Tilemap rework, removed LDTK
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonWebster committed Dec 9, 2023
1 parent 170822f commit 07e3cdd
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ bevy = { version = "0.12", default-features = false, features = [
] }
bevy_kira_audio = { version = "0.18" }
bevy_asset_loader = { version = "0.18", features = ["2d"] }
rand = { version = "0.8.3" }
rand = { version = "0.8.3", features = [] }
webbrowser = { version = "0.8", features = ["hardened"] }

# keep the following in sync with Bevy's dependencies
winit = { version = "0.28.7", default-features = false }
image = { version = "0.24", default-features = false }
bevy_ecs_tilemap = { git = "https://github.com/divark/bevy_ecs_tilemap", branch = "0.12-fixes" }
bevy_ecs_ldtk = { git = "https://github.com/Trouv/bevy_ecs_ldtk.git", branch = "feat/bevy-0.12" }
bevy-inspector-egui = "0.21.0"
quadtree = { git = "https://github.com/MickHarrigan/quadtree.git" }
Expand Down
Binary file modified assets/textures/ground/ground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::input::mouse::MouseWheel;
use bevy::prelude::*;
use crate::GameState;
use crate::world::LevelData;
use crate::tilemap::LevelData;

pub struct CameraPlugin;

Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod boids;
mod debug;
mod world;
mod interactions;
mod tilemap;

use crate::actions::ActionsPlugin;
use crate::audio::InternalAudioPlugin;
Expand All @@ -32,6 +33,7 @@ use crate::player::PlayerPlugin;
use crate::bees::BeesPlugin;
use crate::world::WorldPlugin;
use crate::camera::CameraPlugin;
use crate::tilemap::MapPlugin;

use bevy::app::App;
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -68,16 +70,17 @@ impl Plugin for GamePlugin {
// InternalAudioPlugin,
// PlayerPlugin,
CameraPlugin,
WorldPlugin,
InteractionsPlugin,
BeesPlugin,
MapPlugin,
// WorldPlugin,
// InteractionsPlugin,
// BeesPlugin,
));

#[cfg(debug_assertions)]
{
app.add_plugins((
DebugPlugin,
FrameTimeDiagnosticsPlugin,
// FrameTimeDiagnosticsPlugin,
LogDiagnosticsPlugin::default(),
WorldInspectorPlugin::new(),
))
Expand Down
2 changes: 2 additions & 0 deletions src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ pub struct TextureAssets {
))]
#[asset(path = "textures/airplanes.png")]
pub planes: Handle<TextureAtlas>,
#[asset(path = "textures/ground/ground.png")]
pub ground: Handle<Image>
}
82 changes: 82 additions & 0 deletions src/tilemap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::GameState;
use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::*;
use crate::loading::TextureAssets;

use rand::Rng;

pub struct MapPlugin;
impl Plugin for MapPlugin {
fn build(&self, app: &mut App) {
app
.add_plugins(TilemapPlugin)
.add_systems(OnEnter(GameState::Playing), setup_level)
;
}
}

#[derive(Resource, Debug)]
pub struct LevelData {
pub level_height: f32,
pub level_width: f32,
}

fn setup_level(
mut commands: Commands,
// asset_server: Res<AssetServer>,
textures: Res<TextureAssets>,
#[cfg(all(not(feature = "atlas"), feature = "render"))]
array_texture_loader: Res<ArrayTextureLoader>,
) {
let texture_handle: Handle<Image> = textures.ground.clone();

let map_size = TilemapSize { x: 16, y: 16 };

let tilemap_entity = commands.spawn_empty().id();

let mut tile_storage = TileStorage::empty(map_size);
let mut rng = rand::thread_rng();

for x in 0..map_size.x {
for y in 0..map_size.y {
let tile_pos = TilePos{ x , y };
let tile_entity = commands
.spawn(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
texture_index: TileTextureIndex(rng.gen_range(0..3)),
..Default::default()
})
.id();
tile_storage.set(&tile_pos, tile_entity);
}
}

let tile_size = TilemapTileSize{ x: 720.0, y: 720.0 };
let grid_size: TilemapGridSize = tile_size.into();
commands.insert_resource(LevelData {level_height: grid_size.y, level_width: grid_size.x});
let map_type = TilemapType::default();

commands.entity(tilemap_entity).insert(TilemapBundle {
grid_size,
map_type,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});

// Add atlas to array texture loader so it's preprocessed before we need to use it.
// Only used when the atlas feature is off and we are using array textures.
#[cfg(all(not(feature = "atlas"), feature = "render"))]
{
array_texture_loader.add(TilemapArrayTexture {
texture: TilemapTexture::Single(asset_server.load("tiles.png")),
tile_size,
..Default::default()
});
}

}

0 comments on commit 07e3cdd

Please sign in to comment.