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 *inGameMenu = NULL;
|
||||
static Timer *menuTimer = NULL;
|
||||
static UpdateData gUpdateData;
|
||||
static GameState gGameState;
|
||||
static Camera gCamera;
|
||||
static SDL_Rect gameViewport;
|
||||
|
@ -209,9 +208,6 @@ startGame(void *unused)
|
|||
gPlayer = player_create(WARRIOR, gRenderer);
|
||||
mixer_play_music(GAME_MUSIC0 + get_random(2));
|
||||
resetGame();
|
||||
gUpdateData.player = gPlayer;
|
||||
gUpdateData.map = gMap;
|
||||
gUpdateData.matrix = gRoomMatrix;
|
||||
gui_clear_message_log();
|
||||
gui_log("The Dungeon Crawl begins!");
|
||||
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
|
||||
run_game(void)
|
||||
{
|
||||
static UpdateData updateData;
|
||||
|
||||
map_clear_dead_monsters(gMap, gPlayer);
|
||||
map_clear_collected_items(gMap);
|
||||
roommatrix_populate_from_map(gRoomMatrix, gMap);
|
||||
|
@ -458,9 +465,10 @@ run_game(void)
|
|||
|
||||
roommatrix_build_lightmap(gRoomMatrix);
|
||||
|
||||
populateUpdateData(&updateData, deltaTime);
|
||||
gui_update_player_stats(gGui, gPlayer, gMap, gRenderer);
|
||||
particle_engine_update(deltaTime);
|
||||
player_update(&gUpdateData);
|
||||
player_update(&updateData);
|
||||
|
||||
roommatrix_update_with_player(gRoomMatrix, gPlayer);
|
||||
if (currentTurn == PLAYER) {
|
||||
|
@ -594,7 +602,6 @@ void run(void)
|
|||
oldTime = currentTime;
|
||||
currentTime = SDL_GetTicks();
|
||||
deltaTime = (float) ((currentTime - oldTime) / 1000.0);
|
||||
gUpdateData.deltatime = deltaTime;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
58
src/map.c
58
src/map.c
|
@ -97,62 +97,34 @@ void map_add_decoration(Map *map, Position *tile_pos, MapTile *tile)
|
|||
void
|
||||
map_clear_dead_monsters(Map *map, Player *player)
|
||||
{
|
||||
LinkedList *last, *current, *next;
|
||||
|
||||
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;
|
||||
LinkedList *cleared = linkedlist_create();
|
||||
|
||||
while (map->monsters) {
|
||||
Monster *monster = linkedlist_pop(&map->monsters);
|
||||
if (monster->stats.hp <= 0) {
|
||||
// Loot drops
|
||||
monster_drop_loot(monster, map, player);
|
||||
|
||||
monster_destroy(monster);
|
||||
current->data = NULL;
|
||||
next = current->next;
|
||||
current->next = NULL;
|
||||
linkedlist_destroy(¤t);
|
||||
current = next;
|
||||
continue;
|
||||
} else {
|
||||
linkedlist_append(&cleared, monster);
|
||||
}
|
||||
last = current;
|
||||
current = current->next;
|
||||
}
|
||||
map->monsters = cleared;
|
||||
}
|
||||
|
||||
void
|
||||
map_clear_collected_items(Map *map)
|
||||
{
|
||||
LinkedList *last, *current, *next;
|
||||
LinkedList *filtered = linkedlist_create();
|
||||
|
||||
last = NULL;
|
||||
current = map->items;
|
||||
|
||||
while (current != NULL) {
|
||||
if (((Item*) current->data)->collected) {
|
||||
if (last == NULL)
|
||||
map->items = current->next;
|
||||
else
|
||||
last->next = current->next;
|
||||
|
||||
item_destroy(current->data);
|
||||
current->data = NULL;
|
||||
next = current->next;
|
||||
current->next = NULL;
|
||||
linkedlist_destroy(¤t);
|
||||
current = next;
|
||||
continue;
|
||||
}
|
||||
last = current;
|
||||
current = current->next;
|
||||
while (map->items) {
|
||||
Item *item = linkedlist_pop(&map->items);
|
||||
if (item->collected)
|
||||
item_destroy(item);
|
||||
else
|
||||
linkedlist_append(&filtered, item);
|
||||
}
|
||||
map->items = filtered;
|
||||
}
|
||||
|
||||
void
|
||||
|
|
Loading…
Reference in New Issue