introduce local player

This commit is contained in:
MihailRis
2025-01-16 05:59:43 +03:00
parent b5999fe364
commit 65fec4f4a9
16 changed files with 84 additions and 31 deletions
+4 -2
View File
@@ -92,7 +92,7 @@ bool ChunksController::loadVisible(const Player& player, uint padding) const {
}
const auto& chunk = chunks.getChunks()[nearZ * sizeX + nearX];
if (chunk != nullptr || !assigned) {
if (chunk != nullptr || !assigned || !player.isLoadingChunks()) {
return false;
}
int offsetX = chunks.getOffsetX();
@@ -101,7 +101,9 @@ bool ChunksController::loadVisible(const Player& player, uint padding) const {
return true;
}
bool ChunksController::buildLights(const Player& player, const std::shared_ptr<Chunk>& chunk) const {
bool ChunksController::buildLights(
const Player& player, const std::shared_ptr<Chunk>& chunk
) const {
int surrounding = 0;
for (int oz = -1; oz <= 1; oz++) {
for (int ox = -1; ox <= 1; ox++) {
+11 -5
View File
@@ -143,7 +143,9 @@ static bool load_world_content(Engine& engine, const fs::path& folder) {
}
static void load_world(
Engine& engine, const std::shared_ptr<WorldFiles>& worldFiles
Engine& engine,
const std::shared_ptr<WorldFiles>& worldFiles,
int64_t localPlayer
) {
try {
auto content = engine.getContent();
@@ -151,7 +153,7 @@ static void load_world(
auto& settings = engine.getSettings();
auto level = World::load(worldFiles, settings, *content, packs);
engine.onWorldOpen(std::move(level));
engine.onWorldOpen(std::move(level), localPlayer);
} catch (const world_load_error& error) {
guiutil::alert(
engine,
@@ -233,7 +235,7 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
}
return;
}
load_world(engine, std::move(worldFiles));
load_world(engine, std::move(worldFiles), localPlayer);
}
inline uint64_t str2seed(const std::string& seedstr) {
@@ -279,9 +281,13 @@ void EngineController::createWorld(
engine.getContentPacks()
);
if (!engine.isHeadless()) {
level->players->create();
level->players->create(localPlayer);
}
engine.onWorldOpen(std::move(level));
engine.onWorldOpen(std::move(level), localPlayer);
}
void EngineController::setLocalPlayer(int64_t player) {
localPlayer = player;
}
void EngineController::reopenWorld(World* world) {
+3
View File
@@ -12,6 +12,7 @@ class LevelController;
class EngineController {
Engine& engine;
int64_t localPlayer = -1;
void onMissingContent(const std::shared_ptr<ContentReport>& report);
public:
EngineController(Engine& engine);
@@ -37,5 +38,7 @@ public:
const std::string& generatorID
);
void setLocalPlayer(int64_t player);
void reopenWorld(World* world);
};
+5
View File
@@ -50,6 +50,10 @@ LevelController::LevelController(
do {
confirmed = 0;
for (const auto& [_, player] : *level->players) {
if (!player->isLoadingChunks()) {
confirmed++;
continue;
}
glm::vec3 position = player->getPosition();
player->chunks->configure(
std::floor(position.x), std::floor(position.z), 1
@@ -69,6 +73,7 @@ void LevelController::update(float delta, bool pause) {
if (player->isSuspended()) {
continue;
}
player->updateEntity();
glm::vec3 position = player->getPosition();
player->chunks->configure(
position.x,
-1
View File
@@ -305,7 +305,6 @@ void PlayerController::resetKeyboard() {
}
void PlayerController::updatePlayer(float delta) {
player.updateEntity();
player.updateInput(input, delta);
}
+6
View File
@@ -55,10 +55,15 @@ static int l_new_world(lua::State* L) {
auto name = lua::require_string(L, 1);
auto seed = lua::require_string(L, 2);
auto generator = lua::require_string(L, 3);
int64_t localPlayer = 0;
if (lua::gettop(L) >= 4) {
localPlayer = lua::tointeger(L, 4);
}
if (level != nullptr) {
throw std::runtime_error("world must be closed before");
}
auto controller = engine->getController();
controller->setLocalPlayer(localPlayer);
controller->createWorld(name, seed, generator);
return 0;
}
@@ -71,6 +76,7 @@ static int l_open_world(lua::State* L) {
throw std::runtime_error("world must be closed before");
}
auto controller = engine->getController();
controller->setLocalPlayer(0);
controller->openWorld(name, false);
return 0;
}
+9 -4
View File
@@ -52,6 +52,7 @@ static int l_set_vel(lua::State* L) {
auto x = lua::tonumber(L, 2);
auto y = lua::tonumber(L, 3);
auto z = lua::tonumber(L, 4);
if (auto hitbox = player->getHitbox()) {
hitbox->velocity = glm::vec3(x, y, z);
}
@@ -272,15 +273,19 @@ static int l_set_name(lua::State* L) {
}
static int l_create(lua::State* L) {
auto player = level->players->create();
if (lua::isstring(L, 1)) {
player->setName(lua::require_string(L, 1));
int64_t playerId = Players::NONE;
if (lua::gettop(L) >= 2) {
playerId = lua::tointeger(L, 2);
}
auto player = level->players->create(playerId);
player->setName(lua::require_string(L, 1));
return lua::pushinteger(L, player->getId());
}
static int l_delete(lua::State* L) {
level->players->remove(lua::tointeger(L, 1));
auto id = lua::tointeger(L, 1);
level->players->suspend(id);
level->players->remove(id);
return 0;
}