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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
use ggez::event::{self, EventHandler};
use ggez::graphics::{
self, spritebatch::SpriteBatch, Color, DrawMode, DrawParam, FilterMode, Image, Mesh, Rect,
StrokeOptions, WrapMode,
};
use ggez::input::mouse;
use ggez::mint::{Point2, Vector2};
use ggez::{Context, ContextBuilder, GameResult};
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;
pub const BORDER_SIZE: f32 = 3.0;
pub const SHIFT_X: f32 = 50.0;
pub const SHIFT_Y: f32 = 50.0;
#[derive(Clone, Copy)]
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,
}
}
}
#[derive(Clone, Copy)]
struct Cell {
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 {
if position.x > self.position.x
&& position.y > self.position.y
&& position.x < self.position.x + TILE_WIDTH * TILE_SCALE
&& position.y < self.position.y + TILE_WIDTH * TILE_SCALE
{
return true;
}
false
}
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::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 {
x: TILE_SCALE,
y: TILE_SCALE,
}),
);
}
if self.hover {
let mesh = Mesh::new_rectangle(
context,
DrawMode::Stroke(StrokeOptions::default()),
Rect::new(
self.position.x,
self.position.y,
TILE_WIDTH * TILE_SCALE,
TILE_HEIGHT * 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,
TILE_WIDTH * TILE_SCALE,
TILE_HEIGHT * TILE_SCALE,
),
Color::from_rgb(100, 255, 100),
)?;
graphics::draw(context, &mesh, DrawParam::default())?;
}
Ok(())
}
}
struct Game {
selected: Option<(usize, usize)>,
spritebatch: SpriteBatch,
grid: Vec<Vec<Cell>>,
background: Image,
}
impl Game {
fn new(context: &mut Context) -> GameResult<Game> {
let mut background = Image::new(context, "/background.png")?;
background.set_filter(FilterMode::Nearest);
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 {
x: SHIFT_X,
y: y + SHIFT_Y,
}));
column.push(Cell::new(Point2 {
x: 1.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE) + SHIFT_X,
y: y + SHIFT_Y,
}));
column.push(Cell::new(Point2 {
x: 2.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE) + SHIFT_X,
y: y + SHIFT_Y,
}));
column.push(Cell::new(Point2 {
x: 3.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE) + SHIFT_X,
y: y + SHIFT_Y,
}));
column.push(Cell::new(Point2 {
x: 4.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE) + SHIFT_X,
y: y + SHIFT_Y,
}));
column.push(Cell::new(Point2 {
x: 5.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE) + SHIFT_X,
y: y + SHIFT_Y,
}));
column.push(Cell::new(Point2 {
x: 6.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE) + SHIFT_X,
y: y + SHIFT_Y,
}));
column.push(Cell::new(Point2 {
x: 7.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE) + SHIFT_X,
y: y + SHIFT_Y,
}));
y += TILE_HEIGHT * TILE_SCALE + BORDER_SIZE;
grid.push(column);
}
Ok(Game {
grid,
selected: None,
spritebatch: SpriteBatch::new(image),
background,
})
}
}
impl EventHandler for Game {
fn update(&mut self, context: &mut Context) -> GameResult {
for row in self.grid.iter_mut() {
for cell in row.iter_mut() {
cell.hover_off();
}
}
let position = mouse::position(context);
for row in self.grid.iter_mut() {
for cell in row.iter_mut() {
if cell.contains(position) {
cell.hover_on();
}
}
}
Ok(())
}
fn mouse_button_down_event(
&mut self,
_context: &mut Context,
button: mouse::MouseButton,
x: f32,
y: f32,
) {
if button == mouse::MouseButton::Left {
let position = Point2 { x, y };
for (i, row) in self.grid.iter_mut().enumerate() {
for (j, cell) in row.iter_mut().enumerate() {
if cell.contains(position) {
self.selected = Some((i, j));
cell.clicked_on();
}
}
}
}
}
fn mouse_button_up_event(
&mut self,
_context: &mut Context,
button: mouse::MouseButton,
x: f32,
y: f32,
) {
if button == mouse::MouseButton::Left {
for row in self.grid.iter_mut() {
for cell in row.iter_mut() {
cell.clicked_off();
}
}
if let Some(selected) = self.selected {
let position = Point2 { x, y };
let mut swap = None;
for (i, row) in self.grid.iter_mut().enumerate() {
for (j, cell) in row.iter_mut().enumerate() {
if cell.contains(position)
&& (((i + 1 == selected.0) && (j == selected.1))
|| ((i.overflowing_sub(1).0 == selected.0) && (j == selected.1))
|| ((i == selected.0) && (j + 1 == selected.1))
|| ((i == selected.0) && (j.overflowing_sub(1)).0 == selected.1))
{
swap = Some((i, j));
}
}
}
if let Some((i, j)) = swap {
let clone = self.grid[i][j].occupant;
self.grid[i][j].occupant = self.grid[selected.0][selected.1].occupant;
self.grid[selected.0][selected.1].occupant = clone;
self.selected = None;
}
}
}
}
fn draw(&mut self, context: &mut Context) -> GameResult {
graphics::clear(context, [0.0, 0.0, 0.0, 1.0].into());
graphics::draw(context, &self.background, DrawParam::default())?;
for row in self.grid.iter() {
for cell in row.iter() {
cell.draw(context, &mut self.spritebatch)?;
}
}
graphics::draw(context, &self.spritebatch, DrawParam::default())?;
self.spritebatch.clear();
graphics::present(context)?;
Ok(())
}
}
pub fn main() -> GameResult {
let (ref mut context, ref mut event_loop) = ContextBuilder::new("gems", "jw&tb")
.add_resource_path("./resources")
.build()?;
let game = &mut Game::new(context)?;
event::run(context, event_loop, game)
}
|