intra to gitea
This commit is contained in:
+51
-54
@@ -5,76 +5,73 @@
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jle-neze <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/12 18:24:21 by jle-neze #+# #+# */
|
||||
/* Updated: 2025/09/12 18:24:22 by jle-neze ### ########.fr */
|
||||
/* Created: 2025/10/28 16:20:00 by jle-neze #+# #+# */
|
||||
/* Updated: 2025/10/28 16:20:00 by jle-neze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub.h"
|
||||
#include <math.h>
|
||||
|
||||
void player_init(t_game *g, double px, double py, double dx, double dy)
|
||||
void player_init(t_game *g, t_player_init p)
|
||||
{
|
||||
g->cam.pos_x = px;
|
||||
g->cam.pos_y = py;
|
||||
g->cam.dir_x = dx;
|
||||
g->cam.dir_y = dy;
|
||||
/* FOV ≈ 66° → plane perpendiculaire (0.66) */
|
||||
g->cam.plane_x = -dy * 0.66;
|
||||
g->cam.plane_y = dx * 0.66;
|
||||
g->move_speed = 0.07; /* ajuste si besoin */
|
||||
g->rot_speed = 0.05; /* rad/frame */
|
||||
g->cam.pos_x = p.px;
|
||||
g->cam.pos_y = p.py;
|
||||
g->cam.dir_x = p.dx;
|
||||
g->cam.dir_y = p.dy;
|
||||
g->cam.plane_x = -p.dy * 0.66;
|
||||
g->cam.plane_y = p.dx * 0.66;
|
||||
g->move_speed = 0.03;
|
||||
g->rot_speed = 0.05;
|
||||
}
|
||||
|
||||
/* petits helpers collision */
|
||||
static int is_wall(t_world *w, int mx, int my)
|
||||
static int is_wall(t_world *w, int mx, int my)
|
||||
{
|
||||
if (mx < 0 || my < 0 || mx >= w->w || my >= w->h) return (1);
|
||||
if (mx < 0 || my < 0 || mx >= w->w || my >= w->h)
|
||||
return (1);
|
||||
return (w->grid[my][mx] == '1');
|
||||
}
|
||||
|
||||
static void player_move(t_game *g, double dx, double dy)
|
||||
{
|
||||
double nx;
|
||||
double ny;
|
||||
|
||||
nx = g->cam.pos_x + dx * g->move_speed;
|
||||
ny = g->cam.pos_y + dy * g->move_speed;
|
||||
if (!is_wall(&g->world, (int)nx, (int)g->cam.pos_y))
|
||||
g->cam.pos_x = nx;
|
||||
if (!is_wall(&g->world, (int)g->cam.pos_x, (int)ny))
|
||||
g->cam.pos_y = ny;
|
||||
}
|
||||
|
||||
static void player_rotate(t_game *g, int dir)
|
||||
{
|
||||
double rs;
|
||||
double old_dir_x;
|
||||
double old_plane_x;
|
||||
|
||||
rs = g->rot_speed * (dir);
|
||||
old_dir_x = g->cam.dir_x;
|
||||
g->cam.dir_x = g->cam.dir_x * cos(rs) - g->cam.dir_y * sin(rs);
|
||||
g->cam.dir_y = old_dir_x * sin(rs) + g->cam.dir_y * cos(rs);
|
||||
old_plane_x = g->cam.plane_x;
|
||||
g->cam.plane_x = g->cam.plane_x * cos(rs) - g->cam.plane_y * sin(rs);
|
||||
g->cam.plane_y = old_plane_x * sin(rs) + g->cam.plane_y * cos(rs);
|
||||
}
|
||||
|
||||
void player_update(t_game *g)
|
||||
{
|
||||
double nx, ny;
|
||||
|
||||
/* Avancer/retour W/S */
|
||||
if (g->in.w)
|
||||
{
|
||||
nx = g->cam.pos_x + g->cam.dir_x * g->move_speed;
|
||||
ny = g->cam.pos_y + g->cam.dir_y * g->move_speed;
|
||||
if (!is_wall(&g->world, (int)nx, (int)g->cam.pos_y)) g->cam.pos_x = nx;
|
||||
if (!is_wall(&g->world, (int)g->cam.pos_x, (int)ny)) g->cam.pos_y = ny;
|
||||
}
|
||||
player_move(g, g->cam.dir_x, g->cam.dir_y);
|
||||
if (g->in.s)
|
||||
{
|
||||
nx = g->cam.pos_x - g->cam.dir_x * g->move_speed;
|
||||
ny = g->cam.pos_y - g->cam.dir_y * g->move_speed;
|
||||
if (!is_wall(&g->world, (int)nx, (int)g->cam.pos_y)) g->cam.pos_x = nx;
|
||||
if (!is_wall(&g->world, (int)g->cam.pos_x, (int)ny)) g->cam.pos_y = ny;
|
||||
}
|
||||
/* Strafe A/D (gauche/droite) */
|
||||
player_move(g, -g->cam.dir_x, -g->cam.dir_y);
|
||||
if (g->in.a)
|
||||
{
|
||||
nx = g->cam.pos_x - g->cam.dir_y * g->move_speed;
|
||||
ny = g->cam.pos_y + g->cam.dir_x * g->move_speed;
|
||||
if (!is_wall(&g->world, (int)nx, (int)g->cam.pos_y)) g->cam.pos_x = nx;
|
||||
if (!is_wall(&g->world, (int)g->cam.pos_x, (int)ny)) g->cam.pos_y = ny;
|
||||
}
|
||||
player_move(g, -g->cam.dir_y, g->cam.dir_x);
|
||||
if (g->in.d)
|
||||
{
|
||||
nx = g->cam.pos_x + g->cam.dir_y * g->move_speed;
|
||||
ny = g->cam.pos_y - g->cam.dir_x * g->move_speed;
|
||||
if (!is_wall(&g->world, (int)nx, (int)g->cam.pos_y)) g->cam.pos_x = nx;
|
||||
if (!is_wall(&g->world, (int)g->cam.pos_x, (int)ny)) g->cam.pos_y = ny;
|
||||
}
|
||||
/* Rotation gauche/droite */
|
||||
if (g->in.left || g->in.right)
|
||||
{
|
||||
double rs = g->rot_speed * (g->in.right ? 1.0 : -1.0);
|
||||
double old_dir_x = g->cam.dir_x;
|
||||
g->cam.dir_x = g->cam.dir_x * cos(rs) - g->cam.dir_y * sin(rs);
|
||||
g->cam.dir_y = old_dir_x * sin(rs) + g->cam.dir_y * cos(rs);
|
||||
double old_plane_x = g->cam.plane_x;
|
||||
g->cam.plane_x = g->cam.plane_x * cos(rs) - g->cam.plane_y * sin(rs);
|
||||
g->cam.plane_y = old_plane_x * sin(rs) + g->cam.plane_y * cos(rs);
|
||||
}
|
||||
player_move(g, g->cam.dir_y, -g->cam.dir_x);
|
||||
if (g->in.left)
|
||||
player_rotate(g, -1);
|
||||
else if (g->in.right)
|
||||
player_rotate(g, 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user