Use Map resource to define the world

This commit is contained in:
Daniel Lynn 2021-07-18 00:54:09 -05:00
parent cbf9e47ff8
commit 5f9f15b2af
Signed by: daniel
GPG Key ID: 28496A140E180A9D
2 changed files with 34 additions and 17 deletions

View File

@ -1,18 +1,24 @@
mod components; mod components;
mod map;
mod plugins; mod plugins;
mod resources; mod resources;
mod systems; mod systems;
mod prelude { mod prelude {
pub use crate::components::*; pub use crate::components::*;
pub use crate::map::*;
pub use crate::plugins::*; pub use crate::plugins::*;
pub use crate::resources::*; pub use crate::resources::*;
pub use crate::systems::*; pub use crate::systems::*;
pub use bevy::prelude::*; pub use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
};
pub const SCREEN_WIDTH: f32 = 80.0; pub const MAP_WIDTH: i32 = 80;
pub const SCREEN_HEIGHT: f32 = 50.0; pub const MAP_HEIGHT: i32 = 50;
pub const TILE_SIZE: f32 = 64.0; pub const CELL_SIZE: i32 = 64;
pub const NUM_TILES: usize = (MAP_WIDTH * MAP_HEIGHT) as usize;
} }
use prelude::*; use prelude::*;
@ -20,7 +26,11 @@ use prelude::*;
fn main() { fn main() {
App::build() App::build()
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0))) .insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.init_resource::<Map>()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
//.add_plugin(FrameTimeDiagnosticsPlugin)
//.add_plugin(LogDiagnosticsPlugin)
//.add_plugin(bevy::asset::diagnostic::AssetCountDiagnosticsPlugin::< ColorMaterial>)
.add_startup_system(setup.system()) .add_startup_system(setup.system())
.add_startup_system(add_player.system()) .add_startup_system(add_player.system())
.add_system(player_movement.system()) .add_system(player_movement.system())

View File

@ -5,27 +5,34 @@ pub fn setup(
mut commands: Commands, mut commands: Commands,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
map: Res<Map>,
) { ) {
// Camera // Camera
commands.spawn_bundle(OrthographicCameraBundle::new_2d()); commands.spawn_bundle(OrthographicCameraBundle::new_2d());
// Map // Map
let grass_material = materials.add(asset_server.load("grass.png").into()); let grass_material = materials.add(asset_server.load("grass.png").into());
let tile_size = Vec2::new(TILE_SIZE, TILE_SIZE); let wall_material = materials.add(asset_server.load("wall.png").into());
let sprite = Sprite::new(tile_size); let half_w = MAP_WIDTH * CELL_SIZE / 2;
let map_size = Vec2::new(SCREEN_WIDTH, SCREEN_HEIGHT); let half_h = MAP_HEIGHT * CELL_SIZE / 2;
let half_x = (map_size.x / 2.0) as i32;
let half_y = (map_size.y / 2.0) as i32;
for y in -half_y..half_y { for j in 0..MAP_HEIGHT {
for x in -half_x..half_x { let y = j * CELL_SIZE - half_h;
let position = Vec2::new(x as f32, y as f32); for i in 0..MAP_WIDTH {
let translation = (position * tile_size).extend(0.0); 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 { commands.spawn_bundle(SpriteBundle {
material: grass_material.clone(), material,
sprite: sprite.clone(), sprite,
transform: Transform::from_translation(translation), transform,
..Default::default() ..Default::default()
}); });
} }
@ -45,7 +52,7 @@ pub fn add_player(
.insert(Name("Player 1".to_string())) .insert(Name("Player 1".to_string()))
.insert_bundle(SpriteBundle { .insert_bundle(SpriteBundle {
material: player_material, 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)), transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
..Default::default() ..Default::default()
}); });