summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--resources/gem.pngbin387 -> 1288 bytes
-rw-r--r--src/main.rs129
4 files changed, 107 insertions, 24 deletions
diff --git a/Cargo.lock b/Cargo.lock
index c979f2a..8be645f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -564,6 +564,7 @@ name = "gems"
version = "0.1.0"
dependencies = [
"ggez",
+ "rand 0.8.3",
]
[[package]]
diff --git a/Cargo.toml b/Cargo.toml
index 9c0202e..e3cd0e1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,3 +8,4 @@ edition = "2018"
[dependencies]
ggez = "0.5.1"
+rand = "0.8.0"
diff --git a/resources/gem.png b/resources/gem.png
index e1fa943..47b21ee 100644
--- a/resources/gem.png
+++ b/resources/gem.png
Binary files differ
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)
}