diff --git a/src/main.rs b/src/main.rs index 6dd4981..3e9275b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,24 @@ mod components; +mod map; mod plugins; mod resources; mod systems; mod prelude { pub use crate::components::*; + pub use crate::map::*; pub use crate::plugins::*; pub use crate::resources::*; pub use crate::systems::*; - pub use bevy::prelude::*; + pub use bevy::{ + diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, + prelude::*, + }; - pub const SCREEN_WIDTH: f32 = 80.0; - pub const SCREEN_HEIGHT: f32 = 50.0; - pub const TILE_SIZE: f32 = 64.0; + pub const MAP_WIDTH: i32 = 80; + pub const MAP_HEIGHT: i32 = 50; + pub const CELL_SIZE: i32 = 64; + pub const NUM_TILES: usize = (MAP_WIDTH * MAP_HEIGHT) as usize; } use prelude::*; @@ -20,7 +26,11 @@ use prelude::*; fn main() { App::build() .insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0))) + .init_resource::() .add_plugins(DefaultPlugins) + //.add_plugin(FrameTimeDiagnosticsPlugin) + //.add_plugin(LogDiagnosticsPlugin) + //.add_plugin(bevy::asset::diagnostic::AssetCountDiagnosticsPlugin::< ColorMaterial>) .add_startup_system(setup.system()) .add_startup_system(add_player.system()) .add_system(player_movement.system()) diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 36a636d..d6a48ca 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -5,27 +5,34 @@ pub fn setup( mut commands: Commands, asset_server: Res, mut materials: ResMut>, + map: Res, ) { // Camera commands.spawn_bundle(OrthographicCameraBundle::new_2d()); // Map let grass_material = materials.add(asset_server.load("grass.png").into()); - let tile_size = Vec2::new(TILE_SIZE, TILE_SIZE); - let sprite = Sprite::new(tile_size); - let map_size = Vec2::new(SCREEN_WIDTH, SCREEN_HEIGHT); - let half_x = (map_size.x / 2.0) as i32; - let half_y = (map_size.y / 2.0) as i32; + let wall_material = materials.add(asset_server.load("wall.png").into()); + let half_w = MAP_WIDTH * CELL_SIZE / 2; + let half_h = MAP_HEIGHT * CELL_SIZE / 2; - for y in -half_y..half_y { - for x in -half_x..half_x { - let position = Vec2::new(x as f32, y as f32); - let translation = (position * tile_size).extend(0.0); + for j in 0..MAP_HEIGHT { + let y = j * CELL_SIZE - half_h; + for i in 0..MAP_WIDTH { + let x = i * CELL_SIZE - half_w; + let index = map.index(i, j); + let tile = map.tiles[index]; + let material = match tile { + Tile::Floor => grass_material.clone(), + Tile::Wall => wall_material.clone(), + }; + let sprite = Sprite::new(Vec2::new(CELL_SIZE as f32, CELL_SIZE as f32)); + let transform = Transform::from_xyz(x as f32, y as f32, 0.0); commands.spawn_bundle(SpriteBundle { - material: grass_material.clone(), - sprite: sprite.clone(), - transform: Transform::from_translation(translation), + material, + sprite, + transform, ..Default::default() }); } @@ -45,7 +52,7 @@ pub fn add_player( .insert(Name("Player 1".to_string())) .insert_bundle(SpriteBundle { material: player_material, - sprite: Sprite::new(Vec2::new(TILE_SIZE, TILE_SIZE)), + sprite: Sprite::new(Vec2::new(CELL_SIZE as f32, CELL_SIZE as f32)), transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)), ..Default::default() });