mirror of
https://github.com/IgLemp/platformer.git
synced 2025-12-06 15:35:33 +01:00
Compare commits
5 Commits
c5d1b05d8c
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| c69a601a4f | |||
| 4723632792 | |||
|
|
933538a42f | ||
|
|
8fdca7bbb9 | ||
|
|
c003155edd |
26
src/display.zig
Normal file
26
src/display.zig
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
const rl = @import("raylib");
|
||||||
|
|
||||||
|
// functions for drawing shapes from WS coordinates
|
||||||
|
|
||||||
|
// RECTANGLES ===============
|
||||||
|
pub inline fn DrawRectangleWS(posX: c_int, posY: c_int, width: c_int, height: c_int, color: rl.Color) void
|
||||||
|
{ rl.DrawRectangle(posX, -(posY + height), width, height, color); }
|
||||||
|
|
||||||
|
pub inline fn DrawRectangleVWS(position: rl.Vector2, size: rl.Vector2, color: rl.Color) void
|
||||||
|
{ rl.DrawRectangleV(.{ .x = position.x, .y = -(position.y + size.y) }, size, color); }
|
||||||
|
|
||||||
|
pub inline fn DrawRectangleRecWS(rec: rl.Rectangle, color: rl.Color) void
|
||||||
|
{ rl.DrawRectangleRec(.{ .x = rec.x, .y = -(rec.y + rec.height), .width = rec.width, .height = rec.height}, color); }
|
||||||
|
|
||||||
|
|
||||||
|
// CIRCLES ===============
|
||||||
|
pub inline fn DrawCircleVWS(center: rl.Vector2, radius: f32, color: rl.Color) void
|
||||||
|
{ rl.DrawCircleV(.{ .x = center.x, .y = -center.y }, radius, color); }
|
||||||
|
|
||||||
|
|
||||||
|
// TEXTURES ===============
|
||||||
|
pub inline fn DrawTextureVWS(texture: rl.Texture2D, position: rl.Vector2, tint: rl.Color) void
|
||||||
|
{ rl.DrawTextureV(texture, .{ .x = position.x, .y = -position.y}, tint); }
|
||||||
|
|
||||||
|
pub inline fn DrawTextureRecWS(texture: rl.Texture2D, source: rl.Rectangle, position: rl.Vector2, tint: rl.Color) void
|
||||||
|
{ rl.DrawTextureRec(texture, .{ .x = source.x, .y = -source.y, .width = source.width, .height = source.height }, .{ .x = position.x, .y = -position.y }, tint); }
|
||||||
0
src/io.zig
Normal file
0
src/io.zig
Normal file
89
src/main.zig
89
src/main.zig
@@ -3,19 +3,21 @@ const rlm = @import("raylib-math");
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const obj = @import("types.zig");
|
const obj = @import("types.zig");
|
||||||
const phs = @import("physics.zig");
|
const phs = @import("physics.zig");
|
||||||
|
const utils = @import("utils.zig");
|
||||||
|
const disp = @import("display.zig");
|
||||||
|
|
||||||
const DEBUG = true;
|
const DEBUG = true;
|
||||||
|
|
||||||
// NOTE
|
// NOTE
|
||||||
// Coordinate system starts at top left corner, whitch means THE Y AXIS IS FLIPPED!!!
|
// Coordinate system starts at top left corner, whitch means THE Y AXIS IS FLIPPED!!! (for on screen coordinates)
|
||||||
|
|
||||||
// MAIN
|
// MAIN
|
||||||
pub fn main() anyerror!void
|
pub fn main() anyerror!void
|
||||||
{
|
{
|
||||||
// MEMORY ALLOCATOR
|
// MEMORY ALLOCATOR
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
var gp_alloc = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
var arenaAlloc = arena.allocator();
|
var allocator = gp_alloc.allocator();
|
||||||
|
|
||||||
// Initialization
|
// Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
@@ -26,20 +28,15 @@ pub fn main() anyerror!void
|
|||||||
// rl.SetWindowState(rl.ConfigFlags.FLAG_WINDOW_RESIZABLE);
|
// rl.SetWindowState(rl.ConfigFlags.FLAG_WINDOW_RESIZABLE);
|
||||||
defer rl.CloseWindow(); // Close window and OpenGL context
|
defer rl.CloseWindow(); // Close window and OpenGL context
|
||||||
|
|
||||||
var camera = rl.Camera2D {
|
var ws_camera = rl.Camera2D {
|
||||||
.target = rl.Vector2 { .x = 0, .y = 0 },
|
.target = rl.Vector2 { .x = 0, .y = 0 },
|
||||||
.offset = rl.Vector2 { .x = screenWidth / 2, .y = screenHeight / 2 },
|
.offset = rl.Vector2 { .x = screenWidth / 2, .y = screenHeight / 2 },
|
||||||
.rotation = 0,
|
.rotation = 0,
|
||||||
.zoom = 1,
|
.zoom = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
// 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);
|
var objects = std.ArrayList(obj.Object).init(allocator);
|
||||||
try objects.append(.{ .box = .{ .x = 0, .y = 0, .width = 300, .height = 20, }, .texture = null });
|
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 });
|
try objects.append(.{ .box = .{ .x = 100, .y = 0, .width = 20, .height = 200, }, .texture = null });
|
||||||
|
|
||||||
@@ -55,13 +52,15 @@ pub fn main() anyerror!void
|
|||||||
player.detection_box = .{ .x = &player.box.x, .y = &player.box.y, .width = 22, .height = 22 };
|
player.detection_box = .{ .x = &player.box.x, .y = &player.box.y, .width = 22, .height = 22 };
|
||||||
|
|
||||||
var texture = rl.LoadTexture("./resources/log.png");
|
var texture = rl.LoadTexture("./resources/log.png");
|
||||||
var playerTexture = rl.LoadTexture("./resources/cursor.png");
|
var player_texture = rl.LoadTexture("./resources/cursor.png");
|
||||||
|
|
||||||
// freefly
|
// freefly
|
||||||
var fly = true;
|
var fly = true;
|
||||||
|
|
||||||
rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
|
|
||||||
|
var mouse_ws_position: rl.Vector2 = undefined;
|
||||||
|
var mouse_ws_end_position: rl.Vector2 = undefined;
|
||||||
|
|
||||||
// Main game loop ================================================================
|
// Main game loop ================================================================
|
||||||
while (!rl.WindowShouldClose()) // Detect window close button or ESC key
|
while (!rl.WindowShouldClose()) // Detect window close button or ESC key
|
||||||
@@ -83,62 +82,86 @@ 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) };
|
ws_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;
|
ws_camera.zoom += rl.GetMouseWheelMove() * 0.05 * ws_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});
|
// std.log.debug("{}", .{player.detection_box});
|
||||||
|
// std.debug.print("camera terget: x = {d}, y = {d}\n", .{camera.target.x, camera.target.y});
|
||||||
|
|
||||||
|
|
||||||
|
// MOUSE SELECTION LOGIC =======
|
||||||
|
if (rl.IsMouseButtonPressed(.MOUSE_BUTTON_LEFT)) { mouse_ws_position = utils.GetMousePositionWS(ws_camera, screenWidth, screenHeight); }
|
||||||
|
if (rl.IsMouseButtonDown(.MOUSE_BUTTON_LEFT)) { mouse_ws_end_position = utils.GetMousePositionWS(ws_camera, screenWidth, screenHeight); }
|
||||||
|
|
||||||
|
var rec_fixed = .{ .x = mouse_ws_position.x, .y = mouse_ws_position.y };
|
||||||
|
var rec_wh = .{ .x = @fabs(mouse_ws_position.x - mouse_ws_end_position.x), .y = @fabs(mouse_ws_position.y - mouse_ws_end_position.y)};
|
||||||
|
if (mouse_ws_end_position.x < mouse_ws_position.x) { rec_fixed.x = mouse_ws_end_position.x; }
|
||||||
|
if (mouse_ws_end_position.y < mouse_ws_position.y) { rec_fixed.y = mouse_ws_end_position.y; }
|
||||||
|
|
||||||
|
if (rl.IsMouseButtonReleased(.MOUSE_BUTTON_LEFT)) { try map.tiles.append( .{ .box = .{ .x = rec_fixed.x, .y = rec_fixed.y, .width = rec_wh.x, .height = rec_wh.y }, .texture = null } ); }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
// init drawing
|
// init drawing
|
||||||
rl.BeginDrawing();
|
rl.BeginDrawing();
|
||||||
|
defer rl.EndDrawing();
|
||||||
|
|
||||||
// clear screen with WHITE
|
// clear screen with WHITE
|
||||||
defer rl.ClearBackground(rl.RAYWHITE);
|
rl.ClearBackground(rl.RAYWHITE);
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
// init camera
|
// init camera
|
||||||
camera.Begin();
|
ws_camera.Begin();
|
||||||
defer camera.End();
|
defer ws_camera.End();
|
||||||
|
|
||||||
defer rl.DrawCircleV( .{ .x = 0, .y = 0 } , 4, rl.BLUE);
|
|
||||||
|
defer disp.DrawCircleVWS( .{ .x = 0, .y = 0 }, 4, rl.BLUE);
|
||||||
|
|
||||||
// what have I done
|
// what have I done
|
||||||
rl.DrawTextureV(texture, .{ .x = -100, .y = -200}, rl.WHITE);
|
rl.DrawTextureV(texture, .{ .x = -100, .y = -200}, rl.WHITE);
|
||||||
|
|
||||||
|
disp.DrawCircleVWS( mouse_ws_position, 10, rl.RED);
|
||||||
|
disp.DrawCircleVWS( mouse_ws_end_position, 10, rl.BLUE);
|
||||||
|
|
||||||
|
// for drawing region selected with mouse
|
||||||
|
disp.DrawRectangleVWS( rec_fixed, rec_wh, rl.BLUE );
|
||||||
|
|
||||||
|
|
||||||
// draw player
|
|
||||||
var playerRenderBox = .{ .x = player.box.x, .y = -player.box.y - player.box.height, .width = player.box.width, .height = player.box.height };
|
// draw player =====
|
||||||
_ = playerRenderBox;
|
// 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.DrawRectangleRec( playerRenderBox, rl.RED);
|
||||||
defer rl.DrawTextureV( playerTexture, .{ .x = player.box.x, .y = -player.box.y - player.box.height }, rl.WHITE);
|
defer disp.DrawTextureVWS( player_texture, .{ .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);
|
// 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);
|
||||||
|
|
||||||
|
|
||||||
// tile drawing
|
// tile drawing
|
||||||
for (map.tiles.allocatedSlice()) |tile| {
|
for (map.tiles.items) |tile| {
|
||||||
var dispTile = .{ .x = tile.box.x, .y = tile.box.y - tile.box.height, .width = tile.box.width, .height = tile.box.height };
|
disp.DrawRectangleRecWS(tile.box, rl.GRAY);
|
||||||
rl.DrawRectangleRec(dispTile, rl.GRAY);
|
|
||||||
|
|
||||||
// DEBUG
|
// DEBUG
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
// draw block midpoints
|
// draw block midpoints
|
||||||
var midpoint: rl.Vector2 = .{ .x = tile.box.x + (tile.box.width / 2), .y = -(tile.box.y + (tile.box.height / 2)) };
|
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);
|
disp.DrawCircleVWS( midpoint, 4, rl.GREEN);
|
||||||
|
|
||||||
// draw collision rectangles (with player)
|
// 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);
|
disp.DrawRectangleRecWS(col_rec, rl.BLUE);
|
||||||
|
|
||||||
// draw tile origin points
|
// draw tile origin points
|
||||||
rl.DrawCircleV( .{ .x = tile.box.x, .y = -tile.box.y }, 4, rl.ORANGE );
|
disp.DrawCircleVWS( .{ .x = tile.box.x, .y = tile.box.y }, 4, rl.ORANGE );
|
||||||
|
|
||||||
// draw collision rectangle (with player collision box)
|
// draw collision rectangle (with player detection 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);
|
disp.DrawRectangleRecWS( rl.GetCollisionRec( .{ .x = player.detection_box.x.* - 1, .y = player.detection_box.y.* - 1, .width = player.detection_box.width, .height = player.detection_box.height}, tile.box ), rl.BLUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fly) {
|
if (!fly) {
|
||||||
@@ -148,9 +171,9 @@ pub fn main() anyerror!void
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rl.EndDrawing();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ pub const movement = struct {
|
|||||||
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.allocatedSlice()) |tile| {
|
for (map.tiles.items) |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) ) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ const std = @import("std");
|
|||||||
|
|
||||||
pub const Object = struct {
|
pub const Object = struct {
|
||||||
box: rl.Rectangle,
|
box: rl.Rectangle,
|
||||||
texture: ?rl.Texture2D,
|
texture: ?*rl.Texture2D,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Player = struct {
|
pub const Player = struct {
|
||||||
|
|||||||
17
src/utils.zig
Normal file
17
src/utils.zig
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
const rl = @import("raylib");
|
||||||
|
|
||||||
|
pub fn GetMousePositionWS(camera: rl.Camera2D, screenWidth: f32, screenHeight: f32) rl.Vector2 {
|
||||||
|
var mouse_position = rl.GetMousePosition();
|
||||||
|
|
||||||
|
// wold space position vectors for left-top and right-botton respectivly
|
||||||
|
var ws_position_vector_lt: rl.Vector2 = .{ .x = camera.target.x - (screenWidth / 2) * (1 / camera.zoom), .y = camera.target.y - (screenHeight / 2) * (1 / camera.zoom)};
|
||||||
|
var ws_position_vector_rb: rl.Vector2 = .{ .x = camera.target.x + (screenWidth / 2) * (1 / camera.zoom), .y = camera.target.y + (screenHeight / 2) * (1 / camera.zoom)};
|
||||||
|
|
||||||
|
// range vector of distances of screen height and width translated to worldspace coordinates
|
||||||
|
var ws_position_vector_range: rl.Vector2 = .{ .x = ws_position_vector_rb.x - ws_position_vector_lt.x, .y = ws_position_vector_rb.y - ws_position_vector_lt.y};
|
||||||
|
|
||||||
|
// mouse position vector but it's a range between 0 and 1
|
||||||
|
var scaled_mouse_position_vector: rl.Vector2 = .{ .x = mouse_position.x / screenWidth, .y = mouse_position.y / screenHeight};
|
||||||
|
|
||||||
|
return .{ .x = ws_position_vector_lt.x + scaled_mouse_position_vector.x * ws_position_vector_range.x, .y = -(ws_position_vector_lt.y + scaled_mouse_position_vector.y * ws_position_vector_range.y)};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user