From e7a19b7d91ebf94b2a354271f7de968a21ca6f1c Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sun, 29 Dec 2024 18:49:04 +0000 Subject: [PATCH] Fix camera when at edge of map --- app/main.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/app/main.c b/app/main.c index 4e2551a..5d8a91b 100644 --- a/app/main.c +++ b/app/main.c @@ -25,6 +25,10 @@ #define MAPHEIGHT 64 #define MAPSHIFTX 32 #define MAPSHIFTY 32 +#define MAPMINX (TILESIZE * (-32)) +#define MAPMAXX (TILESIZE * 32) +#define MAPMINY (TILESIZE * (-32)) +#define MAPMAXY (TILESIZE * 32) #define TSASSET "/overworld.png" #define TSCOLS 40 @@ -75,6 +79,11 @@ static inline double mag(dvec_t v) return sqrt(v.x * v.x + v.y * v.y); } +static inline double dot(dvec_t v, dvec_t u) +{ + return v.x * u.x + v.y * u.y; +} + static inline unsigned tileat(double x, double y) { const unsigned row = (unsigned)floor(x / TILESIZE) + MAPSHIFTX; @@ -317,9 +326,15 @@ int main(int argc, char *argv[]) .x = ppos.x - (vpos.x + VIEWWIDTH / 2), .y = ppos.y - (vpos.y + VIEWHEIGHT / 2), }; - if (mag(pvdisp) > 72) { - vpos.x += (double)dt / 1000.0 * pvel.x; - vpos.y += (double)dt / 1000.0 * pvel.y; + if (mag(pvdisp) > 72 && dot(pvdisp, pvel) > 0) { + const double nextx = vpos.x + (double)dt / 1000.0 * pvel.x; + const double nexty = vpos.y + (double)dt / 1000.0 * pvel.y; + const bool validx = nextx >= MAPMINX && nextx < MAPMAXX; + const bool validy = nexty >= MAPMINY && nexty < MAPMAXY; + if (validx && validy) { + vpos.x = nextx; + vpos.y = nexty; + } } SDL_RenderClear(renderer);