Compare commits

..

No commits in common. "5f9f15b2af823561987ef9ced73baec10569e8cc" and "main" have entirely different histories.

3 changed files with 17 additions and 59 deletions

View File

@ -1,24 +1,18 @@
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::{ pub use bevy::prelude::*;
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
};
pub const MAP_WIDTH: i32 = 80; pub const SCREEN_WIDTH: f32 = 80.0;
pub const MAP_HEIGHT: i32 = 50; pub const SCREEN_HEIGHT: f32 = 50.0;
pub const CELL_SIZE: i32 = 64; pub const TILE_SIZE: f32 = 64.0;
pub const NUM_TILES: usize = (MAP_WIDTH * MAP_HEIGHT) as usize;
} }
use prelude::*; use prelude::*;
@ -26,11 +20,7 @@ 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

@ -1,25 +0,0 @@
use crate::prelude::*;
#[derive(Clone, Copy)]
pub enum Tile {
Floor,
Wall,
}
pub struct Map {
pub tiles: Vec<Tile>,
}
impl Map {
pub fn index(&self, x: i32, y: i32) -> usize {
((y * MAP_HEIGHT) + x) as usize
}
}
impl Default for Map {
fn default() -> Self {
Self {
tiles: vec![Tile::Floor; NUM_TILES],
}
}
}

View File

@ -5,34 +5,27 @@ 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 wall_material = materials.add(asset_server.load("wall.png").into()); let tile_size = Vec2::new(TILE_SIZE, TILE_SIZE);
let half_w = MAP_WIDTH * CELL_SIZE / 2; let sprite = Sprite::new(tile_size);
let half_h = MAP_HEIGHT * CELL_SIZE / 2; 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;
for j in 0..MAP_HEIGHT { for y in -half_y..half_y {
let y = j * CELL_SIZE - half_h; for x in -half_x..half_x {
for i in 0..MAP_WIDTH { let position = Vec2::new(x as f32, y as f32);
let x = i * CELL_SIZE - half_w; let translation = (position * tile_size).extend(0.0);
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, material: grass_material.clone(),
sprite, sprite: sprite.clone(),
transform, transform: Transform::from_translation(translation),
..Default::default() ..Default::default()
}); });
} }
@ -52,7 +45,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(CELL_SIZE as f32, CELL_SIZE as f32)), sprite: Sprite::new(Vec2::new(TILE_SIZE, TILE_SIZE)),
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()
}); });