summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 1caa62a5c2f01e94de52d9cc5ce1eaa10beac889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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<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 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(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)
}