Skip to content
This repository was archived by the owner on May 29, 2024. It is now read-only.

Commit c0d003c

Browse files
committed
Generate monsters on the map
For now, the rats are the only monsters. There is a limit of 2 monsters per room.
1 parent fe6a583 commit c0d003c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/generate.zig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const Vec2i = math.Vec(2, i64);
55
const vec2i = Vec2i.init;
66
const Map = @import("./map.zig").Map;
77
const platform = @import("platform");
8+
const component = @import("./component.zig");
89

910
const Room = struct {
1011
pos: Vec2i,
@@ -27,6 +28,7 @@ pub const Options = struct {
2728
min: i64,
2829
max: i64,
2930
},
31+
max_monsters_per_room: u64,
3032
};
3133

3234
pub fn generateMap(allocator: *std.mem.Allocator, opts: Options) !Map {
@@ -130,6 +132,16 @@ pub fn generateMap(allocator: *std.mem.Allocator, opts: Options) !Map {
130132
map.set(pos, .Floor);
131133
}
132134
}
135+
136+
// Place monsters in room
137+
const num_monsters = rand.intRangeAtMostBiased(u64, 0, opts.max_monsters_per_room);
138+
var c: u64 = 0;
139+
while (c < num_monsters) : (c += 1) {
140+
createRatEntity(&map, Vec2i{
141+
.x = room.pos.x + rand.intRangeAtMostBiased(i64, 0, room.size.x),
142+
.y = room.pos.y + rand.intRangeAtMostBiased(i64, 0, room.size.y),
143+
});
144+
}
133145
}
134146

135147
map.spawn = rooms.items[0].pos.addv(rooms.items[0].size.scaleDivFloor(2));
@@ -182,3 +194,9 @@ fn create_v_tunnel(map: *Map, y0: i64, y1: i64, x: i64) void {
182194
}
183195
}
184196
}
197+
198+
pub fn createRatEntity(map: *Map, pos: Vec2i) void {
199+
var e = map.registry.create();
200+
map.registry.add(e, component.Position{ .pos = pos });
201+
map.registry.add(e, component.Render{ .tid = 415 });
202+
}

src/main.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub fn onInit() !void {
8383
.min = 3,
8484
.max = 10,
8585
},
86+
.max_monsters_per_room = 2,
8687
});
8788

8889
var player = map.registry.create();
@@ -160,6 +161,7 @@ pub fn onEvent(event: platform.event.Event) !void {
160161
.min = 3,
161162
.max = 10,
162163
},
164+
.max_monsters_per_room = 2,
163165
});
164166

165167
var player = map.registry.create();
@@ -211,6 +213,7 @@ pub fn render(alpha: f64) !void {
211213
var iter = view.iterator();
212214
while (iter.next()) |entity| {
213215
const pos = view.getConst(component.Position, entity);
216+
if (map.visible.get(pos.pos) == null) continue;
214217
const r = view.getConst(component.Render, entity);
215218
render_tile(&flatRenderer, .{ .pos = r.tid }, pos.pos, 1);
216219
}

0 commit comments

Comments
 (0)