summaryrefslogtreecommitdiff
path: root/main.ha
blob: 90099475b616f2bbe61d0103e948cbcc6a7cca3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use math::random;

type player = struct {
	x: int,
	y: int,
};

type wall = void;
type floor = void;

type tile = (wall | floor);

type room = struct {
	w: size,
	h: size,
	tiles: []tile,
};

fn tile_at(r: room, x: size, y: size) tile = {
	const ix = x+(r.w*y);
	return r.tiles[ix];
};

fn set_tile_at(r: room, x: size, y: size, t: tile) void = {
	const ix = x+(r.w*y);
	r.tiles[ix] = t;
};

fn tile_repr(t: tile) str = match (t) {
	case wall => yield  "\x1b[90m\u2588";
	case floor => yield "\x1b[37m.";
};

fn draw_room(r: room) void = {
	assert(r.w < 20);
	assert(r.h < 20);
	for (let x = 0z; x < r.w; x+=1)
		for (let y = 0z; y < r.h; y+=1)
			put_at(x:int,y:int, tile_repr(tile_at(r,x,y)));
};


export fn main() void = {
	const term = setup_term();

	const tiles: [100]tile = [floor...];
	const room = room { w=10, h=10, tiles=tiles };

	for (let i = 0z; i < 10; i+=1) {
		set_tile_at(room, 0, i, wall);
		set_tile_at(room, 9, i, wall);
		set_tile_at(room, i, 0, wall);
		set_tile_at(room, i, 9, wall);
	};

	let rand = random::init(12345);
	for (let i = 0; i < 6; i+=1) {
		const x = (random::u64n(&rand, 9)+1): size;
		const y = (random::u64n(&rand, 9)+1): size;
		set_tile_at(room, x,y, wall);
	};




	let player = player { x=1, y=1 };
	for (true) {
		clear();
		draw_room(room);
		put_at(player.x, player.y, "@");
		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;
		};
		if (tile_at(room, nx:size, ny:size) is floor) {
			player.x = nx;
			player.y = ny;
		};
	};

	finish_term(&term);
};