From d2c59a4d07efa3ec4418d7e82352a1cdf38e1518 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Fri, 19 Mar 2021 19:21:11 +0100 Subject: branch trying out bevy --- src/cell.rs | 143 ------------------------------------------------------------ 1 file changed, 143 deletions(-) delete mode 100644 src/cell.rs (limited to 'src/cell.rs') diff --git a/src/cell.rs b/src/cell.rs deleted file mode 100644 index b085c5e..0000000 --- a/src/cell.rs +++ /dev/null @@ -1,143 +0,0 @@ -use crate::constants; -use ggez::graphics::{ - self, spritebatch::SpriteBatch, Color, DrawMode, DrawParam, Mesh, Rect, StrokeOptions, -}; -use ggez::mint::{Point2, Vector2}; -use ggez::{Context, GameResult}; -use rand::{ - distributions::{Distribution, Standard}, - Rng, -}; - -use std::time::Instant; -#[derive(Clone, Copy, PartialEq)] -pub enum Occupant { - None, - Green, - Yellow, - Diamond, - Red, - Explosion { frame: usize, timer: Instant }, -} - -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(Clone, Copy)] -pub struct Cell { - pub occupant: Occupant, - position: Point2, - hover: bool, - clicked: bool, -} - -impl Cell { - pub fn new(position: Point2) -> Cell { - Cell { - occupant: rand::random(), - position, - hover: false, - clicked: false, - } - } - - pub fn contains(&self, position: Point2) -> bool { - position.x > self.position.x - && position.y > self.position.y - && position.x < self.position.x + constants::TILE_WIDTH * constants::TILE_SCALE - && position.y < self.position.y + constants::TILE_WIDTH * constants::TILE_SCALE - } - - pub fn moveable(&self) -> bool { - //!matches!(self.occupant, Occupant::Explosion{ ..} | Occupant::None) - match self.occupant { - Occupant::Explosion { .. } => false, - Occupant::None => false, - _ => true, - } - } - - pub fn clicked_on(&mut self) { - self.clicked = true; - } - - pub fn clicked_off(&mut self) { - self.clicked = false; - } - - pub fn hover_on(&mut self) { - self.hover = true; - } - - pub fn hover_off(&mut self) { - self.hover = false; - } - - pub fn draw(&self, context: &mut Context, spritebatch: &mut SpriteBatch) -> GameResult { - let source = match self.occupant { - Occupant::None => None, - Occupant::Explosion { frame, .. } => match frame { - 0 => Some(Rect::new(0.0, 0.5, 0.25, 0.5)), - 1 => Some(Rect::new(0.25, 0.5, 0.25, 0.5)), - 2 => Some(Rect::new(0.50, 0.5, 0.25, 0.5)), - 3 => Some(Rect::new(0.75, 0.5, 0.25, 0.5)), - _ => None, - }, - Occupant::Green => Some(Rect::new(0.0, 0.0, 0.25, 0.5)), - Occupant::Yellow => Some(Rect::new(0.25, 0.0, 0.25, 0.5)), - Occupant::Diamond => Some(Rect::new(0.50, 0.0, 0.25, 0.5)), - Occupant::Red => Some(Rect::new(0.75, 0.0, 0.25, 0.5)), - }; - - if let Some(source) = source { - spritebatch.add( - DrawParam::default() - .src(source) - .dest(self.position) - .scale(Vector2 { - x: constants::TILE_SCALE, - y: constants::TILE_SCALE, - }), - ); - } - - if self.hover { - let mesh = Mesh::new_rectangle( - context, - DrawMode::Stroke(StrokeOptions::default()), - Rect::new( - self.position.x, - self.position.y, - constants::TILE_WIDTH * constants::TILE_SCALE, - constants::TILE_HEIGHT * constants::TILE_SCALE, - ), - Color::from_rgb(255, 100, 100), - )?; - graphics::draw(context, &mesh, DrawParam::default())?; - } - if self.clicked { - let mesh = Mesh::new_rectangle( - context, - DrawMode::Stroke(StrokeOptions::default()), - Rect::new( - self.position.x, - self.position.y, - constants::TILE_WIDTH * constants::TILE_SCALE, - constants::TILE_HEIGHT * constants::TILE_SCALE, - ), - Color::from_rgb(100, 255, 100), - )?; - graphics::draw(context, &mesh, DrawParam::default())?; - } - Ok(()) - } -} -- cgit v1.2.3 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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 src/cell.rs (limited to '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