Compare commits

...

12 Commits

6 changed files with 77 additions and 4 deletions

View File

@ -8,3 +8,9 @@ edition = "2018"
[dependencies] [dependencies]
bevy = { version = "0.5", features = ["dynamic"] } bevy = { version = "0.5", features = ["dynamic"] }
[profile.dev.package."*"]
opt-level = 3
[profile.dev]
opt-level = 1

Binary file not shown.

Before

Width:  |  Height:  |  Size: 281 B

After

Width:  |  Height:  |  Size: 153 B

BIN
assets/marshmallow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 335 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 381 B

After

Width:  |  Height:  |  Size: 624 B

View File

@ -9,6 +9,10 @@ mod prelude {
pub use crate::resources::*; pub use crate::resources::*;
pub use crate::systems::*; pub use crate::systems::*;
pub use bevy::prelude::*; pub use bevy::prelude::*;
pub const SCREEN_WIDTH: f32 = 80.0;
pub const SCREEN_HEIGHT: f32 = 50.0;
pub const TILE_SIZE: f32 = 64.0;
} }
use prelude::*; use prelude::*;
@ -18,5 +22,8 @@ fn main() {
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0))) .insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_startup_system(setup.system()) .add_startup_system(setup.system())
.add_startup_system(add_player.system())
.add_system(player_movement.system())
.add_system(follow_player.system())
.run(); .run();
} }

View File

@ -1,4 +1,5 @@
use crate::prelude::*; use crate::prelude::*;
use bevy::render::camera::Camera;
pub fn setup( pub fn setup(
mut commands: Commands, mut commands: Commands,
@ -10,9 +11,9 @@ pub fn setup(
// 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(32.0, 32.0); let tile_size = Vec2::new(TILE_SIZE, TILE_SIZE);
let sprite = Sprite::new(tile_size); let sprite = Sprite::new(tile_size);
let map_size = Vec2::new(80.0, 50.0); let map_size = Vec2::new(SCREEN_WIDTH, SCREEN_HEIGHT);
let half_x = (map_size.x / 2.0) as i32; let half_x = (map_size.x / 2.0) as i32;
let half_y = (map_size.y / 2.0) as i32; let half_y = (map_size.y / 2.0) as i32;
@ -31,11 +32,70 @@ pub fn setup(
} }
} }
pub fn add_player(mut commands: Commands) { pub fn add_player(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let player_material = materials.add(asset_server.load("marshmallow.png").into());
commands commands
.spawn() .spawn()
.insert(Player) .insert(Player)
.insert(Name("Player 1".to_string())); .insert(Name("Player 1".to_string()))
.insert_bundle(SpriteBundle {
material: player_material,
sprite: Sprite::new(Vec2::new(TILE_SIZE, TILE_SIZE)),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
..Default::default()
});
}
pub fn player_movement(
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&Player, &mut Transform)>,
time: Res<Time>,
) {
if let Ok((_, mut transform)) = query.single_mut() {
let mut direction = Vec2::ZERO;
if keyboard_input.pressed(KeyCode::A) {
direction.x -= 1.0;
}
if keyboard_input.pressed(KeyCode::D) {
direction.x += 1.0;
}
if keyboard_input.pressed(KeyCode::S) {
direction.y -= 1.0;
}
if keyboard_input.pressed(KeyCode::W) {
direction.y += 1.0;
}
let translation = &mut transform.translation;
let delta_seconds = time.delta_seconds();
translation.x += delta_seconds * direction.x * 300.0;
translation.y += delta_seconds * direction.y * 300.0;
}
}
pub fn follow_player(
mut q: QuerySet<(
Query<&Transform, With<Player>>,
Query<&mut Transform, With<Camera>>,
)>,
) {
if let Ok(player_transform) = q.q0().single() {
let player_translation = player_transform.translation;
if let Ok(mut camera_transform) = q.q1_mut().single_mut() {
let camera_translation = &mut camera_transform.translation;
camera_translation.x = player_translation.x;
camera_translation.y = player_translation.y;
}
}
} }
pub fn greet_player( pub fn greet_player(