From cb24bee3f80fe86bbcfa365a478ab42260d958b9 Mon Sep 17 00:00:00 2001 From: SlyVTT Date: Mon, 14 Aug 2023 20:46:51 +0200 Subject: [PATCH] implementation of map change during gameplay by crossing map borders --- src/main.c | 6 +++--- src/map.c | 6 +++--- src/player.c | 30 ++++++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index f448a84..09153bb 100644 --- a/src/main.c +++ b/src/main.c @@ -32,11 +32,11 @@ extern Map *worldRPG[]; /* Game data (defined in "game.h")*/ Game game = { NULL, - {10*PXSIZE, 48*PXSIZE, 0, 0, 0, 0, 100, SPEED}, + {10*PXSIZE, 48*PXSIZE, 0, 0, 10*PXSIZE, 48*PXSIZE, 100, SPEED}, false, false, false, 0 /* debug variables*/ - , false, false + , false, true }; /* screen capture management code */ @@ -93,7 +93,7 @@ int main(void) { } timer_start(timer); - game.map_level = worldRPG[2]; + game.map_level = worldRPG[0]; #if USB_FEATURE diff --git a/src/map.c b/src/map.c index e04091b..df771d0 100644 --- a/src/map.c +++ b/src/map.c @@ -199,7 +199,7 @@ short int get_tile(Game *game, int x, int y, int l) { /* Get the tile at (x, y) on layer l. Returns the tile ID or MAP_OUTSIDE if * she's not found. */ - return x>=0 && x < map_level->w && y>=0 && y < map_level->h ? + return (x>=0 && x < map_level->w && y>=0 && y < map_level->h) ? map_level->layers[l][y * map_level->w + x] : MAP_OUTSIDE; } @@ -208,7 +208,7 @@ short int get_walkable(Game *game, int x, int y) { Map *map_level = game->map_level; /* Get the tile at (x, y). Returns the tile ID or MAP_OUTSIDE if she's not * found. */ - return x>=0 && x < map_level->w && y>=0 && y < map_level->h ? + return (x>=0 && x < map_level->w && y>=0 && y < map_level->h) ? map_level->walkable[y * map_level->w + x] : MAP_OUTSIDE; } @@ -231,7 +231,7 @@ Map *get_map_for_coordinates( Game *game, int x, int y ) i++; current = worldRPG[i]; } - while (current!=NULL) + while (current!=NULL); /* else we return NULL cause the point is a not within a map */ return NULL; diff --git a/src/player.c b/src/player.c index 48f0f4b..d495c1b 100644 --- a/src/player.c +++ b/src/player.c @@ -41,6 +41,7 @@ void player_move(Game *game, Direction direction) { /* If the player will collide with a hard tile or if the will go outside of * the map. */ + if(player_collision(game, direction, P_CENTER)){ /* If the will collide with the center of the player. */ @@ -102,7 +103,32 @@ bool player_collision(Game *game, Direction direction, int player_tile_x = player->x+dx; int player_tile_y = player->y+dy; - /* Handle a negative position diffrently than a positive one. */ + /* check where the player is expected to go on the next move */ + /* if outside the map, we check if there is a map on the other */ + /* side of the current map*/ + if (get_walkable(game, player_tile_x, player_tile_y) == MAP_OUTSIDE) + { + // we compute the expected world coordinates accordingly + // while taking care of the scaling between fx and cg models (PXSIZE) + int worldX = (player->wx+dx) / PXSIZE; + int worldY = (player->wy+dy) / PXSIZE; + Map *map = get_map_for_coordinates(game, worldX, worldY ); + if (map!=NULL && map!=game->map_level) + { + game->map_level = map; + + player->wx = worldX * PXSIZE; + player->wy = worldY * PXSIZE; + + player->x = (worldX - map->xmin ) * PXSIZE; + player->y = (worldY - map->ymin ) * PXSIZE; + + return false; + } + } + + + /* Handle a negative position differently than a positive one. */ if(player_tile_x < 0) player_tile_x = player_tile_x/T_WIDTH-1; else player_tile_x = player_tile_x/T_WIDTH; @@ -111,7 +137,7 @@ bool player_collision(Game *game, Direction direction, int on_walkable = get_walkable(game, player_tile_x, player_tile_y); - int speed = on_walkable >= 0 && on_walkable < WALKABLE_TILE_MAX ? + int speed = (on_walkable >= 0 && on_walkable < WALKABLE_TILE_MAX) ? walkable_speed[on_walkable] : 0; /* if he's on a hard tile */