feat(): bevy 101
This commit is contained in:
parent
c528f2ae55
commit
faf326203e
|
@ -1,3 +1,7 @@
|
||||||
|
# Assets and songs
|
||||||
|
assets
|
||||||
|
songs
|
||||||
|
|
||||||
# ---> Rust
|
# ---> Rust
|
||||||
# Generated by Cargo
|
# Generated by Cargo
|
||||||
# will have compiled files and executables
|
# will have compiled files and executables
|
||||||
|
@ -10,3 +14,8 @@ Cargo.lock
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Added by cargo
|
||||||
|
|
||||||
|
/target
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
[package]
|
||||||
|
name = "sky_hero"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bevy = { version = "0.10.1", features = ["dynamic_linking"] }
|
||||||
|
|
||||||
|
# Enable a small amount of optimization in debug mode
|
||||||
|
[profile.dev]
|
||||||
|
opt-level = 1
|
||||||
|
|
||||||
|
# Enable high optimizations for dependencies (incl. Bevy), but not for our code:
|
||||||
|
[profile.dev.package."*"]
|
||||||
|
opt-level = 3
|
|
@ -0,0 +1,136 @@
|
||||||
|
use bevy::{
|
||||||
|
prelude::*,
|
||||||
|
window::PresentMode,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
struct Person;
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
struct Name(String);
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
struct GreetTimer(Timer);
|
||||||
|
|
||||||
|
pub struct HelloPlugin;
|
||||||
|
|
||||||
|
impl Plugin for HelloPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)))
|
||||||
|
.add_startup_system(add_people)
|
||||||
|
.add_system(greet_people);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
App::new()
|
||||||
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
||||||
|
primary_window: Some(Window {
|
||||||
|
title: "Sky Hero".into(),
|
||||||
|
resolution:(640., 480.).into(),
|
||||||
|
present_mode: PresentMode::AutoVsync,
|
||||||
|
// Tells wasm to resize the window according to the available canvas
|
||||||
|
fit_canvas_to_parent: false,
|
||||||
|
// Tells wasm not to override default event handling, like F5, Ctrl+R etc.
|
||||||
|
prevent_default_event_handling: false,
|
||||||
|
..default()
|
||||||
|
}),
|
||||||
|
..default()}))
|
||||||
|
.add_plugin(HelloPlugin)
|
||||||
|
.add_startup_system(setup)
|
||||||
|
.add_system(button_system)
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn hello_world(time: Res<Time>, mut timer: ResMut<GreetTimer>) {
|
||||||
|
if timer.0.tick(time.delta()).just_finished() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15);
|
||||||
|
const HOVERED_BUTTON: Color = Color::rgb(0.25, 0.25, 0.25);
|
||||||
|
const PRESSED_BUTTON: Color = Color::rgb(0.35, 0.75, 0.35);
|
||||||
|
|
||||||
|
fn button_system(
|
||||||
|
mut interaction_query: Query<
|
||||||
|
(&Interaction, &mut BackgroundColor, &Children),
|
||||||
|
(Changed<Interaction>, With<Button>),
|
||||||
|
>,
|
||||||
|
mut text_query: Query<&mut Text>,
|
||||||
|
) {
|
||||||
|
for (interaction, mut color, children) in &mut interaction_query {
|
||||||
|
let mut text = text_query.get_mut(children[0]).unwrap();
|
||||||
|
match *interaction {
|
||||||
|
Interaction::Clicked => {
|
||||||
|
text.sections[0].value = "Press".to_string();
|
||||||
|
*color = PRESSED_BUTTON.into();
|
||||||
|
println!("TA APPUYE SUR LE BOUTON MAON SANG");
|
||||||
|
}
|
||||||
|
Interaction::Hovered => {
|
||||||
|
text.sections[0].value = "Hover".to_string();
|
||||||
|
*color = HOVERED_BUTTON.into();
|
||||||
|
}
|
||||||
|
Interaction::None => {
|
||||||
|
text.sections[0].value = "Button".to_string();
|
||||||
|
*color = NORMAL_BUTTON.into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_people(mut commands: Commands) {
|
||||||
|
commands.spawn((Person, Name("Elaine Proctor".to_string())));
|
||||||
|
commands.spawn((Person, Name("Renzo Hume".to_string())));
|
||||||
|
commands.spawn((Person, Name("Zayna Nieves".to_string())));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn greet_people(time: Res<Time>, mut timer: ResMut<GreetTimer>, query: Query<&Name, With<Person>>) {
|
||||||
|
if timer.0.tick(time.delta()).just_finished() {
|
||||||
|
for name in &query {
|
||||||
|
println!("hello {}!", name.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
|
// ui camera
|
||||||
|
commands.spawn(Camera2dBundle::default());
|
||||||
|
commands
|
||||||
|
.spawn(NodeBundle {
|
||||||
|
style: Style {
|
||||||
|
size: Size::width(Val::Percent(100.0)),
|
||||||
|
align_items: AlignItems::Center,
|
||||||
|
justify_content: JustifyContent::Center,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
..default()
|
||||||
|
})
|
||||||
|
.with_children(|parent| {
|
||||||
|
parent
|
||||||
|
.spawn(ButtonBundle {
|
||||||
|
style: Style {
|
||||||
|
size: Size::new(Val::Px(150.0), Val::Px(65.0)),
|
||||||
|
// horizontally center child text
|
||||||
|
justify_content: JustifyContent::Center,
|
||||||
|
// vertically center child text
|
||||||
|
align_items: AlignItems::Center,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
background_color: NORMAL_BUTTON.into(),
|
||||||
|
..default()
|
||||||
|
})
|
||||||
|
.with_children(|parent| {
|
||||||
|
parent.spawn(TextBundle::from_section(
|
||||||
|
"Button",
|
||||||
|
TextStyle {
|
||||||
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
||||||
|
font_size: 40.0,
|
||||||
|
color: Color::rgb(0.9, 0.9, 0.9),
|
||||||
|
},
|
||||||
|
));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue