Assigning and drawing health

main
Elijah Voigt 1 month ago
parent db480af595
commit 76733e20d8

@ -4,8 +4,9 @@
use bevy::{ use bevy::{
asset::RenderAssetUsages, asset::RenderAssetUsages,
math::FloatOrd, math::{FloatOrd},
render::{ render::{
mesh::MeshAabb,
camera::{ImageRenderTarget, RenderTarget}, camera::{ImageRenderTarget, RenderTarget},
render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages}, render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages},
view::RenderLayers, view::RenderLayers,
@ -79,6 +80,8 @@ fn main() {
.run_if(any_component_changed::<Line>) .run_if(any_component_changed::<Line>)
.after(clear_line), .after(clear_line),
assert_grid_position_uniqueness.run_if(any_component_changed::<GridPosition>), assert_grid_position_uniqueness.run_if(any_component_changed::<GridPosition>),
sync_health
.run_if(any_component_changed::<Health>.or(any_component_added::<Health>)),
), ),
) )
// UI systems // UI systems
@ -245,6 +248,9 @@ impl Display for Score {
} }
} }
#[derive(Component, Debug)]
struct Health(f32);
/// ShapesBuffer resource stores non-active shapes /// ShapesBuffer resource stores non-active shapes
#[derive(Resource, Debug, Default)] #[derive(Resource, Debug, Default)]
struct ShapesBuffer { struct ShapesBuffer {
@ -314,6 +320,12 @@ fn init_tetris(
}); });
} }
#[derive(Component, Debug)]
struct Protagonist;
#[derive(Component, Debug)]
struct Enemy;
fn init_battler( fn init_battler(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
@ -331,7 +343,9 @@ fn init_battler(
MeshMaterial2d(mat), MeshMaterial2d(mat),
t, t,
BATTLER, BATTLER,
Health(100.0),
Name::new("Protagonist"), Name::new("Protagonist"),
Protagonist,
)); ));
} }
@ -347,12 +361,13 @@ fn init_battler(
MeshMaterial2d(mat), MeshMaterial2d(mat),
t, t,
BATTLER, BATTLER,
Health(100.0),
Name::new("ENEMY"), Name::new("ENEMY"),
Enemy,
)); ));
} }
} }
#[derive(Resource, Default)] #[derive(Resource, Default)]
struct OutputImages { struct OutputImages {
tetris: Handle<Image>, tetris: Handle<Image>,
@ -482,7 +497,8 @@ fn init_ui(mut commands: Commands, output_images: Res<OutputImages>, images: Res
}); });
}); });
commands.spawn(( commands
.spawn((
Node { Node {
align_self: AlignSelf::End, align_self: AlignSelf::End,
justify_self: JustifySelf::Center, justify_self: JustifySelf::Center,
@ -490,7 +506,8 @@ fn init_ui(mut commands: Commands, output_images: Res<OutputImages>, images: Res
..default() ..default()
}, },
BorderColor(WHITE.into()), BorderColor(WHITE.into()),
)).with_children(|parent| { ))
.with_children(|parent| {
let img = images.get(&output_images.tetris).unwrap(); let img = images.get(&output_images.tetris).unwrap();
parent.spawn(( parent.spawn((
Node { Node {
@ -506,7 +523,8 @@ fn init_ui(mut commands: Commands, output_images: Res<OutputImages>, images: Res
)); ));
}); });
commands.spawn(( commands
.spawn((
Node { Node {
align_self: AlignSelf::Start, align_self: AlignSelf::Start,
justify_self: JustifySelf::Center, justify_self: JustifySelf::Center,
@ -514,7 +532,8 @@ fn init_ui(mut commands: Commands, output_images: Res<OutputImages>, images: Res
..default() ..default()
}, },
BorderColor(WHITE.into()), BorderColor(WHITE.into()),
)).with_children(|parent| { ))
.with_children(|parent| {
let img = images.get(&output_images.battler).unwrap(); let img = images.get(&output_images.battler).unwrap();
parent.spawn(( parent.spawn((
Node { Node {
@ -995,7 +1014,10 @@ enum Movement {
// TODO: When out of bounds left/right, try to move piece away from wall // TODO: When out of bounds left/right, try to move piece away from wall
fn movement( fn movement(
trigger: Trigger<Movement>, trigger: Trigger<Movement>,
mut grid_positions: Query<&mut GridPosition, Or<(With<ShapeBlock>, With<ShapeBlocks>, Without<LineBlock>)>>, mut grid_positions: Query<
&mut GridPosition,
Or<(With<ShapeBlock>, With<ShapeBlocks>, Without<LineBlock>)>,
>,
mut shape: Query<&mut Shape>, mut shape: Query<&mut Shape>,
inactive: Query<&GridPosition, (Without<ShapeBlock>, Without<ShapeBlocks>, With<LineBlock>)>, inactive: Query<&GridPosition, (Without<ShapeBlock>, Without<ShapeBlocks>, With<LineBlock>)>,
mut commands: Commands, mut commands: Commands,
@ -1106,6 +1128,7 @@ fn deactivate_shape(
parent: Query<&ShapeBlocks>, parent: Query<&ShapeBlocks>,
lines: Query<(Entity, &Line), With<LineBlocks>>, lines: Query<(Entity, &Line), With<LineBlocks>>,
mut commands: Commands, mut commands: Commands,
mut enemy_health: Single<&mut Health, With<Enemy>>
) { ) {
events.read().for_each(|target| { events.read().for_each(|target| {
parent.iter_descendants(target).for_each(|block| { parent.iter_descendants(target).for_each(|block| {
@ -1122,6 +1145,9 @@ fn deactivate_shape(
} }
}); });
commands.entity(target).despawn(); commands.entity(target).despawn();
// TODO: Turn this into an event
enemy_health.0 -= 1.0;
}); });
} }
@ -1143,9 +1169,41 @@ fn update_next_shapes(mut buffer: ResMut<ShapesBuffer>) {
} }
fn assert_grid_position_uniqueness( fn assert_grid_position_uniqueness(
grid_positions: Query<&GridPosition, (Without<GridBackground>, Without<Shape>)> grid_positions: Query<&GridPosition, (Without<GridBackground>, Without<Shape>)>,
) { ) {
grid_positions.iter_combinations().for_each(|[a, b]| { grid_positions.iter_combinations().for_each(|[a, b]| {
assert_ne!(a, b, "Two entities are in the same grid position!"); assert_ne!(a, b, "Two entities are in the same grid position!");
}); });
} }
fn sync_health(
query: Query<(Entity, &Health, &Mesh2d), Or<(Changed<Health>, Added<Health>)>>,
parent: Query<&Children>,
meshes: Res<Assets<Mesh>>,
mut texts: Query<&mut Text2d>,
mut commands: Commands,
) {
query.iter().for_each(|(e, h, m)| {
if let Some(child) = parent
.iter_descendants(e)
.find(|child| texts.contains(*child))
{
info!("Updating health");
let mut t = texts.get_mut(child).unwrap();
t.0 = format!("{}", h.0);
} else {
info!("Creating health display");
commands.entity(e).with_children(|parent| {
let mesh = meshes.get(&m.0).unwrap();
let aabb = mesh.compute_aabb().unwrap();
let offset = Vec3::new(0.0, aabb.half_extents.y + 10.0, 0.0);
parent.spawn((
Text2d(format!("{}", h.0)),
TextColor(BLACK.into()),
Transform::from_translation(offset),
BATTLER,
));
});
}
})
}

Loading…
Cancel
Save