Skip to content

Commit

Permalink
easing to ease transition to board..
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Dec 19, 2023
1 parent bbbe714 commit 758cc88
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/buildings.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use std::time::Duration;

use crate::inventory::{self};
use crate::inventory::{Inventory, SpawnInventory};
use crate::random::RandomDeterministic;
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;
Expand Down Expand Up @@ -186,6 +190,14 @@ pub(crate) fn spawn_layout(mut commands: Commands, window_size: ResMut<WindowSiz
positions: positions_from_anchor_point(anchor_point),
},
))
.insert(SpatialBundle::default())
.insert(Transform::from_translation(vec3(-100.0, 0.0, 0.0)).ease_to(
Transform::from_translation(vec3(0.0, 0.0, 0.0)),
EaseFunction::QuadraticIn,
EasingType::Once {
duration: Duration::from_secs_f32(0.5f32),
},
))
.insert(MarkerGameStatePlaying)
.insert(RandomDeterministic::new_from_seed(0));
}
Expand Down Expand Up @@ -264,8 +276,12 @@ impl EntityCommand for BuildingItemSpriteBuilder {
material: assets.color_def[&self.building.color].clone(),
..default()
};
let mut q_inventory: SystemState<Query<Entity, With<Inventory<Building>>>> =
SystemState::new(world);
let inventory_entity = q_inventory.get_mut(world).single();
world
.entity_mut(id)
.set_parent(inventory_entity)
.insert(visual)
.insert(MarkerGameStatePlaying);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MarkerGameStatePlaying>,
Expand Down
38 changes: 34 additions & 4 deletions src/overload.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +11,7 @@ pub struct OverloadPlugin;

impl Plugin for OverloadPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, custom_ease_system::<OverloadPosition>);
app.add_systems(Update, draw_ui);
app.add_systems(Update, update_overload);
app.add_systems(Update, react_to_spawned_enemy);
Expand All @@ -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
Expand Down Expand Up @@ -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<WindowSize>) {
let Ok(overload) = q_overload.get_single() else {
fn draw_ui(
mut painter: ShapePainter,
q_overload: Query<(&Overload, &OverloadPosition)>,
window_size: Res<WindowSize>,
) {
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);
Expand All @@ -92,7 +123,6 @@ fn update_overload(time: Res<Time>, mut q_overload: Query<&mut Overload>) {
let Ok(mut overload) = q_overload.get_single_mut() else {
return;
};
//dbg!(&overload);
overload.0 = (overload.0 - 0.03 * time.delta_seconds()).clamp(0.0, 1.0);
}

Expand Down

0 comments on commit 758cc88

Please sign in to comment.