use ggez; use ggez::event; use ggez::graphics::{ self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image, Rect, WrapMode, }; use ggez::nalgebra::{Point2, Vector2}; use rand::{ distributions::{Distribution, Standard}, Rng, }; pub const TILE_SCALE: f32 = 3.0; pub const TILE_HEIGHT: f32 = 16.0; pub const TILE_WIDTH: f32 = 16.0; enum Occupant { None, Green, Yellow, Diamond, Red, } 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, } } } struct Cell { occupant: Occupant, position: Point2, } impl Cell { pub fn new(position: Point2) -> Cell { Cell { occupant: rand::random(), position: position, } } pub fn draw(&self, spritebatch: &mut SpriteBatch) { let source = match self.occupant { Occupant::None => None, Occupant::Green => Some(Rect::new(0.0, 0.0, 0.25, 1.0)), Occupant::Yellow => Some(Rect::new(0.25, 0.0, 0.25, 1.0)), Occupant::Diamond => Some(Rect::new(0.50, 0.0, 0.25, 1.0)), Occupant::Red => Some(Rect::new(0.75, 0.0, 0.25, 1.0)), }; if let Some(source) = source { spritebatch.add( DrawParam::default() .src(source) .dest(self.position) .scale(Vector2::new(TILE_SCALE, TILE_SCALE)), ); } } } struct Game { spritebatch: SpriteBatch, grid: Vec>, } impl Game { fn new(context: &mut ggez::Context) -> ggez::GameResult { let mut image = Image::new(context, "/gem.png")?; image.set_filter(FilterMode::Nearest); image.set_wrap(WrapMode::Mirror, WrapMode::Mirror); let mut grid = Vec::new(); let mut y = 0.0; for _ in 0..8 { let mut column = Vec::new(); column.push(Cell::new(Point2::new(0.0, y))); column.push(Cell::new(Point2::new(TILE_WIDTH * TILE_SCALE, y))); column.push(Cell::new(Point2::new(2.0 * TILE_WIDTH * TILE_SCALE, y))); column.push(Cell::new(Point2::new(3.0 * TILE_WIDTH * TILE_SCALE, y))); column.push(Cell::new(Point2::new(4.0 * TILE_WIDTH * TILE_SCALE, y))); column.push(Cell::new(Point2::new(5.0 * TILE_WIDTH * TILE_SCALE, y))); column.push(Cell::new(Point2::new(6.0 * TILE_WIDTH * TILE_SCALE, y))); column.push(Cell::new(Point2::new(7.0 * TILE_WIDTH * TILE_SCALE, y))); y += TILE_HEIGHT * TILE_SCALE; grid.push(column); } Ok(Game { grid: grid, spritebatch: SpriteBatch::new(image), }) } } impl event::EventHandler for Game { fn update(&mut self, _context: &mut ggez::Context) -> ggez::GameResult { Ok(()) } fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult { graphics::clear(context, [0.1, 0.2, 0.3, 1.0].into()); for row in self.grid.iter() { for cell in row.iter() { cell.draw(&mut self.spritebatch) } } graphics::draw(context, &self.spritebatch, DrawParam::default())?; self.spritebatch.clear(); graphics::present(context)?; Ok(()) } } pub fn main() -> ggez::GameResult { let cb = ggez::ContextBuilder::new("super_simple", "ggez"); let (context, event_loop) = &mut cb.add_resource_path("./resources").build()?; let game = &mut Game::new(context)?; event::run(context, event_loop, game) }