1
1
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
+ } ,
4
7
lifecycle:: { run, Asset , Settings , State , Window } ,
5
8
Future , Result ,
6
9
} ;
@@ -16,24 +19,35 @@ struct Game {
16
19
player_id : usize ,
17
20
tileset : Asset < HashMap < char , Image > > ,
18
21
tile_size_px : Vector ,
22
+ square_font_info : Asset < Image > ,
19
23
}
20
24
21
25
impl State for Game {
22
26
fn new ( ) -> Result < Self > {
23
27
let font_mononoki = "mononoki-Regular.ttf" ;
24
28
29
+ // 标题
25
30
let title = Asset :: new ( Font :: load ( font_mononoki) . and_then ( |font| {
26
31
font. render ( "Quicksilver Roguelike" , & FontStyle :: new ( 72.0 , Color :: BLACK ) )
27
32
} ) ) ;
28
33
34
+ // 字体使用感谢
29
35
let mononoki_font_info = Asset :: new ( Font :: load ( font_mononoki) . and_then ( |font| {
30
36
font. render (
31
37
"Mononoki ofnt by Matthias Tellen, thrms: SIL Open Font License 1.1" ,
32
38
& FontStyle :: new ( 20.0 , Color :: BLACK ) ,
33
39
)
34
40
} ) ) ;
35
41
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 ) ;
37
51
let map = generate_map ( map_size) ;
38
52
let mut entities = generate_entities ( ) ;
39
53
@@ -44,14 +58,15 @@ impl State for Game {
44
58
color : Color :: BLUE ,
45
59
hp : 3 ,
46
60
max_hp : 5 ,
47
- } )
61
+ } ) ;
48
62
63
+ let font_square = "square.ttf" ;
49
64
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." ) ;
55
70
let mut tileset = HashMap :: new ( ) ;
56
71
for ( index, glyph) in game_glyphs. chars ( ) . enumerate ( ) {
57
72
let pos = ( index as i32 * tile_size_px. x as i32 , 0 ) ;
@@ -68,7 +83,12 @@ impl State for Game {
68
83
// entities,
69
84
// })
70
85
Ok ( Self {
71
- ...
86
+ title,
87
+ mononoki_font_info,
88
+ square_font_info,
89
+ map_size,
90
+ map,
91
+ entities,
72
92
player_id,
73
93
tileset,
74
94
tile_size_px,
@@ -102,6 +122,49 @@ impl State for Game {
102
122
Ok ( ( ) )
103
123
} ) ?;
104
124
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
+
105
168
Ok ( ( ) )
106
169
}
107
170
}
@@ -115,7 +178,7 @@ struct Tile {
115
178
116
179
fn generate_map ( size : Vector ) -> Vec < Tile > {
117
180
let width = size. x as usize ;
118
- let height = siez . y as usize ;
181
+ let height = size . y as usize ;
119
182
let mut map = Vec :: with_capacity ( width * height) ;
120
183
for x in 0 ..width {
121
184
for y in 0 ..height {
0 commit comments