diff --git a/src/main.c b/src/main.c index ce71196..2968d55 100644 --- a/src/main.c +++ b/src/main.c @@ -43,6 +43,11 @@ #include "mixer.h" #include "random.h" +typedef enum Turn_t { + PLAYER, + MONSTER +} Turn; + static SDL_Window *gWindow = NULL; static SDL_Renderer *gRenderer = NULL; static Player *gPlayer = NULL; @@ -62,6 +67,7 @@ static SDL_Rect gameViewport; static SDL_Rect bottomGuiViewport; static SDL_Rect rightGuiViewport; static SDL_Rect menuViewport; +static Turn currentTurn = PLAYER; static SDL_Color C_MENU_DEFAULT = { 255, 255, 0, 0 }; static SDL_Color C_MENU_HOVER = { 255, 0, 0, 0 }; @@ -362,9 +368,10 @@ handle_events(void) continue; if (gGameState == PLAYING) { - gPlayer->handle_event(gPlayer, - gRoomMatrix, - &event); + if (currentTurn == PLAYER) + gPlayer->handle_event(gPlayer, + gRoomMatrix, + &event); camera_follow_position(&gCamera, &gPlayer->sprite->pos); map_set_current_room(gMap, &gPlayer->sprite->pos); roommatrix_handle_event(gRoomMatrix, &event); @@ -421,10 +428,16 @@ run_game(void) gui_update_player_stats(gGui, gPlayer, gMap, gRenderer); particle_engine_update(deltaTime); - if (gPlayer->steps >= gPlayer->stats.speed) { - player_reset_steps(gPlayer); - roommatrix_update_with_player(gRoomMatrix, gPlayer); - map_move_monsters(gMap, gRoomMatrix); + roommatrix_update_with_player(gRoomMatrix, gPlayer); + if (currentTurn == PLAYER) { + if (gPlayer->steps >= gPlayer->stats.speed) { + currentTurn = MONSTER; + player_reset_steps(gPlayer); + } + } + if (currentTurn == MONSTER) { + if (map_move_monsters(gMap, gRoomMatrix)) + currentTurn = PLAYER; } SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 0); diff --git a/src/map.c b/src/map.c index 952d35a..e5f5f93 100644 --- a/src/map.c +++ b/src/map.c @@ -51,6 +51,7 @@ Map* map_create() map->items = linkedlist_create(); map->currentRoom = (Position) { 0, 0 }; map->renderTimer = timer_create(); + map->monsterMoveTimer = timer_create(); map->level = 1; for (i=0; i < MAP_H_ROOM_COUNT; ++i) { @@ -177,17 +178,30 @@ map_add_monster(Map *map, Monster *m) linkedlist_append(&map->monsters, m); } -void +bool map_move_monsters(Map *map, RoomMatrix *rm) { LinkedList *m = map->monsters; + bool allDone = true; + + if (timer_started(map->monsterMoveTimer) + && timer_get_ticks(map->monsterMoveTimer) < 100) + return false; + while (m) { Monster *monster = m->data; m = m->next; if (!position_in_room(&monster->sprite->pos, &map->currentRoom)) continue; - monster_move(monster, rm); + allDone = allDone && monster_move(monster, rm); } + + if (allDone) + timer_stop(map->monsterMoveTimer); + else + timer_start(map->monsterMoveTimer); + + return allDone; } int map_add_texture(Map *map, const char *path, SDL_Renderer *renderer) diff --git a/src/map.h b/src/map.h index d2bb874..ffc3e68 100644 --- a/src/map.h +++ b/src/map.h @@ -53,31 +53,44 @@ typedef struct Map_t { LinkedList *items; Position currentRoom; Timer *renderTimer; + Timer *monsterMoveTimer; int level; } Map; -Map* map_create(void); +Map* +map_create(void); -int map_add_texture(Map*, const char *path, SDL_Renderer*); +int +map_add_texture(Map*, const char *path, SDL_Renderer*); -void map_add_tile(Map *map, Position *tile_pos, MapTile*); +void +map_add_tile(Map *map, Position *tile_pos, MapTile*); -void map_add_decoration(Map *map, Position *tile_pos, MapTile*); +void +map_add_decoration(Map *map, Position *tile_pos, MapTile*); -Texture* map_add_monster_texture(Map*, const char *path, SDL_Renderer*); +Texture* +map_add_monster_texture(Map*, const char *path, SDL_Renderer*); -void map_add_monster(Map*, Monster*); +void +map_add_monster(Map*, Monster*); -void map_move_monsters(Map*, RoomMatrix*); +bool +map_move_monsters(Map*, RoomMatrix*); -void map_clear_dead_monsters(Map*); +void +map_clear_dead_monsters(Map*); -void map_clear_collected_items(Map*); +void +map_clear_collected_items(Map*); -void map_render(Map*, Camera*); +void +map_render(Map*, Camera*); -void map_set_current_room(Map*, Position*); +void +map_set_current_room(Map*, Position*); -void map_destroy(Map*); +void +map_destroy(Map*); #endif // MAP_H_ diff --git a/src/monster.c b/src/monster.c index a5bf184..b9007d7 100644 --- a/src/monster.c +++ b/src/monster.c @@ -60,6 +60,7 @@ monster_create(SDL_Renderer *renderer) m->state.current = m->state.normal; m->label = NULL; m->lclabel = NULL; + m->steps = 0; monster_load_texts(m, renderer); @@ -216,7 +217,7 @@ monster_coward_walk(Monster *m, RoomMatrix *rm) } } -void +bool monster_move(Monster *m, RoomMatrix *rm) { Position monsterRoomPos; @@ -242,6 +243,13 @@ monster_move(Monster *m, RoomMatrix *rm) monsterRoomPos = position_to_matrix_coords(&m->sprite->pos); rm->spaces[monsterRoomPos.x][monsterRoomPos.y].occupied = true; rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = m; + + m->steps++; + if (m->steps >= m->stats.speed) { + m->steps = 0; + return true; + } + return false; } void diff --git a/src/monster.h b/src/monster.h index 54ee984..4e74397 100644 --- a/src/monster.h +++ b/src/monster.h @@ -40,6 +40,7 @@ typedef struct Monster_t { ActionText *missText; Stats stats; State state; + unsigned int steps; } Monster; Monster* monster_create(SDL_Renderer*); @@ -47,7 +48,7 @@ Monster* monster_create(SDL_Renderer*); void monster_update_pos(Monster*, Position); -void +bool monster_move(Monster*, RoomMatrix*); void