From 965ccb56c4c58066939d74bea22c2a9b59416d0d Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Sun, 21 Mar 2021 17:51:55 +0100 Subject: moved cell stuff to another file --- src/cell.rs | 207 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 215 ++---------------------------------------------------------- 3 files changed, 214 insertions(+), 209 deletions(-) create mode 100644 src/cell.rs diff --git a/src/cell.rs b/src/cell.rs new file mode 100644 index 0000000..6b42fdf --- /dev/null +++ b/src/cell.rs @@ -0,0 +1,207 @@ +use crate::constants; +use bevy::prelude::*; +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; + +#[derive(Debug, PartialEq, Clone, Copy)] +pub enum Occupant { + None, + Green, + Yellow, + Red, + Diamond, + Explosion, +} + +impl Default for Occupant { + fn default() -> Occupant { + Occupant::None + } +} + +impl Occupant { + pub fn to_index(&self) -> u32 { + match self { + Occupant::Green => 0, + Occupant::Yellow => 1, + Occupant::Red => 2, + Occupant::Diamond => 3, + Occupant::Explosion => 4, + Occupant::None => 13, + } + } +} + +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Occupant { + match rng.gen_range(0..=3) { + 0 => Occupant::Green, + 1 => Occupant::Yellow, + 2 => Occupant::Diamond, + 3 => Occupant::Red, + _ => Occupant::None, + } + } +} + +#[derive(Debug, Clone, Default, Copy)] +pub struct Cell { + pub x: usize, + pub y: usize, + pub occupant: Occupant, + pub selected: bool, + pub hovered: bool, +} + +impl Cell { + pub fn new(x: usize, y: usize) -> Cell { + Cell { + x, + y, + occupant: Occupant::None, + selected: false, + hovered: false, + } + } +} + +pub fn insert(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) { + for (mut cell, mut sprite) in cell_query.iter_mut() { + if cell.occupant == Occupant::None && cell.y == constants::ROWS - 1 { + cell.occupant = rand::random(); + sprite.index = cell.occupant.to_index(); + } + } +} + +pub fn explosion_animation( + time: Res