refactor players

This commit is contained in:
MihailRis
2024-11-22 16:17:13 +03:00
parent 7f41f95013
commit bd2acd5766
13 changed files with 101 additions and 151 deletions
+1 -18
View File
@@ -5,7 +5,6 @@
#include "debug/Logger.hpp"
#include "engine.hpp"
#include "files/WorldFiles.hpp"
#include "interfaces/Object.hpp"
#include "objects/Entities.hpp"
#include "physics/Hitbox.hpp"
#include "settings.hpp"
@@ -26,7 +25,7 @@ LevelController::LevelController(Engine* engine, std::unique_ptr<Level> levelPtr
*level, settings.chunks.padding.get()
)),
player(std::make_unique<PlayerController>(
settings, this->level.get(), blocks.get()
settings, level.get(), blocks.get()
)) {
scripting::on_world_load(this);
}
@@ -45,11 +44,6 @@ void LevelController::update(float delta, bool input, bool pause) {
if (!pause) {
// update all objects that needed
for (const auto& obj : level->objects) {
if (obj && obj->shouldUpdate) {
obj->update(delta);
}
}
blocks->update(delta);
player->update(delta, input, pause);
level->entities->updatePhysics(delta);
@@ -57,17 +51,6 @@ void LevelController::update(float delta, bool input, bool pause) {
}
level->entities->clean();
player->postUpdate(delta, input, pause);
// erease null pointers
auto& objects = level->objects;
objects.erase(
std::remove_if(
objects.begin(),
objects.end(),
[](auto obj) { return obj == nullptr; }
),
objects.end()
);
}
void LevelController::saveWorld() {
+18 -18
View File
@@ -40,7 +40,7 @@ const float C_ZOOM = 0.1f;
const float CROUCH_SHIFT_Y = -0.2f;
CameraControl::CameraControl(
const std::shared_ptr<Player>& player, const CameraSettings& settings
Player* player, const CameraSettings& settings
)
: player(player),
camera(player->fpCamera),
@@ -193,7 +193,7 @@ PlayerController::PlayerController(
BlocksController* blocksController
)
: settings(settings), level(level),
player(level->getObject<Player>(0)),
player(level->getPlayer(0)),
camControl(player, settings.camera),
blocksController(blocksController),
playerTickClock(20, 3) {
@@ -215,7 +215,7 @@ void PlayerController::onFootstep(const Hitbox& hitbox) {
continue;
}
blocksController->onBlockInteraction(
player.get(),
player,
glm::ivec3(x, y, z),
def,
BlockInteraction::step
@@ -254,7 +254,7 @@ void PlayerController::update(float delta, bool input, bool pause) {
if (playerTickClock.update(delta)) {
if (player->getId() % playerTickClock.getParts() ==
playerTickClock.getPart()) {
scripting::on_player_tick(player.get(), playerTickClock.getTickRate());
scripting::on_player_tick(player, playerTickClock.getTickRate());
}
}
}
@@ -390,12 +390,12 @@ voxel* PlayerController::updateSelection(float maxDistance) {
if (selection.entity != prevEntity) {
if (prevEntity != ENTITY_NONE) {
if (auto pentity = level->entities->get(prevEntity)) {
scripting::on_aim_off(*pentity, player.get());
scripting::on_aim_off(*pentity, player);
}
}
if (selection.entity != ENTITY_NONE) {
if (auto pentity = level->entities->get(selection.entity)) {
scripting::on_aim_on(*pentity, player.get());
scripting::on_aim_on(*pentity, player);
}
}
}
@@ -432,8 +432,8 @@ void PlayerController::processRightClick(const Block& def, const Block& target)
if (!input.shift && target.rt.funcsset.oninteract) {
if (scripting::on_block_interact(
player.get(), target, selection.actualPosition
)) {
player, target, selection.actualPosition
)) {
return;
}
}
@@ -474,7 +474,7 @@ void PlayerController::processRightClick(const Block& def, const Block& target)
slot.setCount(slot.getCount()-1);
}
blocksController->placeBlock(
player.get(), def, state, coord.x, coord.y, coord.z
player, def, state, coord.x, coord.y, coord.z
);
}
}
@@ -488,10 +488,10 @@ void PlayerController::updateEntityInteraction(
}
auto entity = *entityOpt;
if (lclick) {
scripting::on_attacked(entity, player.get(), player->getEntity());
scripting::on_attacked(entity, player, player->getEntity());
}
if (rclick) {
scripting::on_entity_used(entity, player.get());
scripting::on_entity_used(entity, player);
}
}
@@ -523,7 +523,7 @@ void PlayerController::updateInteraction(float delta) {
auto vox = updateSelection(maxDistance);
if (vox == nullptr) {
if (rclick && item.rt.funcsset.on_use) {
scripting::on_item_use(player.get(), item);
scripting::on_item_use(player, item);
}
if (selection.entity) {
updateEntityInteraction(selection.entity, lattack, rclick);
@@ -534,7 +534,7 @@ void PlayerController::updateInteraction(float delta) {
auto iend = selection.position;
if (lclick && !input.shift && item.rt.funcsset.on_block_break_by) {
if (scripting::on_item_break_block(
player.get(), item, iend.x, iend.y, iend.z
player, item, iend.x, iend.y, iend.z
)) {
return;
}
@@ -543,7 +543,7 @@ void PlayerController::updateInteraction(float delta) {
if (lclick) {
if (player->isInstantDestruction() && target.breakable) {
blocksController->breakBlock(
player.get(), target, iend.x, iend.y, iend.z
player, target, iend.x, iend.y, iend.z
);
}
}
@@ -551,10 +551,10 @@ void PlayerController::updateInteraction(float delta) {
bool preventDefault = false;
if (item.rt.funcsset.on_use_on_block) {
preventDefault = scripting::on_item_use_on_block(
player.get(), item, iend, selection.normal
player, item, iend, selection.normal
);
} else if (item.rt.funcsset.on_use) {
preventDefault = scripting::on_item_use(player.get(), item);
preventDefault = scripting::on_item_use(player, item);
}
if (preventDefault) {
return;
@@ -566,10 +566,10 @@ void PlayerController::updateInteraction(float delta) {
}
if (Events::jactive(BIND_PLAYER_PICK)) {
auto coord = selection.actualPosition;
pick_block(indices, chunks, player.get(), coord.x, coord.y, coord.z);
pick_block(indices, chunks, player, coord.x, coord.y, coord.z);
}
}
Player* PlayerController::getPlayer() {
return player.get();
return player;
}
+3 -3
View File
@@ -18,7 +18,7 @@ struct CameraSettings;
struct EngineSettings;
class CameraControl {
std::shared_ptr<Player> player;
Player* player;
std::shared_ptr<Camera> camera;
const CameraSettings& settings;
glm::vec3 offset;
@@ -40,7 +40,7 @@ class CameraControl {
void switchCamera();
public:
CameraControl(
const std::shared_ptr<Player>& player, const CameraSettings& settings
Player* player, const CameraSettings& settings
);
void updateMouse(PlayerInput& input);
void update(PlayerInput input, float delta, Chunks* chunks);
@@ -50,7 +50,7 @@ public:
class PlayerController {
const EngineSettings& settings;
Level* level;
std::shared_ptr<Player> player;
Player* player;
PlayerInput input {};
CameraControl camControl;
BlocksController* blocksController;
+4 -6
View File
@@ -350,9 +350,9 @@ static int l_place(lua::State* L) {
"there is no block with index " + std::to_string(id)
);
}
auto player = level->getObject<Player>(playerid);
auto player = level->getPlayer(playerid);
controller->getBlocksController()->placeBlock(
player ? player.get() : nullptr, *def, int2blockstate(state), x, y, z
player, *def, int2blockstate(state), x, y, z
);
return 0;
}
@@ -367,10 +367,8 @@ static int l_destruct(lua::State* L) {
return 0;
}
auto& def = level->content->getIndices()->blocks.require(voxel->id);
auto player = level->getObject<Player>(playerid);
controller->getBlocksController()->breakBlock(
player ? player.get() : nullptr, def, x, y, z
);
auto player = level->getPlayer(playerid);
controller->getBlocksController()->breakBlock(player, def, x, y, z);
return 0;
}
+2 -2
View File
@@ -11,8 +11,8 @@
using namespace scripting;
inline std::shared_ptr<Player> get_player(lua::State* L, int idx) {
return level->getObject<Player>(lua::tointeger(L, idx));
inline Player* get_player(lua::State* L, int idx) {
return level->getPlayer(lua::tointeger(L, idx));
}
static int l_get_pos(lua::State* L) {