card placing 2/2

This commit is contained in:
Rendo 2026-01-27 20:29:52 +05:00
commit e46d4517b9
3 changed files with 22 additions and 32 deletions

View file

@ -2,7 +2,7 @@ use bevy::prelude::*;
use crate::{ use crate::{
animation::transform::AnimatedTransform, animation::transform::AnimatedTransform,
card::{CardInHand, Hand, InsertCard}, card::{Card, CardInHand, Hand, InsertCard},
}; };
#[derive(EntityEvent)] #[derive(EntityEvent)]
@ -11,9 +11,6 @@ pub struct CardDropped(pub Entity);
#[derive(Component)] #[derive(Component)]
pub struct DraggableCard; pub struct DraggableCard;
#[derive(Component)]
pub struct DraggedCard;
#[derive(Resource)] #[derive(Resource)]
pub struct MousePosition(pub Vec3); pub struct MousePosition(pub Vec3);
@ -38,20 +35,13 @@ pub fn card_drag_start(
event: On<Pointer<DragStart>>, event: On<Pointer<DragStart>>,
mut commands: Commands, mut commands: Commands,
card_query: Query<(), With<DraggableCard>>, card_query: Query<(), With<DraggableCard>>,
dragged_option: Option<Single<&DraggedCard>>,
mut hand: Single<&mut Hand>, mut hand: Single<&mut Hand>,
) { ) {
if dragged_option.is_some() {
return;
}
if let Err(_) = card_query.get(event.entity) { if let Err(_) = card_query.get(event.entity) {
return; return;
} }
commands commands.entity(event.entity).remove::<CardInHand>();
.entity(event.entity)
.insert(DraggedCard)
.remove::<CardInHand>();
if let Some(pos) = hand.0.iter().position(|ent| *ent == event.entity) { if let Some(pos) = hand.0.iter().position(|ent| *ent == event.entity) {
hand.0.remove(pos); hand.0.remove(pos);
@ -59,22 +49,21 @@ pub fn card_drag_start(
} }
pub fn card_drag( pub fn card_drag(
_: On<Pointer<Drag>>, event: On<Pointer<Drag>>,
mut card_transform: Single<&mut AnimatedTransform, With<DraggedCard>>, mut card_transform: Query<&mut AnimatedTransform, With<Card>>,
mouse_pos: Res<MousePosition>, mouse_pos: Res<MousePosition>,
) { ) {
card_transform.translation = mouse_pos.0; if let Ok(mut card) = card_transform.get_mut(event.entity) {
card.translation = mouse_pos.0;
}
} }
pub fn card_drag_end( pub fn card_drag_end(
_: On<Pointer<DragEnd>>, event: On<Pointer<DragEnd>>,
mut commands: Commands, mut commands: Commands,
dragged: Single<Entity, With<DraggedCard>>, cards: Query<(), With<Card>>,
) { ) {
commands.trigger(InsertCard(dragged.entity())); if let Ok(_) = cards.get(event.entity) {
commands.trigger(CardDropped(dragged.entity())); commands.trigger(CardDropped(event.entity));
commands }
.entity(dragged.entity())
.remove::<DraggedCard>()
.insert(CardInHand);
} }

View file

@ -2,7 +2,10 @@ use bevy::prelude::*;
use crate::{ use crate::{
animation::transform::AnimatedTransform, animation::transform::AnimatedTransform,
card::drag::{CardDropped, MousePosition}, card::{
CardInHand, InsertCard,
drag::{CardDropped, MousePosition},
},
grid::Grid, grid::Grid,
unit::UnitTextures, unit::UnitTextures,
}; };
@ -24,5 +27,7 @@ pub fn card_playing_system(
commands.entity(event.0).despawn(); commands.entity(event.0).despawn();
} else { } else {
commands.entity(fosma).despawn(); commands.entity(fosma).despawn();
commands.entity(event.0).insert(CardInHand);
commands.trigger(InsertCard(event.0));
} }
} }

View file

@ -24,10 +24,10 @@ impl Grid {
let mut elements: Vec<Option<Entity>> = Vec::new(); let mut elements: Vec<Option<Entity>> = Vec::new();
elements.resize(cells * cells, None); elements.resize(cells * cells, None);
Grid { Grid {
bounds: Rect { bounds: Rect::from_corners(
min: start, start,
max: start + Vec2::new(side * (cells as f32), side * (cells as f32)), start + Vec2::new(side * (cells as f32), -side * (cells as f32)),
}, ),
columns: cells, columns: cells,
rows: cells, rows: cells,
elements: elements, elements: elements,
@ -57,10 +57,6 @@ impl Grid {
} }
} }
pub fn is_free(&self, position: Vec2) -> bool {
self.is_occupied(position) == false
}
pub fn try_set(&mut self, position: Vec2, entity: Entity) -> bool { pub fn try_set(&mut self, position: Vec2, entity: Entity) -> bool {
if self.is_occupied(position) { if self.is_occupied(position) {
return false; return false;