@@ -3,12 +3,6 @@ import gg
3
3
import gx
4
4
5
5
const csize = 32
6
- const color_box = gx.rgb (139 , 69 , 19 ) // Brown
7
- const color_wall = gx.rgb (100 , 100 , 100 ) // Gray
8
- const color_floor = gx.rgb (200 , 200 , 200 ) // Light gray
9
- const color_player = gx.rgb (0 , 0 , 255 ) // Blue
10
- const color_storage = gx.rgb (0 , 255 , 0 ) // Green
11
- const color_box_on_storage = gx.rgb (255 , 255 , 0 ) // Yellow
12
6
13
7
struct Pos {
14
8
x int
36
30
levels []string
37
31
moves int
38
32
ctx & gg.Context = unsafe { nil }
33
+ //
34
+ id_box int
35
+ id_box_on_storage int
36
+ id_wall int
37
+ id_floor int
38
+ id_other int
39
+ id_player int
40
+ id_storage int
39
41
}
40
42
41
43
fn (mut g Game) parse_level (lnumber int ) ! {
@@ -132,24 +134,24 @@ fn (mut g Game) move_player(dir Direction) {
132
134
g.moves++
133
135
}
134
136
135
- fn (g &Game) get_cell_color (pos Pos) gx.Color {
137
+ fn (g &Game) get_cell_iid (pos Pos) int {
136
138
c := g.warehouse[pos.y][pos.x]
137
139
if pos.x == g.player.x && pos.y == g.player.y {
138
- return color_player
140
+ return g.id_player
139
141
}
140
142
for box in g.boxes {
141
143
if box.x == pos.x && box.y == pos.y {
142
144
if c == `@` {
143
- return color_box_on_storage
145
+ return g.id_box_on_storage
144
146
}
145
- return color_box
147
+ return g.id_box
146
148
}
147
149
}
148
150
match c {
149
- ` ` { return color_floor }
150
- `#` { return color_wall }
151
- `@` { return color_storage }
152
- else { return gx.black }
151
+ ` ` { return g.id_floor }
152
+ `#` { return g.id_wall }
153
+ `@` { return g.id_storage }
154
+ else { return g.id_other }
153
155
}
154
156
}
155
157
@@ -212,8 +214,12 @@ fn (g &Game) draw_frame(_ voidptr) {
212
214
for y in 0 .. g.warehouse.len {
213
215
for x in 0 .. g.warehouse[y].len {
214
216
pos := Pos{x, y}
215
- color := g.get_cell_color (pos)
216
- g.ctx.draw_rect_filled (ox + x * csize, oy + y * csize, csize, csize, color)
217
+ iid := g.get_cell_iid (pos)
218
+ if iid == g.id_player {
219
+ // the player is transparent
220
+ g.ctx.draw_image_by_id (ox + x * csize, oy + y * csize, 32 , 32 , g.id_floor)
221
+ }
222
+ g.ctx.draw_image_by_id (ox + x * csize, oy + y * csize, 32 , 32 , iid)
217
223
}
218
224
}
219
225
g.ctx.draw_rect_filled (0 , ws.height - 70 , ws.width, 70 , gx.black)
@@ -231,6 +237,10 @@ fn (g &Game) draw_frame(_ voidptr) {
231
237
g.ctx.end ()
232
238
}
233
239
240
+ fn (mut g Game) iid (name string ) ! int {
241
+ return g.ctx.create_image (asset.get_path ('/' , name))! .id
242
+ }
243
+
234
244
fn main () {
235
245
mut g := & Game{}
236
246
all_level_names := asset.read_bytes ('/' , '_all_levels.txt' )! .bytestr ().split_into_lines ()
@@ -244,5 +254,12 @@ fn main() {
244
254
frame_fn: g.draw_frame
245
255
keydown_fn: g.key_down
246
256
)
257
+ g.id_box = g.iid ('box.png' )!
258
+ g.id_box_on_storage = g.iid ('box_on_storage.png' )!
259
+ g.id_wall = g.iid ('wall.png' )!
260
+ g.id_floor = g.iid ('floor.png' )!
261
+ g.id_other = g.iid ('other.png' )!
262
+ g.id_player = g.iid ('player.png' )!
263
+ g.id_storage = g.iid ('storage.png' )!
247
264
g.ctx.run ()
248
265
}
0 commit comments