Compare commits
17 Commits
6705870f81
...
main
Author | SHA1 | Date | |
---|---|---|---|
7b56c52fff
|
|||
6a2c77404b
|
|||
aa7e888d9e
|
|||
170088b836
|
|||
34d1de5847
|
|||
70e4ac0c81
|
|||
ef4a98627e
|
|||
e816846d19
|
|||
9e16c22eb5
|
|||
66854be921
|
|||
04fe103b25
|
|||
4ab62ea921
|
|||
32f6a822a2
|
|||
89ddbd2483
|
|||
b7b89b801b
|
|||
3fe46fd116
|
|||
bc5eb9b006
|
@ -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
Normal file
BIN
assets/grass.png
Normal file
Binary file not shown.
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
Normal file
BIN
assets/wall.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 624 B |
23
src/main.rs
23
src/main.rs
@ -1,12 +1,29 @@
|
|||||||
mod components;
|
mod components;
|
||||||
|
mod plugins;
|
||||||
|
mod resources;
|
||||||
mod systems;
|
mod systems;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
mod prelude {
|
||||||
use systems::*;
|
pub use crate::components::*;
|
||||||
|
pub use crate::plugins::*;
|
||||||
|
pub use crate::resources::*;
|
||||||
|
pub use crate::systems::*;
|
||||||
|
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::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::build()
|
App::build()
|
||||||
|
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
|
||||||
|
.add_plugins(DefaultPlugins)
|
||||||
|
.add_startup_system(setup.system())
|
||||||
.add_startup_system(add_player.system())
|
.add_startup_system(add_player.system())
|
||||||
.add_system(greet_player.system())
|
.add_system(player_movement.system())
|
||||||
|
.add_system(follow_player.system())
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
11
src/plugins/mod.rs
Normal file
11
src/plugins/mod.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
pub struct PlayerPlugin;
|
||||||
|
|
||||||
|
impl Plugin for PlayerPlugin {
|
||||||
|
fn build(&self, app: &mut AppBuilder) {
|
||||||
|
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, true)))
|
||||||
|
.add_startup_system(add_player.system())
|
||||||
|
.add_system(greet_player.system());
|
||||||
|
}
|
||||||
|
}
|
3
src/resources/mod.rs
Normal file
3
src/resources/mod.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
pub struct GreetTimer(pub Timer);
|
@ -1,15 +1,111 @@
|
|||||||
use crate::components::{Name, Player};
|
use crate::prelude::*;
|
||||||
use bevy::prelude::*;
|
use bevy::render::camera::Camera;
|
||||||
|
|
||||||
|
pub fn setup(
|
||||||
|
mut commands: Commands,
|
||||||
|
asset_server: Res<AssetServer>,
|
||||||
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||||
|
) {
|
||||||
|
// Camera
|
||||||
|
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
|
||||||
|
|
||||||
|
// Map
|
||||||
|
let grass_material = materials.add(asset_server.load("grass.png").into());
|
||||||
|
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 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: grass_material.clone(),
|
||||||
|
sprite: sprite.clone(),
|
||||||
|
transform: Transform::from_translation(translation),
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
|
||||||
pub fn add_player(mut commands: Commands) {
|
|
||||||
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 greet_player(query: Query<&Name, With<Player>>) {
|
pub fn player_movement(
|
||||||
for name in query.iter() {
|
keyboard_input: Res<Input<KeyCode>>,
|
||||||
println!("Hello, {}!", name.0);
|
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(
|
||||||
|
query: Query<&Name, With<Player>>,
|
||||||
|
time: Res<Time>,
|
||||||
|
mut timer: ResMut<GreetTimer>,
|
||||||
|
) {
|
||||||
|
if timer.0.tick(time.delta()).just_finished() {
|
||||||
|
for name in query.iter() {
|
||||||
|
println!("Hello, {}!", name.0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user