Maint: Even more cleanup

This commit is contained in:
Linus Probert 2018-09-13 13:12:43 +02:00
parent 620a7e3ed5
commit e1b3364d88
4 changed files with 73 additions and 61 deletions

View File

@ -770,19 +770,22 @@ run_game_update(void)
map_set_current_room(gMap, &gPlayer->sprite->pos); map_set_current_room(gMap, &gPlayer->sprite->pos);
map_update(&updateData); map_update(&updateData);
bool turnSwitch = false;
if (currentTurn == PLAYER) { if (currentTurn == PLAYER) {
if (player_turn_over(gPlayer)) { if (player_turn_over(gPlayer)) {
currentTurn = MONSTER; currentTurn = MONSTER;
player_reset_steps(gPlayer); player_reset_steps(gPlayer);
map_on_new_turn(gMap); map_on_new_turn(gMap);
turnSwitch = true;
} }
} else if (currentTurn == MONSTER) { } else if (currentTurn == MONSTER) {
if (map_move_monsters(gMap, gRoomMatrix)) { if (map_move_monsters(gMap, gRoomMatrix)) {
currentTurn = PLAYER; currentTurn = PLAYER;
} }
} }
if (map_clear_expired_entities(gMap, gPlayer)) if (map_clear_expired_entities(gMap, gPlayer) || turnSwitch)
repopulate_roommatrix(); repopulate_roommatrix();
} }

View File

@ -298,7 +298,7 @@ has_collided(Monster *monster, RoomMatrix *matrix, Vector2d direction)
monster_behaviour_check_post_attack(monster); monster_behaviour_check_post_attack(monster);
} }
return space->occupied; return space->occupied || space->monster;
} }
static bool static bool

View File

@ -148,78 +148,89 @@ on_monster_collision(Player *player,
} }
static void
player_pickup_artifacts(Player *player, RoomSpace *space)
{
LinkedList *artifacts = space->artifacts;
while (artifacts) {
player_add_artifact(player, artifacts->data);
artifacts = artifacts->next;
}
}
static void
player_interact_objects(Player *player, RoomSpace *space)
{
LinkedList *objects = space->objects;
while (objects) {
object_damage(objects->data, player);
objects = objects->next;
}
}
static void
player_collect_items(Player *player, RoomSpace *space)
{
LinkedList *items = space->items;
while (items != NULL) {
Item *item = items->data;
items = items->next;
item_collected(item, player);
}
}
static void
player_interact_traps_and_pits(Player *player, RoomSpace *space)
{
if (space->lethal) {
player_set_falling(player);
}
if (space->trap) {
trap_activate(space->trap, player);
}
}
static bool
player_has_collided(RoomSpace *space)
{
if (space->occupied)
return true;
return space->monster && space->monster->sprite->state != SPRITE_STATE_FALLING;
}
static bool static bool
has_collided(Player *player, RoomMatrix *matrix, Vector2d direction) has_collided(Player *player, RoomMatrix *matrix, Vector2d direction)
{ {
bool collided = false;
Position roomCoord = position_to_room_coords(&player->sprite->pos); Position roomCoord = position_to_room_coords(&player->sprite->pos);
if (roomCoord.x != matrix->roomPos.x if (roomCoord.x != matrix->roomPos.x || roomCoord.y != matrix->roomPos.y) {
|| roomCoord.y != matrix->roomPos.y) { return false;
return collided;
} }
Position matrixPos = position_to_matrix_coords(&player->sprite->pos); Position matrixPos = position_to_matrix_coords(&player->sprite->pos);
RoomSpace *space = &matrix->spaces[matrixPos.x][matrixPos.y]; RoomSpace *space = &matrix->spaces[matrixPos.x][matrixPos.y];
collided = space->occupied;
if (space->monster != NULL if (player_has_collided(space)) {
&& (space->monster->sprite->state == SPRITE_STATE_FALLING
|| space->monster->stats.hp <= 0))
{
collided = false;
}
if (collided) {
player->sprite->pos.x -= TILE_DIMENSION * (int)direction.x; player->sprite->pos.x -= TILE_DIMENSION * (int)direction.x;
player->sprite->pos.y -= TILE_DIMENSION * (int)direction.y; player->sprite->pos.y -= TILE_DIMENSION * (int)direction.y;
}
if (space->monster != NULL if (space->monster) {
&& space->monster->stats.hp > 0 on_monster_collision(player, space->monster, matrix, direction);
&& space->monster->sprite->state != SPRITE_STATE_FALLING) } else {
{ mixer_play_effect(BONK);
on_monster_collision(player, space->monster, matrix, direction); camera_shake(direction, 100);
} else if (collided) { gui_log("Ouch! There is something in the way");
mixer_play_effect(BONK);
camera_shake(direction, 100);
gui_log("Ouch! There is something in the way");
}
if (space->items != NULL && !collided) {
LinkedList *items = space->items;
while (items != NULL) {
Item *item = items->data;
items = items->next;
item_collected(item, player);
} }
return true;
} }
else {
if (space->artifacts != NULL && !collided) { player_collect_items(player, space);
LinkedList *artifacts = space->artifacts; player_pickup_artifacts(player, space);
while (artifacts) { player_interact_objects(player, space);
player_add_artifact(player, artifacts->data); player_interact_traps_and_pits(player, space);
artifacts = artifacts->next; return false;
}
} }
if (space->objects && !collided) {
LinkedList *objects = space->objects;
while (objects) {
object_damage(objects->data, player);
objects = objects->next;
}
}
if (space->lethal && !collided) {
player_set_falling(player);
}
if (space->trap && !collided) {
trap_activate(space->trap, player);
}
return collided;
} }
static void static void

View File

@ -144,8 +144,6 @@ void roommatrix_populate_from_map(RoomMatrix *rm, Map *m)
position = position =
position_to_matrix_coords(&monster->sprite->pos); position_to_matrix_coords(&monster->sprite->pos);
rm->spaces[position.x][position.y]
.occupied = true;
rm->spaces[position.x][position.y] rm->spaces[position.x][position.y]
.monster = monster; .monster = monster;
} }