bevy-snake-tutorial/src/main.rs

39 lines
1.1 KiB
Rust
Raw Normal View History

2020-11-04 10:09:02 -05:00
use bevy::prelude::*;
2020-12-24 13:41:55 -05:00
struct SnakeHead;
struct Materials {
head_material: Handle<ColorMaterial>,
}
fn setup(commands: &mut Commands, mut materials: ResMut<Assets<ColorMaterial>>) {
2020-12-24 13:34:47 -05:00
commands.spawn(Camera2dBundle::default());
2020-12-24 13:41:55 -05:00
commands.insert_resource(Materials {
head_material: materials.add(Color::rgb(0.7, 0.7, 0.7).into()),
});
}
fn spawn_snake(commands: &mut Commands, materials: Res<Materials>) {
commands
.spawn(SpriteBundle {
material: materials.head_material.clone(),
sprite: Sprite::new(Vec2::new(10.0, 10.0)),
..Default::default()
})
.with(SnakeHead);
2020-11-04 10:09:02 -05:00
}
2020-12-24 13:45:07 -05:00
fn snake_movement(mut head_positions: Query<(&SnakeHead, &mut Transform)>) {
for (_head, mut transform) in head_positions.iter_mut() {
transform.translation.y += 2.;
}
}
2020-11-04 10:09:02 -05:00
fn main() {
App::build()
.add_startup_system(setup.system())
2020-12-24 13:41:55 -05:00
.add_startup_stage("game_setup", SystemStage::single(spawn_snake.system()))
2020-12-24 13:45:07 -05:00
.add_system(snake_movement.system())
2020-12-24 13:34:47 -05:00
.add_plugins(DefaultPlugins)
2020-11-04 10:09:02 -05:00
.run();
}