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 --- .gitignore | 1 + entity.ha | 36 ++++++++++++++++++++++++++++++++++ input.ha | 45 ++++++++++++++++++++++++++++++++++++++++++ main.ha | 66 ++++++++++++++++++++++++++++++++++++++++++++++---------------- 4 files changed, 131 insertions(+), 17 deletions(-) create mode 100644 .gitignore create mode 100644 entity.ha create mode 100644 input.ha diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c4d41b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +foamlike diff --git a/entity.ha b/entity.ha new file mode 100644 index 0000000..07b8b69 --- /dev/null +++ b/entity.ha @@ -0,0 +1,36 @@ +use vt; +use math::random; +use time; +use fmt; +use os; + +type entity = struct { + x: uint, + y: uint, + letter: rune, + color: u8, +}; + +fn entity_draw(ent: *entity, term: *vt::term) void = { + vt::move(term, ent.y+1, ent.x+1)!; + fmt::fprintf(os::stdout_file,"\x1b[3{}m{}",ent.color,ent.letter)!; +}; + +fn entity_tick(ent: *entity, room: *room) void = { + ent.x += 1; + if (ent.x >= 13) ent.x = 1; + ent.y += 1; + if (ent.y >= 11) ent.y = 1; +}; + +fn entity_new(r: rune, col: u8) entity = entity { + x = 1, + y = 1, + letter = r, + color = col, +}; + + + + + diff --git a/input.ha b/input.ha new file mode 100644 index 0000000..dd3f34e --- /dev/null +++ b/input.ha @@ -0,0 +1,45 @@ +use vt; +use io; + +type move = enum { + UP, + DOWN, + LEFT, + RIGHT, +}; +type wait = void; +type quit = void; + +type unknown = !void; +type input = (move | wait | quit | unknown); + + +fn get_input(term: *vt::term) (input | io::EOF | vt::error) = { + const key = match (vt::getevent(term)?) { + case io::EOF => return io::EOF; + case let e: vt::event => yield e.value; + }; + match (key) { + case let r: rune => switch (r) { + case 'h' => return move::LEFT; + case 'l' => return move::RIGHT; + case 'j' => return move::DOWN; + case 'k' => return move::UP; + case '.' => return wait; + case 'q' => return quit; + case => return unknown; + }; + case let s: vt::specialkey => switch (s) { + case vt::specialkey::UP => return move::UP; + case vt::specialkey::DOWN => return move::DOWN; + case vt::specialkey::LEFT => return move::LEFT; + case vt::specialkey::RIGHT => return move::RIGHT; + case => return unknown; + }; + case => return unknown; + }; +}; + + + + 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