Some animations
This commit is contained in:
parent
e4b14d94ac
commit
809d472c56
6 changed files with 71 additions and 31 deletions
|
|
@ -4,17 +4,23 @@ use bevy::prelude::*;
|
|||
#[require(Transform)]
|
||||
pub struct AnimatedTransform {
|
||||
pub translation: Vec3,
|
||||
pub rotation: Quat,
|
||||
pub scale: Vec3,
|
||||
}
|
||||
|
||||
impl AnimatedTransform {
|
||||
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {
|
||||
AnimatedTransform {
|
||||
translation: Vec3 { x, y, z },
|
||||
rotation: Quat::IDENTITY,
|
||||
scale: Vec3::ONE,
|
||||
}
|
||||
}
|
||||
pub fn identity() -> Self {
|
||||
AnimatedTransform {
|
||||
translation: Vec3::ZERO,
|
||||
rotation: Quat::IDENTITY,
|
||||
scale: Vec3::ONE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -22,5 +28,7 @@ impl AnimatedTransform {
|
|||
pub fn transform_animation(query: Query<(&mut Transform, &AnimatedTransform)>) {
|
||||
for (mut transform, anim_transform) in query {
|
||||
transform.translation = transform.translation.lerp(anim_transform.translation, 0.25);
|
||||
transform.rotation = transform.rotation.lerp(anim_transform.rotation, 0.25);
|
||||
transform.scale = transform.scale.lerp(anim_transform.scale, 0.25);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy::{picking::hover::PickingInteraction, prelude::*};
|
||||
|
||||
use crate::{
|
||||
animation::transform::AnimatedTransform,
|
||||
card::{Card, CardInHand, CardType, Hand},
|
||||
mouse_position::MousePosition,
|
||||
};
|
||||
|
||||
#[derive(EntityEvent)]
|
||||
|
|
@ -14,26 +15,6 @@ pub struct CardDropped {
|
|||
#[derive(Component)]
|
||||
pub struct DraggableCard;
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct MousePosition(pub Vec3);
|
||||
|
||||
pub fn mouse_position_system(
|
||||
mut mouse: ResMut<MousePosition>,
|
||||
window: Single<&Window>,
|
||||
camera: Single<(&Camera, &GlobalTransform)>,
|
||||
) {
|
||||
if let Some(Ok(mouse_pos)) = window
|
||||
.cursor_position()
|
||||
.and_then(|viewport_pos| Some(camera.0.viewport_to_world_2d(camera.1, viewport_pos)))
|
||||
{
|
||||
mouse.0 = Vec3 {
|
||||
x: mouse_pos.x,
|
||||
y: mouse_pos.y,
|
||||
z: 0.,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn card_drag_start(
|
||||
event: On<Pointer<DragStart>>,
|
||||
mut commands: Commands,
|
||||
|
|
@ -56,8 +37,12 @@ pub fn card_drag(
|
|||
mut card_transform: Query<&mut AnimatedTransform, With<Card>>,
|
||||
mouse_pos: Res<MousePosition>,
|
||||
) {
|
||||
const MAX_DISTANCE: f32 = 2.0;
|
||||
if let Ok(mut card) = card_transform.get_mut(event.entity) {
|
||||
card.translation = mouse_pos.0.truncate().extend(10.);
|
||||
card.translation = mouse_pos.position.truncate().extend(10.);
|
||||
card.rotation = Quat::from_rotation_z(
|
||||
-0.261799387799 * (mouse_pos.delta.x / MAX_DISTANCE).clamp(-1., 1.),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -57,20 +57,26 @@ pub struct ClearCards;
|
|||
impl Plugin for CardPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, hand_setup)
|
||||
.add_systems(PreUpdate, card_transform_reset)
|
||||
.add_systems(Update, card_arrange)
|
||||
.add_observer(card_add)
|
||||
.add_observer(card_clear)
|
||||
.add_observer(insert_card)
|
||||
.add_observer(despawn_card);
|
||||
app.insert_resource(MousePosition(Vec3::new(0., 0., 0.)))
|
||||
.add_systems(PreUpdate, mouse_position_system)
|
||||
.add_observer(card_drag_start)
|
||||
app.add_observer(card_drag_start)
|
||||
.add_observer(card_drag)
|
||||
.add_observer(card_drag_end);
|
||||
app.add_observer(card_playing_system);
|
||||
}
|
||||
}
|
||||
|
||||
fn card_transform_reset(query: Query<&mut AnimatedTransform, With<Card>>) {
|
||||
for mut card in query {
|
||||
card.rotation = Quat::IDENTITY;
|
||||
card.scale = Vec3::ONE
|
||||
}
|
||||
}
|
||||
|
||||
fn hand_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
commands.spawn((
|
||||
Hand(Vec::new()),
|
||||
|
|
|
|||
|
|
@ -2,11 +2,9 @@ use bevy::prelude::*;
|
|||
|
||||
use crate::{
|
||||
animation::transform::AnimatedTransform,
|
||||
card::{
|
||||
CardInHand, InsertCard,
|
||||
drag::{CardDropped, MousePosition},
|
||||
},
|
||||
card::{CardInHand, InsertCard, drag::CardDropped},
|
||||
grid::Grid,
|
||||
mouse_position::MousePosition,
|
||||
turns::TurnUnit,
|
||||
unit::UnitTextures,
|
||||
};
|
||||
|
|
@ -28,12 +26,12 @@ pub fn card_playing_system(
|
|||
let unit = commands
|
||||
.spawn((
|
||||
AnimatedTransform::identity(),
|
||||
Transform::from_translation(mouse_position.0),
|
||||
Transform::from_translation(mouse_position.position),
|
||||
bundle,
|
||||
TurnUnit,
|
||||
))
|
||||
.id();
|
||||
if grid.try_set(mouse_position.0.truncate(), unit) {
|
||||
if grid.try_set(mouse_position.position.truncate(), unit) {
|
||||
commands.entity(event.entity).despawn();
|
||||
} else {
|
||||
commands.entity(unit).despawn();
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ mod buttons;
|
|||
mod card;
|
||||
mod dev_tools;
|
||||
mod grid;
|
||||
mod mouse_position;
|
||||
mod music_player;
|
||||
mod turns;
|
||||
mod unit;
|
||||
|
|
@ -21,6 +22,7 @@ fn main() {
|
|||
turns::TurnSystemPlugin,
|
||||
unit::UnitPlugin,
|
||||
music_player::MusicPlayerPlugin,
|
||||
mouse_position::MousePositionPlugin,
|
||||
))
|
||||
.add_systems(Startup, setup)
|
||||
.run();
|
||||
|
|
|
|||
41
src/mouse_position.rs
Normal file
41
src/mouse_position.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
pub struct MousePositionPlugin;
|
||||
|
||||
impl Plugin for MousePositionPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_resource(MousePosition {
|
||||
position: Vec3::ZERO,
|
||||
delta: Vec3::ZERO,
|
||||
})
|
||||
.add_systems(PreUpdate, mouse_position_system);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct MousePosition {
|
||||
pub position: Vec3,
|
||||
pub delta: Vec3,
|
||||
}
|
||||
|
||||
pub fn mouse_position_system(
|
||||
mut mouse: ResMut<MousePosition>,
|
||||
window: Single<&Window>,
|
||||
camera: Single<(&Camera, &GlobalTransform)>,
|
||||
) {
|
||||
if let Some(Ok(mouse_pos)) = window
|
||||
.cursor_position()
|
||||
.and_then(|viewport_pos| Some(camera.0.viewport_to_world_2d(camera.1, viewport_pos)))
|
||||
{
|
||||
mouse.delta = Vec3 {
|
||||
x: mouse_pos.x - mouse.position.x,
|
||||
y: mouse_pos.y - mouse.position.y,
|
||||
z: 0.,
|
||||
};
|
||||
mouse.position = Vec3 {
|
||||
x: mouse_pos.x,
|
||||
y: mouse_pos.y,
|
||||
z: 0.,
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue