From d413e6b54d0c505a529ca6a9a90ddf73c07a8c42 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 13 Nov 2023 01:11:53 +0000 Subject: add entities, sort of --- main.ha | 66 ++++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 17 deletions(-) (limited to 'main.ha') diff --git a/main.ha b/main.ha index 41917c8..967e92c 100644 --- a/main.ha +++ b/main.ha @@ -2,10 +2,11 @@ use math::random; use vt; use io; use os; +use fmt; type player = struct { - x: int, - y: int, + x: uint, + y: uint, }; type wall = void; @@ -29,6 +30,18 @@ fn set_tile_at(r: room, x: size, y: size, t: tile) void = { r.tiles[ix] = t; }; +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'; + })!; + }; + }; +}; + fn get_key() u8 = { let b = [0u8]; io::read(os::stdin_file, b)!; @@ -36,7 +49,7 @@ fn get_key() u8 = { }; def W = 50; -def H = 90; +def H = 40; export fn main() void = { @@ -69,26 +82,45 @@ export fn main() void = { const playerpen = vt::newpen(vt::color::BLACK, vt::color::RED); const playerstr = vt::tag(playerpen, "@"); + let entities: []entity = []; + for (let i = 0u; i < 1; i += 1) { + append(entities, entity_new('g',vt::color::BLUE)); + entities[i].x=i*2; + }; + for (true) { vt::clear(term)!; - draw_room(room); + 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)!; - const c = get_key(): rune; - let nx = player.x; - let ny = player.y; - switch (c) { - case 'h' => nx -= 1; - case 'l' => nx += 1; - case 'j' => ny += 1; - case 'k' => ny -= 1; - case 'q' => break; - case => yield; + + + match (get_input(term)) { + case let m: move => { + let nx = player.x; + let ny = player.y; + switch (m) { + case move::UP => ny -= 1; + case move::DOWN => ny += 1; + case move::LEFT => nx -= 1; + case move::RIGHT => nx += 1; + }; + + if (tile_at(room, nx: size, ny: size) is floor) { + player.x = nx; + player.y = ny; + }; }; - if (tile_at(room, nx:size, ny:size) is floor) { - player.x = nx; - player.y = ny; + case quit => break; + case => yield; }; + + for (let i = 0z; i < len(entities); i += 1) + entity_tick(&entities[i],&room); + }; }; -- cgit v1.2.3