card playing 1/2
This commit is contained in:
parent
0e3afb5e7a
commit
c24a9f3cec
7 changed files with 117 additions and 2 deletions
36
src/grid.rs
36
src/grid.rs
|
|
@ -12,7 +12,7 @@ impl Plugin for GridPlugin {
|
|||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Grid {
|
||||
pub struct Grid {
|
||||
bounds: Rect,
|
||||
columns: usize,
|
||||
rows: usize,
|
||||
|
|
@ -42,9 +42,43 @@ impl Grid {
|
|||
.extend(0.)
|
||||
}
|
||||
|
||||
pub fn is_in_grid(&self, position: Vec2) -> bool {
|
||||
self.bounds.contains(position)
|
||||
}
|
||||
|
||||
pub fn is_occupied(&self, position: Vec2) -> bool {
|
||||
if self.is_in_grid(position) == false {
|
||||
return true;
|
||||
}
|
||||
|
||||
match self.elements[self.indexify_position(position)] {
|
||||
Some(_) => true,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_free(&self, position: Vec2) -> bool {
|
||||
self.is_occupied(position) == false
|
||||
}
|
||||
|
||||
pub fn try_set(&mut self, position: Vec2, entity: Entity) -> bool {
|
||||
if self.is_occupied(position) {
|
||||
return false;
|
||||
}
|
||||
let index = self.indexify_position(position);
|
||||
|
||||
self.elements[index] = Some(entity);
|
||||
true
|
||||
}
|
||||
|
||||
fn cell_size(&self) -> Vec2 {
|
||||
self.bounds.size() / vec2(self.columns as f32, -(self.rows as f32))
|
||||
}
|
||||
|
||||
fn indexify_position(&self, position: Vec2) -> usize {
|
||||
let indexified_position = (position / self.cell_size()).floor();
|
||||
indexified_position.x as usize + indexified_position.y as usize * self.columns
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_grid(mut commands: Commands) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue