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 "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,6 +368,7 @@ handle_events(void)
continue;
if (gGameState == PLAYING) {
if (currentTurn == PLAYER)
gPlayer->handle_event(gPlayer,
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);
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);

View File

@ -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)

View File

@ -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_

View File

@ -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

View File

@ -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