diff --git a/src/assets/map.cpp b/src/assets/map.cpp index 21c7762..22a2e5d 100644 --- a/src/assets/map.cpp +++ b/src/assets/map.cpp @@ -95,7 +95,7 @@ int assets::MapLoader::GetPercent() const return 10; case ML_LOAD_MODELS: - return 60 + models_.size() * 30 / model_names_.size(); + return 60 + (model_names_.size() > 0 ? (models_.size() * 30 / model_names_.size()) : 30); case ML_STRUCTS: return 90; diff --git a/src/game/enterable_world.cpp b/src/game/enterable_world.cpp index 5a52124..36ef069 100644 --- a/src/game/enterable_world.cpp +++ b/src/game/enterable_world.cpp @@ -1,20 +1,11 @@ #include "enterable_world.hpp" #include "player_character.hpp" -static uint32_t GetRandomColor24() -{ - uint8_t r,g,b; - r = rand() % 256; - g = rand() % 256; - b = rand() % 256; - return (b << 16) | (g << 8) | r; -} - game::EnterableWorld::EnterableWorld(std::string mapname) : World(std::move(mapname)) {} -void game::EnterableWorld::InsertPlayer(Player& player, const glm::vec3& pos, float yaw) +game::PlayerCharacter& game::EnterableWorld::InsertPlayer(Player& player, const CharacterTuning& tuning, const glm::vec3& pos, float yaw) { - CreatePlayerCharacter(player, pos, yaw); + return CreatePlayerCharacter(player, tuning, pos, yaw); } void game::EnterableWorld::PlayerInput(Player& player, PlayerInputType type, bool enabled) @@ -37,11 +28,6 @@ void game::EnterableWorld::PlayerInput(Player& player, PlayerInputType type, boo } break; - case IN_DEBUG2: - if (enabled) - CreatePlayerCharacter(player, glm::vec3(100.0f, 100.0f, 5.0f), 0.0f); - break; - default: character->ProcessInput(type, enabled); break; @@ -65,14 +51,22 @@ void game::EnterableWorld::RemovePlayer(Player& player) RemovePlayerCharacter(player); } -game::PlayerCharacter& game::EnterableWorld::CreatePlayerCharacter(Player& player, const glm::vec3& position, float yaw) +game::PlayerCharacter& game::EnterableWorld::MovePlayerToWorld(Player& player, EnterableWorld& new_world, const glm::vec3& pos, float yaw) +{ + auto old_character = player_characters_.at(&player); + auto& tuning = old_character->GetTuning(); + RemovePlayer(player); + + player.SetWorld(&new_world); + auto& new_character = new_world.InsertPlayer(player, tuning, pos, yaw); + + return new_character; +} + +game::PlayerCharacter& game::EnterableWorld::CreatePlayerCharacter(Player& player, const CharacterTuning& tuning, const glm::vec3& position, float yaw) { RemovePlayerCharacter(player); - CharacterTuning tuning{}; - tuning.clothes.push_back({ "tshirt", GetRandomColor24() }); - tuning.clothes.push_back({ "shorts", GetRandomColor24() }); - auto& character = Spawn(player, tuning); character.SetPosition(position); character.SetYaw(yaw); diff --git a/src/game/enterable_world.hpp b/src/game/enterable_world.hpp index f3a805b..27a1f34 100644 --- a/src/game/enterable_world.hpp +++ b/src/game/enterable_world.hpp @@ -15,13 +15,16 @@ public: EnterableWorld(std::string mapname); // events - virtual void InsertPlayer(Player& player, const glm::vec3& pos, float yaw); + virtual PlayerCharacter& InsertPlayer(Player& player, const CharacterTuning& tuning, const glm::vec3& pos, float yaw); virtual void PlayerInput(Player& player, PlayerInputType type, bool enabled); virtual void PlayerViewAnglesChanged(Player& player, float yaw, float pitch); virtual void RemovePlayer(Player& player); + // moves + PlayerCharacter& MovePlayerToWorld(Player& player, EnterableWorld& new_world, const glm::vec3& pos, float yaw); + private: - PlayerCharacter& CreatePlayerCharacter(Player& player, const glm::vec3& position, float yaw); + PlayerCharacter& CreatePlayerCharacter(Player& player, const CharacterTuning& tuning, const glm::vec3& position, float yaw); void RemovePlayerCharacter(Player& player); private: diff --git a/src/game/game.cpp b/src/game/game.cpp index 0855b63..b41d401 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -1,12 +1,27 @@ #include "game.hpp" -#include "player.hpp" #include "openworld.hpp" +#include "player.hpp" + +static constexpr glm::vec3 openworld_spawn(100.0f, 100.0f, 5.0f); +static constexpr glm::vec3 test_spawn(0.0f, 0.0f, 5.0f); + +static uint32_t GetRandomColor24() +{ + uint8_t r, g, b; + r = rand() % 256; + g = rand() % 256; + b = rand() % 256; + return (b << 16) | (g << 8) | r; +} game::Game::Game() { openworld_ = std::make_shared(); all_worlds_.push_back(openworld_.get()); + + testworld_ = std::make_shared("testarena"); + all_worlds_.push_back(testworld_.get()); } void game::Game::Update() @@ -29,12 +44,16 @@ void game::Game::PlayerJoined(Player& player) { BroadcastChat(player.GetName() + "^r se připoojil jupí jupí jupííí"); - players_.insert({ &player, PlayerGameInfo(player) }); + players_.insert({&player, PlayerGameInfo(player)}); auto& player_info = players_.at(&player); player_info.world = openworld_.get(); - player.SetWorld(openworld_); + player.SetWorld(openworld_.get()); - openworld_->InsertPlayer(player, glm::vec3(100.0f, 100.0f, 5.0f), 0.0f); + CharacterTuning tuning{}; + tuning.clothes.push_back({"tshirt", GetRandomColor24()}); + tuning.clothes.push_back({"shorts", GetRandomColor24()}); + + openworld_->InsertPlayer(player, tuning, openworld_spawn, 0.0f); } void game::Game::PlayerViewAnglesChanged(Player& player, float yaw, float pitch) @@ -46,9 +65,34 @@ void game::Game::PlayerViewAnglesChanged(Player& player, float yaw, float pitch) void game::Game::PlayerInput(Player& player, PlayerInputType type, bool enabled) { - auto world = FindPlayerWorld(player); - if (world) - world->PlayerInput(player, type, enabled); + switch (type) + { + case IN_DEBUG2: { + if (!enabled) + return; + + auto& player_info = players_.at(&player); + + if (player_info.world == openworld_.get()) + { + MovePlayerToWorld(player_info, testworld_.get(), test_spawn, 0.0f); + } + else + { + MovePlayerToWorld(player_info, openworld_.get(), openworld_spawn, 0.0f); + } + + break; + } + + default: { + auto world = FindPlayerWorld(player); + if (world) + world->PlayerInput(player, type, enabled); + + break; + } + } } void game::Game::PlayerLeft(Player& player) @@ -70,6 +114,13 @@ void game::Game::BroadcastChat(const std::string& text) } } +void game::Game::MovePlayerToWorld(PlayerGameInfo& player_info, EnterableWorld* new_world, const glm::vec3& pos, + float yaw) +{ + player_info.world->MovePlayerToWorld(player_info.player, *new_world, pos, yaw); + player_info.world = new_world; +} + game::EnterableWorld* game::Game::FindPlayerWorld(Player& player) const { auto it = players_.find(&player); diff --git a/src/game/game.hpp b/src/game/game.hpp index 1f6184c..794ad08 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -34,11 +34,13 @@ public: private: void BroadcastChat(const std::string& text); + void MovePlayerToWorld(PlayerGameInfo& player_info, EnterableWorld* new_world, const glm::vec3& pos, float yaw); + EnterableWorld* FindPlayerWorld(Player& player) const; private: std::shared_ptr openworld_; - + std::shared_ptr testworld_; std::vector all_worlds_; // for common update etc. std::map players_; diff --git a/src/game/player.cpp b/src/game/player.cpp index cc4b4d9..115b730 100644 --- a/src/game/player.cpp +++ b/src/game/player.cpp @@ -30,10 +30,10 @@ bool game::Player::ProcessMsg(net::MessageType type, net::InMessage& msg) void game::Player::Update() { - if (world_.get() != known_world_) + if (world_ != known_world_) { SendWorldMsg(); - known_world_ = world_.get(); + known_world_ = world_; known_ents_.clear(); } @@ -44,12 +44,12 @@ void game::Player::Update() } } -void game::Player::SetWorld(std::shared_ptr world) +void game::Player::SetWorld(World* world) { if (world == world_) return; - world_ = std::move(world); + world_ = world; } void game::Player::SetCamera(net::EntNum entnum) diff --git a/src/game/player.hpp b/src/game/player.hpp index 05435cc..09d42d4 100644 --- a/src/game/player.hpp +++ b/src/game/player.hpp @@ -28,7 +28,7 @@ public: bool ProcessMsg(net::MessageType type, net::InMessage& msg); void Update(); - void SetWorld(std::shared_ptr world); + void SetWorld(World* world); void SetCamera(net::EntNum entnum); void SendChat(const std::string& text); @@ -63,7 +63,7 @@ private: Game& game_; std::string name_; - std::shared_ptr world_ = nullptr; + World* world_ = nullptr; World* known_world_ = nullptr; std::set known_ents_;