diff options
Diffstat (limited to 'main.ha')
-rw-r--r-- | main.ha | 67 |
1 files changed, 48 insertions, 19 deletions
@@ -30,14 +30,17 @@ fn set_tile_at(r: room, x: size, y: size, t: tile) void = { r.tiles[ix] = t; }; +fn tile_repr(t: tile) str = match(t) { + case floor => yield "\x1b[33m."; + case wall => yield "\x1b[32m\u2588"; +}; + + fn draw_room(t: *vt::term, r: room) void = { for (let y = 0z; y < r.h; y+=1) { vt::move(t, (y+1):uint, 0)!; for (let x = 0z; x < r.w; x+=1) { - fmt::fprint(t, match (tile_at(r,x,y)) { - case floor => yield '.'; - case wall => yield '\u2588'; - })!; + fmt::fprint(t, tile_repr(tile_at(r,x,y)))!; }; }; }; @@ -51,15 +54,8 @@ fn get_key() u8 = { def W = 50; def H = 40; - -export fn main() void = { - const term = vt::open(); - defer vt::close(term); - - vt::enablealt(term)!; - vt::disablecur(term)!; - - const tiles: [W*H]tile = [floor...]; +fn get_room() room = { + static const tiles: [W*H]tile = [floor...]; const room = room { w=W, h=H, tiles=tiles }; for (let i = 0z; i < W; i+=1) { @@ -78,9 +74,34 @@ export fn main() void = { set_tile_at(room, x,y, wall); }; + return room; +}; + +fn can_move(r: room, x: size, y: size) bool = + tile_at(r,x,y) is floor; + + +fn clear_ent(t: *vt::term, r: room, x:size, y:size) void = { + vt::move(t,y:uint+1,x:uint+1)!; + vt::print(t,tile_repr(tile_at(r,x,y)))!; +}; +fn draw_ent(t: *vt::term, r: room, x:size, y:size, s:str) void = { + vt::move(t,y:uint+1,x:uint+1)!; + vt::print(t,s)!; +}; + + +export fn main() void = { + const term = vt::open(); + defer vt::close(term); + + vt::enablealt(term)!; + vt::disablecur(term)!; + let player = player { x=1, y=1 }; - const playerpen = vt::newpen(vt::color::BLACK, vt::color::RED); - const playerstr = vt::tag(playerpen, "@"); + const playerstr = "\x1b[31m@"; + + const room = get_room(); let entities: []entity = []; for (let i = 0u; i < 1; i += 1) { @@ -88,16 +109,17 @@ export fn main() void = { entities[i].x=i*2; }; + vt::clear(term)!; + draw_room(term, room); + + for (true) { - vt::clear(term)!; - draw_room(term, room); for (let i = 0z; i < len(entities); i +=1) entity_draw(&entities[i], term); vt::move(term, player.y:uint+1, player.x:uint+1)!; vt::print(term, playerstr)!; - match (get_input(term)) { case let m: move => { let nx = player.x; @@ -109,7 +131,9 @@ export fn main() void = { case move::RIGHT => nx += 1; }; - if (tile_at(room, nx: size, ny: size) is floor) { + if (can_move(room,nx,ny)) { + clear_ent(term,room,player.x,player.y); + draw_ent(term,room,nx,ny,playerstr); player.x = nx; player.y = ny; }; @@ -118,6 +142,11 @@ export fn main() void = { case => yield; }; + for (let i = 0z; i < len(entities); i += 1) { + const ent = entities[i]; + clear_ent(term,room,ent.x,ent.y); + }; + for (let i = 0z; i < len(entities); i += 1) entity_tick(&entities[i],&room); |