refactor and cleanup player controller

This commit is contained in:
MihailRis
2024-07-08 19:43:13 +03:00
parent 487ba84517
commit 3cd0261f3b
8 changed files with 21 additions and 13 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
{ {
"hitbox": [0.4, 0.9, 0.4] "hitbox": [0.3, 0.9, 0.3]
} }
+2 -1
View File
@@ -40,10 +40,11 @@ void LevelController::update(float delta, bool input, bool pause) {
} }
} }
blocks->update(delta); blocks->update(delta);
player->update(delta, input, pause);
level->entities->updatePhysics(delta); level->entities->updatePhysics(delta);
level->entities->update(); level->entities->update();
} }
player->update(delta, input, pause); player->postUpdate(delta, input, pause);
// erease null pointers // erease null pointers
level->objects.erase( level->objects.erase(
+7 -2
View File
@@ -239,11 +239,16 @@ void PlayerController::update(float delta, bool input, bool pause) {
} else { } else {
resetKeyboard(); resetKeyboard();
} }
updatePlayer(delta);
}
}
void PlayerController::postUpdate(float delta, bool input, bool pause) {
if (!pause) {
updateFootsteps(delta); updateFootsteps(delta);
updateCamera(delta, input); updateCamera(delta, input);
updatePlayer(delta);
} }
player->postUpdate(this->input, delta);
camControl.refresh(); camControl.refresh();
if (input) { if (input) {
updateInteraction(); updateInteraction();
+1 -1
View File
@@ -89,7 +89,7 @@ public:
BlocksController* blocksController BlocksController* blocksController
); );
void update(float delta, bool input, bool pause); void update(float delta, bool input, bool pause);
void postUpdate(float delta, bool input, bool pause);
Player* getPlayer(); Player* getPlayer();
void listenBlockInteraction(const on_block_interaction& callback); void listenBlockInteraction(const on_block_interaction& callback);
+1 -2
View File
@@ -286,11 +286,10 @@ void scripting::on_entity_spawn(
lua::call(L, 1); lua::call(L, 1);
} }
if (components.size() > 1) { if (components.size() > 1) {
for (int i = 0; i < components.size()-1; i++) { for (size_t i = 0; i < components.size()-1; i++) {
lua::pushvalue(L, -1); lua::pushvalue(L, -1);
} }
} }
std::cout << "entity duplicated " << (components.size()-1) << " times" << std::endl;
for (auto& component : components) { for (auto& component : components) {
auto compenv = create_component_environment(get_root_environment(), -1, auto compenv = create_component_environment(get_root_environment(), -1,
component->name); component->name);
-3
View File
@@ -66,7 +66,6 @@ entityid_t Entities::spawn(
dynamic::Map_sptr saved, dynamic::Map_sptr saved,
entityid_t uid) entityid_t uid)
{ {
std::cout << "spawn entity " << def.name << std::endl;
auto rig = level->content->getRig(def.rigName); auto rig = level->content->getRig(def.rigName);
if (rig == nullptr) { if (rig == nullptr) {
throw std::runtime_error("rig "+def.rigName+" not found"); throw std::runtime_error("rig "+def.rigName+" not found");
@@ -109,10 +108,8 @@ entityid_t Entities::spawn(
componentName, entity_funcs_set {}, nullptr); componentName, entity_funcs_set {}, nullptr);
scripting.components.emplace_back(std::move(component)); scripting.components.emplace_back(std::move(component));
} }
std::cout << "on_entity_spawn" << std::endl;
scripting::on_entity_spawn( scripting::on_entity_spawn(
def, id, scripting.components, std::move(args), std::move(saved)); def, id, scripting.components, std::move(args), std::move(saved));
std::cout << "done" << std::endl;
return id; return id;
} }
+8 -3
View File
@@ -96,9 +96,15 @@ void Player::updateInput(PlayerInput& input, float delta) {
dir = glm::normalize(dir); dir = glm::normalize(dir);
hitbox->velocity += dir * speed * delta * 9.0f; hitbox->velocity += dir * speed * delta * 9.0f;
} }
}
void Player::postUpdate(PlayerInput& input, float delta) {
auto hitbox = getHitbox();
if (hitbox == nullptr) {
return;
}
position = hitbox->position;
// physics calculation was here
if (flight && hitbox->grounded) { if (flight && hitbox->grounded) {
flight = false; flight = false;
} }
@@ -272,7 +278,6 @@ void Player::deserialize(dynamic::Map *src) {
if (auto invmap = src->map("inventory")) { if (auto invmap = src->map("inventory")) {
getInventory()->deserialize(invmap.get()); getInventory()->deserialize(invmap.get());
} }
std::cout << "Player::DESERIALIZE " << eid << std::endl;
} }
void Player::convert(dynamic::Map* data, const ContentLUT* lut) { void Player::convert(dynamic::Map* data, const ContentLUT* lut) {
+1
View File
@@ -65,6 +65,7 @@ public:
void teleport(glm::vec3 position); void teleport(glm::vec3 position);
void updateEntity(); void updateEntity();
void updateInput(PlayerInput& input, float delta); void updateInput(PlayerInput& input, float delta);
void postUpdate(PlayerInput& input, float delta);
void attemptToFindSpawnpoint(); void attemptToFindSpawnpoint();