Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions common/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const std::vector<std::reference_wrapper<Tile>>& Map::get_spawns_cts() const { r

const std::vector<std::reference_wrapper<Tile>>& Map::get_bomb_sites() const { return bomb_sites; }

const std::vector<std::pair<GunType, Vector2D>>& Map::get_guns() const { return guns; }

void Map::validate() const {
if (spawns_tts.empty())
throw std::runtime_error("Map '" + name + "' has no Terrorist spawns");
Expand All @@ -54,3 +56,7 @@ void Map::add_tile(Tile&& tile) {
if (added_tile.is_bomb_site)
bomb_sites.emplace_back(added_tile);
}

void Map::add_gun(GunType gun_type, const Vector2D& pos) {
guns.push_back(std::make_pair(gun_type, pos));
}
6 changes: 6 additions & 0 deletions common/map/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

#include "common/models.h"

#include "tile.h"

class Map {
Expand All @@ -19,6 +22,7 @@ class Map {
std::vector<std::reference_wrapper<Tile>> spawns_tts;
std::vector<std::reference_wrapper<Tile>> spawns_cts;
std::vector<std::reference_wrapper<Tile>> bomb_sites;
std::vector<std::pair<GunType, Vector2D>> guns;

public:
Map(const std::string& name, int max_players, int height, int width);
Expand All @@ -32,8 +36,10 @@ class Map {
const std::vector<std::reference_wrapper<Tile>>& get_spawns_tts() const;
const std::vector<std::reference_wrapper<Tile>>& get_spawns_cts() const;
const std::vector<std::reference_wrapper<Tile>>& get_bomb_sites() const;
const std::vector<std::pair<GunType, Vector2D>>& get_guns() const;

void validate() const;

void add_tile(Tile&& tile);
void add_gun(GunType gun_type, const Vector2D& pos);
};
3 changes: 2 additions & 1 deletion server/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#include "game_config.h"

Game::Game(const std::string& name, std::shared_ptr<Clock>&& game_clock, Map&& map):
Logic<GameState, GameUpdate>(GameState(std::move(game_clock), map.get_max_players())),
Logic<GameState, GameUpdate>(
GameState(std::move(game_clock), map.get_max_players(), map.get_guns())),
name(name),
physics_system(std::move(map), state.get_players(), state.get_dropped_guns(),
state.get_bomb()) {}
Expand Down
18 changes: 18 additions & 0 deletions server/map/map_builder.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "map_builder.h"

#include "common/map/tile.h"
#include "common/models.h"
#include "common/physics/physics_config.h"
#include "common/utils/vector_2d.h"

Expand All @@ -18,6 +19,9 @@ Map MapBuilder::build() {
YAML::Node tiles = map_data["tiles"];
load_tiles(tiles, map);

YAML::Node guns = map_data["guns"];
load_guns(guns, map);

map.validate();
return map;
}
Expand All @@ -39,3 +43,17 @@ void MapBuilder::load_tiles(const YAML::Node& tiles, Map& map) {
map.add_tile(Tile{pos, id, is_collidable, is_spawn_tt, is_spawn_ct, is_bomb_site});
}
}

void MapBuilder::load_guns(const YAML::Node& guns, Map& map) {
if (!guns || !guns.IsSequence())
return;

for (const auto& gun_data: guns) {
int x = gun_data["x"].as<int>();
int y = gun_data["y"].as<int>();
GunType gun_type = static_cast<GunType>(gun_data["type"].as<int>());

Vector2D pos = Vector2D(x, y) * PhysicsConfig::meter_size;
map.add_gun(gun_type, pos);
}
}
2 changes: 2 additions & 0 deletions server/map/map_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class MapBuilder {

void load_tiles(const YAML::Node& tiles, Map& map);

void load_guns(const YAML::Node& guns, Map& map);

public:
explicit MapBuilder(const std::string& filename);

Expand Down
6 changes: 5 additions & 1 deletion server/states/game_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
#include <utility>

#include "server/game/game_config.h"
#include "server/items/gun.h"
#include "server/physics/circular_hitbox.h"
#include "server/physics/rect_hitbox.h"

GameState::GameState(std::shared_ptr<Clock>&& game_clock, int max_players):
GameState::GameState(std::shared_ptr<Clock>&& game_clock, int max_players,
const std::vector<std::pair<GunType, Vector2D>>& guns):
phase(std::move(game_clock)), max_players(max_players) {
for (const auto& [gun_type, pos]: guns)
add_dropped_gun(std::move(Gun::make_gun(gun_type)), pos);
updates = get_full_update();
}

Expand Down
3 changes: 2 additions & 1 deletion server/states/game_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class GameState: public State<GameUpdate> {
bool is_tts_win_condition() const;

public:
GameState(std::shared_ptr<Clock>&& game_clock, int max_players);
GameState(std::shared_ptr<Clock>&& game_clock, int max_players,
const std::vector<std::pair<GunType, Vector2D>>& guns);

bool player_is_in_game(const std::string& player_name) const;
bool all_players_ready() const;
Expand Down
16 changes: 16 additions & 0 deletions tests/server/game/test_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,19 @@ TEST_F(TestGame, PlayerCanPickUpDroppedItem) {
EXPECT_EQ(updates.get_dropped_guns()[0].hitbox.get_pos(),
updates.get_players().at("ct").get_pos());
}

TEST_F(TestGame, GameStartWithDroppedGunsWhenMapHasDefaultDroppedGuns) {
Map map_with_dropped_guns(MapBuilder("./tests/server/map/map3.yaml").build());
Game game_with_dropped_guns("test_game_with_dropped_guns", clock,
std::move(map_with_dropped_guns));
game_with_dropped_guns.join_player("tt");

GameUpdate updates = game_with_dropped_guns.get_full_update();
EXPECT_EQ(updates.get_dropped_guns().size(), 2);
EXPECT_EQ(updates.get_dropped_guns()[0].item, GunType::AK47);
EXPECT_EQ(updates.get_dropped_guns()[0].hitbox.get_pos(),
Vector2D(PhysicsConfig::meter_size, PhysicsConfig::meter_size));
EXPECT_EQ(updates.get_dropped_guns()[1].item, GunType::M3);
EXPECT_EQ(updates.get_dropped_guns()[1].hitbox.get_pos(),
Vector2D(3 * PhysicsConfig::meter_size, 3 * PhysicsConfig::meter_size));
}
187 changes: 187 additions & 0 deletions tests/server/map/map3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
name: test_map
max_players: 10
height: 5
width: 5
tiles:
- x: 1
y: 1
id: 1
is_collidable: false
is_spawn_tt: true
is_spawn_ct: false
is_bomb_site: false
- x: 3
y: 3
id: 1
is_collidable: false
is_spawn_tt: false
is_spawn_ct: true
is_bomb_site: false
- x: 1
y: 2
id: 1
is_collidable: false
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 2
y: 1
id: 1
is_collidable: false
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 2
y: 2
id: 1
is_collidable: false
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: true
- x: 3
y: 2
id: 1
is_collidable: false
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 1
y: 3
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 2
y: 3
id: 1
is_collidable: false
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 0
y: 0
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 1
y: 0
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 2
y: 0
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 3
y: 0
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 4
y: 0
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 0
y: 1
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 4
y: 1
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 0
y: 2
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 4
y: 2
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 0
y: 3
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 4
y: 3
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 0
y: 4
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 1
y: 4
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 2
y: 4
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 3
y: 4
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 4
y: 4
id: 1
is_collidable: true
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
- x: 3
y: 1
id: 1
is_collidable: false
is_spawn_tt: false
is_spawn_ct: false
is_bomb_site: false
guns:
- x: 1
y: 1
type: 0
- x: 3
y: 3
type: 1
3 changes: 2 additions & 1 deletion tests/server/map/test_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TEST(YamlTest, LoadMap) {
}

TEST(YamlTest, BuildMap) {
MapBuilder builder("./tests/server/map/map.yaml");
MapBuilder builder("./tests/server/map/map3.yaml");
Map map = builder.build();

EXPECT_EQ(map.get_name(), "test_map");
Expand All @@ -27,4 +27,5 @@ TEST(YamlTest, BuildMap) {
EXPECT_EQ(map.get_spawns_tts().size(), 1);
EXPECT_EQ(map.get_spawns_cts().size(), 1);
EXPECT_EQ(map.get_bomb_sites().size(), 1);
EXPECT_EQ(map.get_guns().size(), 2);
}