diff --git a/src/main.zig b/src/main.zig index 2695c88..a5d5f9e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -12,6 +12,11 @@ const DEBUG = true; // MAIN pub fn main() anyerror!void { + // MEMORY ALLOCATOR + //-------------------------------------------------------------------------------------- + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + var arenaAlloc = arena.allocator(); + // Initialization //-------------------------------------------------------------------------------------- const screenWidth = 800; @@ -28,26 +33,29 @@ pub fn main() anyerror!void .zoom = 1, }; - // var map: Map = Map { .tiles = undefined }; - var objects = [_]obj.Object{ - .{ .box = .{ .x = 0, .y = 0, .width = 300, .height = 20, }, .texture = null }, - .{ .box = .{ .x = 100, .y = 0, .width = 20, .height = 200, }, .texture = null } - }; + // var map: obj.Map = obj.Map { .tiles = undefined }; + // var objects = [_]obj.Object{ + // .{ .box = .{ .x = 0, .y = 0, .width = 300, .height = 20, }, .texture = null }, + // .{ .box = .{ .x = 100, .y = 0, .width = 20, .height = 200, }, .texture = null } + // }; + + var objects = std.ArrayList(obj.Object).init(arenaAlloc); + try objects.append(.{ .box = .{ .x = 0, .y = 0, .width = 300, .height = 20, }, .texture = null }); + try objects.append(.{ .box = .{ .x = 100, .y = 0, .width = 20, .height = 200, }, .texture = null }); var map: obj.Map = .{ .tiles = &objects }; var player: obj.Player = .{ .box = .{ .x = 20, .y = 300, .width = 20, .height = 20 }, + .detection_box = undefined, .velocity = .{ .x = 0, .y = 0 }, }; - - // dirty object fixer upper - player.box.y = -player.box.y; - for (map.tiles) |*tile| { - tile.box.y = -tile.box.height; - } + player.detection_box = .{ .x = &player.box.x, .y = &player.box.y, .width = 22, .height = 22 }; + + var texture = rl.LoadTexture("./resources/log.png"); + var playerTexture = rl.LoadTexture("./resources/cursor.png"); // freefly var fly = true; @@ -67,8 +75,7 @@ pub fn main() anyerror!void 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.movement.FreeFly(&player); - } else { - if (rl.IsKeyPressed(rl.KeyboardKey.KEY_UP)) { player.velocity.y = 15; } // jump + } else { phs.ApplyForces(&player); } @@ -76,15 +83,15 @@ pub fn main() anyerror!void phs.ApplyPlayerCollisions(&player, map); // camera setup - camera.target = rl.Vector2 { .x = player.box.x + player.box.width / 2, .y = player.box.y + player.box.height / 2 }; + camera.target = rl.Vector2 { .x = player.box.x + player.box.width / 2, .y = -(player.box.y + player.box.height / 2) }; // input related to camera - camera.zoom += rl.GetMouseWheelMove() * 0.05; + camera.zoom += rl.GetMouseWheelMove() * 0.05 * camera.zoom; // DEBUG // try stdout.print("player position: x = {d}, y = {d}\n", .{player.box.x, player.box.y}); // std.log.debug("player position: x = {d}, y = {d}, velocity: x = {d}, y = {d}", .{player.box.x, player.box.y, player.velocity.x, player.velocity.y}); - + // std.log.debug("{}", .{player.detection_box}); { // init drawing @@ -96,29 +103,51 @@ pub fn main() anyerror!void { // init camera camera.Begin(); + defer camera.End(); - rl.DrawCircleV( .{ .x = 0, .y = 0 } , 4, rl.BLUE); + defer rl.DrawCircleV( .{ .x = 0, .y = 0 } , 4, rl.BLUE); + // what have I done + rl.DrawTextureV(texture, .{ .x = -100, .y = -200}, rl.WHITE); + + // draw player - rl.DrawRectangleRec(player.box, rl.RED); + var playerRenderBox = .{ .x = player.box.x, .y = -player.box.y - player.box.height, .width = player.box.width, .height = player.box.height }; + _ = playerRenderBox; + // defer rl.DrawRectangleRec( playerRenderBox, rl.RED); + defer rl.DrawTextureV( playerTexture, .{ .x = player.box.x, .y = -player.box.y - player.box.height }, rl.WHITE); + // rl.DrawRectangleRec(.{ .x = player.detection_box.x.* - 5 , .y = player.detection_box.y.* - 5, .width = player.detection_box.width, .height = player.detection_box.height }, rl.RED); - for (map.tiles) |tile| { - rl.DrawRectangleRec(tile.box, rl.GRAY); + + // tile drawing + for (map.tiles.allocatedSlice()) |tile| { + var dispTile = .{ .x = tile.box.x, .y = tile.box.y - tile.box.height, .width = tile.box.width, .height = tile.box.height }; + rl.DrawRectangleRec(dispTile, rl.GRAY); // DEBUG if (DEBUG) { - var midpoint: rl.Vector2 = .{ .x = tile.box.x + (tile.box.width / 2), .y = tile.box.y + (tile.box.height / 2) }; + // draw block midpoints + 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); + // draw collision rectangles (with player) 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 ); + // draw tile origin points + rl.DrawCircleV( .{ .x = tile.box.x, .y = -tile.box.y }, 4, rl.ORANGE ); + + // draw collision rectangle (with player collision box) + rl.DrawRectangleRec( rl.GetCollisionRec( .{ .x = player.detection_box.x.* - 1, .y = -player.detection_box.y.* - 1 - player.box.height, .width = player.detection_box.width, .height = player.detection_box.height}, dispTile ), rl.BLUE); + } + + if (!fly) { + if (rl.IsKeyPressed(rl.KeyboardKey.KEY_UP) and + rl.CheckCollisionRecs( .{ .x = player.detection_box.x.* - 1, .y = player.detection_box.y.* - 1, .width = player.detection_box.width, .height = player.detection_box.height}, tile.box ) + ) { player.velocity.y = 15; } // jump } } - - camera.End(); } rl.EndDrawing(); diff --git a/src/physics.zig b/src/physics.zig index 1c80183..d088658 100644 --- a/src/physics.zig +++ b/src/physics.zig @@ -11,7 +11,7 @@ const FRICTION: f32 = 10; pub fn ApplyForces(player: *obj.Player) void { // apply gravity player.velocity.y += GRAVITY * rl.GetFrameTime(); - player.box.y -= player.velocity.y; + player.box.y += player.velocity.y; // apply friction if (player.velocity.x > 0) { player.velocity.x -= FRICTION * rl.GetFrameTime(); } @@ -24,7 +24,7 @@ pub fn ApplyForces(player: *obj.Player) void { player.velocity.y = rlm.Clamp(player.velocity.y, -8, 8); } - pub const movement = struct { +pub const movement = struct { pub fn FreeFly(player: *obj.Player) void { // apply friction for X if (player.velocity.x > 0) { player.velocity.x -= FRICTION * rl.GetFrameTime(); } @@ -36,7 +36,7 @@ pub fn ApplyForces(player: *obj.Player) void { 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; + 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); @@ -49,7 +49,7 @@ pub fn ApplyForces(player: *obj.Player) void { pub fn ApplyPlayerCollisions(player: *obj.Player, map: obj.Map) void { // for every tile - for (map.tiles) |tile| { + for (map.tiles.allocatedSlice()) |tile| { // check if any collision occured if ( rl.CheckCollisionRecs(player.box, tile.box) ) { diff --git a/src/resources/cursor.png b/src/resources/cursor.png new file mode 100644 index 0000000..93fcf2e Binary files /dev/null and b/src/resources/cursor.png differ diff --git a/src/resources/log.png b/src/resources/log.png new file mode 100644 index 0000000..3456f0e Binary files /dev/null and b/src/resources/log.png differ diff --git a/src/types.zig b/src/types.zig index df461ce..9c7cf06 100644 --- a/src/types.zig +++ b/src/types.zig @@ -1,4 +1,5 @@ -const rl = @import("raylib"); +const rl = @import("raylib"); +const std = @import("std"); pub const Object = struct { box: rl.Rectangle, @@ -7,16 +8,18 @@ pub const Object = struct { pub const Player = struct { box: rl.Rectangle, - // detection_box: NullOriginBox, + detection_box: DetectionBox, velocity: rl.Vector2, // max_velocity: f32, }; -// const NullOriginBox = struct { -// height: f32, -// width: f32 -// }; +pub const DetectionBox = struct { + x: *f32, + y: *f32, + width: f32, + height: f32 +}; pub const Map = struct { - tiles: []Object, + tiles: *std.ArrayList(Object), }; \ No newline at end of file