From 75e7ef772d3798a42677ce40ffc31c858914fd3e Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Sun, 28 May 2023 22:12:33 +0200 Subject: [PATCH] Fixed vmap, added a function to check for collisions. --- microfx_src/Makefile | 3 ++- microfx_src/include/microfx/ext/gametools.h | 18 ++++++++++++------ microfx_src/src/ext/gametools/collisions.c | 10 ++++++++++ microfx_src/src/ext/gametools/map.c | 12 ++++++------ template/lib/libMicrofx.a | Bin 16566 -> 17446 bytes 5 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 microfx_src/src/ext/gametools/collisions.c diff --git a/microfx_src/Makefile b/microfx_src/Makefile index 328275f..6bdc7f8 100644 --- a/microfx_src/Makefile +++ b/microfx_src/Makefile @@ -14,7 +14,8 @@ SRC = src/start.c \ src/memory.c \ src/rtc.c \ src/ext/img.c \ - src/ext/gametools/map.c + src/ext/gametools/map.c \ + src/ext/gametools/collisions.c OBJ = $(SRC:src/%=build/%.o) CFLAGS = -Os -Wall -Wextra -Wpedantic -Werror -std=c89 diff --git a/microfx_src/include/microfx/ext/gametools.h b/microfx_src/include/microfx/ext/gametools.h index 18bf222..6f0f434 100644 --- a/microfx_src/include/microfx/ext/gametools.h +++ b/microfx_src/include/microfx/ext/gametools.h @@ -22,14 +22,10 @@ typedef struct { /* Prototypes */ -/* void vmap(int padx, int pady, int w, int h, int sx, int sy, MMap *map); +/* void vmap(int sx, int sy, MMap *map); Draws a map contained in a MMap struct. -dmap draw the map from sx, sy, at padx, pady on the screen with width w and -height h on screen. -The map should be the first thing to be drawn, because the map is not perfectly -drawn at (padx, pady), and vplayer and vitem use some variables that are updated -when calling vmap. +vmap draw the map with the player at sx, sy. */ void vmap(int sx, int sy, MMap *map); @@ -54,4 +50,14 @@ anim_frame is the frame of the animation of item that should be drawn. void vitem(MMap *map, int x, int y, int w, int h, unsigned char **item, int anim_frame); +/******* COLLISIONS *******/ + +/* int vget_tile_at_point(MMap *map, int x, int y); + +Get the tile located at x, y on MMap map map. +Returns the tile number or -1 if the tile is outside of the map. +*/ + +int vget_tile_at_point(MMap *map, int x, int y); + #endif diff --git a/microfx_src/src/ext/gametools/collisions.c b/microfx_src/src/ext/gametools/collisions.c new file mode 100644 index 0000000..cda5a45 --- /dev/null +++ b/microfx_src/src/ext/gametools/collisions.c @@ -0,0 +1,10 @@ +#include "../../../include/microfx/ext/gametools.h" + +int vget_tile_at_point(MMap *map, int x, int y) { + /* tx and ty contain the tile position. */ + int tx = x/map->tw, ty = y/map->th; + if(tx>=0 && tx < map->w && ty>=0 && ty < map->h){ + return (int)map->map[ty*map->w+tx]; + } + return -1; +} diff --git a/microfx_src/src/ext/gametools/map.c b/microfx_src/src/ext/gametools/map.c index f937e56..e3ff08b 100644 --- a/microfx_src/src/ext/gametools/map.c +++ b/microfx_src/src/ext/gametools/map.c @@ -27,8 +27,8 @@ void vmap(int sx, int sy, MMap *map) { }else if(sx+w/2>mw){ /* If I can't center the player because I'm near the right border of the map. */ - map->px = sx-(mw-w/2); - sx = mw-w/2; + map->px = sx-(mw-w); + sx = mw-w; }else{ /* I can center the player. */ sx = sx-w/2; @@ -43,8 +43,8 @@ void vmap(int sx, int sy, MMap *map) { }else if(sy+h/2>mh){ /* If I can't center the player because I'm near the bottom border of the map. */ - map->py = sy-(mh-h/2); - sy = mh-h/2; + map->py = sy-(mh-h); + sy = mh-h; }else{ /* I can center the player. */ sy = sy-h/2; @@ -76,8 +76,8 @@ void vmap(int sx, int sy, MMap *map) { } void vplayer(MMap *map, unsigned char **player_sprites, int anim_frame) { - simage(map->px, map->py, map->pw, map->ph, player_sprites[anim_frame], - SNORMAL); + simage(map->px-map->pw/2, map->py-map->ph/2, map->pw, map->ph, + player_sprites[anim_frame], SNORMAL); } void vitem(MMap *map, int x, int y, int w, int h, unsigned char **item, diff --git a/template/lib/libMicrofx.a b/template/lib/libMicrofx.a index 5f9dbbb235fe0ed977f0bb35a8c72e32a419d358..270804c137dc118b4aeb6cba6c6a981c90e3d03d 100644 GIT binary patch delta 1170 zcmdni$hfS7ae_>}v4M%HnT3LafdU91lL`ukrWQtEVTA-P1_p+71_rhw2BO7>LEPd<+aQtZc_xfVa32; zy$gXI-fe!sSjD0jUzVO)5?_*;lNz5`5?_#?nODM4T#{H+qL-|fuMde%Lo*9gNW@Rl z6yMyp&RZh)o|j5�e6zhS zHwR2pfax>1CT_9KK^7@6^MF<{8<`tT{->pA?@7`QXOZJuSz%*g8@#K6D^Ovsu*YR%+TcEJKG7#KJ}B0#|S1IV7Ns8r9y z_-FDtX=PRwprFBIJ{=_{CWFcEl+-{}o;jGZQ&wYRaskS3n7mF$iH+$CSm`Qm$n40_$ro5`$llNP@IMoK zLJ^~s!5l`JM41F(f3?hSpa59^^Wp!=4wkinz$6O>A<(c;g7O&zKn!G;GGI$d@$vD+ zDVb%(nZ{6k0h8sFni)eT&y!bX^qDMYuFlv23@i|t2WHzTt254-Y-g>`Dg)G^JGoCm LSu_I3!U!Y)(#;o^ delta 820 zcmZ41!MLrFae_>}k%hUbsgZ($fdU91lL`ukrluBPVTA-P1_p*S1_rh$2}m7>LC(-xwHRSm7800|+ah!jCn84n@N5V2&;y0|P18)Qo|_LWzOF zdJzn7e!^JAGP#INgbfm*lP`#gZ(hun&jRB+=!$RF=bp_CIO%`+8$rH2glKja17I1-W>1`d!25HQ{VvL{b8t7l@oGx?vk5)(7a z