Add moving of players between worlds

This commit is contained in:
tovjemam 2026-03-21 17:23:31 +01:00
parent 737165a152
commit 3d7db1184f
7 changed files with 88 additions and 38 deletions

View File

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

View File

@ -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<PlayerCharacter>(player, tuning);
character.SetPosition(position);
character.SetYaw(yaw);

View File

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

View File

@ -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<OpenWorld>();
all_worlds_.push_back(openworld_.get());
testworld_ = std::make_shared<EnterableWorld>("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)
{
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);

View File

@ -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> openworld_;
std::shared_ptr<EnterableWorld> testworld_;
std::vector<World*> all_worlds_; // for common update etc.
std::map<Player*, PlayerGameInfo> players_;

View File

@ -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> world)
void game::Player::SetWorld(World* world)
{
if (world == world_)
return;
world_ = std::move(world);
world_ = world;
}
void game::Player::SetCamera(net::EntNum entnum)

View File

@ -28,7 +28,7 @@ public:
bool ProcessMsg(net::MessageType type, net::InMessage& msg);
void Update();
void SetWorld(std::shared_ptr<World> 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> world_ = nullptr;
World* world_ = nullptr;
World* known_world_ = nullptr;
std::set<net::EntNum> known_ents_;