Fix camera when at edge of map

This commit is contained in:
Camden Dixie O'Brien 2024-12-29 18:49:04 +00:00
parent 31f7895bf3
commit e7a19b7d91

View File

@ -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);