From 819642bc907720f6ec8578ee81563e7c2978ed4c Mon Sep 17 00:00:00 2001 From: djonker Date: Mon, 6 Nov 2023 00:57:40 +0100 Subject: [PATCH] animations run on a timer --- cub3d.h | 6 +++++- maps/bigmap.cub | 2 +- src/draw/stats.c | 6 +++--- src/input/rest.c | 28 ++-------------------------- src/main/init.c | 12 ++++++------ src/main/main.c | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 6 files changed, 62 insertions(+), 40 deletions(-) diff --git a/cub3d.h b/cub3d.h index 709215d..34a0837 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */ -/* Updated: 2023/11/05 23:27:12 by houtworm ######## odam.nl */ +/* Updated: 2023/11/06 00:37:18 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -52,6 +52,7 @@ typedef struct s_varlist mlx_image_t *img; mlx_image_t *fstat; mlx_image_t *tstat; + mlx_image_t *cstat; mlx_image_t *kstat; mlx_image_t *wimg; mlx_texture_t *temptext; @@ -94,6 +95,9 @@ typedef struct s_varlist int *distance; int treasure; int tottreasure; + int tottime; + double anitime; + double firetime; int enemies; int kills; int mgun; diff --git a/maps/bigmap.cub b/maps/bigmap.cub index 3bf1d0e..0583062 100644 --- a/maps/bigmap.cub +++ b/maps/bigmap.cub @@ -12,7 +12,7 @@ WE ./assets/wall/wood.png 1 1V A A 1 1 ] 1 1 1 1 11111111111 11111111111 111111111111111111111 1 1111111111 1111111111 1 1B f $ f B1 1 1 1 17 1 D KC K 1 1 -1 1 Kl T lK 1 1s K l K D 1 9&9 1 K l K D l 1 91 K K K K K K K K 1 +1 14Kl T lK 1 1s K l K D 1 9&9 1 K l K D l 1 91 K K K K K K K K 1 1 13 8 8 21 1 1 1 C 17 1 1111111111 K K K K K K 1 1 11111 11111 11111111111 D K T K 11111111111 1 1 K K K K 1 1 A A 1 1 1 K K 1 1 D Kl K 1 K K 1 diff --git a/src/draw/stats.c b/src/draw/stats.c index 2046e7a..9a03c10 100644 --- a/src/draw/stats.c +++ b/src/draw/stats.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/05 07:56:06 by houtworm ######## odam.nl */ +/* Updated: 2023/11/06 00:21:45 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ void ft_printstats(t_varlist *vl) total = ft_itoa(vl->enemies); current = ft_itoa(vl->kills); temp = ft_vastrjoin(4, "Kills: ", current, "/", total); - vl->tstat = mlx_put_string(vl->mlx, temp, 10, 30); + vl->cstat = mlx_put_string(vl->mlx, temp, 10, 30); ft_vafree(3, temp, total, current); total = ft_itoa(vl->tottreasure); current = ft_itoa(vl->treasure); @@ -33,6 +33,6 @@ void ft_printstats(t_varlist *vl) vl->kstat = mlx_put_string(vl->mlx, temp, 10, 50); ft_vafree(3, temp, total, current); mlx_set_instance_depth(vl->fstat->instances, 2); - mlx_set_instance_depth(vl->tstat->instances, 3); + mlx_set_instance_depth(vl->cstat->instances, 3); mlx_set_instance_depth(vl->kstat->instances, 4); } diff --git a/src/input/rest.c b/src/input/rest.c index a8626bb..d8b9e99 100644 --- a/src/input/rest.c +++ b/src/input/rest.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/11/05 23:53:12 by houtworm ######## odam.nl */ +/* Updated: 2023/11/06 00:52:07 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -30,34 +30,10 @@ void ft_processacro(t_varlist *vl, double movespeed) void ft_processguns(t_varlist *vl) { - if (vl->reload == 4) - { - - vl->reload = 0; - ft_putendl("recovered"); - } - if (vl->reload == 3) - { - - vl->reload = 4; - ft_putendl("recover"); - } - if (vl->reload == 2) - { - - vl->reload = 3; - ft_putendl("shoot"); - } - if (vl->reload == 1) - { - - vl->reload = 2; - ft_putendl("aim"); - } if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT) && vl->reload == 0) { + vl->anitime = vl->frametime; vl->reload = 1; - ft_putendl("ready"); } if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_RIGHT)) ft_putendl("zoom"); diff --git a/src/main/init.c b/src/main/init.c index 0d849f8..a9a9bbe 100644 --- a/src/main/init.c +++ b/src/main/init.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */ -/* Updated: 2023/11/05 22:29:03 by houtworm ######## odam.nl */ +/* Updated: 2023/11/06 00:53:16 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -73,11 +73,11 @@ void ft_initweapons(t_varlist *vl) vl->gun2text[3] = mlx_load_png("./assets/weapon/mgun/4.png"); vl->gun2text[4] = mlx_load_png("./assets/weapon/mgun/5.png"); vl->gun3text = ft_calloc(4096, 8); - vl->gun3text[0] = mlx_load_png("./assets/weapon/mgun/1.png"); - vl->gun3text[1] = mlx_load_png("./assets/weapon/mgun/2.png"); - vl->gun3text[2] = mlx_load_png("./assets/weapon/mgun/3.png"); - vl->gun3text[3] = mlx_load_png("./assets/weapon/mgun/4.png"); - vl->gun3text[4] = mlx_load_png("./assets/weapon/mgun/5.png"); + vl->gun3text[0] = mlx_load_png("./assets/weapon/ggun/1.png"); + vl->gun3text[1] = mlx_load_png("./assets/weapon/ggun/2.png"); + vl->gun3text[2] = mlx_load_png("./assets/weapon/ggun/3.png"); + vl->gun3text[3] = mlx_load_png("./assets/weapon/ggun/4.png"); + vl->gun3text[4] = mlx_load_png("./assets/weapon/ggun/5.png"); } void ft_initmainstuff(t_varlist *vl) diff --git a/src/main/main.c b/src/main/main.c index c3fcaba..6a3d383 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/05 23:41:31 by houtworm ######## odam.nl */ +/* Updated: 2023/11/06 00:49:31 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ void ft_replaceimage(t_varlist *vl) { mlx_delete_image(vl->mlx, vl->img); mlx_delete_image(vl->mlx, vl->fstat); + mlx_delete_image(vl->mlx, vl->cstat); mlx_delete_image(vl->mlx, vl->kstat); mlx_delete_image(vl->mlx, vl->tstat); mlx_delete_image(vl->mlx, vl->wimg); @@ -44,6 +45,47 @@ void ft_printmap(t_varlist *vl) printf("right: x: %c, y: %c\n", vl->map[(int)(vl->posx + vl->dirx * movespeed)][(int)vl->posy], vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * movespeed)]); } +void ft_timers(t_varlist *vl) +{ + char *time; + char *temp; + + vl->frametime = vl->mlx->delta_time; + vl->anitime = vl->anitime + vl->frametime; + vl->tottime = (int)mlx_get_time(); + time = ft_itoa(vl->tottime); + temp = ft_vastrjoin(3, "Time: ", time, " Seconds"); + vl->tstat = mlx_put_string(vl->mlx, temp, 10, 70); + mlx_set_instance_depth(vl->tstat->instances, 5); + ft_vafree(2, time, temp); +} + +void ft_fireweapon(t_varlist *vl) +{ + double reloadtime; + + if (vl->weapon == 0) + reloadtime = 0.05; + if (vl->weapon == 1) + reloadtime = 0.1; + if (vl->weapon == 2) + reloadtime = 0.05; + if (vl->weapon == 3) + reloadtime = 0.03; + if (vl->reload) + { + if (vl->anitime - vl->firetime > reloadtime) + { + vl->anitime = 0; + vl->firetime = vl->anitime; + vl->reload++; + if (vl->reload > 4) + vl->reload = 0; + } + } + +} + void mainloop(void *param) { t_varlist *vl; @@ -55,10 +97,10 @@ void mainloop(void *param) /*ft_enemyaction(vl);*/ ft_drawsprites(vl); ft_drawweapon(vl); + ft_fireweapon(vl); /*ft_drawminimap(vl);*/ - vl->frametime = vl->mlx->delta_time; + ft_timers(vl); ft_printstats(vl); - vl->frametime = vl->mlx->delta_time; ft_processinput(vl); /*ft_printmap(vl);*/ if (!vl->img || (mlx_image_to_window(vl->mlx, vl->img, 0, 0) < 0))