diff options
author | Tom Barrett <tom@tombarrett.xyz> | 2021-01-31 14:28:41 +0100 |
---|---|---|
committer | Tom Barrett <tom@tombarrett.xyz> | 2021-01-31 14:28:41 +0100 |
commit | ea6c184e7b058429be0049c3bdc749303d05dabd (patch) | |
tree | 761fb7ca212c91aebc31111bde99f16a381fea6d /src/main.rs | |
parent | 6a322b552583e75f8dc5b08ca1e1983c0e90af5b (diff) |
drawm
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 129 |
1 files changed, 105 insertions, 24 deletions
diff --git a/src/main.rs b/src/main.rs index 4e18421..1caa62a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,51 +1,132 @@ use ggez; use ggez::event; -use ggez::graphics; -use ggez::nalgebra as na; -use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image, WrapMode}; +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<Occupant> for Standard { + fn sample<R: Rng + ?Sized>(&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<f32>, +} + +impl Cell { + pub fn new(position: Point2<f32>) -> 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<Vec<Cell>>, } -impl MainState { - fn new() -> ggez::GameResult<Game> { +impl Game { + fn new(context: &mut ggez::Context) -> ggez::GameResult<Game> { 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(MainState { spritebatch: SpriteBatch::new(image) }) + Ok(Game { + grid: grid, + spritebatch: SpriteBatch::new(image), + }) } } -impl event::EventHandler for MainState { - fn update(&mut self, _ctx: &mut ggez::Context) -> ggez::GameResult { - self.pos_x = self.pos_x % 800.0 + 1.0; +impl event::EventHandler for Game { + fn update(&mut self, _context: &mut ggez::Context) -> ggez::GameResult { Ok(()) } - fn draw(&mut self, ctx: &mut ggez::Context) -> ggez::GameResult { - graphics::clear(ctx, [0.1, 0.2, 0.3, 1.0].into()); + 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())?; - let circle = graphics::Mesh::new_circle( - ctx, - graphics::DrawMode::fill(), - na::Point2::new(self.pos_x, 380.0), - 100.0, - 2.0, - graphics::WHITE, - )?; - graphics::draw(ctx, &circle, (na::Point2::new(0.0, 0.0),))?; + self.spritebatch.clear(); - graphics::present(ctx)?; + graphics::present(context)?; Ok(()) } } pub fn main() -> ggez::GameResult { let cb = ggez::ContextBuilder::new("super_simple", "ggez"); - let (ctx, event_loop) = &mut cb.build()?; - let state = &mut MainState::new()?; - event::run(ctx, event_loop, state) + let (context, event_loop) = &mut cb.add_resource_path("./resources").build()?; + let game = &mut Game::new(context)?; + event::run(context, event_loop, game) } |