summaryrefslogtreecommitdiff
path: root/src/cell.rs
blob: b085c5ef56b8cef6e9220dc159f57dec50678fc4 (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
133
134
135
136
137
138
139
140
141
142
143
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<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,
        }
    }
}

#[derive(Clone, Copy)]
pub struct Cell {
    pub occupant: Occupant,
    position: Point2<f32>,
    hover: bool,
    clicked: bool,
}

impl Cell {
    pub fn new(position: Point2<f32>) -> Cell {
        Cell {
            occupant: rand::random(),
            position,
            hover: false,
            clicked: false,
        }
    }

    pub fn contains(&self, position: Point2<f32>) -> 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(())
    }
}