auts: place walls in hud
This commit is contained in:
parent
20fcfeadb6
commit
c2cd0716d1
|
@ -1,5 +1,10 @@
|
|||
module main
|
||||
|
||||
enum HudBuildMode {
|
||||
tile
|
||||
wall
|
||||
}
|
||||
|
||||
struct Hud {
|
||||
mut:
|
||||
mx int
|
||||
|
@ -7,24 +12,52 @@ mut:
|
|||
|
||||
mcx int
|
||||
mcy int
|
||||
|
||||
build_mode HudBuildMode
|
||||
}
|
||||
|
||||
fn new_hud() &Hud {
|
||||
println("new_hud()")
|
||||
|
||||
h := &Hud{}
|
||||
h := &Hud{
|
||||
build_mode: .tile,
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
fn (mut h Hud) place_tile(tile_id int, cx, cy int) {
|
||||
// Check if cx, cy are within field
|
||||
fn (mut h Hud) check_coords(cx, cy int) bool {
|
||||
f := game.field
|
||||
if cx < 0 || cx > f.w-1 || cy < 0 || cy > f.h-1 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fn (mut h Hud) place_tile(tile_id int, cx, cy int) {
|
||||
if !h.check_coords(cx, cy) {
|
||||
return
|
||||
}
|
||||
|
||||
game.field.cells[cx][cy].tile_id = tile_id
|
||||
}
|
||||
|
||||
fn (mut h Hud) place_wall(cx, cy int) {
|
||||
if !h.check_coords(cx, cy) {
|
||||
return
|
||||
}
|
||||
|
||||
mut wall := new_wall()
|
||||
wall.spawn()
|
||||
|
||||
wall.obj.set_cx(cx)
|
||||
wall.obj.set_cy(cy)
|
||||
|
||||
// XXX upd sprite
|
||||
wall.view.update(0)
|
||||
}
|
||||
|
||||
fn (mut h Hud) draw() {
|
||||
|
||||
s := "mx: $h.mx, my: $h.my"
|
||||
|
@ -74,7 +107,12 @@ fn (mut h Hud) update(dt f32) {
|
|||
// Draw tiles
|
||||
key := C.SDLK_SPACE
|
||||
if ctx.app.is_key_pressed(key) {
|
||||
// Place tile
|
||||
h.place_tile(2, h.mcx, h.mcy)
|
||||
|
||||
if h.build_mode == .tile {
|
||||
// Place tile
|
||||
h.place_tile(2, h.mcx, h.mcy)
|
||||
} else if h.build_mode == .wall {
|
||||
h.place_wall(h.mcx, h.mcy)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,6 +4,8 @@ import spytheman.vperlin as perlin
|
|||
import time
|
||||
import rand
|
||||
|
||||
|
||||
|
||||
fn main() {
|
||||
println("main()")
|
||||
|
||||
|
@ -134,17 +136,26 @@ fn main() {
|
|||
spr2.y = y1
|
||||
}
|
||||
|
||||
|
||||
mut wall1 := new_wall()
|
||||
wall1.spawn()
|
||||
|
||||
wall1.obj.set_cx(8)
|
||||
wall1.obj.set_cy(8)
|
||||
|
||||
//wall1.obj.set_cx(2)
|
||||
//wall1.obj.set_cy(1)
|
||||
|
||||
//wall1.obj.set_cx(2)
|
||||
//wall1.obj.set_cy(1)
|
||||
|
||||
// XXX fixme
|
||||
wall1.view.update(0)
|
||||
|
||||
|
||||
//ctx.vars.scroll_speed = 0
|
||||
ctx.vars.mouse_scroll = false
|
||||
|
||||
game.hud.build_mode = .wall
|
||||
|
||||
for app.step() {
|
||||
//app.step()
|
||||
|
@ -172,7 +183,7 @@ fn main() {
|
|||
//spr1.draw()
|
||||
//spr2.draw()
|
||||
|
||||
wall1.view.draw()
|
||||
//wall1.view.draw()
|
||||
|
||||
app.flip()
|
||||
|
||||
|
|
|
@ -21,7 +21,8 @@ fn (mut o Obj) set_cx(cx int) {
|
|||
//o.x = cx * c_cell_w
|
||||
//x1, y1 := cell_to_world_pos(cx, o.cy())
|
||||
x1, y1 := cell_to_world_pos_center(cx, o.cy())
|
||||
o.x = x1
|
||||
o.x = x1 // XXX fixme?
|
||||
o.y = y1
|
||||
}
|
||||
|
||||
fn (o &Obj) cy() int {
|
||||
|
@ -32,6 +33,7 @@ fn (mut o Obj) set_cy(cy int) {
|
|||
//o.y = cy * c_cell_h
|
||||
//x1, y1 := cell_to_world_pos(o.cx(), cy)
|
||||
x1, y1 := cell_to_world_pos_center(o.cx(), cy)
|
||||
o.x = x1 // XXX fixme?
|
||||
o.y = y1
|
||||
}
|
||||
|
||||
|
|
|
@ -43,8 +43,8 @@ fn (mut v WallView) draw() {
|
|||
return
|
||||
}
|
||||
|
||||
v.spr.x = v.m.obj.x
|
||||
v.spr.y = v.m.obj.y
|
||||
// XXX fixme
|
||||
v.update(0)
|
||||
|
||||
println("x: $v.spr.x, y: $v.spr.y")
|
||||
//pp(2)
|
||||
|
@ -58,9 +58,10 @@ fn (mut v WallView) draw() {
|
|||
v.spr.y = y1
|
||||
*/
|
||||
|
||||
v.spr.draw()
|
||||
//v.spr.draw()
|
||||
}
|
||||
|
||||
fn (mut v WallView) update(dt f32) {
|
||||
|
||||
v.spr.x = v.m.obj.x
|
||||
v.spr.y = v.m.obj.y
|
||||
}
|
Loading…
Reference in New Issue