auts/isotest/fieldview.v
2020-08-23 01:04:31 +03:00

125 lines
2 KiB
V

module main
import os
struct FieldView {
mut:
m &Field
tiles map[string]&Texture
}
fn new_fieldview(m &Field) &FieldView {
v := &FieldView{
m: m,
}
return v
}
fn (mut v FieldView) load() {
println("fieldview load()")
if isnil(ctx) {
println("ctx is nil")
pp(2)
}
//path := "res/images/tiles/test1"
path := v.m.tiles_path
for f in os.walk_ext(path, ".png") {
println("f: $f")
//mut name := os.file_name(f).trim_right(".png")
mut name := os.file_name(f).all_before_last(".png")
//println("z: $name")
if name.count("_") != 1 {
//idx := name.index_after(5)
name = name.all_before_last("_")
}
println("name: $name")
//tex := ctx.drawer.load_image_tex(f)
mut tex := new_texture()
tex.load(f)
v.tiles[name] = tex
//pp(2)
}
//pp(2)
}
// Draw tile at position
// XXX draw with top-left corner of 32x16 tile
// XXX add draw_tile_center function to drawing tiles at center position
[inline]
fn (mut v FieldView) draw_tile(tile_id int, px, py int) {
name := "tile_${tile_id}"
//println("name: $name")
mut tex := v.tiles[name]
if isnil(tex) {
println("tex is nil")
pp(2)
}
mut sx, mut sy := world_to_screen_pos(px, py)
// XXX apply half-tile yshift for 32x32 tiles
sy -= c_cell_h
//println("sx: $sx")
//println("sy: $sx")
tex.draw_pos(sx, sy)
//tex.draw_pos(px, py)
}
// Draw tile at cell position
[inline]
fn (mut v FieldView) draw_tile_cell_pos(tile_id int, cx, cy int) {
x := cx
y := cy
mut shx := 0
if y % 2 != 0 {
shx = c_cell_w / 2
}
mut px := x * c_cell_w + shx
mut py := y * c_cell_h / 2
v.draw_tile(tile_id, px, py)
}
fn (mut v FieldView) spawn() {
println("fiedview spawn()")
v.load()
}
fn (mut v FieldView) draw() {
println("fieldview draw()")
for y in 0 .. v.m.h {
for x in 0 .. v.m.w {
c := v.m.cells[x][y]
//mut px := c_cell_w * x
//mut py := c_cell_h * y
v.draw_tile_cell_pos(c.tile_id, x, y)
//pp(2)
//v.m.ctx.drawer.draw_tex(px, py, tex)
//ctx.drawer.draw_tex(tex, px, py)
//tex.draw_pos(px, py)
//tex.draw_pos(sx, sy)
}
}
}