Skip to content

Commit c9d84d3

Browse files
author
irrationke
committed
1 parent bea4414 commit c9d84d3

File tree

4 files changed

+113
-11
lines changed

4 files changed

+113
-11
lines changed

.vscode/launch.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
// 使用 IntelliSense 了解相关属性。
3+
// 悬停以查看现有属性的描述。
4+
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Debug executable 'show_posts'", // 配置名称,将会在调试配置下拉列表中显示
9+
"type": "lldb", // 调试器类型:Windows表示器使用cppvsdbg;GDB和LLDB使用cppdbg。该值自动生成
10+
"request": "launch", // 调试方式
11+
"cargo": { // 运行的参数
12+
"args": [
13+
"run",
14+
// "--bin=show_posts",
15+
// "--package=diesel_demo"
16+
],
17+
// "filter": {
18+
// "name": "show_posts",
19+
// "kind": "bin"
20+
// }
21+
},
22+
"args": [], // 传递给程序的参数,没有参数留空即可
23+
"cwd": "${workspaceFolder}" // 调试程序时的工作目录
24+
},
25+
]
26+
}

.vscode/tasks.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"type": "shell",
7+
"command": "cargo",
8+
"args": [
9+
"build"
10+
]
11+
}
12+
]
13+
}

src/main.rs

+74-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use quicksilver::{
2-
geom::{Shape, Vector},
3-
graphics::{Background::Img, Color, Font, FontStyle, Image},
2+
geom::{Rectangle, Shape, Vector},
3+
graphics::{
4+
Background::{Blended, Img},
5+
Color, Font, FontStyle, Image,
6+
},
47
lifecycle::{run, Asset, Settings, State, Window},
58
Future, Result,
69
};
@@ -16,24 +19,35 @@ struct Game {
1619
player_id: usize,
1720
tileset: Asset<HashMap<char, Image>>,
1821
tile_size_px: Vector,
22+
square_font_info: Asset<Image>,
1923
}
2024

2125
impl State for Game {
2226
fn new() -> Result<Self> {
2327
let font_mononoki = "mononoki-Regular.ttf";
2428

29+
// 标题
2530
let title = Asset::new(Font::load(font_mononoki).and_then(|font| {
2631
font.render("Quicksilver Roguelike", &FontStyle::new(72.0, Color::BLACK))
2732
}));
2833

34+
// 字体使用感谢
2935
let mononoki_font_info = Asset::new(Font::load(font_mononoki).and_then(|font| {
3036
font.render(
3137
"Mononoki ofnt by Matthias Tellen, thrms: SIL Open Font License 1.1",
3238
&FontStyle::new(20.0, Color::BLACK),
3339
)
3440
}));
3541

36-
let map_size = Vector::new(20,15);
42+
// 字体使用感谢
43+
let square_font_info = Asset::new(Font::load(font_mononoki).and_then(|font| {
44+
font.render(
45+
"Square font by Wouter Van Oortmerssen, terms: CC BY 3.0",
46+
&FontStyle::new(20.0, Color::BLACK),
47+
)
48+
}));
49+
50+
let map_size = Vector::new(20, 15);
3751
let map = generate_map(map_size);
3852
let mut entities = generate_entities();
3953

@@ -44,14 +58,15 @@ impl State for Game {
4458
color: Color::BLUE,
4559
hp: 3,
4660
max_hp: 5,
47-
})
61+
});
4862

63+
let font_square = "square.ttf";
4964
let game_glyphs = "#@g.%";
50-
let tile_size_px = Vector::new(12, 24);
51-
let tileset = Asset::new(Font::load(font_mononoki).and_then(move |text| {
52-
let liles = text
53-
.rander(game_glyphs, &FontStyle::new(tile_size_px.y, Color::WHITE))
54-
.expect("Could not rander the font tileset.");
65+
let tile_size_px = Vector::new(24, 24);
66+
let tileset = Asset::new(Font::load(font_square).and_then(move |text| {
67+
let tiles = text
68+
.render(game_glyphs, &FontStyle::new(tile_size_px.y, Color::WHITE))
69+
.expect("Could not render the font tileset.");
5570
let mut tileset = HashMap::new();
5671
for (index, glyph) in game_glyphs.chars().enumerate() {
5772
let pos = (index as i32 * tile_size_px.x as i32, 0);
@@ -68,7 +83,12 @@ impl State for Game {
6883
// entities,
6984
// })
7085
Ok(Self {
71-
...
86+
title,
87+
mononoki_font_info,
88+
square_font_info,
89+
map_size,
90+
map,
91+
entities,
7292
player_id,
7393
tileset,
7494
tile_size_px,
@@ -102,6 +122,49 @@ impl State for Game {
102122
Ok(())
103123
})?;
104124

125+
self.square_font_info.execute(|image| {
126+
window.draw(
127+
&image
128+
.area()
129+
.translate((2, window.screen_size().y as i32 - 30)),
130+
Img(&image),
131+
);
132+
Ok(())
133+
})?;
134+
135+
let tile_size_px = self.tile_size_px;
136+
137+
let (tileset, map) = (&mut self.tileset, &self.map);
138+
tileset.execute(|tileset| {
139+
for tile in map.iter() {
140+
if let Some(image) = tileset.get(&tile.glyph) {
141+
let pos_px = tile.pos.times(tile_size_px);
142+
let offset_px = Vector::new(50, 120);
143+
window.draw(
144+
&Rectangle::new(offset_px + pos_px, image.area().size()),
145+
Blended(&image, tile.color),
146+
);
147+
}
148+
}
149+
Ok(())
150+
})?;
151+
152+
let (tileset, entities) = (&mut self.tileset, &self.entities);
153+
tileset.execute(|tileset| {
154+
for entity in entities.iter() {
155+
if let Some(image) = tileset.get(&entity.glyph) {
156+
let offset_px = Vector::new(50, 120);
157+
let pos_px = offset_px + entity.pos.times(tile_size_px);
158+
// let pos_px = entity.pos.times(tile_size_px);
159+
window.draw(
160+
&Rectangle::new(pos_px, image.area().size()),
161+
Blended(&image, entity.color),
162+
);
163+
}
164+
}
165+
Ok(())
166+
})?;
167+
105168
Ok(())
106169
}
107170
}
@@ -115,7 +178,7 @@ struct Tile {
115178

116179
fn generate_map(size: Vector) -> Vec<Tile> {
117180
let width = size.x as usize;
118-
let height = siez.y as usize;
181+
let height = size.y as usize;
119182
let mut map = Vec::with_capacity(width * height);
120183
for x in 0..width {
121184
for y in 0..height {

static/square.ttf

8.33 KB
Binary file not shown.

0 commit comments

Comments
 (0)