Reduced readability of code

This commit is contained in:
Igor Łempicki 2023-03-23 21:44:27 +01:00
parent 69eb723614
commit deb26d5d49
4 changed files with 39 additions and 36 deletions

View File

@ -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

View File

@ -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();

View File

@ -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 {

View File

@ -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,
};