-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_hook.c
74 lines (66 loc) · 2.68 KB
/
game_hook.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game_hook.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fatkeski <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 18:37:48 by fatkeski #+# #+# */
/* Updated: 2025/02/21 21:35:21 by fatkeski ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/cub3d.h"
void set_player_movement(t_game *game)
{
if (game->keys.w)
move_on_direction_plane(game, 1);
if (game->keys.s)
move_on_direction_plane(game, -1);
if (game->keys.d)
move_on_camera_plane(game, 1);
if (game->keys.a)
move_on_camera_plane(game, -1);
if (game->keys.right)
rotate(game, 1);
if (game->keys.left)
rotate(game, -1);
}
void move_on_direction_plane(t_game *game, int multiplier)
{
double des_x;
double des_y;
des_x = game->player.x + (game->player.dir_x * (double)1 / 50) * multiplier;
des_y = game->player.y + (game->player.dir_y * (double)1 / 50) * multiplier;
if (game->map.map_2d[(int)game->player.y][(int)des_x] != '1')
game->player.x = des_x;
if (game->map.map_2d[(int)des_y][(int)game->player.x] != '1')
game->player.y = des_y;
}
void move_on_camera_plane(t_game *game, int multiplier)
{
double des_x;
double des_y;
des_x = game->player.x + game->player.plane_x * (double)1 / 50 * multiplier;
des_y = game->player.y + game->player.plane_y * (double)1 / 50 * multiplier;
if (game->map.map_2d[(int)game->player.y][(int)des_x] != '1')
game->player.x = des_x;
if (game->map.map_2d[(int)des_y][(int)game->player.x] != '1')
game->player.y = des_y;
}
void rotate(t_game *game, int multiplier)
{
double old_dirx;
double old_planex;
double rotate_angle;
rotate_angle = 0.01 * multiplier;
old_dirx = game->player.dir_x;
game->player.dir_x = game->player.dir_x * cos(rotate_angle)
- game->player.dir_y * sin(rotate_angle);
game->player.dir_y = old_dirx * sin(rotate_angle) + game->player.dir_y
* cos(rotate_angle);
old_planex = game->player.plane_x;
game->player.plane_x = game->player.plane_x * cos(rotate_angle)
- game->player.plane_y * sin(rotate_angle);
game->player.plane_y = old_planex * sin(rotate_angle) + game->player.plane_y
* cos(rotate_angle);
}