More defined turns

This commit is contained in:
Linus_Probert 2018-02-19 15:09:04 +01:00
parent d5914071d4
commit 46e03af0b8
5 changed files with 72 additions and 23 deletions

View File

@ -43,6 +43,11 @@
#include "mixer.h" #include "mixer.h"
#include "random.h" #include "random.h"
typedef enum Turn_t {
PLAYER,
MONSTER
} Turn;
static SDL_Window *gWindow = NULL; static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL; static SDL_Renderer *gRenderer = NULL;
static Player *gPlayer = NULL; static Player *gPlayer = NULL;
@ -62,6 +67,7 @@ static SDL_Rect gameViewport;
static SDL_Rect bottomGuiViewport; static SDL_Rect bottomGuiViewport;
static SDL_Rect rightGuiViewport; static SDL_Rect rightGuiViewport;
static SDL_Rect menuViewport; static SDL_Rect menuViewport;
static Turn currentTurn = PLAYER;
static SDL_Color C_MENU_DEFAULT = { 255, 255, 0, 0 }; static SDL_Color C_MENU_DEFAULT = { 255, 255, 0, 0 };
static SDL_Color C_MENU_HOVER = { 255, 0, 0, 0 }; static SDL_Color C_MENU_HOVER = { 255, 0, 0, 0 };
@ -362,9 +368,10 @@ handle_events(void)
continue; continue;
if (gGameState == PLAYING) { if (gGameState == PLAYING) {
gPlayer->handle_event(gPlayer, if (currentTurn == PLAYER)
gRoomMatrix, gPlayer->handle_event(gPlayer,
&event); gRoomMatrix,
&event);
camera_follow_position(&gCamera, &gPlayer->sprite->pos); camera_follow_position(&gCamera, &gPlayer->sprite->pos);
map_set_current_room(gMap, &gPlayer->sprite->pos); map_set_current_room(gMap, &gPlayer->sprite->pos);
roommatrix_handle_event(gRoomMatrix, &event); roommatrix_handle_event(gRoomMatrix, &event);
@ -421,10 +428,16 @@ run_game(void)
gui_update_player_stats(gGui, gPlayer, gMap, gRenderer); gui_update_player_stats(gGui, gPlayer, gMap, gRenderer);
particle_engine_update(deltaTime); particle_engine_update(deltaTime);
if (gPlayer->steps >= gPlayer->stats.speed) { roommatrix_update_with_player(gRoomMatrix, gPlayer);
player_reset_steps(gPlayer); if (currentTurn == PLAYER) {
roommatrix_update_with_player(gRoomMatrix, gPlayer); if (gPlayer->steps >= gPlayer->stats.speed) {
map_move_monsters(gMap, gRoomMatrix); currentTurn = MONSTER;
player_reset_steps(gPlayer);
}
}
if (currentTurn == MONSTER) {
if (map_move_monsters(gMap, gRoomMatrix))
currentTurn = PLAYER;
} }
SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 0); SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 0);

View File

@ -51,6 +51,7 @@ Map* map_create()
map->items = linkedlist_create(); map->items = linkedlist_create();
map->currentRoom = (Position) { 0, 0 }; map->currentRoom = (Position) { 0, 0 };
map->renderTimer = timer_create(); map->renderTimer = timer_create();
map->monsterMoveTimer = timer_create();
map->level = 1; map->level = 1;
for (i=0; i < MAP_H_ROOM_COUNT; ++i) { 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); linkedlist_append(&map->monsters, m);
} }
void bool
map_move_monsters(Map *map, RoomMatrix *rm) map_move_monsters(Map *map, RoomMatrix *rm)
{ {
LinkedList *m = map->monsters; LinkedList *m = map->monsters;
bool allDone = true;
if (timer_started(map->monsterMoveTimer)
&& timer_get_ticks(map->monsterMoveTimer) < 100)
return false;
while (m) { while (m) {
Monster *monster = m->data; Monster *monster = m->data;
m = m->next; m = m->next;
if (!position_in_room(&monster->sprite->pos, &map->currentRoom)) if (!position_in_room(&monster->sprite->pos, &map->currentRoom))
continue; 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) int map_add_texture(Map *map, const char *path, SDL_Renderer *renderer)

View File

@ -53,31 +53,44 @@ typedef struct Map_t {
LinkedList *items; LinkedList *items;
Position currentRoom; Position currentRoom;
Timer *renderTimer; Timer *renderTimer;
Timer *monsterMoveTimer;
int level; int level;
} Map; } 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_ #endif // MAP_H_

View File

@ -60,6 +60,7 @@ monster_create(SDL_Renderer *renderer)
m->state.current = m->state.normal; m->state.current = m->state.normal;
m->label = NULL; m->label = NULL;
m->lclabel = NULL; m->lclabel = NULL;
m->steps = 0;
monster_load_texts(m, renderer); monster_load_texts(m, renderer);
@ -216,7 +217,7 @@ monster_coward_walk(Monster *m, RoomMatrix *rm)
} }
} }
void bool
monster_move(Monster *m, RoomMatrix *rm) monster_move(Monster *m, RoomMatrix *rm)
{ {
Position monsterRoomPos; Position monsterRoomPos;
@ -242,6 +243,13 @@ monster_move(Monster *m, RoomMatrix *rm)
monsterRoomPos = position_to_matrix_coords(&m->sprite->pos); monsterRoomPos = position_to_matrix_coords(&m->sprite->pos);
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].occupied = true; rm->spaces[monsterRoomPos.x][monsterRoomPos.y].occupied = true;
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = m; rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = m;
m->steps++;
if (m->steps >= m->stats.speed) {
m->steps = 0;
return true;
}
return false;
} }
void void

View File

@ -40,6 +40,7 @@ typedef struct Monster_t {
ActionText *missText; ActionText *missText;
Stats stats; Stats stats;
State state; State state;
unsigned int steps;
} Monster; } Monster;
Monster* monster_create(SDL_Renderer*); Monster* monster_create(SDL_Renderer*);
@ -47,7 +48,7 @@ Monster* monster_create(SDL_Renderer*);
void void
monster_update_pos(Monster*, Position); monster_update_pos(Monster*, Position);
void bool
monster_move(Monster*, RoomMatrix*); monster_move(Monster*, RoomMatrix*);
void void