aboutsummaryrefslogtreecommitdiff
path: root/src/raycast.zig
diff options
context:
space:
mode:
authortslil clingman <>2021-09-05 20:45:43 -0400
committertslil clingman <>2021-09-05 20:45:43 -0400
commite95d2bd264df2682f05db4a5873594af52a7583a (patch)
tree62a9b720b4377b67f05a88c728157282c246274f /src/raycast.zig
parent1c4874c9f2855ee776bf8d5c35aef3d245c0519d (diff)
Changed map scaling, texture indexing, finished object rendering!
Diffstat (limited to 'src/raycast.zig')
-rw-r--r--src/raycast.zig23
1 files changed, 13 insertions, 10 deletions
diff --git a/src/raycast.zig b/src/raycast.zig
index 1f1fd7f..41458c7 100644
--- a/src/raycast.zig
+++ b/src/raycast.zig
@@ -47,7 +47,7 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type {
acc_x: f32 = 0,
acc_y: f32 = 0,
fov: f32 = std.math.pi / 3.0,
- height: f32 = 1.8, // TODO
+ height: f32 = 1.8 / 2.5, // TODO
z_buffer: std.BoundedArray(f32, PlanePixels),
const FOV: f32 = std.math.pi / 3.0;
@@ -107,16 +107,15 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type {
self.renderObjects(window, objects_sprite, renderSlice, map);
}
- fn playerDistComp(self: @This(), lhs: Object, rhs: Object) bool {
- const lx = lhs.pos_x - self.pos_x;
- const ly = lhs.pos_y - self.pos_y;
- const rx = rhs.pos_x - self.pos_x;
- const ry = rhs.pos_y - self.pos_y;
+ fn playerDistComp(pos : [2]f32, lhs: Object, rhs: Object) bool {
+ const lx = lhs.pos_x - pos[0];
+ const ly = lhs.pos_y - pos[1];
+ const rx = rhs.pos_x - pos[0];
+ const ry = rhs.pos_y - pos[1];
- return (lx * lx + ly * ly < rx * rx + ry * ry);
+ return (lx * lx + ly * ly > rx * rx + ry * ry);
}
- // Assumes objects are sorted by proximity!
fn renderObjects(
self: @This(),
window: RenderWindow,
@@ -124,7 +123,11 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type {
renderSlice: RenderSliceFunction,
map: Map,
) void {
- //std.sort.sort(Object, map.objects.items, {}, self.playerDistComp);
+ std.sort.sort(Object, map.objects.items,
+ // Wow, context with an arbitrary type! No macros, just
+ // Zig all the way down!
+ [2]f32{self.pos_x, self.pos_y},
+ playerDistComp);
const self_cos = std.math.cos(self.ang);
const self_sin = std.math.sin(self.ang);
@@ -349,7 +352,7 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type {
if (map.inBounds(ix, iy)) {
const tex = @as(c_uint, map.lookup(ix, iy).floor_texture);
- const toff = tex * @floatToInt(c_uint, 1 + TextureDim);
+ const toff = tex * @floatToInt(c_uint, TextureDim);
const px = @floatToInt(c_uint, TextureDim * std.math.fabs(sx.fpart));
const py = @floatToInt(c_uint, TextureDim * std.math.fabs(sy.fpart));