This repository was archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.rs
305 lines (273 loc) · 9.02 KB
/
main.rs
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
use quicksilver::prelude::*;
use std::collections::HashMap;
#[derive(Clone, Debug, PartialEq)]
struct Tile {
pos: Vector,
glyph: char,
color: Color,
}
fn generate_map(size: Vector) -> Vec<Tile> {
let width = size.x as usize;
let height = size.y as usize;
let mut map = Vec::with_capacity(width * height);
for x in 0..width {
for y in 0..height {
let mut tile = Tile {
pos: Vector::new(x as f32, y as f32),
glyph: '.',
color: Color::BLACK,
};
if x == 0 || x == width - 1 || y == 0 || y == height - 1 {
tile.glyph = '#';
};
map.push(tile);
}
}
map
}
#[derive(Clone, Debug, PartialEq)]
struct Entity {
pos: Vector,
glyph: char,
color: Color,
hp: i32,
max_hp: i32,
}
fn generate_entities() -> Vec<Entity> {
vec![
Entity {
pos: Vector::new(9, 6),
glyph: 'g',
color: Color::RED,
hp: 1,
max_hp: 1,
},
Entity {
pos: Vector::new(2, 4),
glyph: 'g',
color: Color::RED,
hp: 1,
max_hp: 1,
},
Entity {
pos: Vector::new(7, 5),
glyph: '%',
color: Color::PURPLE,
hp: 0,
max_hp: 0,
},
Entity {
pos: Vector::new(4, 8),
glyph: '%',
color: Color::PURPLE,
hp: 0,
max_hp: 0,
},
]
}
struct Game {
title: Asset<Image>,
mononoki_font_info: Asset<Image>,
square_font_info: Asset<Image>,
inventory: Asset<Image>,
map_size: Vector,
map: Vec<Tile>,
entities: Vec<Entity>,
player_id: usize,
tileset: Asset<HashMap<char, Image>>,
tile_size_px: Vector,
}
impl State for Game {
/// Load the assets and initialise the game
fn new() -> Result<Self> {
// The Mononoki font: https://madmalik.github.io/mononoki/
// License: SIL Open Font License 1.1
let font_mononoki = "mononoki-Regular.ttf";
let title = Asset::new(Font::load(font_mononoki).and_then(|font| {
font.render("Quicksilver Roguelike", &FontStyle::new(72.0, Color::BLACK))
}));
let mononoki_font_info = Asset::new(Font::load(font_mononoki).and_then(|font| {
font.render(
"Mononoki font by Matthias Tellen, terms: SIL Open Font License 1.1",
&FontStyle::new(20.0, Color::BLACK),
)
}));
let square_font_info = Asset::new(Font::load(font_mononoki).and_then(move |font| {
font.render(
"Square font by Wouter Van Oortmerssen, terms: CC BY 3.0",
&FontStyle::new(20.0, Color::BLACK),
)
}));
let inventory = Asset::new(Font::load(font_mononoki).and_then(move |font| {
font.render(
"Inventory:\n[A] Sword\n[B] Shield\n[C] Darts",
&FontStyle::new(20.0, Color::BLACK),
)
}));
let map_size = Vector::new(20, 15);
let map = generate_map(map_size);
let mut entities = generate_entities();
let player_id = entities.len();
entities.push(Entity {
pos: Vector::new(5, 3),
glyph: '@',
color: Color::BLUE,
hp: 3,
max_hp: 5,
});
// The Square font: http://strlen.com/square/?s[]=font
// License: CC BY 3.0 https://creativecommons.org/licenses/by/3.0/deed.en_US
let font_square = "square.ttf";
let game_glyphs = "#@g.%";
let tile_size_px = Vector::new(24, 24);
let tileset = Asset::new(Font::load(font_square).and_then(move |text| {
let tiles = text
.render(game_glyphs, &FontStyle::new(tile_size_px.y, Color::WHITE))
.expect("Could not render the font tileset.");
let mut tileset = HashMap::new();
for (index, glyph) in game_glyphs.chars().enumerate() {
let pos = (index as i32 * tile_size_px.x as i32, 0);
let tile = tiles.subimage(Rectangle::new(pos, tile_size_px));
tileset.insert(glyph, tile);
}
Ok(tileset)
}));
Ok(Self {
title,
mononoki_font_info,
square_font_info,
inventory,
map_size,
map,
entities,
player_id,
tileset,
tile_size_px,
})
}
/// Process keyboard and mouse, update the game state
fn update(&mut self, window: &mut Window) -> Result<()> {
use ButtonState::*;
let player = &mut self.entities[self.player_id];
if window.keyboard()[Key::Left] == Pressed {
player.pos.x -= 1.0;
}
if window.keyboard()[Key::Right] == Pressed {
player.pos.x += 1.0;
}
if window.keyboard()[Key::Up] == Pressed {
player.pos.y -= 1.0;
}
if window.keyboard()[Key::Down] == Pressed {
player.pos.y += 1.0;
}
if window.keyboard()[Key::Escape].is_down() {
window.close();
}
Ok(())
}
/// Draw stuff on the screen
fn draw(&mut self, window: &mut Window) -> Result<()> {
window.clear(Color::WHITE)?;
// Draw the game title
self.title.execute(|image| {
window.draw(
&image
.area()
.with_center((window.screen_size().x as i32 / 2, 40)),
Img(&image),
);
Ok(())
})?;
// Draw the mononoki font credits
self.mononoki_font_info.execute(|image| {
window.draw(
&image
.area()
.translate((2, window.screen_size().y as i32 - 60)),
Img(&image),
);
Ok(())
})?;
// Draw the Square font credits
self.square_font_info.execute(|image| {
window.draw(
&image
.area()
.translate((2, window.screen_size().y as i32 - 30)),
Img(&image),
);
Ok(())
})?;
let tile_size_px = self.tile_size_px;
let offset_px = Vector::new(50, 120);
// Draw the map
let (tileset, map) = (&mut self.tileset, &self.map);
tileset.execute(|tileset| {
for tile in map.iter() {
if let Some(image) = tileset.get(&tile.glyph) {
let pos_px = tile.pos.times(tile_size_px);
window.draw(
&Rectangle::new(offset_px + pos_px, image.area().size()),
Blended(&image, tile.color),
);
}
}
Ok(())
})?;
// Draw entities
let (tileset, entities) = (&mut self.tileset, &self.entities);
tileset.execute(|tileset| {
for entity in entities.iter() {
if let Some(image) = tileset.get(&entity.glyph) {
let pos_px = offset_px + entity.pos.times(tile_size_px);
window.draw(
&Rectangle::new(pos_px, image.area().size()),
Blended(&image, entity.color),
);
}
}
Ok(())
})?;
let player = &self.entities[self.player_id];
let full_health_width_px = 100.0;
let current_health_width_px =
(player.hp as f32 / player.max_hp as f32) * full_health_width_px;
let map_size_px = self.map_size.times(tile_size_px);
let health_bar_pos_px = offset_px + Vector::new(map_size_px.x, 0.0);
// Full health
window.draw(
&Rectangle::new(health_bar_pos_px, (full_health_width_px, tile_size_px.y)),
Col(Color::RED.with_alpha(0.5)),
);
// Current health
window.draw(
&Rectangle::new(health_bar_pos_px, (current_health_width_px, tile_size_px.y)),
Col(Color::RED),
);
self.inventory.execute(|image| {
window.draw(
&image
.area()
.translate(health_bar_pos_px + Vector::new(0, tile_size_px.y)),
Img(&image),
);
Ok(())
})?;
Ok(())
}
}
fn main() {
// NOTE: Set HIDPI to 1.0 to get pixel-perfect rendering.
// Otherwise the window resizes to whatever value the OS sets and
// scales the contents.
// https://docs.rs/glutin/0.19.0/glutin/dpi/index.html
std::env::set_var("WINIT_HIDPI_FACTOR", "1.0");
let settings = Settings {
// If the graphics do need to be scaled (e.g. using
// `with_center`), blur them. This looks better with fonts.
scale: quicksilver::graphics::ImageScaleStrategy::Blur,
..Default::default()
};
run::<Game>("Quicksilver Roguelike", Vector::new(800, 600), settings);
}