From 758cc8889d75bb8bbd163c55564bc769da585685 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 19 Dec 2023 22:25:13 +0100 Subject: [PATCH] easing to ease transition to board.. --- src/buildings.rs | 16 ++++++++++++++++ src/lib.rs | 2 +- src/overload.rs | 38 ++++++++++++++++++++++++++++++++++---- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/buildings.rs b/src/buildings.rs index bcc8c51..74c31ac 100644 --- a/src/buildings.rs +++ b/src/buildings.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use crate::inventory::{self}; use crate::inventory::{Inventory, SpawnInventory}; use crate::random::RandomDeterministic; @@ -5,12 +7,14 @@ use crate::window::WindowSize; use crate::{GameState, MarkerGameStatePlaying}; use bevy::ecs::system::{EntityCommand, SystemParam, SystemState}; +use bevy::math::vec3; use bevy::prelude::*; use bevy::render::mesh::Indices; use bevy::render::render_resource::PrimitiveTopology; use bevy::sprite::MaterialMesh2dBundle; use bevy::sprite::Mesh2dHandle; use bevy::utils::HashMap; +use bevy_easings::{Ease, EaseFunction, EasingType}; use rand::seq::SliceRandom; pub struct Plugin; @@ -186,6 +190,14 @@ pub(crate) fn spawn_layout(mut commands: Commands, window_size: ResMut>>> = + SystemState::new(world); + let inventory_entity = q_inventory.get_mut(world).single(); world .entity_mut(id) + .set_parent(inventory_entity) .insert(visual) .insert(MarkerGameStatePlaying); } diff --git a/src/lib.rs b/src/lib.rs index 55ff6fd..c79cb27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,7 @@ impl Plugin for GamePlugin { PrimitivesPlugin, OverloadPlugin, )) - .add_plugins((menu_playing::MenuPlayingPlugin, WorldInspectorPlugin::new())) + //.add_plugins((menu_playing::MenuPlayingPlugin, WorldInspectorPlugin::new())) .add_systems( OnExit(GameState::Playing), primitives::ecs_extensions::despawn_entities::, diff --git a/src/overload.rs b/src/overload.rs index 74bac41..17c1b12 100644 --- a/src/overload.rs +++ b/src/overload.rs @@ -1,4 +1,6 @@ +use bevy::math::vec2; use bevy::prelude::*; +use bevy_easings::{custom_ease_system, CustomComponentEase, EaseFunction, EasingType}; use bevy_vector_shapes::prelude::*; use crate::window::WindowSize; @@ -9,6 +11,7 @@ pub struct OverloadPlugin; impl Plugin for OverloadPlugin { fn build(&self, app: &mut App) { + app.add_systems(Update, custom_ease_system::); app.add_systems(Update, draw_ui); app.add_systems(Update, update_overload); app.add_systems(Update, react_to_spawned_enemy); @@ -18,8 +21,30 @@ impl Plugin for OverloadPlugin { } } +#[derive(Component, Default)] +struct OverloadPosition(Vec2); + +impl bevy_easings::Lerp for OverloadPosition { + type Scalar = f32; + + fn lerp(&self, other: &Self, scalar: &Self::Scalar) -> Self { + OverloadPosition(self.0.lerp(other.0, *scalar)) + } +} + fn setup(mut commands: Commands) { - commands.spawn((Overload(0.5f32), MarkerGameStatePlaying)); + commands.spawn(( + Overload(0.5f32), + OverloadPosition(vec2(0f32, 100f32)), + OverloadPosition(vec2(0f32, 100f32)).ease_to( + OverloadPosition(vec2(0f32, 0f32)), + EaseFunction::QuadraticIn, + EasingType::Once { + duration: std::time::Duration::from_secs_f32(0.5f32), + }, + ), + MarkerGameStatePlaying, + )); } /// Basically the HP bar, but it decreases naturally over time @@ -77,12 +102,18 @@ fn draw_overload_bar(painter: &mut ShapePainter, hp: f32) { painter.rect(Vec2::new(width, min_width)); } -fn draw_ui(mut painter: ShapePainter, q_overload: Query<&Overload>, window_size: Res) { - let Ok(overload) = q_overload.get_single() else { +fn draw_ui( + mut painter: ShapePainter, + q_overload: Query<(&Overload, &OverloadPosition)>, + window_size: Res, +) { + let Ok((overload, position)) = q_overload.get_single() else { return; }; // translate to the center-top of the screen painter.translate(Vec3::Y * window_size.size.y / 2.0); + info!("{:?}", position.0); + painter.translate(position.0.extend(0f32)); painter.scale(Vec3::ONE * 300.0); draw_overload_bar(&mut painter, overload.0); @@ -92,7 +123,6 @@ fn update_overload(time: Res