From deb26d5d49504269bcf8cf9f6016f83c5332f7bf Mon Sep 17 00:00:00 2001 From: IgLemp Date: Thu, 23 Mar 2023 21:44:27 +0100 Subject: [PATCH] Reduced readability of code --- README.md | 4 +++- src/main.zig | 33 +++++++++++++-------------------- src/physics.zig | 32 +++++++++++++++++--------------- src/types.zig | 6 ++++++ 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 22d8716..fb2d4ba 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ zig version: 0.10.1 *TODO* -Swap raylib bindings to raylib.zig from raylib-zig +- Swap raylib bindings to raylib.zig from raylib-zig +- Setup level loading +- Setup texture loading diff --git a/src/main.zig b/src/main.zig index e226c56..a43f15b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,24 +4,11 @@ const std = @import("std"); const obj = @import("types.zig"); const phs = @import("physics.zig"); -// fn load_level(comptime file_path: []const u8) void { - // // TODO -// } +const DEBUG = true; // NOTE // Coordinate system starts at top left corner, whitch means THE Y AXIS IS FLIPPED!!! -// TODO -// Setup level loading -// Setup texture loading - -// DEBUG -// pub fn main() anyerror!void { -// load_level("./resources/scene.dat"); -// } - - - // MAIN pub fn main() anyerror!void { @@ -52,6 +39,7 @@ pub fn main() anyerror!void var player: obj.Player = .{ .box = .{ .x = 20, .y = 300, .width = 20, .height = 20 }, + .detection_box = .{ .width = 21, .height = 21 }, .velocity = .{ .x = 0, .y = 0 }, }; @@ -79,7 +67,7 @@ pub fn main() anyerror!void if (fly) { if (rl.IsKeyDown(rl.KeyboardKey.KEY_UP)) { player.velocity.y += 1.5; } if (rl.IsKeyDown(rl.KeyboardKey.KEY_DOWN)) { player.velocity.y -= 1.5; } - phs.free_fly(&player); + phs.movement.free_fly(&player); } else { if (rl.IsKeyPressed(rl.KeyboardKey.KEY_UP)) { player.velocity.y = 15; } // jump phs.apply_forces(&player); @@ -117,13 +105,18 @@ pub fn main() anyerror!void for (map.tiles) |tile| { rl.DrawRectangleRec(tile.box, rl.GRAY); - var midpoint: rl.Vector2 = .{ .x = tile.box.x + (tile.box.width / 2), .y = tile.box.y + (tile.box.height / 2) }; - rl.DrawCircleV( midpoint, 4, rl.GREEN); - var col_rec = rl.GetCollisionRec(player.box, tile.box); - rl.DrawRectangleRec(col_rec, rl.BLUE); + // DEBUG + if (DEBUG) { + var midpoint: rl.Vector2 = .{ .x = tile.box.x + (tile.box.width / 2), .y = tile.box.y + (tile.box.height / 2) }; + rl.DrawCircleV( midpoint, 4, rl.GREEN); + + var col_rec = rl.GetCollisionRec(player.box, tile.box); + rl.DrawRectangleRec(col_rec, rl.BLUE); + + rl.DrawCircleV( .{ .x = tile.box.x, .y = tile.box.y }, 4, rl.ORANGE ); + } - rl.DrawCircleV( .{ .x = tile.box.x, .y = tile.box.y }, 4, rl.ORANGE ); } camera.End(); diff --git a/src/physics.zig b/src/physics.zig index 024e8fd..c727d7c 100644 --- a/src/physics.zig +++ b/src/physics.zig @@ -24,24 +24,26 @@ pub fn apply_forces(player: *obj.Player) void { player.velocity.y = rlm.Clamp(player.velocity.y, -8, 8); } + pub const movement = struct { + pub fn free_fly(player: *obj.Player) void { + // apply friction for X + if (player.velocity.x > 0) { player.velocity.x -= FRICTION * rl.GetFrameTime(); } + if (player.velocity.x < 0) { player.velocity.x += FRICTION * rl.GetFrameTime(); } + if (player.velocity.x >= -1 and player.velocity.x <= 1) { player.velocity.x = 0; } + player.box.x += player.velocity.x; -pub fn free_fly(player: *obj.Player) void { - // apply friction for X - if (player.velocity.x > 0) { player.velocity.x -= FRICTION * rl.GetFrameTime(); } - if (player.velocity.x < 0) { player.velocity.x += FRICTION * rl.GetFrameTime(); } - if (player.velocity.x >= -1 and player.velocity.x <= 1) { player.velocity.x = 0; } - player.box.x += player.velocity.x; + // apply friction for Y + if (player.velocity.y > 0) { player.velocity.y -= FRICTION * rl.GetFrameTime(); } + if (player.velocity.y < 0) { player.velocity.y += FRICTION * rl.GetFrameTime(); } + if (player.velocity.y >= -1 and player.velocity.y <= 1) { player.velocity.y = 0; } + player.box.y -= player.velocity.y; - // apply friction for Y - if (player.velocity.y > 0) { player.velocity.y -= FRICTION * rl.GetFrameTime(); } - if (player.velocity.y < 0) { player.velocity.y += FRICTION * rl.GetFrameTime(); } - if (player.velocity.y >= -1 and player.velocity.y <= 1) { player.velocity.y = 0; } - player.box.y -= player.velocity.y; + // clamp velocities to not reach light speed in a second + player.velocity.x = rlm.Clamp(player.velocity.x, -4, 4); + player.velocity.y = rlm.Clamp(player.velocity.y, -4, 4); + } +}; - // clamp velocities to not reach light speed in a second - player.velocity.x = rlm.Clamp(player.velocity.x, -4, 4); - player.velocity.y = rlm.Clamp(player.velocity.y, -4, 4); -} pub fn apply_player_collisions(player: *obj.Player, map: obj.Map) void { diff --git a/src/types.zig b/src/types.zig index 4f7e210..7385afd 100644 --- a/src/types.zig +++ b/src/types.zig @@ -7,10 +7,16 @@ pub const Object = struct { pub const Player = struct { box: rl.Rectangle, + detection_box: NullOriginBox, velocity: rl.Vector2, // max_velocity: f32, }; +const NullOriginBox = struct { + height: f32, + width: f32 +}; + pub const Map = struct { tiles: []Object, }; \ No newline at end of file