Refactored code

This commit is contained in:
Igor Łempicki 2023-03-23 20:55:34 +01:00
parent 462ed25b77
commit 69eb723614
4 changed files with 120 additions and 108 deletions

View File

@ -1 +1,4 @@
zig version: 0.10.1
*TODO*
Swap raylib bindings to raylib.zig from raylib-zig

View File

@ -1,95 +1,8 @@
const rl = @import("raylib");
const rl = @import("raylib");
const rlm = @import("raylib-math");
const std = @import("std");
const Object = struct {
box: rl.Rectangle,
texture: ?rl.Texture2D,
};
const Player = struct {
box: rl.Rectangle,
velocity: rl.Vector2,
// max_velocity: f32,
};
const Map = struct {
tiles: []Object,
};
const GRAVITY: f32 = -10;
const FRICTION: f32 = 10;
fn apply_player_phisics(player: *Player) void {
// apply gravity
player.velocity.y += GRAVITY * rl.GetFrameTime();
player.box.y -= player.velocity.y;
// apply friction
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;
// 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, -8, 8);
}
fn free_fly(player: *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;
// 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);
}
fn apply_player_collisions(player: *Player, map: Map) void {
// for every tile
for (map.tiles) |tile| {
// check if any collision occured
if ( rl.CheckCollisionRecs(player.box, tile.box) ) {
// get collision rectangle
var collision_rectangle = rl.GetCollisionRec(player.box, tile.box);
// extract ranges
var range: rl.Vector2 = .{ .x = collision_rectangle.width, .y = collision_rectangle.height };
// calculate midpoints
var player_midpoint: rl.Vector2 = .{ .x = player.box.x + (player.box.width / 2), .y = player.box.y + (player.box.height / 2) };
var tile_midpoint: rl.Vector2 = .{ .x = tile.box.x + (tile.box.width / 2), .y = tile.box.y + (tile.box.height / 2) };
var player_on_top = if (player_midpoint.y > tile_midpoint.y) true else false;
var player_on_right = if (player_midpoint.x > tile_midpoint.x) true else false;
// check on whitch side a collision occured and apply proper collisions
if (range.x > range.y) {
if (player_on_top) { player.box.y = tile.box.y + tile.box.height; } else { player.box.y = tile.box.y - player.box.height; }
player.velocity.y = 0;
}
else {
if (player_on_right) { player.box.x = tile.box.x + tile.box.width; } else { player.box.x = tile.box.x - player.box.width; }
player.velocity.x = 0;
}
}
}
}
const obj = @import("types.zig");
const phs = @import("physics.zig");
// fn load_level(comptime file_path: []const u8) void {
// // TODO
@ -129,15 +42,15 @@ pub fn main() anyerror!void
};
// var map: Map = Map { .tiles = undefined };
var objects = [_]Object{
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: Map = .{ .tiles = &objects };
var map: obj.Map = .{ .tiles = &objects };
var player: Player = .{
var player: obj.Player = .{
.box = .{ .x = 20, .y = 300, .width = 20, .height = 20 },
.velocity = .{ .x = 0, .y = 0 },
};
@ -158,25 +71,26 @@ pub fn main() anyerror!void
// Main game loop ================================================================
while (!rl.WindowShouldClose()) // Detect window close button or ESC key
{
// run game logic
apply_player_collisions(&player, map);
// handle user input
if (rl.IsKeyDown(rl.KeyboardKey.KEY_RIGHT)) { player.velocity.x += 1.5; } // move right
if (rl.IsKeyDown(rl.KeyboardKey.KEY_LEFT )) { player.velocity.x -= 1.5; } // move left
if (rl.IsKeyPressed(rl.KeyboardKey.KEY_F)) { fly = !fly; }
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);
} else {
if (rl.IsKeyPressed(rl.KeyboardKey.KEY_UP)) { player.velocity.y = 15; } // jump
phs.apply_forces(&player);
}
// handle user input
if (rl.IsKeyDown(rl.KeyboardKey.KEY_RIGHT)) { player.velocity.x += 1.5; } // move right
if (rl.IsKeyDown(rl.KeyboardKey.KEY_LEFT )) { player.velocity.x -= 1.5; } // move left
if (rl.IsKeyPressed(rl.KeyboardKey.KEY_F)) { fly = !fly; }
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; }
free_fly(&player);
} else {
if (rl.IsKeyPressed(rl.KeyboardKey.KEY_UP)) { player.velocity.y = 15; } // jump
apply_player_phisics(&player);
}
// run collisions
phs.apply_player_collisions(&player, map);
// camera setup
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;

79
src/physics.zig Normal file
View File

@ -0,0 +1,79 @@
const rl = @import("raylib");
const rlm = @import("raylib-math");
const obj = @import("types.zig");
// constants
const GRAVITY: f32 = -10;
const FRICTION: f32 = 10;
pub fn apply_forces(player: *obj.Player) void {
// apply gravity
player.velocity.y += GRAVITY * rl.GetFrameTime();
player.box.y -= player.velocity.y;
// apply friction
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;
// 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, -8, 8);
}
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;
// 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 {
// for every tile
for (map.tiles) |tile| {
// check if any collision occured
if ( rl.CheckCollisionRecs(player.box, tile.box) ) {
// get collision rectangle
var collision_rectangle = rl.GetCollisionRec(player.box, tile.box);
// extract ranges
var range: rl.Vector2 = .{ .x = collision_rectangle.width, .y = collision_rectangle.height };
// calculate midpoints
var player_midpoint: rl.Vector2 = .{ .x = player.box.x + (player.box.width / 2), .y = player.box.y + (player.box.height / 2) };
var tile_midpoint: rl.Vector2 = .{ .x = tile.box.x + (tile.box.width / 2), .y = tile.box.y + (tile.box.height / 2) };
var player_on_top = if (player_midpoint.y > tile_midpoint.y) true else false;
var player_on_right = if (player_midpoint.x > tile_midpoint.x) true else false;
// check on whitch side a collision occured and apply proper collisions
if (range.x > range.y) {
if (player_on_top) { player.box.y = tile.box.y + tile.box.height; } else { player.box.y = tile.box.y - player.box.height; }
player.velocity.y = 0;
}
else {
if (player_on_right) { player.box.x = tile.box.x + tile.box.width; } else { player.box.x = tile.box.x - player.box.width; }
player.velocity.x = 0;
}
}
}
}

16
src/types.zig Normal file
View File

@ -0,0 +1,16 @@
const rl = @import("raylib");
pub const Object = struct {
box: rl.Rectangle,
texture: ?rl.Texture2D,
};
pub const Player = struct {
box: rl.Rectangle,
velocity: rl.Vector2,
// max_velocity: f32,
};
pub const Map = struct {
tiles: []Object,
};