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 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::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
};
pub use bevy::prelude::*;
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;
pub const SCREEN_WIDTH: f32 = 80.0;
pub const SCREEN_HEIGHT: f32 = 50.0;
pub const TILE_SIZE: f32 = 64.0;
}
use prelude::*;
@ -26,11 +20,7 @@ use prelude::*;
fn main() {
App::build()
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.init_resource::<Map>()
.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())

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,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
map: Res<Map>,
) {
// Camera
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
// Map
let grass_material = materials.add(asset_server.load("grass.png").into());
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;
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;
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);
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);
commands.spawn_bundle(SpriteBundle {
material,
sprite,
transform,
material: grass_material.clone(),
sprite: sprite.clone(),
transform: Transform::from_translation(translation),
..Default::default()
});
}
@ -52,7 +45,7 @@ pub fn add_player(
.insert(Name("Player 1".to_string()))
.insert_bundle(SpriteBundle {
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)),
..Default::default()
});