grid indexing issues fixed

This commit is contained in:
Rendo 2026-01-27 23:56:00 +05:00
commit 0aa3cdd5cd
4 changed files with 19 additions and 9 deletions

View file

@ -2,7 +2,7 @@ use bevy::prelude::*;
use crate::{ use crate::{
animation::transform::AnimatedTransform, animation::transform::AnimatedTransform,
card::{Card, CardInHand, Hand, InsertCard}, card::{Card, CardInHand, Hand},
}; };
#[derive(EntityEvent)] #[derive(EntityEvent)]
@ -54,7 +54,7 @@ pub fn card_drag(
mouse_pos: Res<MousePosition>, mouse_pos: Res<MousePosition>,
) { ) {
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; card.translation = mouse_pos.0.truncate().extend(10.);
} }
} }

View file

@ -51,7 +51,7 @@ impl Plugin for CardPlugin {
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()),
Transform::from_xyz(0., -360., 0.), Transform::from_xyz(0., -360., 10.),
InheritedVisibility::VISIBLE, InheritedVisibility::VISIBLE,
)); ));
@ -86,8 +86,8 @@ fn card_add(
let card = commands let card = commands
.spawn(( .spawn((
Sprite::from_image(card_image.0.clone()), Sprite::from_image(card_image.0.clone()),
Transform::from_xyz(0., 0., 0.), Transform::from_xyz(0., 0., 10.),
AnimatedTransform::from_xyz(0., 0., 0.), AnimatedTransform::from_xyz(0., 0., 10.),
Card, Card,
DraggableCard, DraggableCard,
CardInHand, CardInHand,

View file

@ -20,6 +20,7 @@ pub fn card_playing_system(
let fosma = commands let fosma = commands
.spawn(( .spawn((
AnimatedTransform::identity(), AnimatedTransform::identity(),
Transform::from_translation(mouse_position.0),
Sprite::from_image(textures.fosma_texture()), Sprite::from_image(textures.fosma_texture()),
)) ))
.id(); .id();

View file

@ -38,8 +38,10 @@ impl Grid {
let x = index % self.columns; let x = index % self.columns;
let y = (index / self.columns).clamp(0, self.rows - 1); let y = (index / self.columns).clamp(0, self.rows - 1);
(self.bounds.min + self.cell_size() / 2. + vec2(x as f32, y as f32) * self.cell_size()) (self.upper_left_corner()
.extend(0.) + self.cell_size() / 2.
+ vec2(x as f32, y as f32) * self.cell_size())
.extend(0.)
} }
pub fn is_in_grid(&self, position: Vec2) -> bool { pub fn is_in_grid(&self, position: Vec2) -> bool {
@ -67,13 +69,20 @@ impl Grid {
true true
} }
#[inline]
fn cell_size(&self) -> Vec2 { fn cell_size(&self) -> Vec2 {
self.bounds.size() / vec2(self.columns as f32, -(self.rows as f32)) self.bounds.size() / vec2(self.columns as f32, -(self.rows as f32))
} }
#[inline]
fn indexify_position(&self, position: Vec2) -> usize { fn indexify_position(&self, position: Vec2) -> usize {
let indexified_position = (position / self.cell_size()).floor(); let indexified_position = ((position - self.bounds.min) / self.cell_size()).floor();
indexified_position.x as usize + indexified_position.y as usize * self.columns indexified_position.x as usize + (5. + indexified_position.y) as usize * self.columns
}
#[inline]
fn upper_left_corner(&self) -> Vec2 {
Vec2::new(self.bounds.min.x, self.bounds.max.y)
} }
} }