Compare commits
12 Commits
32f6a822a2
...
main
Author | SHA1 | Date | |
---|---|---|---|
7b56c52fff
|
|||
6a2c77404b
|
|||
aa7e888d9e
|
|||
170088b836
|
|||
34d1de5847
|
|||
70e4ac0c81
|
|||
ef4a98627e
|
|||
e816846d19
|
|||
9e16c22eb5
|
|||
66854be921
|
|||
04fe103b25
|
|||
4ab62ea921
|
@ -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
|
||||||
|
BIN
assets/grass.png
BIN
assets/grass.png
Binary file not shown.
Before Width: | Height: | Size: 281 B After Width: | Height: | Size: 153 B |
BIN
assets/marshmallow.png
Normal file
BIN
assets/marshmallow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 335 B |
BIN
assets/wall.png
BIN
assets/wall.png
Binary file not shown.
Before Width: | Height: | Size: 381 B After Width: | Height: | Size: 624 B |
@ -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();
|
||||||
}
|
}
|
||||||
|
@ -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(
|
||||||
|
Reference in New Issue
Block a user