From c20a05626315926faded64db607f509708714c02 Mon Sep 17 00:00:00 2001 From: djonker Date: Sat, 4 Nov 2023 04:40:26 +0100 Subject: [PATCH] treasure can be picked up --- cub3d.h | 11 ++++++++-- maps/treasuretest.cub | 15 +++++++++++++ src/init.c | 8 ++++++- src/keys.c | 2 +- src/main.c | 51 ++++++++++++++++++++++++++++++++++++++++++- src/map.c | 8 +++++-- 6 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 maps/treasuretest.cub diff --git a/cub3d.h b/cub3d.h index a63ba10..a4ae012 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/04 03:13:42 by houtworm ######## odam.nl */ +/* Updated: 2023/11/04 04:36:12 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,6 +34,8 @@ typedef struct s_varlist mlx_t *mlx; mlx_image_t *img; mlx_image_t *fps; + mlx_image_t *tstat; + mlx_image_t *kstat; mlx_texture_t *curtext; mlx_texture_t *northtext; mlx_texture_t *easttext; @@ -46,7 +48,6 @@ typedef struct s_varlist mlx_texture_t *endtext; mlx_texture_t *nazitext; t_sprite *sprite; - t_sprite *spriteorder; int spritecount; int fpsrefresh; int w; @@ -85,6 +86,12 @@ typedef struct s_varlist int32_t ccolor; int resize; int *zbuffer; + int treasure; + int tottreasure; + int enemies; + int kills; + int mgun; + int ggun; } t_varlist; void ft_frametime(t_varlist *vl); diff --git a/maps/treasuretest.cub b/maps/treasuretest.cub new file mode 100644 index 0000000..10c0a49 --- /dev/null +++ b/maps/treasuretest.cub @@ -0,0 +1,15 @@ +C 0,100,255 +F 100,100,100 + +NO ./assets/bluestone.png +EA ./assets/colorstone.png +SO ./assets/redbrick.png +WE ./assets/wood.png + +1111111 +1CCCCC1 +1 1 +1 1 +1 N 1 +1 1 +1111111 diff --git a/src/init.c b/src/init.c index 8eb32fa..58da96d 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */ -/* Updated: 2023/11/04 03:31:53 by houtworm ######## odam.nl */ +/* Updated: 2023/11/04 04:08:23 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,6 +22,12 @@ t_varlist initvarlist(void) vl.zbuffer = ft_calloc(4096, 8); vl.spritecount = 0; vl.hoffset = 0; + vl.treasure = 0; + vl.tottreasure = 0; + vl.enemies = 0; + vl.kills = 0; + vl.mgun = 0; + vl.ggun = 0; vl.ccolor = 0; vl.fcolor = 0; vl.northtext = NULL; diff --git a/src/keys.c b/src/keys.c index f07a6ac..187ac48 100644 --- a/src/keys.c +++ b/src/keys.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/11/02 04:25:38 by houtworm ######## odam.nl */ +/* Updated: 2023/11/04 03:51:16 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/main.c b/src/main.c index 18e156b..75813e6 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,34 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/04 02:31:20 by houtworm ######## odam.nl */ +/* Updated: 2023/11/04 04:34:54 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../cub3d.h" +void ft_printstats(t_varlist *vl) +{ + char *enemies; + char *kills; + char *treasure; + char *found; + char *temp; + + enemies = ft_itoa(vl->enemies); + kills = ft_itoa(vl->kills); + temp = ft_vastrjoin(4, "Kills: ", kills, "/", enemies); + vl->tstat = mlx_put_string(vl->mlx, temp, 10, 30); + free(temp); + treasure = ft_itoa(vl->tottreasure); + found = ft_itoa(vl->treasure); + temp = ft_vastrjoin(4, "Treasure: ", found, "/", treasure); + vl->kstat = mlx_put_string(vl->mlx, temp, 10, 50); + mlx_set_instance_depth(vl->tstat->instances, 3); + mlx_set_instance_depth(vl->kstat->instances, 4); + ft_vafree(5, enemies, kills, treasure, found, temp); +} + void ft_frametime(t_varlist *vl) { char *itoa; @@ -27,6 +49,29 @@ void ft_frametime(t_varlist *vl) ft_vafree(2, itoa, print); } +void ft_pickup(t_varlist *vl) +{ + int i; + + i = 0; + while (vl->sprite[i].x) + { + if ((int)vl->sprite[i].x == (int)vl->posx) + if ((int)vl->sprite[i].y == (int)vl->posy) + if (vl->sprite[i].type == 4) + { + while (vl->sprite[i].x) + { + vl->sprite[i] = vl->sprite[i + 1]; + i++; + } + vl->spritecount--; + vl->treasure++; + } + i++; + } +} + void mainloop(void *param) { t_varlist *vl; @@ -34,10 +79,14 @@ void mainloop(void *param) vl = param; mlx_delete_image(vl->mlx, vl->img); mlx_delete_image(vl->mlx, vl->fps); + mlx_delete_image(vl->mlx, vl->tstat); + mlx_delete_image(vl->mlx, vl->kstat); vl->img = mlx_new_image(vl->mlx, vl->w, vl->h); ft_raycast(vl); + ft_pickup(vl); ft_drawsprites(vl); ft_frametime(vl); + ft_printstats(vl); ft_movementkeys(vl); if (!vl->img || (mlx_image_to_window(vl->mlx, vl->img, 0, 0) < 0)) ft_errorexit("image to window failed ", "mainloop", 1); diff --git a/src/map.c b/src/map.c index a5a0d4f..39820a1 100644 --- a/src/map.c +++ b/src/map.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */ -/* Updated: 2023/11/04 02:39:42 by houtworm ######## odam.nl */ +/* Updated: 2023/11/04 04:09:24 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,6 +53,8 @@ char ft_addwalktroughsprite(t_varlist *vl, int x, int y, int type) vl->sprite[vl->spritecount].x = x + 0.5; vl->sprite[vl->spritecount].y = y + 0.5; vl->sprite[vl->spritecount].type = type; + if (type == 4) + vl->tottreasure++; vl->spritecount++; return ('0'); } @@ -62,6 +64,8 @@ char ft_addsolidsprite(t_varlist *vl, int x, int y, int type) vl->sprite[vl->spritecount].x = x + 0.5; vl->sprite[vl->spritecount].y = y + 0.5; vl->sprite[vl->spritecount].type = type; + if (type == 6) + vl->enemies++; vl->spritecount++; return ('2'); } @@ -114,7 +118,7 @@ char **ft_getmap(t_varlist *vl, int fd) else if (line[x] == 'H') map[y][x] = ft_addwalktroughsprite(vl, y, x, 3); else if (line[x] == 'C') - map[y][x] = ft_addsolidsprite(vl, y, x, 4); + map[y][x] = ft_addwalktroughsprite(vl, y, x, 4); else if (line[x] == 'X') map[y][x] = ft_addsolidsprite(vl, y, x, 5); else if (line[x] == 'K')