Better linkedlist clearing
Fixed a projectile update bug due to invalid map pointer. Cleaned up some of the "clear stuff from linkedlist" functions to a nicer implementation. Perhaps not as efficient but certainly nicer.
This commit is contained in:
parent
d6c21745cd
commit
d983318453
19
src/main.c
19
src/main.c
|
@ -66,7 +66,6 @@ static double renderScale = 1.0;
|
||||||
static Menu *mainMenu = NULL;
|
static Menu *mainMenu = NULL;
|
||||||
static Menu *inGameMenu = NULL;
|
static Menu *inGameMenu = NULL;
|
||||||
static Timer *menuTimer = NULL;
|
static Timer *menuTimer = NULL;
|
||||||
static UpdateData gUpdateData;
|
|
||||||
static GameState gGameState;
|
static GameState gGameState;
|
||||||
static Camera gCamera;
|
static Camera gCamera;
|
||||||
static SDL_Rect gameViewport;
|
static SDL_Rect gameViewport;
|
||||||
|
@ -209,9 +208,6 @@ startGame(void *unused)
|
||||||
gPlayer = player_create(WARRIOR, gRenderer);
|
gPlayer = player_create(WARRIOR, gRenderer);
|
||||||
mixer_play_music(GAME_MUSIC0 + get_random(2));
|
mixer_play_music(GAME_MUSIC0 + get_random(2));
|
||||||
resetGame();
|
resetGame();
|
||||||
gUpdateData.player = gPlayer;
|
|
||||||
gUpdateData.map = gMap;
|
|
||||||
gUpdateData.matrix = gRoomMatrix;
|
|
||||||
gui_clear_message_log();
|
gui_clear_message_log();
|
||||||
gui_log("The Dungeon Crawl begins!");
|
gui_log("The Dungeon Crawl begins!");
|
||||||
gui_event_message("Welcome to the dungeon!");
|
gui_event_message("Welcome to the dungeon!");
|
||||||
|
@ -447,9 +443,20 @@ check_next_level(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
populateUpdateData(UpdateData *data, float deltatime)
|
||||||
|
{
|
||||||
|
data->player = gPlayer;
|
||||||
|
data->map = gMap;
|
||||||
|
data->matrix = gRoomMatrix;
|
||||||
|
data->deltatime = deltatime;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
run_game(void)
|
run_game(void)
|
||||||
{
|
{
|
||||||
|
static UpdateData updateData;
|
||||||
|
|
||||||
map_clear_dead_monsters(gMap, gPlayer);
|
map_clear_dead_monsters(gMap, gPlayer);
|
||||||
map_clear_collected_items(gMap);
|
map_clear_collected_items(gMap);
|
||||||
roommatrix_populate_from_map(gRoomMatrix, gMap);
|
roommatrix_populate_from_map(gRoomMatrix, gMap);
|
||||||
|
@ -458,9 +465,10 @@ run_game(void)
|
||||||
|
|
||||||
roommatrix_build_lightmap(gRoomMatrix);
|
roommatrix_build_lightmap(gRoomMatrix);
|
||||||
|
|
||||||
|
populateUpdateData(&updateData, deltaTime);
|
||||||
gui_update_player_stats(gGui, gPlayer, gMap, gRenderer);
|
gui_update_player_stats(gGui, gPlayer, gMap, gRenderer);
|
||||||
particle_engine_update(deltaTime);
|
particle_engine_update(deltaTime);
|
||||||
player_update(&gUpdateData);
|
player_update(&updateData);
|
||||||
|
|
||||||
roommatrix_update_with_player(gRoomMatrix, gPlayer);
|
roommatrix_update_with_player(gRoomMatrix, gPlayer);
|
||||||
if (currentTurn == PLAYER) {
|
if (currentTurn == PLAYER) {
|
||||||
|
@ -594,7 +602,6 @@ void run(void)
|
||||||
oldTime = currentTime;
|
oldTime = currentTime;
|
||||||
currentTime = SDL_GetTicks();
|
currentTime = SDL_GetTicks();
|
||||||
deltaTime = (float) ((currentTime - oldTime) / 1000.0);
|
deltaTime = (float) ((currentTime - oldTime) / 1000.0);
|
||||||
gUpdateData.deltatime = deltaTime;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
56
src/map.c
56
src/map.c
|
@ -97,62 +97,34 @@ void map_add_decoration(Map *map, Position *tile_pos, MapTile *tile)
|
||||||
void
|
void
|
||||||
map_clear_dead_monsters(Map *map, Player *player)
|
map_clear_dead_monsters(Map *map, Player *player)
|
||||||
{
|
{
|
||||||
LinkedList *last, *current, *next;
|
LinkedList *cleared = linkedlist_create();
|
||||||
|
|
||||||
last = NULL;
|
|
||||||
current = map->monsters;
|
|
||||||
|
|
||||||
while (current != NULL) {
|
|
||||||
if (((Monster*) current->data)->stats.hp <= 0) {
|
|
||||||
if (last == NULL)
|
|
||||||
map->monsters = current->next;
|
|
||||||
else
|
|
||||||
last->next = current->next;
|
|
||||||
|
|
||||||
Monster *monster = current->data;
|
|
||||||
|
|
||||||
|
while (map->monsters) {
|
||||||
|
Monster *monster = linkedlist_pop(&map->monsters);
|
||||||
|
if (monster->stats.hp <= 0) {
|
||||||
// Loot drops
|
// Loot drops
|
||||||
monster_drop_loot(monster, map, player);
|
monster_drop_loot(monster, map, player);
|
||||||
|
|
||||||
monster_destroy(monster);
|
monster_destroy(monster);
|
||||||
current->data = NULL;
|
} else {
|
||||||
next = current->next;
|
linkedlist_append(&cleared, monster);
|
||||||
current->next = NULL;
|
|
||||||
linkedlist_destroy(¤t);
|
|
||||||
current = next;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
last = current;
|
|
||||||
current = current->next;
|
|
||||||
}
|
}
|
||||||
|
map->monsters = cleared;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
map_clear_collected_items(Map *map)
|
map_clear_collected_items(Map *map)
|
||||||
{
|
{
|
||||||
LinkedList *last, *current, *next;
|
LinkedList *filtered = linkedlist_create();
|
||||||
|
|
||||||
last = NULL;
|
while (map->items) {
|
||||||
current = map->items;
|
Item *item = linkedlist_pop(&map->items);
|
||||||
|
if (item->collected)
|
||||||
while (current != NULL) {
|
item_destroy(item);
|
||||||
if (((Item*) current->data)->collected) {
|
|
||||||
if (last == NULL)
|
|
||||||
map->items = current->next;
|
|
||||||
else
|
else
|
||||||
last->next = current->next;
|
linkedlist_append(&filtered, item);
|
||||||
|
|
||||||
item_destroy(current->data);
|
|
||||||
current->data = NULL;
|
|
||||||
next = current->next;
|
|
||||||
current->next = NULL;
|
|
||||||
linkedlist_destroy(¤t);
|
|
||||||
current = next;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
last = current;
|
|
||||||
current = current->next;
|
|
||||||
}
|
}
|
||||||
|
map->items = filtered;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
Loading…
Reference in New Issue