diff --git a/Makefile b/Makefile index 5884e04..09f9c06 100644 --- a/Makefile +++ b/Makefile @@ -6,35 +6,47 @@ # By: houtworm +#+ # # +#+ # # Created: 2023/10/26 10:46:29 by houtworm #+# #+# # -# Updated: 2023/11/05 22:29:47 by houtworm ######## odam.nl # +# Updated: 2023/11/10 10:44:46 by houtworm ######## odam.nl # # # # **************************************************************************** # NAME =cub3d CC =gcc -FC =-Wall -Werror -Wextra -Wunreachable-code -flto -Ofast -march=native -g #-fsanitize=address +FC =-Wall -Werror -Wextra -Wunreachable-code -flto -Ofast -march=native -g# -fsanitize=address HEAD =-I ./include -I $(MLX)/include RM =rm -rf LIB =libft/libft.a getnextline/get_next_line.a mlx/build/libmlx42.a -ldl -lglfw -pthread -lm SRC =src/main/main.c\ - src/main/pickup.c\ src/main/init.c\ + src/main/reset.c\ + src/main/raycast.c\ src/main/error.c\ src/main/cleanup.c\ - src/parse/parse.c\ + src/action/fire.c\ + src/action/enemy.c\ + src/action/pickup.c\ + src/action/interact.c\ + src/parse/file.c\ + src/parse/line.c\ src/parse/map.c\ + src/parse/elements.c\ + src/parse/player.c\ src/parse/sprite.c\ - src/draw/raycast.c\ + src/parse/flood.c\ src/draw/world.c\ src/draw/texture.c\ + src/draw/sprite1.c\ + src/draw/sprite2.c\ + src/draw/minimap.c\ src/draw/weapon.c\ src/draw/stats.c\ + src/draw/flash.c\ + src/draw/menu.c\ src/input/game.c\ src/input/move.c\ src/input/turn.c\ - src/input/rest.c\ - src/sprite/check.c\ - src/sprite/draw.c + src/input/acro.c\ + src/input/guns.c OBJ =$(SRC:src/%.c=obj/%.o) all: libft getnextline mlx/build/mlx42.a $(NAME) diff --git a/Readme.md b/Readme.md index 6214fc1..6c5b1d4 100644 --- a/Readme.md +++ b/Readme.md @@ -3,25 +3,8 @@ Cub3D is a simple raycasting game using the mlx library --- ## Todo -### Parsing -- Flood fill the map starting from the player to check if it is a closed map -- Check if all needed elements are present in the closed part of the map ### Bonus - Doors -- Minimap -### Extra -- Help screen -- End level screen -- Zoom with right mouse button -- Shooting Enemies -- Moving Enemies -- HP system -- Score system -- Levels -- Start Menu // breaks mandatory subject -- Skybox // Breaks mandatory subject -- Sounds // need forbidden functions -- Music // need forbidden functions --- ## Features @@ -38,33 +21,43 @@ Cub3D is a simple raycasting game using the mlx library - Wall Collision - Rotate with the mouse - Animated Sprites +- Minimap ### Extra -- FPS counter -- Timer +- Map sizes up to 1 Megabyte (1000x1000) +- Fullscreen - Player can walk in 8 directions - Player can Run - Player can Jump - Player can Crouch -- Map sizes up to 1 Megabyte (1000x1000) -- Player can collect Treasure - Vertical Aiming -- Fullscreen -- Barrels -- Lights +- FPS counter +- Timer +- Decoration - Weapon pickups - Weapon Animations -- Ammo system +- Stabs and bullets kill Enemies +- Ammo Pickups +- Ammo System +- Shooting Enemies +- Animated Enemies +- Destructable Sprites +- Heath Pickups +- HP system +- Treasure Pickups +- Score system +- Level end elevator +- End level screen +- You died screen --- ## Bugs -- Player can get too close to walls -- Player walks twice as fast when holding W and A or W and D +- none? --- ## Usage 1. Simply clone or download the repository 2. Run `make` in the cloned directory -3. Start the game with `./cub3d` +3. Start the game with `./cub3d maps/level.cub` --- [This project is part of the studies at 42](https://42.fr/en/homepage/) diff --git a/assets/decoration/rubble.png b/assets/decoration/rubble.png new file mode 100644 index 0000000..e16506a Binary files /dev/null and b/assets/decoration/rubble.png differ diff --git a/assets/nazi/guard/fire1.png b/assets/nazi/guard/fire1.png index f8015c9..6f6d7f6 100644 Binary files a/assets/nazi/guard/fire1.png and b/assets/nazi/guard/fire1.png differ diff --git a/assets/nazi/guard/fire2.png b/assets/nazi/guard/fire2.png index 8300ac6..f8015c9 100644 Binary files a/assets/nazi/guard/fire2.png and b/assets/nazi/guard/fire2.png differ diff --git a/assets/nazi/guard/fire3.png b/assets/nazi/guard/fire3.png index 46479db..8300ac6 100644 Binary files a/assets/nazi/guard/fire3.png and b/assets/nazi/guard/fire3.png differ diff --git a/assets/nazi/guard/fire4.png b/assets/nazi/guard/fire4.png new file mode 100644 index 0000000..46479db Binary files /dev/null and b/assets/nazi/guard/fire4.png differ diff --git a/assets/wall/door.png b/assets/wall/door.png new file mode 100644 index 0000000..e5b87d8 Binary files /dev/null and b/assets/wall/door.png differ diff --git a/assets/wall/doorside.png b/assets/wall/doorside.png new file mode 100644 index 0000000..8a45e12 Binary files /dev/null and b/assets/wall/doorside.png differ diff --git a/assets/wall/elevator.png b/assets/wall/elevator.png new file mode 100644 index 0000000..9a68867 Binary files /dev/null and b/assets/wall/elevator.png differ diff --git a/assets/wall/elevatordoor.png b/assets/wall/elevatordoor.png new file mode 100644 index 0000000..c86c2a6 Binary files /dev/null and b/assets/wall/elevatordoor.png differ diff --git a/assets/wall/elevatorside.png b/assets/wall/elevatorside.png new file mode 100644 index 0000000..940da73 Binary files /dev/null and b/assets/wall/elevatorside.png differ diff --git a/assets/wall/lockeddoor.png b/assets/wall/lockeddoor.png new file mode 100644 index 0000000..59b5f75 Binary files /dev/null and b/assets/wall/lockeddoor.png differ diff --git a/cub3d.h b/cub3d.h index 4f3b386..e1ad748 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/06 01:16:25 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 10:40:12 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,32 +21,34 @@ # include "mlx/include/MLX42/MLX42.h" # include -typedef struct s_sprite +typedef struct s_sprite { double distance; double x; double y; + double anitime; int type; int number; + int status; } t_sprite; -typedef struct s_draw +typedef struct s_draw { - double transformx; - double transformy; - int spritescreen; - int vmovescreen; - int spriteheight; - int spritewidth; + double tfx; + double tfy; + int sprs; + int vmov; + int sprh; + int sprw; int drawstarty; int drawstartx; int drawendy; int drawendx; - int texx; - int texy; + int x; + int y; } t_draw; -typedef struct s_varlist +typedef struct s_varlist { mlx_t *mlx; mlx_image_t *img; @@ -56,21 +58,29 @@ typedef struct s_varlist mlx_image_t *kstat; mlx_image_t *astat; mlx_image_t *hstat; + mlx_image_t **mstat; mlx_image_t *wimg; - mlx_texture_t *temptext; - mlx_texture_t **walltext; - mlx_texture_t **decotext; - mlx_texture_t **picktext; - mlx_texture_t **nazitext; - mlx_texture_t **gun0text; - mlx_texture_t **gun1text; - mlx_texture_t **gun2text; - mlx_texture_t **gun3text; + mlx_image_t *mimg; + mlx_image_t *oimg; + mlx_image_t *fimg; + mlx_texture_t *temptxt; + mlx_texture_t **walltxt; + mlx_texture_t **elevtxt; + mlx_texture_t **decotxt; + mlx_texture_t **picktxt; + mlx_texture_t **gfiretxt; + mlx_texture_t **gdeadtxt; + mlx_texture_t **gun0txt; + mlx_texture_t **gun1txt; + mlx_texture_t **gun2txt; + mlx_texture_t **gun3txt; t_sprite *sprite; int spritecount; int w; int h; char **map; + int mapsizey; + int mapsizex; double frametime; double posx; double posy; @@ -78,6 +88,8 @@ typedef struct s_varlist double diry; double planex; double planey; + int mapx; + int mapy; int vaim; int jump; double run; @@ -87,8 +99,8 @@ typedef struct s_varlist double sidedisty; double deltadistx; double deltadisty; - double walldist; - int lineheight; + double wdist; + int lineh; int side; double oldmouseposx; double oldmouseposy; @@ -97,8 +109,7 @@ typedef struct s_varlist int *distance; int treasure; int tottreasure; - int tottime; - double anitime; + double tottime; double firetime; int enemies; int kills; @@ -107,7 +118,14 @@ typedef struct s_varlist int ammo; int weapon; int reload; + double reloadtime; int hp; + int minimap; + int stats; + int menu; + char *cubfile; + int flash; + int flashcolor; } t_varlist; // MAIN @@ -115,17 +133,28 @@ t_varlist initgame(void); int ft_errorexit(char *reason, char *function, int code); void ft_printstats(t_varlist *vl); void ft_cleanup(t_varlist *vl); +void ft_resetvars(t_varlist *vl); +void ft_restartgame(t_varlist *vl); +void ft_raycast(t_varlist *vl, int x); +int ft_prepcast(t_varlist *vl, int x); +int ft_getstepx(t_varlist *vl, int mapx); +int ft_getstepy(t_varlist *vl, int mapy); // PARSE t_varlist ft_parseconfigfile(t_varlist vl, char *filename); +void ft_checkline(t_varlist *vl, char *line); char **ft_getmap(t_varlist *vl, int fd); +char ft_checkmapelement(t_varlist *vl, char element, int x, int y); +char ft_initplayer(t_varlist *vl, char dir, int x, int y); char ft_addwalktroughdecor(t_varlist *vl, int x, int y, int number); char ft_addsoliddecor(t_varlist *vl, int x, int y, int number); char ft_addpickup(t_varlist *vl, int x, int y, int number); char ft_addenemy(t_varlist *vl, int x, int y, int number); +int ft_floodfill(t_varlist vl); +int ft_flood(t_varlist vl, char **fillmap, int x, int y); // INPUT void ft_processinput(t_varlist *vl); void ft_processturn(t_varlist *vl, double rotspeed); -void ft_processmove(t_varlist *vl, double movespeed); +void ft_processmove(t_varlist *vl, double mvs, double mvy, double mvx); void ft_processacro(t_varlist *vl, double movespeed); void ft_processguns(t_varlist *vl); void keyhook(mlx_key_data_t kd, void *param); @@ -133,15 +162,21 @@ void scrollhook(double xdelta, double ydelta, void *param); void resizehook(int x, int y, void *param); void cursorhook(double x, double y, void *param); // DRAW -void ft_raycast(t_varlist *vl, int x, int mapx, int mapy); void ft_drawmap(t_varlist *vl); void ft_drawweapon(t_varlist *vl); +void ft_drawsprite(t_varlist *vl, t_draw *draw, int x, int i); +void ft_drawsprites(t_varlist *vl); int ft_gettextx(t_varlist *vl); uint32_t ft_gettextcolor(mlx_texture_t *texture, int texty, int textx); -// SPRITE -void ft_drawsprite(t_varlist *vl, t_draw *draw, int x); -t_draw *ft_initdrawsprite(t_varlist *vl, int i); -void ft_getdrawstartend(t_varlist *vl, t_draw *draw); -void ft_drawsprites(t_varlist *vl); +void ft_flashscreen(t_varlist *vl, int x, int y, uint32_t color); +void ft_drawminimap(t_varlist *vl); +void ft_finish(t_varlist *vl); +void ft_youdied(t_varlist *vl); +// ACTION void ft_checkpickup(t_varlist *vl); +void ft_firebullet(t_varlist *vl); +void ft_enemyaction(t_varlist *vl); +void ft_fireweapon(t_varlist *vl); +void ft_interact(t_varlist *vl); +void ft_checkhealth(t_varlist *vl); #endif diff --git a/maps/bigmap.cub b/maps/level.cub similarity index 82% rename from maps/bigmap.cub rename to maps/level.cub index 0583062..daa61ec 100644 --- a/maps/bigmap.cub +++ b/maps/level.cub @@ -7,12 +7,12 @@ SO ./assets/wall/redbrick.png WE ./assets/wall/wood.png 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 -1L B1 p 1 1 1 1 1 -1 Kl l l l l l D KC K 1 &TC D D KCK 1 -1 1V A A 1 1 ] 1 1 1 +1L B1 p 1 1111 1 1 1 +1 Kl l l l l l D KC K 1X C D D KCK& 1 +1 1V A A 1 1111 1 1 6 1 1 11111111111 11111111111 111111111111111111111 1 1111111111 1111111111 -1 1B f $ f B1 1 1 1 17 1 D KC K 1 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 1B f $ f B1 1 2 2 1 1 222 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 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 @@ -23,10 +23,10 @@ WE ./assets/wall/wood.png 1B 1s K l K D 1 1 K l K D l 1 1 1 1BB9 1 1 11111111111 1 1111111111 1 111111111 11111111111 1 11111111111 1 1 1 -1B1 1 1 1 D K l K 1 1 D l$ 1 1 +1B1 1 1 1 D K l K 1 222 1 D l$ 1 1 1 K K K 1 1 K K 1 1 1 KCK D l 1 1 1 1 1 l 1 l 1 K T K D 11111111111 K T K 1 1111111111 1 -1 1 # 1P N 1 KCK 1 1 1 KCK 1 1 1 +1 122#221P N 1 KCK 1 1 1 KCK 1 1 1 1 1111111 1 * 1 D K l$K 1 * 1Bl l l b1 -1 b1[ 1BBB1 14 9 1BB B1 bb1 +1 b15 1BBB1 14 9 1BB B1 bb1 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 diff --git a/maps/maplegend b/maps/maplegend new file mode 100644 index 0000000..1c81d18 --- /dev/null +++ b/maps/maplegend @@ -0,0 +1,36 @@ +The map has to include the player, and he should not be able to walk off the map. + +N Player facing North +E Player facing East +S Player facing South +W Player facing West +Space Walkable space +0 Walkable space +1 Wall +2 Ammo (Ammo Pickup) +3 Machine Gun (Weapon Pickup) +4 Gatling Gun (Weapon Pickup) +5 Golden Key (Pickup without function) +6 Blue Key (Pickup without function) +7 Dog food (Health Pickup) +8 Meal (Health Pickup) +9 HP Kit (Health Pickup) +$ Cross (Treasure Pickup) +# Cup (Treasure Pickup) +* Chest (Treasure Pickup) +& Crown (Treasure Pickup) +D Door +X Elevator (Level end) +K Nazi Guard (Killable) +B Wooden Barrel +b Poison Barrel +A Knight Armor +P Pillar +p Plant +V Vase +f Faucet +T Table +s Skeleton +L Standing Lamp +l Ceiling Lamp +C Chandelier diff --git a/maps/postestne.cub b/maps/postestne.cub deleted file mode 100644 index a080ebb..0000000 --- a/maps/postestne.cub +++ /dev/null @@ -1,15 +0,0 @@ -C 0,100,255 -F 100,100,100 - -NO ./assets/wall/bluestone.png -EA ./assets/wall/colorstone.png -SO ./assets/wall/redbrick.png -WE ./assets/wall/wood.png - -1111111 -1 N1 -1 1 -1 1 -1 1 -1 1 -1111111 diff --git a/maps/postestnw.cub b/maps/postestnw.cub deleted file mode 100644 index 6833b4e..0000000 --- a/maps/postestnw.cub +++ /dev/null @@ -1,15 +0,0 @@ -C 0,100,255 -F 100,100,100 - -NO ./assets/wall/bluestone.png -EA ./assets/wall/colorstone.png -SO ./assets/wall/redbrick.png -WE ./assets/wall/wood.png - -1111111 -1N 1 -1 1 -1 1 -1 1 -1 1 -1111111 diff --git a/maps/postestse.cub b/maps/postestse.cub deleted file mode 100644 index 291dc6d..0000000 --- a/maps/postestse.cub +++ /dev/null @@ -1,15 +0,0 @@ -C 0,100,255 -F 100,100,100 - -NO ./assets/wall/bluestone.png -EA ./assets/wall/colorstone.png -SO ./assets/wall/redbrick.png -WE ./assets/wall/wood.png - -1111111 -1 1 -1 1 -1 1 -1 1 -1 N1 -1111111 diff --git a/maps/postestsw.cub b/maps/postestsw.cub deleted file mode 100644 index a94ad7b..0000000 --- a/maps/postestsw.cub +++ /dev/null @@ -1,15 +0,0 @@ -C 0,100,255 -F 100,100,100 - -NO ./assets/wall/bluestone.png -EA ./assets/wall/colorstone.png -SO ./assets/wall/redbrick.png -WE ./assets/wall/wood.png - -1111111 -1 1 -1 1 -1 1 -1 1 -1N 1 -1111111 diff --git a/maps/smallmap.cub b/maps/smallmap.cub deleted file mode 100644 index 5d983e4..0000000 --- a/maps/smallmap.cub +++ /dev/null @@ -1,32 +0,0 @@ -C 0,100,255 -F 100,100,100 - -NO ./assets/wall/bluestone.png -EA ./assets/wall/colorstone.png -SO ./assets/wall/redbrick.png -WE ./assets/wall/wood.png - -111111111111111111111111 -1 B1 -1 K 1 -1 1 -1 11111111111 1 -1 1B C B1 1 -1 1 K K 1 1 -1 1 1 1 -1 11111 11111 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1B 1 -1BB 1 -111111111 1 -1B1 1 1 -1 K K K 1 1 -1 1 C 1 1 -1 1 1 N 1 -1 1111111 1 -1 1 -111111111111111111111111 diff --git a/maps/spritetest.cub b/maps/spritetest.cub deleted file mode 100644 index 611d22e..0000000 --- a/maps/spritetest.cub +++ /dev/null @@ -1,15 +0,0 @@ -C 0,100,255 -F 100,100,100 - -NO ./assets/wall/bluestone.png -EA ./assets/wall/colorstone.png -SO ./assets/wall/redbrick.png -WE ./assets/wall/wood.png - -11111111111111 -1BbLAPpVfTslC1 -1$#*&987[]3421 -1 1 -1 N 1 -1 1 -11111111111111 diff --git a/maps/treasuretest.cub b/maps/treasuretest.cub deleted file mode 100644 index a385bb3..0000000 --- a/maps/treasuretest.cub +++ /dev/null @@ -1,15 +0,0 @@ -C 0,100,255 -F 100,100,100 - -NO ./assets/wall/bluestone.png -EA ./assets/wall/colorstone.png -SO ./assets/wall/redbrick.png -WE ./assets/wall/wood.png - -1111111 -1CCCCC1 -1 1 -1 1 -1 N 1 -1 1 -1111111 diff --git a/src/action/enemy.c b/src/action/enemy.c new file mode 100644 index 0000000..dfd1b0a --- /dev/null +++ b/src/action/enemy.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* enemy.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ +/* Updated: 2023/11/10 07:09:43 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_checkhealth(t_varlist *vl) +{ + if (vl->hp <= 0) + ft_youdied(vl); +} + +void ft_dieanimation(t_varlist *vl, int i) +{ + if (vl->tottime - vl->sprite[i].anitime > 0.2 && vl->sprite[i].number < 4) + { + vl->sprite[i].anitime = vl->tottime; + vl->sprite[i].number++; + } +} + +void ft_fireanimation(t_varlist *vl, int i) +{ + if (vl->tottime - vl->sprite[i].anitime > 0.8) + { + vl->sprite[i].anitime = vl->tottime; + vl->sprite[i].number++; + if (vl->sprite[i].number == 4) + vl->sprite[i].number = 2; + else if (vl->sprite[i].number == 3) + { + vl->hp = vl->hp - 10; + vl->flash = 6; + vl->flashcolor = 1; + } + } +} + +void ft_enemyaction(t_varlist *vl) +{ + int i; + + i = 0; + while (vl->spritecount > i) + { + if (vl->sprite[i].status && vl->sprite[i].type == 3) + { + if (vl->sprite[i].status == 1) + ft_fireanimation(vl, i); + if (vl->sprite[i].status == 2) + ft_dieanimation(vl, i); + } + i++; + } +} diff --git a/src/action/fire.c b/src/action/fire.c new file mode 100644 index 0000000..96dbc1f --- /dev/null +++ b/src/action/fire.c @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* fire.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ +/* Updated: 2023/11/10 06:01:44 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_killnazi(t_varlist *vl, int mapx, int mapy) +{ + int i; + + i = 0; + while (vl->spritecount > i) + { + if (mapy == (int)vl->sprite[i].y && mapx == (int)vl->sprite[i].x) + { + if (vl->map[mapx][mapy] == '3') + { + vl->sprite[i].status = 2; + vl->sprite[i].anitime = vl->tottime; + vl->kills++; + } + if (vl->map[mapx][mapy] == '2') + vl->sprite[i].status = 1; + vl->map[mapx][mapy] = '0'; + return ; + } + i++; + } +} + +int ft_checkifhit(t_varlist *vl, int mapx, int mapy) +{ + if (vl->map[mapx][mapy] == '3' || vl->map[mapx][mapy] == '2') + { + ft_killnazi(vl, mapx, mapy); + return (1); + } + if (vl->map[mapx][mapy] == '1' || vl->map[mapx][mapy] == '4') + return (1); + if (vl->weapon == 0) + return (1); + return (0); +} + +int ft_travelstep(t_varlist *vl, int map, int step, int xy) +{ + if (xy == 1) + { + vl->sidedistx += vl->deltadistx; + map += step; + vl->side = 0; + } + else + { + vl->sidedisty += vl->deltadisty; + map += step; + vl->side = 1; + } + return (map); +} + +void ft_firebullet(t_varlist *vl) +{ + int hit; + int stepx; + int stepy; + int mapx; + int mapy; + + mapx = (int)vl->posx; + mapy = (int)vl->posy; + hit = ft_prepcast(vl, vl->w / 2); + stepx = ft_getstepx(vl, mapx); + stepy = ft_getstepy(vl, mapy); + while (!hit) + { + if (vl->sidedistx < vl->sidedisty) + mapx = ft_travelstep(vl, mapx, stepx, 1); + else + mapy = ft_travelstep(vl, mapy, stepy, 2); + hit = ft_checkifhit(vl, mapx, mapy); + } +} + +void ft_fireweapon(t_varlist *vl) +{ + if (vl->reload) + { + if (vl->tottime - vl->firetime > vl->reloadtime) + { + vl->firetime = vl->tottime; + vl->reload++; + if (vl->reload == 2) + { + if (vl->weapon) + vl->ammo--; + ft_firebullet(vl); + } + if (vl->reload > 4) + vl->reload = 0; + } + } +} diff --git a/src/action/interact.c b/src/action/interact.c new file mode 100644 index 0000000..186930d --- /dev/null +++ b/src/action/interact.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* interact.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ +/* Updated: 2023/11/10 05:30:51 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_interact(t_varlist *vl) +{ + int stepx; + int stepy; + int mapx; + int mapy; + + mapx = (int)vl->posx; + mapy = (int)vl->posy; + ft_prepcast(vl, vl->w / 2); + stepx = ft_getstepx(vl, mapx); + stepy = ft_getstepy(vl, mapy); + if (vl->sidedistx < vl->sidedisty) + { + vl->sidedistx += vl->deltadistx; + mapx += stepx; + vl->side = 0; + } + else + { + vl->sidedisty += vl->deltadisty; + mapy += stepy; + vl->side = 1; + } + if (vl->map[mapx][mapy] == '4') + ft_finish(vl); +} diff --git a/src/main/pickup.c b/src/action/pickup.c similarity index 64% rename from src/main/pickup.c rename to src/action/pickup.c index 2215796..be89ce7 100644 --- a/src/main/pickup.c +++ b/src/action/pickup.c @@ -6,28 +6,65 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/05 11:50:21 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 06:52:19 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../../cub3d.h" -void ft_pickup(t_varlist *vl, int i) +void ft_pickuptreasure(t_varlist *vl, int i) { + vl->flash = 6; + vl->flashcolor = 4; if (vl->sprite[i].number < 4) vl->treasure++; - else if (vl->sprite[i].number < 7) - ft_putendl("Yummy\n"); else if (vl->sprite[i].number == 7) ft_putendl("Found gold key\n"); else if (vl->sprite[i].number == 8) ft_putendl("Found blue key\n"); - else if (vl->sprite[i].number == 9) +} + +void ft_pickuphealth(t_varlist *vl, int i) +{ + vl->flash = 6; + vl->flashcolor = 2; + if (vl->sprite[i].number == 4) + vl->hp = vl->hp + 30; + else if (vl->sprite[i].number == 5) + vl->hp = vl->hp + 20; + else if (vl->sprite[i].number == 6) + vl->hp = vl->hp + 10; +} + +void ft_pickupweapon(t_varlist *vl, int i) +{ + vl->flash = 6; + vl->flashcolor = 3; + vl->ammo += 10; + if (vl->sprite[i].number == 9) + { vl->mgun = 1; + vl->weapon = 2; + vl->reloadtime = 0.05; + } else if (vl->sprite[i].number == 10) + { vl->ggun = 1; - else if (vl->sprite[i].number == 11) - vl->ammo += 10; + vl->weapon = 3; + vl->reloadtime = 0.02; + } +} + +void ft_pickup(t_varlist *vl, int i) +{ + if (vl->sprite[i].number < 4) + ft_pickuptreasure(vl, i); + else if (vl->sprite[i].number < 7) + ft_pickuphealth(vl, i); + else if (vl->sprite[i].number < 9) + ft_pickuptreasure(vl, i); + else if (vl->sprite[i].number < 12) + ft_pickupweapon(vl, i); while (vl->sprite[i].x) { vl->sprite[i] = vl->sprite[i + 1]; diff --git a/src/draw/flash.c b/src/draw/flash.c new file mode 100644 index 0000000..7389147 --- /dev/null +++ b/src/draw/flash.c @@ -0,0 +1,118 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* flash.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ +/* Updated: 2023/11/09 22:26:43 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +uint32_t ft_flashred(t_varlist *vl) +{ + uint32_t color; + + if (vl->flash == 6) + color = 0xFF000066; + else if (vl->flash == 5) + color = 0xFF000055; + else if (vl->flash == 4) + color = 0xFF000044; + else if (vl->flash == 3) + color = 0xFF000033; + else if (vl->flash == 2) + color = 0xFF000022; + else + color = 0xFF000011; + return (color); +} + +uint32_t ft_flashgreen(t_varlist *vl) +{ + uint32_t color; + + if (vl->flash == 6) + color = 0x00FF0066; + else if (vl->flash == 5) + color = 0x00FF0055; + else if (vl->flash == 4) + color = 0x00FF0044; + else if (vl->flash == 3) + color = 0x00FF0033; + else if (vl->flash == 2) + color = 0x00FF0022; + else + color = 0x00FF0011; + return (color); +} + +uint32_t ft_flashblue(t_varlist *vl) +{ + uint32_t color; + + if (vl->flash == 6) + color = 0x0000FF66; + else if (vl->flash == 5) + color = 0x0000FF55; + else if (vl->flash == 4) + color = 0x0000FF44; + else if (vl->flash == 3) + color = 0x0000FF33; + else if (vl->flash == 2) + color = 0x0000FF22; + else + color = 0x0000FF11; + return (color); +} + +uint32_t ft_flashyellow(t_varlist *vl) +{ + uint32_t color; + + if (vl->flash == 6) + color = 0xFFFF0066; + else if (vl->flash == 5) + color = 0xFFFF0055; + else if (vl->flash == 4) + color = 0xFFFF0044; + else if (vl->flash == 3) + color = 0xFFFF0033; + else if (vl->flash == 2) + color = 0xFFFF0022; + else + color = 0xFFFF0011; + return (color); +} + +void ft_flashscreen(t_varlist *vl, int x, int y, uint32_t color) +{ + if (vl->flash) + { + vl->fimg = mlx_new_image(vl->mlx, vl->w, vl->h); + if (vl->flashcolor == 1) + color = ft_flashred(vl); + else if (vl->flashcolor == 2) + color = ft_flashgreen(vl); + else if (vl->flashcolor == 3) + color = ft_flashblue(vl); + else + color = ft_flashyellow(vl); + while (y < vl->h) + { + x = 0; + while (x < vl->w) + { + mlx_put_pixel(vl->fimg, x, y, color); + x++; + } + y++; + } + mlx_image_to_window(vl->mlx, vl->fimg, 0, 0); + mlx_set_instance_depth(vl->fimg->instances, 15); + vl->flash--; + } +} diff --git a/src/draw/menu.c b/src/draw/menu.c new file mode 100644 index 0000000..03ebc93 --- /dev/null +++ b/src/draw/menu.c @@ -0,0 +1,146 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* menu.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ +/* Updated: 2023/11/10 08:12:48 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_printstats1(t_varlist *vl) +{ + char *total; + char *current; + char *temp; + + current = ft_itoa(vl->tottime); + temp = ft_vastrjoin(3, "Time: ", current, " Seconds"); + vl->mstat[1] = mlx_put_string(vl->mlx, temp, vl->w / 2 - 100, 180); + mlx_resize_image(vl->mstat[1], 200, 50); + ft_vafree(2, current, temp); + total = ft_itoa(vl->enemies); + current = ft_itoa(vl->kills); + temp = ft_vastrjoin(4, "Nazis Killed: ", current, "/", total); + vl->mstat[2] = mlx_put_string(vl->mlx, temp, vl->w / 2 - 100, 230); + mlx_resize_image(vl->mstat[2], 200, 50); + ft_vafree(3, temp, total, current); + total = ft_itoa(vl->tottreasure); + current = ft_itoa(vl->treasure); + temp = ft_vastrjoin(4, "Treasure found: ", current, "/", total); + vl->mstat[3] = mlx_put_string(vl->mlx, temp, vl->w / 2 - 100, 280); + mlx_resize_image(vl->mstat[3], 200, 50); + ft_vafree(3, temp, total, current); +} + +void ft_printstats2(t_varlist *vl, int score) +{ + char *current; + char *temp; + + current = ft_itoa(vl->ammo); + temp = ft_vastrjoin(2, "Ammo left: ", current); + vl->mstat[4] = mlx_put_string(vl->mlx, temp, vl->w / 2 - 100, 330); + mlx_resize_image(vl->mstat[4], 200, 50); + ft_vafree(2, temp, current); + current = ft_itoa(vl->hp); + temp = ft_vastrjoin(2, "HP left: ", current); + vl->mstat[5] = mlx_put_string(vl->mlx, temp, vl->w / 2 - 100, 380); + mlx_resize_image(vl->mstat[5], 200, 50); + ft_vafree(2, temp, current); + current = ft_itoa(score); + temp = ft_strjoin("Total Score ", current); + vl->mstat[6] = mlx_put_string(vl->mlx, temp, vl->w / 2 - 150, 430); + mlx_resize_image(vl->mstat[6], 300, 50); + ft_vafree(2, current, temp); + temp = ft_strdup("Press Jump to restart"); + vl->mstat[7] = mlx_put_string(vl->mlx, temp, vl->w / 2 - 200, 480); + mlx_resize_image(vl->mstat[7], 400, 50); + free(temp); +} + +int ft_getscore(t_varlist *vl) +{ + int score; + + score = (vl->treasure * 10000) + (vl->kills * 1000); + score = score + (vl->ammo * 100) + (vl->hp * 100); + score = score - (vl->tottime * 10); + if (vl->enemies == vl->kills) + score = score + 50000; + if (vl->treasure == vl->tottreasure) + score = score + 50000; + if (vl->hp >= 100) + score = score + 10000; + if (vl->hp <= 0) + score = score - 100000; + if (vl->ammo >= 100) + score = score + 10000; + if (vl->tottime < 180) + score = score + 50000; + if (vl->minimap) + score = score - 50000; + if (vl->stats) + score = score - 50000; + return (score); +} + +void ft_finish(t_varlist *vl) +{ + int x; + int y; + + x = 0; + y = 0; + vl->oimg = mlx_new_image(vl->mlx, vl->w, vl->h); + while (y < vl->h) + { + while (x < vl->w) + { + mlx_put_pixel(vl->oimg, x, y, 0x0000FF66); + x++; + } + x = 0; + y++; + } + mlx_image_to_window(vl->mlx, vl->oimg, 0, 0); + mlx_set_instance_depth(vl->oimg->instances, 15); + vl->mstat = ft_calloc(4096, 8); + vl->mstat[0] = mlx_put_string(vl->mlx, "YOU ROCK!", vl->w / 2 - 250, 30); + mlx_resize_image(vl->mstat[0], 500, 150); + ft_printstats1(vl); + ft_printstats2(vl, ft_getscore(vl)); + vl->menu = 1; +} + +void ft_youdied(t_varlist *vl) +{ + int x; + int y; + + x = 0; + y = 0; + vl->oimg = mlx_new_image(vl->mlx, vl->w, vl->h); + while (y < vl->h) + { + while (x < vl->w) + { + mlx_put_pixel(vl->oimg, x, y, 0xFF000066); + x++; + } + x = 0; + y++; + } + mlx_image_to_window(vl->mlx, vl->oimg, 0, 0); + mlx_set_instance_depth(vl->oimg->instances, 15); + vl->mstat = ft_calloc(4096, 8); + vl->mstat[0] = mlx_put_string(vl->mlx, "YOU DIED!", vl->w / 2 - 250, 30); + mlx_resize_image(vl->mstat[0], 500, 150); + ft_printstats1(vl); + ft_printstats2(vl, ft_getscore(vl)); + vl->menu = 1; +} diff --git a/src/draw/minimap.c b/src/draw/minimap.c new file mode 100644 index 0000000..e25e278 --- /dev/null +++ b/src/draw/minimap.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* minimap.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ +/* Updated: 2023/11/10 11:09:51 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_drawvertlinemap(t_varlist *vl, int y, int mapy) +{ + int mapx; + int x; + + x = 0; + mapx = (int)vl->posy - 10; + while (x <= 21) + { + if (x == 10 && y == 10) + mlx_put_pixel(vl->mimg, x, y, 0xFFFF00FF); + else if (mapy < 0 || mapx < 0) + mlx_put_pixel(vl->mimg, x, y, 0xFFFFFF00); + else if (vl->mapsizey < mapy || vl->mapsizex < mapx) + mlx_put_pixel(vl->mimg, x, y, 0xFFFFFF00); + else if (vl->map[mapy][mapx] == '0') + mlx_put_pixel(vl->mimg, x, y, 0x646464FF); + else if (vl->map[mapy][mapx] == '1') + mlx_put_pixel(vl->mimg, x, y, 0x0000FFFF); + else if (vl->map[mapy][mapx] == '2') + mlx_put_pixel(vl->mimg, x, y, 0x00FF00FF); + else if (vl->map[mapy][mapx] == '3') + mlx_put_pixel(vl->mimg, x, y, 0xFF0000FF); + mapx++; + x++; + } +} + +void ft_drawminimap(t_varlist *vl) +{ + int mapy; + int y; + + if (vl->minimap % 2) + { + mapy = (int)vl->posx - 10; + y = 0; + while (y <= 21) + { + ft_drawvertlinemap(vl, y, mapy); + mapy++; + y++; + } + mlx_resize_image(vl->mimg, 168, 168); + mlx_image_to_window(vl->mlx, vl->mimg, vl->w - 178, 10); + mlx_set_instance_depth(vl->mimg->instances, 11); + } +} diff --git a/src/draw/sprite1.c b/src/draw/sprite1.c new file mode 100644 index 0000000..87b9382 --- /dev/null +++ b/src/draw/sprite1.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* sprite1.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ +/* Updated: 2023/11/10 09:11:46 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_sortsprites(t_varlist *vl) +{ + int i; + t_sprite temp; + + i = 0; + while (vl->spritecount > i) + { + vl->sprite[i].distance = pow((vl->posx - vl->sprite[i].x), 2) + \ + pow((vl->posy - vl->sprite[i].y), 2); + i++; + } + while (i) + { + if (vl->sprite[i].distance > vl->sprite[i - 1].distance) + { + temp = vl->sprite[i]; + vl->sprite[i] = vl->sprite[i - 1]; + vl->sprite[i - 1] = temp; + } + i--; + } +} + +mlx_texture_t *ft_getsprite(t_varlist *vl, int i) +{ + if (vl->sprite[i].type == 1 && vl->sprite[i].status == 0) + return (vl->decotxt[vl->sprite[i].number]); + if (vl->sprite[i].type == 1 && vl->sprite[i].status == 1) + return (vl->decotxt[12]); + if (vl->sprite[i].type == 2 && vl->sprite[i].status == 0) + return (vl->picktxt[vl->sprite[i].number]); + if (vl->sprite[i].type == 3 && vl->sprite[i].status < 2) + return (vl->gfiretxt[vl->sprite[i].number]); + return (vl->gdeadtxt[vl->sprite[i].number]); +} + +t_draw *ft_initdrawsprite(t_varlist *vl, int i) +{ + double spritex; + double spritey; + double invdet; + t_draw *d; + + d = ft_calloc(12, 8); + spritex = vl->sprite[i].x - vl->posx; + spritey = vl->sprite[i].y - vl->posy; + invdet = 1.0 / (vl->planex * vl->diry - vl->dirx * vl->planey); + d->tfx = invdet * (vl->diry * spritex - vl->dirx * spritey); + d->tfy = invdet * (-vl->planey * spritex + vl->planex * spritey); + d->sprs = (vl->w / 2) * (1 + d->tfx / d->tfy); + d->vmov = vl->vaim + vl->jump / d->tfy; + d->sprh = fabs((vl->h / d->tfy)); + return (d); +} + +void ft_getdrawstartend(t_varlist *vl, t_draw *draw) +{ + draw->drawstarty = -draw->sprh / 2 + vl->h / 2 + draw->vmov; + if (draw->drawstarty < 0) + draw->drawstarty = 0; + draw->drawendy = draw->sprh / 2 + vl->h / 2 + draw->vmov; + if (draw->drawendy >= vl->h) + draw->drawendy = vl->h - 1; + draw->sprw = abs((int)(vl->h / draw->tfy)); + draw->drawstartx = -draw->sprw / 2 + draw->sprs; + if (draw->drawstartx < 0) + draw->drawstartx = 0; + draw->drawendx = draw->sprw / 2 + draw->sprs; + if (draw->drawendx >= vl->w) + draw->drawendx = vl->w - 1; +} + +void ft_drawsprites(t_varlist *vl) +{ + int i; + int x; + t_draw *draw; + + ft_sortsprites(vl); + i = 0; + while (vl->spritecount > i) + { + draw = ft_initdrawsprite(vl, i); + ft_getdrawstartend(vl, draw); + x = draw->drawstartx; + vl->temptxt = ft_getsprite(vl, i); + ft_drawsprite(vl, draw, x, i); + free(draw); + i++; + } +} diff --git a/src/draw/sprite2.c b/src/draw/sprite2.c new file mode 100644 index 0000000..ad9c6ec --- /dev/null +++ b/src/draw/sprite2.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* sprite2.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ +/* Updated: 2023/11/10 09:11:54 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_drawspriteline(t_varlist *vl, t_draw *d, int y, int x) +{ + int t; + uint8_t *texel; + uint32_t color; + + t = (y - d->vmov) * 256 - vl->h * 128 + d->sprh * 128; + d->y = (t * 64) / d->sprh / 256; + if (d->y < 0) + d->y = 0; + if (d->y > 64) + d->y = 64; + if (d->x < 0) + d->x = 0; + if (d->x > 64) + d->x = 64; + texel = &vl->temptxt->pixels[(vl->temptxt->width * d->y + d->x) * 4]; + color = texel[0] << 24 | texel[1] << 16 | texel[2] << 8 | texel[3]; + if (color != 0x980088FF) + mlx_put_pixel(vl->img, x, y, color); +} + +void ft_drawsprite(t_varlist *vl, t_draw *d, int x, int i) +{ + int y; + + while (x < d->drawendx) + { + d->x = (int)(256 * (x - (-d->sprw / 2 + d->sprs)) * 64 / d->sprw) / 256; + if (d->tfy > 0 && x > 0 && x < vl->w && d->tfy < vl->distance[x] + 0.7) + { + if (vl->sprite[i].type == 3 && vl->sprite[i].status == 0) + vl->sprite[i].status = 1; + y = d->drawstarty; + while (y < d->drawendy) + { + ft_drawspriteline(vl, d, y, x); + y++; + } + } + else if (vl->sprite[i].type == 3 && vl->sprite[i].status < 2) + { + vl->sprite[i].status = 0; + vl->sprite[i].number = 0; + } + x++; + } +} diff --git a/src/draw/stats.c b/src/draw/stats.c index 2e7c3a3..6dc0253 100644 --- a/src/draw/stats.c +++ b/src/draw/stats.c @@ -6,15 +6,14 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/06 01:17:31 by houtworm ######## odam.nl */ +/* Updated: 2023/11/09 01:28:43 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../../cub3d.h" -void ft_printgamestats(t_varlist *vl) +void ft_printtimestats(t_varlist *vl) { - char *total; char *current; char *temp; @@ -22,6 +21,20 @@ void ft_printgamestats(t_varlist *vl) temp = ft_strjoin(current, " FPS"); vl->fstat = mlx_put_string(vl->mlx, temp, 10, 10); ft_vafree(2, temp, current); + current = ft_itoa(vl->tottime); + temp = ft_vastrjoin(3, "Time: ", current, " Seconds"); + vl->tstat = mlx_put_string(vl->mlx, temp, 10, 70); + ft_vafree(2, current, temp); + mlx_set_instance_depth(vl->fstat->instances, 2); + mlx_set_instance_depth(vl->tstat->instances, 3); +} + +void ft_printgamestats(t_varlist *vl) +{ + char *total; + char *current; + char *temp; + total = ft_itoa(vl->enemies); current = ft_itoa(vl->kills); temp = ft_vastrjoin(4, "Kills: ", current, "/", total); @@ -32,9 +45,8 @@ void ft_printgamestats(t_varlist *vl) temp = ft_vastrjoin(4, "Treasure: ", current, "/", total); 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->cstat->instances, 3); - mlx_set_instance_depth(vl->kstat->instances, 4); + mlx_set_instance_depth(vl->cstat->instances, 4); + mlx_set_instance_depth(vl->kstat->instances, 5); } void ft_printplayerstats(t_varlist *vl) @@ -42,20 +54,26 @@ void ft_printplayerstats(t_varlist *vl) char *current; char *temp; - current = ft_itoa(vl->ammo); - temp = ft_vastrjoin(2, "Ammo: ", current); - vl->astat = mlx_put_string(vl->mlx, temp, 10, vl->h - 30); - ft_vafree(2, temp, current); current = ft_itoa(vl->hp); temp = ft_vastrjoin(2, "HP: ", current); - vl->hstat = mlx_put_string(vl->mlx, temp, 10, vl->h - 50); + vl->hstat = mlx_put_string(vl->mlx, temp, 10, vl->h - 40); ft_vafree(2, temp, current); - mlx_set_instance_depth(vl->astat->instances, 6); + mlx_resize_image(vl->hstat, 120, 30); mlx_set_instance_depth(vl->hstat->instances, 7); + current = ft_itoa(vl->ammo); + temp = ft_vastrjoin(2, "Ammo: ", current); + vl->astat = mlx_put_string(vl->mlx, temp, vl->w - 160, vl->h - 40); + ft_vafree(2, temp, current); + mlx_resize_image(vl->astat, 150, 30); + mlx_set_instance_depth(vl->astat->instances, 6); } void ft_printstats(t_varlist *vl) { - ft_printgamestats(vl); + if (vl->stats % 2) + { + ft_printgamestats(vl); + ft_printtimestats(vl); + } ft_printplayerstats(vl); } diff --git a/src/draw/texture.c b/src/draw/texture.c index 5bad97f..dde683a 100644 --- a/src/draw/texture.c +++ b/src/draw/texture.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/11/05 23:26:38 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 10:41:37 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,19 +14,21 @@ void ft_selecttexture(t_varlist *vl) { - if (vl->side == 0) + if (vl->map[vl->mapx][vl->mapy] == '4') + vl->temptxt = vl->elevtxt[0]; + else if (vl->side == 0) { if (vl->raydirx > 0) - vl->temptext = vl->walltext[0]; + vl->temptxt = vl->walltxt[0]; else - vl->temptext = vl->walltext[2]; + vl->temptxt = vl->walltxt[2]; } else { if (vl->raydiry > 0) - vl->temptext = vl->walltext[3]; + vl->temptxt = vl->walltxt[3]; else - vl->temptext = vl->walltext[1]; + vl->temptxt = vl->walltxt[1]; } } @@ -36,9 +38,9 @@ int ft_gettextx(t_varlist *vl) int textx; if (vl->side == 0) - wallx = vl->posy + vl->walldist * vl->raydiry; + wallx = vl->posy + vl->wdist * vl->raydiry; else - wallx = vl->posx + vl->walldist * vl->raydirx; + wallx = vl->posx + vl->wdist * vl->raydirx; ft_selecttexture(vl); wallx -= floor(wallx); textx = wallx * 64.0; diff --git a/src/draw/weapon.c b/src/draw/weapon.c index 63583dc..fe88976 100644 --- a/src/draw/weapon.c +++ b/src/draw/weapon.c @@ -6,17 +6,17 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/05 23:44:25 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 09:12:33 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../../cub3d.h" -void ft_drawknife(t_varlist *vl) +void ft_drawweapontexture(t_varlist *vl, mlx_texture_t **text) { - int x; - int y; - int i; + int x; + int y; + int i; uint32_t color; x = 0; @@ -24,82 +24,7 @@ void ft_drawknife(t_varlist *vl) i = 1; while (i < 4096) { - color = ft_gettextcolor(vl->gun0text[vl->reload], y, x); - if (color != 0x980088FF) - mlx_put_pixel(vl->wimg, x, y, color); - x++; - if (i % 64 == 0) - { - x = 0; - y++; - } - i++; - } -} - -void ft_drawpistol(t_varlist *vl) -{ - int x; - int y; - int i; - uint32_t color; - - x = 0; - y = 0; - i = 1; - while (i < 4096) - { - color = ft_gettextcolor(vl->gun1text[vl->reload], y, x); - if (color != 0x980088FF) - mlx_put_pixel(vl->wimg, x, y, color); - x++; - if (i % 64 == 0) - { - x = 0; - y++; - } - i++; - } -} - -void ft_drawmgun(t_varlist *vl) -{ - int x; - int y; - int i; - uint32_t color; - - x = 0; - y = 0; - i = 1; - while (i < 4096) - { - color = ft_gettextcolor(vl->gun2text[vl->reload], y, x); - if (color != 0x980088FF) - mlx_put_pixel(vl->wimg, x, y, color); - x++; - if (i % 64 == 0) - { - x = 0; - y++; - } - i++; - } -} - -void ft_drawggun(t_varlist *vl) -{ - int x; - int y; - int i; - uint32_t color; - - x = 0; - y = 0; - i = 1; - while (i < 4096) - { - color = ft_gettextcolor(vl->gun3text[vl->reload], y, x); + color = ft_gettextcolor(text[vl->reload], y, x); if (color != 0x980088FF) mlx_put_pixel(vl->wimg, x, y, color); x++; @@ -117,13 +42,13 @@ void ft_drawweapon(t_varlist *vl) int size; if (vl->weapon == 0) - ft_drawknife(vl); + ft_drawweapontexture(vl, vl->gun0txt); if (vl->weapon == 1) - ft_drawpistol(vl); + ft_drawweapontexture(vl, vl->gun1txt); if (vl->weapon == 2) - ft_drawmgun(vl); + ft_drawweapontexture(vl, vl->gun2txt); if (vl->weapon == 3) - ft_drawggun(vl); + ft_drawweapontexture(vl, vl->gun3txt); size = vl->h * 0.8; mlx_resize_image(vl->wimg, size, size); mlx_image_to_window(vl->mlx, vl->wimg, vl->w / 2 - size / 2, vl->h - size); diff --git a/src/draw/world.c b/src/draw/world.c index 4435bad..fcf36a3 100644 --- a/src/draw/world.c +++ b/src/draw/world.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/11/05 23:27:55 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 10:41:13 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ int ft_drawceiling(int x, t_varlist *vl, int drawstart) return (y); } -int ft_drawwall(int x, t_varlist *vl, int drawstart, int drawend, int y) +int ft_drawwall(t_varlist *vl, int x, int y, t_draw *draw) { int textx; double step; @@ -34,13 +34,14 @@ int ft_drawwall(int x, t_varlist *vl, int drawstart, int drawend, int y) uint32_t color; textx = ft_gettextx(vl); - step = 64.0 / vl->lineheight; - textpos = (drawstart - vl->vaim - (vl->jump / vl->walldist) - vl->h / 2 + vl->lineheight / 2) * step; - while (y < drawend) + step = 64.0 / vl->lineh; + textpos = (draw->drawstarty - vl->vaim - (vl->jump / vl->wdist) - \ + vl->h / 2 + vl->lineh / 2) * step; + while (y < draw->drawendy) { texty = (int)textpos & (64 - 1); textpos += step; - color = ft_gettextcolor(vl->temptext, texty, textx); + color = ft_gettextcolor(vl->temptxt, texty, textx); mlx_put_pixel(vl->img, x, y, color); y++; } @@ -62,13 +63,13 @@ int ft_getwallheight(t_varlist *vl, int mode) if (mode == 1) { - ret = -vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->walldist); + ret = -vl->lineh / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->wdist); if (ret < 0) ret = 0; } if (mode == 2) { - ret = vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->walldist); + ret = vl->lineh / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->wdist); if (ret >= vl->h) ret = vl->h - 1; } @@ -77,26 +78,29 @@ int ft_getwallheight(t_varlist *vl, int mode) void ft_drawmap(t_varlist *vl) { - int x; - int y; - int mapx; - int mapy; + int x; + int y; + t_draw *draw; - mapx = (int)vl->posx; - mapy = (int)vl->posy; x = 0; + draw = ft_calloc(12, 8); while (x <= vl->w) { - ft_raycast(vl, x, mapx, mapy); + vl->mapx = (int)vl->posx; + vl->mapy = (int)vl->posy; + ft_raycast(vl, x); y = ft_drawceiling(x, vl, ft_getwallheight(vl, 1)); - y = ft_drawwall(x, vl, ft_getwallheight(vl, 1), ft_getwallheight(vl, 2), y); + draw->drawstarty = ft_getwallheight(vl, 1); + draw->drawendy = ft_getwallheight(vl, 2); + y = ft_drawwall(vl, x, y, draw); ft_drawfloor(x, vl, y); if (vl->side == 0) - vl->walldist = (vl->sidedistx - vl->deltadistx); + vl->wdist = (vl->sidedistx - vl->deltadistx); else - vl->walldist = (vl->sidedisty - vl->deltadisty); - vl->lineheight = vl->h / vl->walldist; - vl->distance[x] = vl->walldist; + vl->wdist = (vl->sidedisty - vl->deltadisty); + vl->lineh = vl->h / vl->wdist; + vl->distance[x] = vl->wdist; x++; } + free(draw); } diff --git a/src/input/acro.c b/src/input/acro.c new file mode 100644 index 0000000..b0838b5 --- /dev/null +++ b/src/input/acro.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* acro.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ +/* Updated: 2023/11/10 05:16:39 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_processacro(t_varlist *vl, double movespeed) +{ + if (mlx_is_key_down(vl->mlx, MLX_KEY_SPACE)) + { + if (vl->menu) + vl->menu = 3; + if (vl->jump < 10) + vl->jump = 200; + } + else if (vl->jump > 0) + vl->jump = vl->jump - 150 * movespeed; + if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT_CONTROL)) + vl->jump = -200; + else if (vl->jump < 0) + vl->jump = vl->jump + 150 * movespeed; + if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT_SHIFT)) + vl->run = 2; + else + vl->run = 1; +} diff --git a/src/input/game.c b/src/input/game.c index a52a1d2..204f06a 100644 --- a/src/input/game.c +++ b/src/input/game.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/11/05 20:16:38 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 11:09:42 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,15 +17,21 @@ void keyhook(mlx_key_data_t kd, void *param) t_varlist *vl; vl = param; - vl->w = vl->w; if (mlx_is_key_down(vl->mlx, MLX_KEY_ESCAPE)) { ft_putendl("escape is pressed"); mlx_close_window(vl->mlx); return ; } - if ((kd.key == MLX_KEY_H || kd.key == MLX_KEY_F1) && kd.action == MLX_PRESS) - ft_putendl("H is pressed"); + if (vl->menu == 0) + { + if (kd.key == MLX_KEY_M && kd.action == MLX_PRESS) + vl->minimap++; + if (kd.key == MLX_KEY_N && kd.action == MLX_PRESS) + vl->stats++; + if (kd.key == MLX_KEY_F && kd.action == MLX_PRESS) + ft_interact(vl); + } } void resizehook(int x, int y, void *param) @@ -47,7 +53,10 @@ void ft_processinput(t_varlist *vl) movespeed = vl->frametime * 3.0; rotspeed = vl->frametime * 3.0; ft_processacro(vl, movespeed); - ft_processmove(vl, rotspeed); - ft_processturn(vl, rotspeed); - ft_processguns(vl); + if (vl->menu == 0) + { + ft_processmove(vl, rotspeed, 0, 0); + ft_processturn(vl, rotspeed); + ft_processguns(vl); + } } diff --git a/src/input/rest.c b/src/input/guns.c similarity index 73% rename from src/input/rest.c rename to src/input/guns.c index 186c1c9..05f59d5 100644 --- a/src/input/rest.c +++ b/src/input/guns.c @@ -1,45 +1,29 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* rest.c :+: :+: */ +/* mouse.c :+: :+: */ /* +:+ */ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/11/06 01:05:43 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 05:16:15 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../../cub3d.h" -void ft_processacro(t_varlist *vl, double movespeed) -{ - if (mlx_is_key_down(vl->mlx, MLX_KEY_SPACE) && vl->jump < 10) - vl->jump = 200; - else if (vl->jump > 0) - vl->jump = vl->jump - 150 * movespeed; - if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT_CONTROL)) - vl->jump = -200; - else if (vl->jump < 0) - vl->jump = vl->jump + 150 * movespeed; - if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT_SHIFT)) - vl->run = 2; - else - vl->run = 1; -} - void ft_processguns(t_varlist *vl) { if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT) && vl->reload == 0) { if (vl->ammo || !vl->weapon) - { - vl->anitime = vl->frametime; vl->reload = 1; + if (!vl->ammo && vl->weapon) + { + vl->weapon = 0; + vl->reloadtime = 0.05; } } - if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_RIGHT)) - ft_putendl("zoom"); } void ft_nextweapon(t_varlist *vl) @@ -101,4 +85,12 @@ void scrollhook(double xdelta, double ydelta, void *param) ft_prevweapon(vl); if (ydelta < 0) ft_nextweapon(vl); + if (vl->weapon == 0) + vl->reloadtime = 0.05; + else if (vl->weapon == 1) + vl->reloadtime = 0.1; + else if (vl->weapon == 2) + vl->reloadtime = 0.05; + else if (vl->weapon == 3) + vl->reloadtime = 0.02; } diff --git a/src/input/move.c b/src/input/move.c index 8b2a33d..ea033af 100644 --- a/src/input/move.c +++ b/src/input/move.c @@ -6,52 +6,53 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/11/05 20:42:52 by houtworm ######## odam.nl */ +/* Updated: 2023/11/09 01:04:20 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../../cub3d.h" -void ft_moveforward(t_varlist *vl, double movespeed) +void ft_checkmove(t_varlist *vl, double movy, double movx, double movespeed) { - if (vl->map[(int)(vl->posx + vl->dirx * movespeed)][(int)vl->posy] == '0') - vl->posx += vl->dirx * movespeed; - if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * movespeed)] == '0') - vl->posy += vl->diry * movespeed; + double length; + double newx; + double newy; + + length = sqrt(movx * movx + movy * movy); + if (length != 0) + { + movx /= length; + movy /= length; + } + newx = vl->posx + movx * movespeed * vl->run; + newy = vl->posy + movy * movespeed * vl->run; + if (vl->map[(int)newx][(int)vl->posy] == '0') + vl->posx = newx; + if (vl->map[(int)vl->posx][(int)newy] == '0') + vl->posy = newy; } -void ft_movebackward(t_varlist *vl, double movespeed) -{ - if (vl->map[(int)(vl->posx - vl->dirx * movespeed)][(int)vl->posy] == '0') - vl->posx -= vl->dirx * movespeed; - if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * movespeed)] == '0') - vl->posy -= vl->diry * movespeed; -} - -void ft_moveleft(t_varlist *vl, double movespeed) -{ - if (vl->map[(int)(vl->posx - vl->dirx * movespeed)][(int)vl->posy] == '0') - vl->posx -= vl->diry * movespeed; - if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * movespeed)] == '0') - vl->posy += vl->dirx * movespeed; -} - -void ft_moveright(t_varlist *vl, double movespeed) -{ - if (vl->map[(int)(vl->posx + vl->dirx * movespeed)][(int)vl->posy] == '0') - vl->posx += vl->diry * movespeed; - if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * movespeed)] == '0') - vl->posy -= vl->dirx * movespeed; -} - -void ft_processmove(t_varlist *vl, double movespeed) +void ft_processmove(t_varlist *vl, double movspd, double movx, double movy) { if (mlx_is_key_down(vl->mlx, MLX_KEY_W)) - ft_moveforward(vl, movespeed * vl->run); + { + movx += vl->dirx; + movy += vl->diry; + } if (mlx_is_key_down(vl->mlx, MLX_KEY_A)) - ft_moveleft(vl, movespeed * vl->run); + { + movx -= vl->diry; + movy += vl->dirx; + } if (mlx_is_key_down(vl->mlx, MLX_KEY_S)) - ft_movebackward(vl, movespeed * vl->run); + { + movx -= vl->dirx; + movy -= vl->diry; + } if (mlx_is_key_down(vl->mlx, MLX_KEY_D)) - ft_moveright(vl, movespeed * vl->run); + { + movx += vl->diry; + movy -= vl->dirx; + } + ft_checkmove(vl, movy, movx, movspd); } diff --git a/src/main/cleanup.c b/src/main/cleanup.c index a7636c3..69fc891 100644 --- a/src/main/cleanup.c +++ b/src/main/cleanup.c @@ -6,41 +6,77 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/05 08:40:06 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 09:13:02 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../../cub3d.h" +void ft_deleteimages(t_varlist *vl) +{ + int i; + + i = 0; + if (vl->menu) + { + while (i < 8) + { + mlx_delete_image(vl->mlx, vl->mstat[i]); + i++; + } + free(vl->mstat); + } + mlx_delete_image(vl->mlx, vl->img); + mlx_delete_image(vl->mlx, vl->oimg); + mlx_delete_image(vl->mlx, vl->wimg); + mlx_delete_image(vl->mlx, vl->mimg); +} + +void ft_deletedecor(t_varlist *vl) +{ + mlx_delete_texture(vl->decotxt[0]); + mlx_delete_texture(vl->decotxt[1]); + mlx_delete_texture(vl->decotxt[2]); + mlx_delete_texture(vl->decotxt[3]); + mlx_delete_texture(vl->decotxt[4]); + mlx_delete_texture(vl->decotxt[5]); + mlx_delete_texture(vl->decotxt[6]); + mlx_delete_texture(vl->decotxt[7]); + mlx_delete_texture(vl->decotxt[8]); + mlx_delete_texture(vl->decotxt[9]); + mlx_delete_texture(vl->decotxt[10]); + mlx_delete_texture(vl->decotxt[11]); +} + +void ft_deletewalls(t_varlist *vl) +{ + mlx_delete_texture(vl->walltxt[0]); + mlx_delete_texture(vl->walltxt[1]); + mlx_delete_texture(vl->walltxt[2]); + mlx_delete_texture(vl->walltxt[3]); + mlx_delete_texture(vl->elevtxt[0]); +} + +void ft_deletepickup(t_varlist *vl) +{ + mlx_delete_texture(vl->picktxt[0]); + mlx_delete_texture(vl->picktxt[1]); + mlx_delete_texture(vl->picktxt[2]); + mlx_delete_texture(vl->picktxt[3]); + mlx_delete_texture(vl->picktxt[4]); + mlx_delete_texture(vl->picktxt[5]); + mlx_delete_texture(vl->picktxt[6]); + mlx_delete_texture(vl->picktxt[7]); + mlx_delete_texture(vl->picktxt[8]); + mlx_delete_texture(vl->picktxt[9]); + mlx_delete_texture(vl->picktxt[10]); + mlx_delete_texture(vl->picktxt[11]); +} + void ft_cleanup(t_varlist *vl) { - mlx_delete_texture(vl->walltext[0]); - mlx_delete_texture(vl->walltext[1]); - mlx_delete_texture(vl->walltext[2]); - mlx_delete_texture(vl->walltext[3]); - mlx_delete_texture(vl->decotext[0]); - mlx_delete_texture(vl->decotext[1]); - mlx_delete_texture(vl->decotext[2]); - mlx_delete_texture(vl->decotext[3]); - mlx_delete_texture(vl->decotext[4]); - mlx_delete_texture(vl->decotext[5]); - mlx_delete_texture(vl->decotext[6]); - mlx_delete_texture(vl->decotext[7]); - mlx_delete_texture(vl->decotext[8]); - mlx_delete_texture(vl->decotext[9]); - mlx_delete_texture(vl->decotext[10]); - mlx_delete_texture(vl->decotext[11]); - mlx_delete_texture(vl->picktext[0]); - mlx_delete_texture(vl->picktext[1]); - mlx_delete_texture(vl->picktext[2]); - mlx_delete_texture(vl->picktext[3]); - mlx_delete_texture(vl->picktext[4]); - mlx_delete_texture(vl->picktext[5]); - mlx_delete_texture(vl->picktext[6]); - mlx_delete_texture(vl->picktext[7]); - mlx_delete_texture(vl->picktext[8]); - mlx_delete_texture(vl->picktext[9]); - mlx_delete_texture(vl->picktext[10]); - mlx_delete_texture(vl->picktext[11]); - mlx_delete_image(vl->mlx, vl->img); + ft_deleteimages(vl); + ft_deletedecor(vl); + ft_deletewalls(vl); + ft_deletepickup(vl); } diff --git a/src/main/init.c b/src/main/init.c index d5887f4..e817ca2 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/06 01:18:00 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 09:13:39 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,105 +14,87 @@ void ft_initsprites(t_varlist *vl) { - vl->decotext = ft_calloc(4096, 8); - vl->decotext[0] = mlx_load_png("./assets/decoration/barrel.png"); - vl->decotext[1] = mlx_load_png("./assets/decoration/gbarrel.png"); - vl->decotext[2] = mlx_load_png("./assets/decoration/slamp.png"); - vl->decotext[3] = mlx_load_png("./assets/decoration/armor.png"); - vl->decotext[4] = mlx_load_png("./assets/decoration/pillar.png"); - vl->decotext[5] = mlx_load_png("./assets/decoration/plant.png"); - vl->decotext[6] = mlx_load_png("./assets/decoration/vase.png"); - vl->decotext[7] = mlx_load_png("./assets/decoration/faucet.png"); - vl->decotext[8] = mlx_load_png("./assets/decoration/table.png"); - vl->decotext[9] = mlx_load_png("./assets/decoration/hskel.png"); - vl->decotext[10] = mlx_load_png("./assets/decoration/hlamp.png"); - vl->decotext[11] = mlx_load_png("./assets/decoration/chandel.png"); + vl->decotxt = ft_calloc(4096, 8); + vl->decotxt[0] = mlx_load_png("./assets/decoration/barrel.png"); + vl->decotxt[1] = mlx_load_png("./assets/decoration/gbarrel.png"); + vl->decotxt[2] = mlx_load_png("./assets/decoration/slamp.png"); + vl->decotxt[3] = mlx_load_png("./assets/decoration/armor.png"); + vl->decotxt[4] = mlx_load_png("./assets/decoration/pillar.png"); + vl->decotxt[5] = mlx_load_png("./assets/decoration/plant.png"); + vl->decotxt[6] = mlx_load_png("./assets/decoration/vase.png"); + vl->decotxt[7] = mlx_load_png("./assets/decoration/faucet.png"); + vl->decotxt[8] = mlx_load_png("./assets/decoration/table.png"); + vl->decotxt[9] = mlx_load_png("./assets/decoration/hskel.png"); + vl->decotxt[10] = mlx_load_png("./assets/decoration/hlamp.png"); + vl->decotxt[11] = mlx_load_png("./assets/decoration/chandel.png"); + vl->decotxt[12] = mlx_load_png("./assets/decoration/rubble.png"); } void ft_initpickups(t_varlist *vl) { - vl->picktext = ft_calloc(4096, 8); - vl->picktext[0] = mlx_load_png("./assets/pickup/treasure1.png"); - vl->picktext[1] = mlx_load_png("./assets/pickup/treasure2.png"); - vl->picktext[2] = mlx_load_png("./assets/pickup/treasure3.png"); - vl->picktext[3] = mlx_load_png("./assets/pickup/treasure4.png"); - vl->picktext[4] = mlx_load_png("./assets/pickup/hpkit.png"); - vl->picktext[5] = mlx_load_png("./assets/pickup/food.png"); - vl->picktext[6] = mlx_load_png("./assets/pickup/biscuits.png"); - vl->picktext[7] = mlx_load_png("./assets/pickup/gkey.png"); - vl->picktext[8] = mlx_load_png("./assets/pickup/bkey.png"); - vl->picktext[9] = mlx_load_png("./assets/pickup/mgun.png"); - vl->picktext[10] = mlx_load_png("./assets/pickup/ggun.png"); - vl->picktext[11] = mlx_load_png("./assets/pickup/ammo.png"); + vl->picktxt = ft_calloc(4096, 8); + vl->picktxt[0] = mlx_load_png("./assets/pickup/treasure1.png"); + vl->picktxt[1] = mlx_load_png("./assets/pickup/treasure2.png"); + vl->picktxt[2] = mlx_load_png("./assets/pickup/treasure3.png"); + vl->picktxt[3] = mlx_load_png("./assets/pickup/treasure4.png"); + vl->picktxt[4] = mlx_load_png("./assets/pickup/hpkit.png"); + vl->picktxt[5] = mlx_load_png("./assets/pickup/food.png"); + vl->picktxt[6] = mlx_load_png("./assets/pickup/biscuits.png"); + vl->picktxt[7] = mlx_load_png("./assets/pickup/gkey.png"); + vl->picktxt[8] = mlx_load_png("./assets/pickup/bkey.png"); + vl->picktxt[9] = mlx_load_png("./assets/pickup/mgun.png"); + vl->picktxt[10] = mlx_load_png("./assets/pickup/ggun.png"); + vl->picktxt[11] = mlx_load_png("./assets/pickup/ammo.png"); } void ft_initenemies(t_varlist *vl) { - vl->nazitext = ft_calloc(4096, 8); - vl->nazitext[0] = mlx_load_png("./assets/nazi/guard/guard1.png"); + vl->gfiretxt = ft_calloc(4096, 8); + vl->gfiretxt[0] = mlx_load_png("./assets/nazi/guard/fire1.png"); + vl->gfiretxt[1] = mlx_load_png("./assets/nazi/guard/fire2.png"); + vl->gfiretxt[2] = mlx_load_png("./assets/nazi/guard/fire3.png"); + vl->gfiretxt[3] = mlx_load_png("./assets/nazi/guard/fire4.png"); + vl->gdeadtxt = ft_calloc(4096, 8); + vl->gdeadtxt[0] = mlx_load_png("./assets/nazi/guard/die1.png"); + vl->gdeadtxt[1] = mlx_load_png("./assets/nazi/guard/die2.png"); + vl->gdeadtxt[2] = mlx_load_png("./assets/nazi/guard/die3.png"); + vl->gdeadtxt[3] = mlx_load_png("./assets/nazi/guard/die4.png"); + vl->gdeadtxt[4] = mlx_load_png("./assets/nazi/guard/die5.png"); } void ft_initweapons(t_varlist *vl) { - vl->gun0text = ft_calloc(4096, 8); - vl->gun0text[0] = mlx_load_png("./assets/weapon/knife/1.png"); - vl->gun0text[1] = mlx_load_png("./assets/weapon/knife/2.png"); - vl->gun0text[2] = mlx_load_png("./assets/weapon/knife/3.png"); - vl->gun0text[3] = mlx_load_png("./assets/weapon/knife/4.png"); - vl->gun0text[4] = mlx_load_png("./assets/weapon/knife/5.png"); - vl->gun1text = ft_calloc(4096, 8); - vl->gun1text[0] = mlx_load_png("./assets/weapon/pistol/1.png"); - vl->gun1text[1] = mlx_load_png("./assets/weapon/pistol/2.png"); - vl->gun1text[2] = mlx_load_png("./assets/weapon/pistol/3.png"); - vl->gun1text[3] = mlx_load_png("./assets/weapon/pistol/4.png"); - vl->gun1text[4] = mlx_load_png("./assets/weapon/pistol/5.png"); - vl->gun2text = ft_calloc(4096, 8); - vl->gun2text[0] = mlx_load_png("./assets/weapon/mgun/1.png"); - vl->gun2text[1] = mlx_load_png("./assets/weapon/mgun/2.png"); - vl->gun2text[2] = mlx_load_png("./assets/weapon/mgun/3.png"); - 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/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) -{ - vl->w = 800; - vl->h = 600; - vl->sprite = ft_calloc(4096, 8); - vl->spritecount = 0; - vl->distance = ft_calloc(4096, 8); - vl->walltext = ft_calloc(4096, 8); - vl->vaim = 0; - vl->jump = 0; - vl->treasure = 0; - vl->tottreasure = 0; - vl->enemies = 0; - vl->kills = 0; - vl->mgun = 0; - vl->ggun = 0; - vl->ammo = 10; - vl->hp = 100; - vl->weapon = 0; - vl->reload = 0; - vl->ccolor = 0; - vl->fcolor = 0; - vl->walltext[0] = NULL; - vl->walltext[1] = NULL; - vl->walltext[2] = NULL; - vl->walltext[3] = NULL; + vl->gun0txt = ft_calloc(4096, 8); + vl->gun0txt[0] = mlx_load_png("./assets/weapon/knife/1.png"); + vl->gun0txt[1] = mlx_load_png("./assets/weapon/knife/2.png"); + vl->gun0txt[2] = mlx_load_png("./assets/weapon/knife/3.png"); + vl->gun0txt[3] = mlx_load_png("./assets/weapon/knife/4.png"); + vl->gun0txt[4] = mlx_load_png("./assets/weapon/knife/5.png"); + vl->gun1txt = ft_calloc(4096, 8); + vl->gun1txt[0] = mlx_load_png("./assets/weapon/pistol/1.png"); + vl->gun1txt[1] = mlx_load_png("./assets/weapon/pistol/2.png"); + vl->gun1txt[2] = mlx_load_png("./assets/weapon/pistol/3.png"); + vl->gun1txt[3] = mlx_load_png("./assets/weapon/pistol/4.png"); + vl->gun1txt[4] = mlx_load_png("./assets/weapon/pistol/5.png"); + vl->gun2txt = ft_calloc(4096, 8); + vl->gun2txt[0] = mlx_load_png("./assets/weapon/mgun/1.png"); + vl->gun2txt[1] = mlx_load_png("./assets/weapon/mgun/2.png"); + vl->gun2txt[2] = mlx_load_png("./assets/weapon/mgun/3.png"); + vl->gun2txt[3] = mlx_load_png("./assets/weapon/mgun/4.png"); + vl->gun2txt[4] = mlx_load_png("./assets/weapon/mgun/5.png"); + vl->gun3txt = ft_calloc(4096, 8); + vl->gun3txt[0] = mlx_load_png("./assets/weapon/ggun/1.png"); + vl->gun3txt[1] = mlx_load_png("./assets/weapon/ggun/2.png"); + vl->gun3txt[2] = mlx_load_png("./assets/weapon/ggun/3.png"); + vl->gun3txt[3] = mlx_load_png("./assets/weapon/ggun/4.png"); + vl->gun3txt[4] = mlx_load_png("./assets/weapon/ggun/5.png"); } t_varlist initgame(void) { t_varlist vl; - ft_initmainstuff(&vl); + ft_resetvars(&vl); ft_initsprites(&vl); ft_initpickups(&vl); ft_initenemies(&vl); @@ -120,6 +102,5 @@ t_varlist initgame(void) vl.mlx = mlx_init(vl.w, vl.h, "Cub3D", true); if (!vl.mlx) ft_errorexit("MLX failed to init", "initvarlist", 1); - vl.img = mlx_new_image(vl.mlx, vl.w, vl.h); return (vl); } diff --git a/src/main/main.c b/src/main/main.c index 8e25a42..4fe9a35 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -6,13 +6,13 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/06 01:15:49 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 07:49:05 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../../cub3d.h" -void ft_replaceimage(t_varlist *vl) +void ft_newframe(t_varlist *vl) { mlx_delete_image(vl->mlx, vl->img); mlx_delete_image(vl->mlx, vl->fstat); @@ -22,74 +22,20 @@ void ft_replaceimage(t_varlist *vl) mlx_delete_image(vl->mlx, vl->astat); mlx_delete_image(vl->mlx, vl->hstat); mlx_delete_image(vl->mlx, vl->wimg); + mlx_delete_image(vl->mlx, vl->fimg); + mlx_delete_image(vl->mlx, vl->mimg); vl->img = mlx_new_image(vl->mlx, vl->w, vl->h); vl->wimg = mlx_new_image(vl->mlx, 64, 64); -} - -void ft_printmap(t_varlist *vl) -{ - int i; - - i = 0; - while (vl->map[i]) - { - ft_putendl(vl->map[i]); - i++; - } - double movespeed; - movespeed = vl->frametime * 3.0; - printf("side: %d\n", vl->side); - printf("raydirx: %lf\n", vl->raydirx); - printf("raydiry: %lf\n", vl->raydiry); - printf("forward: 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)]); - printf("backward: 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)]); - printf("left: 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)]); - 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->mimg = mlx_new_image(vl->mlx, 22, 22); 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); + vl->tottime = vl->tottime + vl->frametime; } -void ft_fireweapon(t_varlist *vl) +void ft_imagetowindow(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) - { - if (vl->weapon) - vl->ammo--; - vl->reload = 0; - } - } - } - + if (!vl->img || (mlx_image_to_window(vl->mlx, vl->img, 0, 0) < 0)) + ft_errorexit("image to window failed ", "mainloop", 1); + mlx_set_instance_depth(vl->img->instances, 1); } void mainloop(void *param) @@ -97,21 +43,24 @@ void mainloop(void *param) t_varlist *vl; vl = param; - ft_replaceimage(vl); - ft_drawmap(vl); - ft_checkpickup(vl); - /*ft_enemyaction(vl);*/ - ft_drawsprites(vl); - ft_drawweapon(vl); - ft_fireweapon(vl); - /*ft_drawminimap(vl);*/ - ft_timers(vl); - ft_printstats(vl); ft_processinput(vl); - /*ft_printmap(vl);*/ - if (!vl->img || (mlx_image_to_window(vl->mlx, vl->img, 0, 0) < 0)) - ft_errorexit("image to window failed ", "mainloop", 1); - mlx_set_instance_depth(vl->img->instances, 1); + if (!vl->menu) + { + ft_newframe(vl); + ft_drawmap(vl); + ft_enemyaction(vl); + ft_checkpickup(vl); + ft_drawsprites(vl); + ft_drawweapon(vl); + ft_fireweapon(vl); + ft_drawminimap(vl); + ft_checkhealth(vl); + ft_printstats(vl); + ft_flashscreen(vl, 0, 0, 0); + ft_imagetowindow(vl); + } + else if (vl->menu == 3) + ft_restartgame(vl); } int main(int argc, char **argv) @@ -125,8 +74,9 @@ int main(int argc, char **argv) vl = ft_parseconfigfile(vl, argv[1]); else ft_errorexit("Please include a .cub file ", "", 2); - if (!vl.img || (mlx_image_to_window(vl.mlx, vl.img, 0, 0) < 0)) - ft_errorexit("image to window failed ", "main", 1); + vl.cubfile = ft_strdup(argv[1]); + if (ft_floodfill(vl)) + ft_errorexit("map is invalid", "floodfill", 1); mlx_key_hook(vl.mlx, &keyhook, &vl); mlx_resize_hook(vl.mlx, &resizehook, &vl); mlx_scroll_hook(vl.mlx, &scrollhook, &vl); diff --git a/src/draw/raycast.c b/src/main/raycast.c similarity index 86% rename from src/draw/raycast.c rename to src/main/raycast.c index 23c238e..fdc5ba3 100644 --- a/src/draw/raycast.c +++ b/src/main/raycast.c @@ -6,7 +6,7 @@ /* By: djonker +#+ */ /* +#+ */ /* Created: 2023/10/27 14:36:42 by djonker #+# #+# */ -/* Updated: 2023/11/05 19:05:24 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 02:32:10 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,30 +58,31 @@ int ft_prepcast(t_varlist *vl, int x) return (0); } -void ft_raycast(t_varlist *vl, int x, int mapx, int mapy) +void ft_raycast(t_varlist *vl, int x) { int hit; int stepx; int stepy; hit = ft_prepcast(vl, x); - stepx = ft_getstepx(vl, mapx); - stepy = ft_getstepy(vl, mapy); + stepx = ft_getstepx(vl, vl->mapx); + stepy = ft_getstepy(vl, vl->mapy); while (hit == 0) { if (vl->sidedistx < vl->sidedisty) { vl->sidedistx += vl->deltadistx; - mapx += stepx; + vl->mapx += stepx; vl->side = 0; } else { vl->sidedisty += vl->deltadisty; - mapy += stepy; + vl->mapy += stepy; vl->side = 1; } - if (vl->map[mapx][mapy] == '1') + if (vl->map[vl->mapx][vl->mapy] == '1' || \ + vl->map[vl->mapx][vl->mapy] == '4') hit = 1; } } diff --git a/src/main/reset.c b/src/main/reset.c new file mode 100644 index 0000000..0ba9ffb --- /dev/null +++ b/src/main/reset.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* reset.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */ +/* Updated: 2023/11/10 09:14:03 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_resetgame(t_varlist *vl) +{ + vl->w = 800; + vl->h = 600; + vl->sprite = ft_calloc(4096, 8); + vl->spritecount = 0; + vl->distance = ft_calloc(4096, 8); + vl->ccolor = 0; + vl->fcolor = 0; + vl->minimap = 0; + vl->stats = 0; + vl->menu = 0; + vl->flash = 0; + vl->tottime = 0; + vl->reloadtime = 0.1; + vl->firetime = 0; + vl->reload = 0; +} + +void ft_resettextures(t_varlist *vl) +{ + vl->elevtxt = ft_calloc(4096, 8); + vl->elevtxt[0] = mlx_load_png("./assets/wall/elevator.png"); + vl->walltxt = ft_calloc(4096, 8); + vl->walltxt[0] = NULL; + vl->walltxt[1] = NULL; + vl->walltxt[2] = NULL; + vl->walltxt[3] = NULL; +} + +void ft_resetplayer(t_varlist *vl) +{ + vl->vaim = 0; + vl->jump = 0; + vl->treasure = 0; + vl->tottreasure = 0; + vl->enemies = 0; + vl->kills = 0; + vl->mgun = 0; + vl->ggun = 0; + vl->ammo = 10; + vl->hp = 100; + vl->weapon = 1; +} + +void ft_resetvars(t_varlist *vl) +{ + ft_resettextures(vl); + ft_resetplayer(vl); + ft_resetgame(vl); +} + +void ft_restartgame(t_varlist *vl) +{ + int i; + + i = 0; + while (i < 8) + { + mlx_delete_image(vl->mlx, vl->mstat[i]); + i++; + } + mlx_delete_image(vl->mlx, vl->oimg); + mlx_delete_texture(vl->walltxt[0]); + mlx_delete_texture(vl->walltxt[1]); + mlx_delete_texture(vl->walltxt[2]); + mlx_delete_texture(vl->walltxt[3]); + mlx_delete_texture(vl->elevtxt[0]); + free(vl->distance); + free(vl->sprite); + free(vl->walltxt); + free(vl->elevtxt); + free(vl->mstat); + ft_frearr(vl->map); + ft_resetvars(vl); + *vl = ft_parseconfigfile(*vl, vl->cubfile); +} diff --git a/src/parse/elements.c b/src/parse/elements.c new file mode 100644 index 0000000..f639943 --- /dev/null +++ b/src/parse/elements.c @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* elements.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */ +/* Updated: 2023/11/10 08:45:58 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +char ft_checkforbasics(t_varlist *vl, char element, int x, int y) +{ + if (ft_strchr(" 0", element)) + return ('0'); + else if (element == '1') + return ('1'); + else if (ft_strchr("NESW", element)) + return (ft_initplayer(vl, element, y, x)); + else if (element == 'D') + return ('0'); + else if (element == 'X') + return ('4'); + return ('\0'); +} + +char ft_checkfordecor(t_varlist *vl, char element, int x, int y) +{ + if (element == 'B') + return (ft_addsoliddecor(vl, y, x, 0)); + else if (element == 'b') + return (ft_addsoliddecor(vl, y, x, 1)); + else if (element == 'L') + return (ft_addsoliddecor(vl, y, x, 2)); + else if (element == 'A') + return (ft_addsoliddecor(vl, y, x, 3)); + else if (element == 'P') + return (ft_addsoliddecor(vl, y, x, 4)); + else if (element == 'p') + return (ft_addsoliddecor(vl, y, x, 5)); + else if (element == 'V') + return (ft_addsoliddecor(vl, y, x, 6)); + else if (element == 'f') + return (ft_addsoliddecor(vl, y, x, 7)); + else if (element == 'T') + return (ft_addsoliddecor(vl, y, x, 8)); + else if (element == 's') + return (ft_addsoliddecor(vl, y, x, 9)); + else if (element == 'l') + return (ft_addwalktroughdecor(vl, y, x, 10)); + else if (element == 'C') + return (ft_addwalktroughdecor(vl, y, x, 11)); + return ('\0'); +} + +char ft_checkforpickup(t_varlist *vl, char element, int x, int y) +{ + if (element == '$') + return (ft_addpickup(vl, y, x, 0)); + else if (element == '#') + return (ft_addpickup(vl, y, x, 1)); + else if (element == '*') + return (ft_addpickup(vl, y, x, 2)); + else if (element == '&') + return (ft_addpickup(vl, y, x, 3)); + else if (element == '9') + return (ft_addpickup(vl, y, x, 4)); + else if (element == '8') + return (ft_addpickup(vl, y, x, 5)); + else if (element == '7') + return (ft_addpickup(vl, y, x, 6)); + else if (element == '5') + return (ft_addpickup(vl, y, x, 7)); + else if (element == '6') + return (ft_addpickup(vl, y, x, 8)); + else if (element == '3') + return (ft_addpickup(vl, y, x, 9)); + else if (element == '4') + return (ft_addpickup(vl, y, x, 10)); + else if (element == '2') + return (ft_addpickup(vl, y, x, 11)); + return ('\0'); +} + +char ft_checkforenemy(t_varlist *vl, char element, int x, int y) +{ + if (element == 'K') + return (ft_addenemy(vl, y, x, 0)); + return ('\0'); +} + +char ft_checkmapelement(t_varlist *vl, char element, int x, int y) +{ + char ret; + + ret = ft_checkforbasics(vl, element, x, y); + if (!ret) + ret = ft_checkforenemy(vl, element, x, y); + if (!ret) + ret = ft_checkfordecor(vl, element, x, y); + if (!ret) + ret = ft_checkforpickup(vl, element, x, y); + if (ret) + return (ret); + ft_putchar(element); + ft_putendl(" character found on map which makes it invalid"); + exit (1); +} diff --git a/src/parse/file.c b/src/parse/file.c new file mode 100644 index 0000000..3e193e9 --- /dev/null +++ b/src/parse/file.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* file.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:48:55 by houtworm #+# #+# */ +/* Updated: 2023/11/10 09:31:08 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +t_varlist ft_parseconfigfile(t_varlist vl, char *filename) +{ + int fd; + char *line; + + fd = open(filename, O_RDONLY); + if (fd == -1) + ft_errorexit("file does not exist", "parseconfigfile", 1); + while (get_next_line(fd, &line)) + { + ft_checkline(&vl, line); + if (vl.walltxt[0] && vl.walltxt[1] && vl.walltxt[2] && vl.walltxt[3]) + { + if (vl.ccolor && vl.fcolor) + { + vl.map = ft_getmap(&vl, fd); + break ; + } + } + free(line); + } + close(fd); + free(line); + return (vl); +} diff --git a/src/parse/flood.c b/src/parse/flood.c new file mode 100644 index 0000000..4e643ce --- /dev/null +++ b/src/parse/flood.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* flood.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ +/* Updated: 2023/11/10 10:51:00 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +int ft_floodvertical(t_varlist vl, char **fillmap, int x, int y) +{ + if (fillmap[y][x - 1] != '1') + { + fillmap[y][x - 1] = '1'; + if (ft_flood(vl, fillmap, x - 1, y)) + return (1); + } + if (x >= vl.mapsizex) + return (1); + if (fillmap[y][x + 1] != '1') + { + fillmap[y][x + 1] = '1'; + if (ft_flood(vl, fillmap, x + 1, y)) + return (1); + } + return (0); +} + +int ft_floodhorizontal(t_varlist vl, char **fillmap, int x, int y) +{ + if (y == 0) + return (1); + if (fillmap[y - 1][x] != '1') + { + fillmap[y - 1][x] = '1'; + if (ft_flood(vl, fillmap, x, y - 1)) + return (1); + } + if (y >= vl.mapsizey) + return (1); + if (fillmap[y + 1][x] != '1') + { + fillmap[y + 1][x] = '1'; + if (ft_flood(vl, fillmap, x, y + 1)) + return (1); + } + if (x == 0) + return (1); + return (0); +} + +int ft_flood(t_varlist vl, char **fillmap, int x, int y) +{ + if (ft_floodhorizontal(vl, fillmap, x, y)) + return (1); + if (ft_floodvertical(vl, fillmap, x, y)) + return (1); + return (0); +} + +int ft_floodfill(t_varlist vl) +{ + int y; + int x; + char **fillmap; + + fillmap = ft_calloc(1024, 8); + y = 0; + while (vl.map[y]) + { + fillmap[y] = ft_strdup(vl.map[y]); + y++; + } + y = (int)vl.posy; + x = (int)vl.posx; + fillmap[y][x] = '1'; + x = ft_flood(vl, fillmap, x, y); + ft_frearr(fillmap); + return (x); +} diff --git a/src/parse/parse.c b/src/parse/line.c similarity index 66% rename from src/parse/parse.c rename to src/parse/line.c index bf25a7d..270c00f 100644 --- a/src/parse/parse.c +++ b/src/parse/line.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* parse.c :+: :+: */ +/* line.c :+: :+: */ /* +:+ */ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:48:55 by houtworm #+# #+# */ -/* Updated: 2023/11/05 08:19:52 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 09:14:29 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,13 +20,13 @@ int ft_settexture(t_varlist *vl, char *line, int direction) while (line[i] != '.') i++; if (direction == 1) - vl->walltext[0] = mlx_load_png(&line[i]); + vl->walltxt[0] = mlx_load_png(&line[i]); if (direction == 2) - vl->walltext[1] = mlx_load_png(&line[i]); + vl->walltxt[1] = mlx_load_png(&line[i]); if (direction == 3) - vl->walltext[2] = mlx_load_png(&line[i]); + vl->walltxt[2] = mlx_load_png(&line[i]); if (direction == 4) - vl->walltext[3] = mlx_load_png(&line[i]); + vl->walltxt[3] = mlx_load_png(&line[i]); return (0); } @@ -58,7 +58,7 @@ int ft_setcolor(t_varlist *vl, char *line, int dest) return (0); } -char *ft_checkline(t_varlist *vl, char *line) +char *ft_checklinecfn(t_varlist *vl, char *line) { if (line[0] == 'C' && line[1] == ' ') { @@ -76,7 +76,7 @@ char *ft_checkline(t_varlist *vl, char *line) } else if (!ft_strncmp(line, "NO ", 3)) { - if (!vl->walltext[0]) + if (!vl->walltxt[0]) ft_settexture(vl, line, 1); else return (" north texture"); @@ -84,25 +84,25 @@ char *ft_checkline(t_varlist *vl, char *line) return (NULL); } -char *ft_checkline2(t_varlist *vl, char *line) +char *ft_checklineesw(t_varlist *vl, char *line) { if (!ft_strncmp(line, "EA ", 3)) { - if (!vl->walltext[1]) + if (!vl->walltxt[1]) ft_settexture(vl, line, 2); else return (" east texture"); } else if (!ft_strncmp(line, "SO ", 3)) { - if (!vl->walltext[2]) + if (!vl->walltxt[2]) ft_settexture(vl, line, 3); else return (" south texture"); } else if (!ft_strncmp(line, "WE ", 3)) { - if (!vl->walltext[3]) + if (!vl->walltxt[3]) ft_settexture(vl, line, 4); else return (" west texture"); @@ -110,32 +110,13 @@ char *ft_checkline2(t_varlist *vl, char *line) return (NULL); } -t_varlist ft_parseconfigfile(t_varlist vl, char *filename) +void ft_checkline(t_varlist *vl, char *line) { - int fd; - char *line; char *error; - fd = open(filename, O_RDONLY); - if (fd == -1) - ft_errorexit("file does not exist", "parseconfigfile", 1); - while (get_next_line(fd, &line)) - { - error = ft_checkline(&vl, line); - if (!error) - error = ft_checkline2(&vl, line); - if (error) - ft_errorexit("Double config declaration in .cub file", error, 1); - if (vl.ccolor && vl.fcolor && vl.walltext[0] && vl.walltext[1] && vl.walltext[2] && vl.walltext[3]) - { - vl.map = ft_getmap(&vl, fd); - break ; - } - free(line); - } - close(fd); - free(line); - if (!vl.map) - ft_errorexit("The map is too big, 500x500 max\n", "", 1); - return (vl); + error = ft_checklinecfn(vl, line); + if (!error) + error = ft_checklineesw(vl, line); + if (error) + ft_errorexit(error, "Double config declaration in .cub file", 1); } diff --git a/src/parse/map.c b/src/parse/map.c index 429c456..90d48da 100644 --- a/src/parse/map.c +++ b/src/parse/map.c @@ -6,54 +6,46 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */ -/* Updated: 2023/11/05 19:05:34 by houtworm ######## odam.nl */ +/* Updated: 2023/11/10 11:08:41 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../../cub3d.h" -char ft_setplayerpos(t_varlist *vl, char dir, int x, int y) +int ft_getmapx(t_varlist *vl, char **map, int y, int fd) { - vl->posx = x + 0.5; - vl->posy = y + 0.5; - if (dir == 'N') + int ret; + int x; + char *line; + + ret = 1; + while (ret > 0) { - vl->dirx = -1; - vl->diry = 0; - vl->planex = 0; - vl->planey = 0.66; + ret = get_next_line(fd, &line); + if (line[0]) + break ; + free(line); } - else if (dir == 'E') + x = 0; + while (ret && x <= 1024 && line[x]) { - vl->dirx = 0; - vl->diry = 1; - vl->planex = 0.66; - vl->planey = 0; + if (x > 1000) + ft_errorexit("The map is too big, 1000x1000 max\n", "", 1); + map[y][x] = ft_checkmapelement(vl, line[x], x, y); + if (x > vl->mapsizex) + vl->mapsizex = x; + x++; } - else if (dir == 'S') - { - vl->dirx = 1; - vl->diry = 0; - vl->planex = 0; - vl->planey = -0.66; - } - else if (dir == 'W') - { - vl->dirx = 0; - vl->diry = -1; - vl->planex = -0.66; - vl->planey = 0; - } - return ('0'); + if (ret) + free(line); + return (ret); } char **ft_getmap(t_varlist *vl, int fd) { int y; - int x; int ret; char **map; - char *line; map = ft_calloc(1024, 8); y = 0; @@ -61,93 +53,12 @@ char **ft_getmap(t_varlist *vl, int fd) while (y <= 1024 && ret > 0) { if (y > 1000) - return (NULL); + ft_errorexit("The map is too big, 1000x1000 max\n", "", 1); map[y] = ft_calloc(1024, 8); - while (ret > 0) - { - ret = get_next_line(fd, &line); - if (line[0]) - break ; - free(line); - } + ret = ft_getmapx(vl, map, y, fd); if (ret == 0) - { - free(map[y]); return (map); - } - x = 0; - while (x <= 1024 && line[x]) - { - if (x > 1000) - return (NULL); - if (ft_strchr(" 0", line[x])) - map[y][x] = '0'; - else if (line[x] == '1') - map[y][x] = '1'; - else if (ft_strchr("NESW", line[x])) - map[y][x] = ft_setplayerpos(vl, line[x], y, x); - else if (line[x] == 'D') - map[y][x] = 'D'; - else if (line[x] == 'B') - map[y][x] = ft_addsoliddecor(vl, y, x, 0); - else if (line[x] == 'b') - map[y][x] = ft_addsoliddecor(vl, y, x, 1); - else if (line[x] == 'L') - map[y][x] = ft_addsoliddecor(vl, y, x, 2); - else if (line[x] == 'A') - map[y][x] = ft_addsoliddecor(vl, y, x, 3); - else if (line[x] == 'P') - map[y][x] = ft_addsoliddecor(vl, y, x, 4); - else if (line[x] == 'p') - map[y][x] = ft_addsoliddecor(vl, y, x, 5); - else if (line[x] == 'V') - map[y][x] = ft_addsoliddecor(vl, y, x, 6); - else if (line[x] == 'f') - map[y][x] = ft_addsoliddecor(vl, y, x, 7); - else if (line[x] == 'T') - map[y][x] = ft_addsoliddecor(vl, y, x, 8); - else if (line[x] == 's') - map[y][x] = ft_addsoliddecor(vl, y, x, 9); - else if (line[x] == 'l') - map[y][x] = ft_addwalktroughdecor(vl, y, x, 10); - else if (line[x] == 'C') - map[y][x] = ft_addwalktroughdecor(vl, y, x, 11); - else if (line[x] == '$') - map[y][x] = ft_addpickup(vl, y, x, 0); - else if (line[x] == '#') - map[y][x] = ft_addpickup(vl, y, x, 1); - else if (line[x] == '*') - map[y][x] = ft_addpickup(vl, y, x, 2); - else if (line[x] == '&') - map[y][x] = ft_addpickup(vl, y, x, 3); - else if (line[x] == '9') - map[y][x] = ft_addpickup(vl, y, x, 4); - else if (line[x] == '8') - map[y][x] = ft_addpickup(vl, y, x, 5); - else if (line[x] == '7') - map[y][x] = ft_addpickup(vl, y, x, 6); - else if (line[x] == '[') - map[y][x] = ft_addpickup(vl, y, x, 7); - else if (line[x] == ']') - map[y][x] = ft_addpickup(vl, y, x, 8); - else if (line[x] == '3') - map[y][x] = ft_addpickup(vl, y, x, 9); - else if (line[x] == '4') - map[y][x] = ft_addpickup(vl, y, x, 10); - else if (line[x] == '2') - map[y][x] = ft_addpickup(vl, y, x, 11); - else if (line[x] == 'K') - map[y][x] = ft_addenemy(vl, y, x, 0); - else - { - ft_putstr("Invalid character on the map\n Valid options are\n"); - ft_putstr("NESW Player\n' ' or 0 empty space\n1 wall\n"); - ft_putstr("D Door\nC Collectible\nB Barrel\nX Level end\n"); - ft_errorexit("K Enemy\nG groundlamp\nH Hanglamp", "", 1); - } - x++; - } - free(line); + vl->mapsizey = y; y++; } ft_errorexit("Something went wrong", "ft_getmap", 1); diff --git a/src/parse/player.c b/src/parse/player.c new file mode 100644 index 0000000..4d12e81 --- /dev/null +++ b/src/parse/player.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* player.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */ +/* Updated: 2023/11/10 08:54:40 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_northeast(t_varlist *vl, char dir) +{ + if (dir == 'N') + { + vl->dirx = -1; + vl->diry = 0; + vl->planex = 0; + vl->planey = 0.66; + } + else if (dir == 'E') + { + vl->dirx = 0; + vl->diry = 1; + vl->planex = 0.66; + vl->planey = 0; + } +} + +void ft_southwest(t_varlist *vl, char dir) +{ + if (dir == 'S') + { + vl->dirx = 1; + vl->diry = 0; + vl->planex = 0; + vl->planey = -0.66; + } + else if (dir == 'W') + { + vl->dirx = 0; + vl->diry = -1; + vl->planex = -0.66; + vl->planey = 0; + } +} + +char ft_initplayer(t_varlist *vl, char dir, int x, int y) +{ + vl->posx = x + 0.5; + vl->posy = y + 0.5; + if (dir == 'N' || dir == 'E') + ft_northeast(vl, dir); + if (dir == 'S' || dir == 'W') + ft_southwest(vl, dir); + return ('0'); +} diff --git a/src/parse/sprite.c b/src/parse/sprite.c index a0c6fb0..8eaa716 100644 --- a/src/parse/sprite.c +++ b/src/parse/sprite.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */ -/* Updated: 2023/11/05 18:50:53 by houtworm ######## odam.nl */ +/* Updated: 2023/11/07 05:54:19 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ char ft_addwalktroughdecor(t_varlist *vl, int x, int y, int number) vl->sprite[vl->spritecount].y = y + 0.5; vl->sprite[vl->spritecount].number = number; vl->sprite[vl->spritecount].type = 1; + vl->sprite[vl->spritecount].status = 0; vl->spritecount++; return ('0'); } @@ -28,6 +29,7 @@ char ft_addsoliddecor(t_varlist *vl, int x, int y, int number) vl->sprite[vl->spritecount].y = y + 0.5; vl->sprite[vl->spritecount].number = number; vl->sprite[vl->spritecount].type = 1; + vl->sprite[vl->spritecount].status = 0; vl->spritecount++; return ('2'); } @@ -38,6 +40,7 @@ char ft_addpickup(t_varlist *vl, int x, int y, int number) vl->sprite[vl->spritecount].y = y + 0.5; vl->sprite[vl->spritecount].number = number; vl->sprite[vl->spritecount].type = 2; + vl->sprite[vl->spritecount].status = 0; if (number < 4) vl->tottreasure++; vl->spritecount++; @@ -50,7 +53,8 @@ char ft_addenemy(t_varlist *vl, int x, int y, int number) vl->sprite[vl->spritecount].y = y + 0.5; vl->sprite[vl->spritecount].number = number; vl->sprite[vl->spritecount].type = 3; + vl->sprite[vl->spritecount].status = 0; vl->enemies++; vl->spritecount++; - return ('2'); + return ('3'); } diff --git a/src/sprite/check.c b/src/sprite/check.c deleted file mode 100644 index c715419..0000000 --- a/src/sprite/check.c +++ /dev/null @@ -1,65 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* check.c :+: :+: */ -/* +:+ */ -/* By: houtworm +#+ */ -/* +#+ */ -/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/11/05 21:27:51 by houtworm ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "../../cub3d.h" - -void ft_sortsprites(t_varlist *vl) -{ - int i; - t_sprite temp; - - i = 0; - while (vl->spritecount > i) - { - vl->sprite[i].distance = pow((vl->posx - vl->sprite[i].x), 2) + pow((vl->posy - vl->sprite[i].y), 2); - i++; - } - while (i) - { - if (vl->sprite[i].distance > vl->sprite[i - 1].distance) - { - temp = vl->sprite[i]; - vl->sprite[i] = vl->sprite[i - 1]; - vl->sprite[i - 1] = temp; - } - i--; - } -} - -mlx_texture_t *ft_getsprite(t_varlist *vl, int i) -{ - if (vl->sprite[i].type == 1) - return (vl->decotext[vl->sprite[i].number]); - if (vl->sprite[i].type == 2) - return (vl->picktext[vl->sprite[i].number]); - return (vl->nazitext[vl->sprite[i].number]); -} - -void ft_drawsprites(t_varlist *vl) -{ - int i; - int x; - t_draw *draw; - - ft_sortsprites(vl); - i = 0; - while (vl->spritecount > i) - { - draw = ft_initdrawsprite(vl, i); - ft_getdrawstartend(vl, draw); - x = draw->drawstartx; - vl->temptext = ft_getsprite(vl, i); - ft_drawsprite(vl, draw, x); - free(draw); - i++; - } -} diff --git a/src/sprite/draw.c b/src/sprite/draw.c deleted file mode 100644 index 9edc4ed..0000000 --- a/src/sprite/draw.c +++ /dev/null @@ -1,91 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* draw.c :+: :+: */ -/* +:+ */ -/* By: houtworm +#+ */ -/* +#+ */ -/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/11/05 11:04:09 by houtworm ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "../../cub3d.h" - -void ft_drawspriteline(t_varlist *vl, t_draw *draw, int y, int x) -{ - int d; - uint8_t *texel; - uint32_t color; - - d = (y - draw->vmovescreen) * 256 - vl->h * 128 + draw->spriteheight * 128; - draw->texy = (d * 64) / draw->spriteheight / 256; - if (draw->texy < 0) - draw->texy = 0; - if (draw->texy > 64) - draw->texy = 64; - if (draw->texx < 0) - draw->texx = 0; - if (draw->texx > 64) - draw->texx = 64; - texel = &vl->temptext->pixels[(vl->temptext->width * draw->texy + draw->texx) * 4]; - color = texel[0] << 24 | texel[1] << 16 | texel[2] << 8 | texel[3]; - if (color != 0x980088FF) - mlx_put_pixel(vl->img, x, y, color); -} - -void ft_drawsprite(t_varlist *vl, t_draw *draw, int x) -{ - int y; - - while (x < draw->drawendx) - { - draw->texx = (int)(256 * (x - (-draw->spritewidth / 2 + draw->spritescreen)) * 64 / draw->spritewidth) / 256; - if (draw->transformy > 0 && x > 0 && x < vl->w && draw->transformy < vl->distance[x] + 0.7) - { - y = draw->drawstarty; - while (y < draw->drawendy) - { - ft_drawspriteline(vl, draw, y, x); - y++; - } - } - x++; - } -} - -t_draw *ft_initdrawsprite(t_varlist *vl, int i) -{ - double spritex; - double spritey; - double invdet; - t_draw *draw; - - draw = ft_calloc(12, 8); - spritex = vl->sprite[i].x - vl->posx; - spritey = vl->sprite[i].y - vl->posy; - invdet = 1.0 / (vl->planex * vl->diry - vl->dirx * vl->planey); - draw->transformx = invdet * (vl->diry * spritex - vl->dirx * spritey); - draw->transformy = invdet * (-vl->planey * spritex + vl->planex * spritey); - draw->spritescreen = (vl->w / 2) * (1 + draw->transformx / draw->transformy); - draw->vmovescreen = vl->vaim + vl->jump / draw->transformy; - draw->spriteheight = fabs((vl->h / draw->transformy)); - return (draw); -} - -void ft_getdrawstartend(t_varlist *vl, t_draw *draw) -{ - draw->drawstarty = -draw->spriteheight / 2 + vl->h / 2 + draw->vmovescreen; - if (draw->drawstarty < 0) - draw->drawstarty = 0; - draw->drawendy = draw->spriteheight / 2 + vl->h / 2 + draw->vmovescreen; - if (draw->drawendy >= vl->h) - draw->drawendy = vl->h - 1; - draw->spritewidth = abs((int)(vl->h / draw->transformy)); - draw->drawstartx = -draw->spritewidth / 2 + draw->spritescreen; - if (draw->drawstartx < 0) - draw->drawstartx = 0; - draw->drawendx = draw->spritewidth / 2 + draw->spritescreen; - if (draw->drawendx >= vl->w) - draw->drawendx = vl->w - 1; -}