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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
use math::random;
use vt;
use io;
use os;
use fmt;
type player = struct {
x: uint,
y: uint,
};
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 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)!;
return b[0];
};
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...];
const room = room { w=W, h=H, tiles=tiles };
for (let i = 0z; i < W; i+=1) {
set_tile_at(room, i, 0, wall);
set_tile_at(room, i, H-1, wall);
};
for (let i = 0z; i < H; i+=1) {
set_tile_at(room, 0, i, wall);
set_tile_at(room, W-1, i, wall);
};
let rand = random::init(12345);
for (let i = 0; i < 6; i+=1) {
const x = (random::u64n(&rand, W-2)+1): size;
const y = (random::u64n(&rand, H-2)+1): size;
set_tile_at(room, x,y, wall);
};
let player = player { x=1, y=1 };
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(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;
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;
};
};
case quit => break;
case => yield;
};
for (let i = 0z; i < len(entities); i += 1)
entity_tick(&entities[i],&room);
};
};
|