summaryrefslogtreecommitdiff
path: root/src/cell.rs
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2022-04-24 21:45:30 +0200
committerTom Barrett <tom@tombarrett.xyz>2022-04-24 21:45:30 +0200
commit718a09f8fd87d6c0d7e69ff0744802cba040b576 (patch)
tree8f359c1e518f2f64b6962d529058a4889b71cb16 /src/cell.rs
parentd8a5e679e931c302da03d675caf4bd208dbde988 (diff)
0_7
Diffstat (limited to 'src/cell.rs')
-rw-r--r--src/cell.rs79
1 files changed, 60 insertions, 19 deletions
diff --git a/src/cell.rs b/src/cell.rs
index e4be4ac..6a818c5 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -1,10 +1,48 @@
use crate::constants;
use bevy::prelude::*;
+use bevy::utils::Duration;
use rand::{
distributions::{Distribution, Standard},
Rng,
};
+#[derive(Component)]
+pub struct AnimationTimer {
+ timer: Timer,
+}
+
+impl AnimationTimer {
+ pub fn from_seconds(duration: f32, repeating: bool) -> AnimationTimer {
+ AnimationTimer {
+ timer: Timer::from_seconds(duration, repeating),
+ }
+ }
+
+ pub fn finished(&self) -> bool {
+ self.timer.finished()
+ }
+
+ pub fn tick(&mut self, delta: Duration) -> &Timer {
+ self.timer.tick(delta)
+ }
+
+ pub fn just_finished(&self) -> bool {
+ self.timer.just_finished()
+ }
+
+ pub fn reset(&mut self) {
+ self.timer.reset();
+ }
+
+ pub fn set_duration(&mut self, duration: Duration) {
+ self.timer.set_duration(duration);
+ }
+
+ pub fn repeating(&self) -> bool {
+ self.timer.repeating()
+ }
+}
+
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Occupant {
None,
@@ -23,7 +61,7 @@ impl Default for Occupant {
}
impl Occupant {
- pub fn to_index(&self) -> u32 {
+ pub fn to_index(&self) -> usize {
match self {
Occupant::Green => constants::TILESHEET_GREEN,
Occupant::Yellow => constants::TILESHEET_YELLOW,
@@ -49,7 +87,7 @@ impl Distribution<Occupant> for Standard {
}
}
-#[derive(Debug, Clone, Default, Copy)]
+#[derive(Debug, Clone, Default, Copy, Component)]
pub struct Cell {
pub x: usize,
pub y: usize,
@@ -73,37 +111,40 @@ impl Cell {
&mut self,
occupant: Occupant,
sprite: &mut TextureAtlasSprite,
- visible: &mut Visible,
+ visibility: &mut Visibility,
) {
self.occupant = occupant;
sprite.index = self.occupant.to_index();
if self.occupant == Occupant::None {
- visible.is_visible = false;
+ visibility.is_visible = false;
} else {
- visible.is_visible = true;
+ visibility.is_visible = true;
}
}
}
-pub fn insert(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) {
- for (mut cell, mut sprite, mut visible) in q.iter_mut() {
+pub fn insert(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visibility)>) {
+ for (mut cell, mut sprite, mut visibility) in q.iter_mut() {
if cell.occupant == Occupant::None && cell.y == constants::GRID_SIZE - 1 {
- cell.set_occupant(rand::random(), &mut sprite, &mut visible);
+ cell.set_occupant(rand::random(), &mut sprite, &mut visibility);
}
}
}
-pub fn start_explosion(mut commands: Commands, mut q: Query<(Entity, &Cell), Without<Timer>>) {
+pub fn start_explosion(
+ mut commands: Commands,
+ mut q: Query<(Entity, &Cell), Without<AnimationTimer>>,
+) {
for (entity, cell) in q.iter_mut() {
if cell.occupant == Occupant::Explosion {
commands
.entity(entity)
- .insert(Timer::from_seconds(0.1, true));
+ .insert(AnimationTimer::from_seconds(0.1, true));
}
}
}
-pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) {
+pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visibility)>) {
let mut cells = [[Cell::default(); constants::GRID_SIZE]; constants::GRID_SIZE];
for (cell, _, _) in q.iter_mut() {
cells[cell.x][cell.y] = *cell;
@@ -150,39 +191,39 @@ pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) {
for c in connected.iter() {
for (i, j) in c.iter() {
- for (mut cell, mut sprite, mut visible) in q.iter_mut() {
+ for (mut cell, mut sprite, mut visibility) in q.iter_mut() {
if &cell.x == i && &cell.y == j && cell.occupant != Occupant::Explosion {
- cell.set_occupant(Occupant::Explosion, &mut sprite, &mut visible);
+ cell.set_occupant(Occupant::Explosion, &mut sprite, &mut visibility);
}
}
}
}
}
-pub fn falling(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) {
+pub fn falling(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visibility)>) {
let mut have_gems = Vec::new();
- for (cell, _sprite, _visible) in q.iter_mut() {
+ for (cell, _sprite, _visibility) in q.iter_mut() {
if cell.occupant != Occupant::None {
have_gems.push(*cell);
}
}
let mut moved_gems = Vec::new();
- for (mut cell, mut sprite, mut visible) in q.iter_mut() {
+ for (mut cell, mut sprite, mut visibility) in q.iter_mut() {
if cell.occupant == Occupant::None {
if let Some(c) = have_gems
.iter()
.find(|&c| (c.x, c.y) == (cell.x, cell.y + 1))
{
- cell.set_occupant(c.occupant, &mut sprite, &mut visible);
+ cell.set_occupant(c.occupant, &mut sprite, &mut visibility);
moved_gems.push(c);
}
}
}
- for (mut cell, mut sprite, mut visible) in q.iter_mut() {
+ for (mut cell, mut sprite, mut visibility) in q.iter_mut() {
if moved_gems.iter().any(|c| (c.x, c.y) == (cell.x, cell.y)) {
- cell.set_occupant(Occupant::None, &mut sprite, &mut visible);
+ cell.set_occupant(Occupant::None, &mut sprite, &mut visibility);
}
}
}