aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authortslil clingman <>2021-10-10 15:31:36 -0400
committertslil clingman <>2021-10-10 16:00:55 -0400
commitcb15709551921e111d2648f7d6545e4d0e3fdcd6 (patch)
treef5407786343e972f3685d14c4d8b91e37ab89452 /src/main.zig
parent442f7210ea40f7f74b752575ee81b5f9394d6ea9 (diff)
Refactor: extract rendering code from Player struct into Renderer
Some bonus off by ones and rounding corrections
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig52
1 files changed, 28 insertions, 24 deletions
diff --git a/src/main.zig b/src/main.zig
index 3c425ba..c3647b4 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -25,7 +25,8 @@ const isKeyPressed = sf.window.keyboard.isKeyPressed;
const std = @import("std");
const constants = @import("constants.zig");
-const raycast = @import("player.zig");
+const player = @import("player.zig");
+const render = @import("render.zig");
const level = @import("level.zig");
pub fn main() !void {
@@ -34,7 +35,9 @@ pub fn main() !void {
// Setup player and map
//--------------------------------------------------------------------------
- var player = raycast.Player(constants.PlaneWidth, constants.PlaneHeight).new(
+ var renderer = render.Renderer(constants.PlaneWidth, constants.PlaneHeight).new();
+
+ var plyr = player.Player.new(
7, // x coord
1.5, // y coord
PI / 2.0, // angle
@@ -131,44 +134,44 @@ pub fn main() !void {
}
if (isKeyPressed(.Escape)) window.close();
- if (isKeyPressed(.C)) player.height -= 0.1;
- if (isKeyPressed(.V)) player.height += 0.1;
+ if (isKeyPressed(.C)) plyr.height -= 0.1;
+ if (isKeyPressed(.V)) plyr.height += 0.1;
const rotation = 2.0 * PI / 200.0;
- if (isKeyPressed(.Left)) player.ang += rotation;
- if (isKeyPressed(.Right)) player.ang -= rotation;
+ if (isKeyPressed(.Left)) plyr.ang += rotation;
+ if (isKeyPressed(.Right)) plyr.ang -= rotation;
- const cos = std.math.cos(player.ang);
- const sin = std.math.sin(player.ang);
+ const cos = std.math.cos(plyr.ang);
+ const sin = std.math.sin(plyr.ang);
- player.acc_x = 0;
- player.acc_y = 0;
+ plyr.acc_x = 0;
+ plyr.acc_y = 0;
const acc_mag = 30;
if (isKeyPressed(.R)) {
- player.acc_x -= sin;
- player.acc_y += cos;
+ plyr.acc_x -= sin;
+ plyr.acc_y += cos;
}
if (isKeyPressed(.T)) {
- player.acc_x += sin;
- player.acc_y -= cos;
+ plyr.acc_x += sin;
+ plyr.acc_y -= cos;
}
if (isKeyPressed(.Up) or isKeyPressed(.F)) {
- player.acc_x += cos;
- player.acc_y += sin;
+ plyr.acc_x += cos;
+ plyr.acc_y += sin;
}
if (isKeyPressed(.Down) or isKeyPressed(.S)) {
- player.acc_x -= cos;
- player.acc_y -= sin;
+ plyr.acc_x -= cos;
+ plyr.acc_y -= sin;
}
// normalise
- const a = std.math.sqrt(player.acc_x * player.acc_x + player.acc_y * player.acc_y);
+ const a = std.math.sqrt(plyr.acc_x * plyr.acc_x + plyr.acc_y * plyr.acc_y);
if (a > 0) {
- player.acc_x *= acc_mag / a;
- player.acc_y *= acc_mag / a;
+ plyr.acc_x *= acc_mag / a;
+ plyr.acc_y *= acc_mag / a;
}
// First the background
- const back_scroll = @floatToInt(i32, -constants.BackTextureWidth * player.ang / (2 * PI));
+ const back_scroll = @floatToInt(i32, -constants.BackTextureWidth * plyr.ang / (2 * PI));
back_sprite.setTextureRect(sf.IntRect{
.left = back_scroll, // fix location of texture in the sky
.top = 0,
@@ -179,7 +182,8 @@ pub fn main() !void {
});
window.draw(back_sprite, null);
- try player.renderWorld(
+ try renderer.renderWorld(
+ plyr,
window,
objects_image,
walls_image,
@@ -191,6 +195,6 @@ pub fn main() !void {
window.display();
- player.tick(map);
+ plyr.tick(map);
}
}