Add egui as a UI system

This commit is contained in:
2023-10-17 13:53:11 -05:00
parent ad2a304455
commit c5a6fc769d
5 changed files with 333 additions and 9 deletions

View File

@@ -12,6 +12,11 @@ pub struct Armor {
pub ac: u32,
}
#[derive(Component)]
pub struct Named {
pub name: String,
}
#[derive(Bundle)]
pub struct Creature {
pub health: Health,
@@ -20,7 +25,9 @@ pub struct Creature {
}
#[derive(Component)]
pub struct PlayerCharacter;
pub struct PlayerCharacter {
pub name: String,
}
#[derive(Component)]
pub struct Battle(pub HashMap<u32, Entity>);

View File

@@ -1,4 +1,5 @@
use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts, EguiPlugin};
pub mod util;
pub mod components;
@@ -10,16 +11,18 @@ use util::roll;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(EguiPlugin)
.add_systems(Startup, setup)
.add_systems(Update, gui)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let names = ["Dane", "Cole"];
for i in 0..2 {
let (hp, _) = roll("1d6 + 3");
commands.spawn(Creature {
@@ -35,7 +38,9 @@ fn setup(
..default()
},
})
.insert(PlayerCharacter)
.insert(PlayerCharacter {
name: names[i].to_string(),
})
.with_children(|parent| {
parent.spawn(PbrBundle {
mesh: meshes.add(shape::Capsule::default().into()),
@@ -47,10 +52,27 @@ fn setup(
commands.spawn(DirectionalLightBundle {
transform: Transform::default().looking_at(Vec3::new(3.0, -50.0, 1.0), Vec3::Y),
..default()
..default::<_>()
});
commands.spawn(Camera3dBundle{
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(1.0, 15.0, 2.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
fn gui(
query: Query<(&Health, &PlayerCharacter)>,
mut contexts: EguiContexts
) {
let ctx = contexts.ctx_mut();
egui::Window::new("Battle Menu")
.title_bar(false)
.collapsible(false)
.resizable(false)
.movable(false)
.show(ctx, |ui| {
for (health, pc) in query.iter() {
ui.label(format!("{} HP: {}", pc.name, health.hp));
}
});
}

View File

@@ -4,12 +4,14 @@ use bevy::prelude::*;
use crate::components::*;
use crate::util::roll;
pub fn initiate_battle(query: Query<Entity, With<PlayerCharacter>>, mut commands: Commands) {
pub fn initiate_battle(
query: Query<Entity, With<PlayerCharacter>>,
mut commands: Commands
) {
let order: HashMap<u32, Entity> = query.iter().map(|ent| {
let (total, _rolls) = roll("1d20");
(total, ent)
let (total, _rolls) = roll("1d20");
(total, ent)
}).collect();
commands.spawn(Battle(order));
}