1
0
Fork 0

auts: test draw walls from cells

This commit is contained in:
coaljoe 2020-08-24 00:55:47 +03:00
parent 25a5d5f5a2
commit 88ef2c0033
9 changed files with 104 additions and 8 deletions

View File

@ -2,7 +2,25 @@ module main
struct Cell {
mut:
z int
// XXX pos
x int
y int
z int
tile_id int
wall &Wall // XXX use wall_id?
}
fn new_cell() Cell {
c := Cell{
wall: &Wall{0},
}
return c
}
fn (c &Cell) draw() {
println("panic: not implemented")
pp(1)
}

View File

@ -1,8 +1,5 @@
module main
import nsauzede.vsdl2
import nsauzede.vsdl2.image as img
struct Context {
pub mut:
app &App

View File

@ -36,9 +36,36 @@ fn (mut f Field) generate(w, h int) {
f.cells[i] = []Cell{ len: h } // Cols
}
// Create cells
for y in 0..f.h {
for x in 0..f.w {
mut c := new_cell()
c.x = x
c.y = y
f.cells[x][y] = c
}
}
f.generated = true
}
fn (f &Field) check_coords(cx, cy int) bool {
if cx < 0 || cx > f.w-1 || cy < 0 || cy > f.h-1 {
return false
}
return true
}
// Get cell as reference
fn (f &Field) get_cell(cx, cy int) &Cell {
if !f.check_coords(cx, cy) {
println("panic: bad coords: cx: $cx, cy: $cy")
}
c := &f.cells[cx][cy]
return c
}
fn (f &Field) width_px() int {
return f.w * c_cell_w
}

View File

@ -97,6 +97,33 @@ fn (mut v FieldView) draw_tile_cell_pos(tile_id int, cx, cy int) {
v.draw_tile(tile_id, px, py)
}
// XXX Cell.draw
// TODO add draw_cell_at ?
[inline]
fn (mut v FieldView) draw_cell(c &Cell) {
cx := c.x
cy := c.y
v.draw_tile_cell_pos(c.tile_id, cx, cy)
// XXX Draw cell's wall
if !isnil(c.wall) && !isnil(c.wall.view) {
//w := c.wall
mut spr := c.wall.view.spr
//pp(2)
// Force draw
if !spr.visible {
spr.toggle_visible()
spr.draw()
spr.toggle_visible()
} else {
spr.draw()
}
}
}
fn (mut v FieldView) spawn() {
println("fiedview spawn()")
@ -113,8 +140,10 @@ fn (mut v FieldView) draw() {
//mut px := c_cell_w * x
//mut py := c_cell_h * y
v.draw_tile_cell_pos(c.tile_id, x, y)
//v.draw_tile_cell_pos(c.tile_id, x, y)
//pp(2)
v.draw_cell(c)
//v.m.ctx.drawer.draw_tex(px, py, tex)
//ctx.drawer.draw_tex(tex, px, py)

View File

@ -49,10 +49,13 @@ fn (mut h Hud) place_wall(cx, cy int) {
}
mut wall := new_wall()
wall.place_at(cx, cy)
wall.spawn()
wall.obj.set_cx(cx)
wall.obj.set_cy(cy)
//wall.obj.set_cx(cx)
//wall.obj.set_cy(cy)
// XXX upd sprite
wall.view.update(0)

View File

@ -63,7 +63,7 @@ fn main() {
nv := perlin.noise2d(f32(x) * scale, f32(y) * scale)
nv_norm := (nv + 1.0) / 2.0
xnv := nv_norm * mag
println(xnv)
//println(xnv)
if xnv > tile_num {
pp(2)
}

View File

@ -21,6 +21,9 @@ mut:
// yCorr
shift_y int
tex &Texture
// XXX disable automatic draw: fixme?
visible bool
spawned bool
}
@ -28,6 +31,7 @@ mut:
fn new_sprite() &Sprite {
s := &Sprite{
tex: &Texture(0),
visible: true,
}
// Add to SpriteSys
@ -35,6 +39,10 @@ fn new_sprite() &Sprite {
return s
}
fn (mut s Sprite) toggle_visible() {
s.visible = !s.visible
}
fn (mut s Sprite) load(path string) {
s.tex = new_texture()
s.tex.load(path)
@ -55,6 +63,10 @@ fn (mut s Sprite) spawn() {
fn (mut s Sprite) draw() {
println("sprite draw()")
if !s.visible {
return
}
//s.tex.draw_pos(s.x, s.y)

View File

@ -27,6 +27,15 @@ fn new_wall() &Wall {
return w
}
fn (mut w Wall) place_at(cx, cy int) {
w.obj.set_cx(cx)
w.obj.set_cy(cy)
//mut c := &game.field.cells[cx][cy]
mut c := game.field.get_cell(cx, cy)
c.wall = w
}
fn (mut w Wall) spawn() {
println("wall spawn()")

View File

@ -31,6 +31,7 @@ fn (mut v WallView) spawn() {
v.spr = new_sprite()
v.spr.load("res/objects/walls/wall1/image_0.png")
v.spr.visible = false
v.spr.spawn()
v.spawned = true