mirror of
https://github.com/IgLemp/platformer.git
synced 2025-04-11 12:45:00 +02:00
Decoupled phisics from rendering and made map heap allocated
This commit is contained in:
parent
58f1dbad74
commit
c5d1b05d8c
73
src/main.zig
73
src/main.zig
@ -12,6 +12,11 @@ const DEBUG = true;
|
|||||||
// MAIN
|
// MAIN
|
||||||
pub fn main() anyerror!void
|
pub fn main() anyerror!void
|
||||||
{
|
{
|
||||||
|
// MEMORY ALLOCATOR
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||||
|
var arenaAlloc = arena.allocator();
|
||||||
|
|
||||||
// Initialization
|
// Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
const screenWidth = 800;
|
const screenWidth = 800;
|
||||||
@ -28,26 +33,29 @@ pub fn main() anyerror!void
|
|||||||
.zoom = 1,
|
.zoom = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
// var map: Map = Map { .tiles = undefined };
|
// var map: obj.Map = obj.Map { .tiles = undefined };
|
||||||
var objects = [_]obj.Object{
|
// var objects = [_]obj.Object{
|
||||||
.{ .box = .{ .x = 0, .y = 0, .width = 300, .height = 20, }, .texture = null },
|
// .{ .box = .{ .x = 0, .y = 0, .width = 300, .height = 20, }, .texture = null },
|
||||||
.{ .box = .{ .x = 100, .y = 0, .width = 20, .height = 200, }, .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 map: obj.Map = .{ .tiles = &objects };
|
||||||
|
|
||||||
|
|
||||||
var player: obj.Player = .{
|
var player: obj.Player = .{
|
||||||
.box = .{ .x = 20, .y = 300, .width = 20, .height = 20 },
|
.box = .{ .x = 20, .y = 300, .width = 20, .height = 20 },
|
||||||
|
.detection_box = undefined,
|
||||||
.velocity = .{ .x = 0, .y = 0 },
|
.velocity = .{ .x = 0, .y = 0 },
|
||||||
};
|
};
|
||||||
|
|
||||||
// dirty object fixer upper
|
player.detection_box = .{ .x = &player.box.x, .y = &player.box.y, .width = 22, .height = 22 };
|
||||||
player.box.y = -player.box.y;
|
|
||||||
|
|
||||||
for (map.tiles) |*tile| {
|
var texture = rl.LoadTexture("./resources/log.png");
|
||||||
tile.box.y = -tile.box.height;
|
var playerTexture = rl.LoadTexture("./resources/cursor.png");
|
||||||
}
|
|
||||||
|
|
||||||
// freefly
|
// freefly
|
||||||
var fly = true;
|
var fly = true;
|
||||||
@ -68,7 +76,6 @@ pub fn main() anyerror!void
|
|||||||
if (rl.IsKeyDown(rl.KeyboardKey.KEY_DOWN)) { player.velocity.y -= 1.5; }
|
if (rl.IsKeyDown(rl.KeyboardKey.KEY_DOWN)) { player.velocity.y -= 1.5; }
|
||||||
phs.movement.FreeFly(&player);
|
phs.movement.FreeFly(&player);
|
||||||
} else {
|
} else {
|
||||||
if (rl.IsKeyPressed(rl.KeyboardKey.KEY_UP)) { player.velocity.y = 15; } // jump
|
|
||||||
phs.ApplyForces(&player);
|
phs.ApplyForces(&player);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,15 +83,15 @@ pub fn main() anyerror!void
|
|||||||
phs.ApplyPlayerCollisions(&player, map);
|
phs.ApplyPlayerCollisions(&player, map);
|
||||||
|
|
||||||
// camera setup
|
// 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
|
// input related to camera
|
||||||
camera.zoom += rl.GetMouseWheelMove() * 0.05;
|
camera.zoom += rl.GetMouseWheelMove() * 0.05 * camera.zoom;
|
||||||
|
|
||||||
|
|
||||||
// DEBUG
|
// DEBUG
|
||||||
// try stdout.print("player position: x = {d}, y = {d}\n", .{player.box.x, player.box.y});
|
// 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 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
|
// init drawing
|
||||||
@ -96,29 +103,51 @@ pub fn main() anyerror!void
|
|||||||
{
|
{
|
||||||
// init camera
|
// init camera
|
||||||
camera.Begin();
|
camera.Begin();
|
||||||
|
defer camera.End();
|
||||||
|
|
||||||
|
defer rl.DrawCircleV( .{ .x = 0, .y = 0 } , 4, rl.BLUE);
|
||||||
|
|
||||||
|
// what have I done
|
||||||
|
rl.DrawTextureV(texture, .{ .x = -100, .y = -200}, rl.WHITE);
|
||||||
|
|
||||||
rl.DrawCircleV( .{ .x = 0, .y = 0 } , 4, rl.BLUE);
|
|
||||||
|
|
||||||
// draw player
|
// 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
|
// DEBUG
|
||||||
if (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);
|
rl.DrawCircleV( midpoint, 4, rl.GREEN);
|
||||||
|
|
||||||
|
// draw collision rectangles (with player)
|
||||||
var col_rec = rl.GetCollisionRec(player.box, tile.box);
|
var col_rec = rl.GetCollisionRec(player.box, tile.box);
|
||||||
rl.DrawRectangleRec(col_rec, rl.BLUE);
|
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();
|
rl.EndDrawing();
|
||||||
|
@ -11,7 +11,7 @@ const FRICTION: f32 = 10;
|
|||||||
pub fn ApplyForces(player: *obj.Player) void {
|
pub fn ApplyForces(player: *obj.Player) void {
|
||||||
// apply gravity
|
// apply gravity
|
||||||
player.velocity.y += GRAVITY * rl.GetFrameTime();
|
player.velocity.y += GRAVITY * rl.GetFrameTime();
|
||||||
player.box.y -= player.velocity.y;
|
player.box.y += player.velocity.y;
|
||||||
|
|
||||||
// apply friction
|
// apply friction
|
||||||
if (player.velocity.x > 0) { player.velocity.x -= FRICTION * rl.GetFrameTime(); }
|
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 < 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; }
|
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
|
// clamp velocities to not reach light speed in a second
|
||||||
player.velocity.x = rlm.Clamp(player.velocity.x, -4, 4);
|
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 {
|
pub fn ApplyPlayerCollisions(player: *obj.Player, map: obj.Map) void {
|
||||||
|
|
||||||
// for every tile
|
// for every tile
|
||||||
for (map.tiles) |tile| {
|
for (map.tiles.allocatedSlice()) |tile| {
|
||||||
|
|
||||||
// check if any collision occured
|
// check if any collision occured
|
||||||
if ( rl.CheckCollisionRecs(player.box, tile.box) ) {
|
if ( rl.CheckCollisionRecs(player.box, tile.box) ) {
|
||||||
|
BIN
src/resources/cursor.png
Normal file
BIN
src/resources/cursor.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 962 B |
BIN
src/resources/log.png
Normal file
BIN
src/resources/log.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 MiB |
@ -1,4 +1,5 @@
|
|||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
pub const Object = struct {
|
pub const Object = struct {
|
||||||
box: rl.Rectangle,
|
box: rl.Rectangle,
|
||||||
@ -7,16 +8,18 @@ pub const Object = struct {
|
|||||||
|
|
||||||
pub const Player = struct {
|
pub const Player = struct {
|
||||||
box: rl.Rectangle,
|
box: rl.Rectangle,
|
||||||
// detection_box: NullOriginBox,
|
detection_box: DetectionBox,
|
||||||
velocity: rl.Vector2,
|
velocity: rl.Vector2,
|
||||||
// max_velocity: f32,
|
// max_velocity: f32,
|
||||||
};
|
};
|
||||||
|
|
||||||
// const NullOriginBox = struct {
|
pub const DetectionBox = struct {
|
||||||
// height: f32,
|
x: *f32,
|
||||||
// width: f32
|
y: *f32,
|
||||||
// };
|
width: f32,
|
||||||
|
height: f32
|
||||||
|
};
|
||||||
|
|
||||||
pub const Map = struct {
|
pub const Map = struct {
|
||||||
tiles: []Object,
|
tiles: *std.ArrayList(Object),
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user