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)]
|
#[require(Transform)]
|
||||||
pub struct AnimatedTransform {
|
pub struct AnimatedTransform {
|
||||||
pub translation: Vec3,
|
pub translation: Vec3,
|
||||||
|
pub rotation: Quat,
|
||||||
|
pub scale: Vec3,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AnimatedTransform {
|
impl AnimatedTransform {
|
||||||
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {
|
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {
|
||||||
AnimatedTransform {
|
AnimatedTransform {
|
||||||
translation: Vec3 { x, y, z },
|
translation: Vec3 { x, y, z },
|
||||||
|
rotation: Quat::IDENTITY,
|
||||||
|
scale: Vec3::ONE,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn identity() -> Self {
|
pub fn identity() -> Self {
|
||||||
AnimatedTransform {
|
AnimatedTransform {
|
||||||
translation: Vec3::ZERO,
|
translation: Vec3::ZERO,
|
||||||
|
rotation: Quat::IDENTITY,
|
||||||
|
scale: Vec3::ONE,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -22,5 +28,7 @@ impl AnimatedTransform {
|
||||||
pub fn transform_animation(query: Query<(&mut Transform, &AnimatedTransform)>) {
|
pub fn transform_animation(query: Query<(&mut Transform, &AnimatedTransform)>) {
|
||||||
for (mut transform, anim_transform) in query {
|
for (mut transform, anim_transform) in query {
|
||||||
transform.translation = transform.translation.lerp(anim_transform.translation, 0.25);
|
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::{
|
use crate::{
|
||||||
animation::transform::AnimatedTransform,
|
animation::transform::AnimatedTransform,
|
||||||
card::{Card, CardInHand, CardType, Hand},
|
card::{Card, CardInHand, CardType, Hand},
|
||||||
|
mouse_position::MousePosition,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(EntityEvent)]
|
#[derive(EntityEvent)]
|
||||||
|
|
@ -14,26 +15,6 @@ pub struct CardDropped {
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct DraggableCard;
|
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(
|
pub fn card_drag_start(
|
||||||
event: On<Pointer<DragStart>>,
|
event: On<Pointer<DragStart>>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
|
@ -56,8 +37,12 @@ pub fn card_drag(
|
||||||
mut card_transform: Query<&mut AnimatedTransform, With<Card>>,
|
mut card_transform: Query<&mut AnimatedTransform, With<Card>>,
|
||||||
mouse_pos: Res<MousePosition>,
|
mouse_pos: Res<MousePosition>,
|
||||||
) {
|
) {
|
||||||
|
const MAX_DISTANCE: f32 = 2.0;
|
||||||
if let Ok(mut card) = card_transform.get_mut(event.entity) {
|
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 {
|
impl Plugin for CardPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(Startup, hand_setup)
|
app.add_systems(Startup, hand_setup)
|
||||||
|
.add_systems(PreUpdate, card_transform_reset)
|
||||||
.add_systems(Update, card_arrange)
|
.add_systems(Update, card_arrange)
|
||||||
.add_observer(card_add)
|
.add_observer(card_add)
|
||||||
.add_observer(card_clear)
|
.add_observer(card_clear)
|
||||||
.add_observer(insert_card)
|
.add_observer(insert_card)
|
||||||
.add_observer(despawn_card);
|
.add_observer(despawn_card);
|
||||||
app.insert_resource(MousePosition(Vec3::new(0., 0., 0.)))
|
app.add_observer(card_drag_start)
|
||||||
.add_systems(PreUpdate, mouse_position_system)
|
|
||||||
.add_observer(card_drag_start)
|
|
||||||
.add_observer(card_drag)
|
.add_observer(card_drag)
|
||||||
.add_observer(card_drag_end);
|
.add_observer(card_drag_end);
|
||||||
app.add_observer(card_playing_system);
|
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>) {
|
fn hand_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Hand(Vec::new()),
|
Hand(Vec::new()),
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,9 @@ use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
animation::transform::AnimatedTransform,
|
animation::transform::AnimatedTransform,
|
||||||
card::{
|
card::{CardInHand, InsertCard, drag::CardDropped},
|
||||||
CardInHand, InsertCard,
|
|
||||||
drag::{CardDropped, MousePosition},
|
|
||||||
},
|
|
||||||
grid::Grid,
|
grid::Grid,
|
||||||
|
mouse_position::MousePosition,
|
||||||
turns::TurnUnit,
|
turns::TurnUnit,
|
||||||
unit::UnitTextures,
|
unit::UnitTextures,
|
||||||
};
|
};
|
||||||
|
|
@ -28,12 +26,12 @@ pub fn card_playing_system(
|
||||||
let unit = commands
|
let unit = commands
|
||||||
.spawn((
|
.spawn((
|
||||||
AnimatedTransform::identity(),
|
AnimatedTransform::identity(),
|
||||||
Transform::from_translation(mouse_position.0),
|
Transform::from_translation(mouse_position.position),
|
||||||
bundle,
|
bundle,
|
||||||
TurnUnit,
|
TurnUnit,
|
||||||
))
|
))
|
||||||
.id();
|
.id();
|
||||||
if grid.try_set(mouse_position.0.truncate(), unit) {
|
if grid.try_set(mouse_position.position.truncate(), unit) {
|
||||||
commands.entity(event.entity).despawn();
|
commands.entity(event.entity).despawn();
|
||||||
} else {
|
} else {
|
||||||
commands.entity(unit).despawn();
|
commands.entity(unit).despawn();
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ mod buttons;
|
||||||
mod card;
|
mod card;
|
||||||
mod dev_tools;
|
mod dev_tools;
|
||||||
mod grid;
|
mod grid;
|
||||||
|
mod mouse_position;
|
||||||
mod music_player;
|
mod music_player;
|
||||||
mod turns;
|
mod turns;
|
||||||
mod unit;
|
mod unit;
|
||||||
|
|
@ -21,6 +22,7 @@ fn main() {
|
||||||
turns::TurnSystemPlugin,
|
turns::TurnSystemPlugin,
|
||||||
unit::UnitPlugin,
|
unit::UnitPlugin,
|
||||||
music_player::MusicPlayerPlugin,
|
music_player::MusicPlayerPlugin,
|
||||||
|
mouse_position::MousePositionPlugin,
|
||||||
))
|
))
|
||||||
.add_systems(Startup, setup)
|
.add_systems(Startup, setup)
|
||||||
.run();
|
.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