moguda la fisica del jugador a un fitxer yamal animal

This commit is contained in:
2025-11-26 13:22:34 +01:00
parent 254ef3d11c
commit 61c6b9087c
4 changed files with 103 additions and 28 deletions

View File

@@ -10,6 +10,8 @@
#include "core/input/input.hpp" // Para Input, InputAction
#include "core/rendering/surface_animated_sprite.hpp" // Para SAnimatedSprite
#include "core/resources/resource_cache.hpp" // Para Resource
#include "core/resources/resource_helper.hpp" // Para Resource::Helper
#include "external/fkyaml_node.hpp" // Para fkyaml::node
#include "game/defaults.hpp" // Para Defaults::Sound
#include "game/gameplay/room.hpp" // Para Room, TileType
#include "game/options.hpp" // Para Cheat, Options, options
@@ -23,6 +25,7 @@
// Constructor
Player::Player(const Data& player)
: room_(player.room) {
loadPhysicsConfig("player_physics.yaml");
initSprite(player.animations_path);
setColor();
applySpawnValues(player.spawn_data);
@@ -122,7 +125,7 @@ void Player::transitionToState(State state) {
break;
case State::ON_AIR:
if (previous_state_ == State::ON_GROUND) {
vy_ = JUMP_VELOCITY; // Impulso de salto
vy_ = physics_.jump_velocity; // Impulso de salto
last_grounded_position_ = y_;
Audio::get()->playSound(Defaults::Sound::JUMP, Audio::Group::GAME);
}
@@ -257,7 +260,7 @@ void Player::switchBorders() {
void Player::applyGravity(float delta_time) {
// La gravedad se aplica siempre que el jugador está en el aire
if (state_ == State::ON_AIR) {
vy_ += GRAVITY_FORCE * delta_time;
vy_ += physics_.gravity_force * delta_time;
}
}
@@ -415,6 +418,54 @@ void Player::initSprite(const std::string& animations_path) {
sprite_->setCurrentAnimation("walk");
}
// Carga la configuración de física desde YAML
void Player::loadPhysicsConfig(const std::string& path) {
try {
auto file_data = Resource::Helper::loadFile(path);
std::string yaml_content(file_data.begin(), file_data.end());
auto yaml = fkyaml::node::deserialize(yaml_content);
// Cargar valores del YAML (con fallback a valores por defecto si falta alguna clave)
if (yaml.contains("walk_velocity")) {
physics_.walk_velocity = yaml["walk_velocity"].get_value<float>();
}
if (yaml.contains("run_velocity")) {
physics_.run_velocity = yaml["run_velocity"].get_value<float>();
}
if (yaml.contains("time_to_run")) {
physics_.time_to_run = yaml["time_to_run"].get_value<float>();
}
if (yaml.contains("run_acceleration")) {
physics_.run_acceleration = yaml["run_acceleration"].get_value<float>();
}
if (yaml.contains("horizontal_deceleration")) {
physics_.horizontal_deceleration = yaml["horizontal_deceleration"].get_value<float>();
}
if (yaml.contains("jump_velocity")) {
physics_.jump_velocity = yaml["jump_velocity"].get_value<float>();
}
if (yaml.contains("gravity_force")) {
physics_.gravity_force = yaml["gravity_force"].get_value<float>();
}
#ifdef _DEBUG
std::cout << "[Player] Physics config loaded: walk=" << physics_.walk_velocity
<< " run=" << physics_.run_velocity
<< " jump=" << physics_.jump_velocity
<< " gravity=" << physics_.gravity_force << std::endl;
#endif
} catch (const std::exception& e) {
std::cerr << "[Player] Error loading physics config from '" << path << "': " << e.what() << std::endl;
std::cerr << "[Player] Using default physics values" << std::endl;
// Los valores por defecto ya están establecidos en PhysicsConfig
}
}
// Recarga la configuración de física desde el archivo YAML
void Player::reloadPhysics() {
loadPhysicsConfig("player_physics.yaml");
}
// Actualiza la posición del sprite y las colisiones
void Player::syncSpriteAndCollider() {
placeSprite(); // Coloca el sprite en la posición del jugador
@@ -434,7 +485,7 @@ void Player::placeSprite() {
void Player::updateVelocity(float delta_time) {
if (auto_movement_) {
// La cinta transportadora tiene el control (velocidad fija)
vx_ = RUN_VELOCITY * room_->getConveyorBeltDirection();
vx_ = physics_.run_velocity * room_->getConveyorBeltDirection();
sprite_->setFlip(vx_ < 0.0F ? Flip::LEFT : Flip::RIGHT);
movement_time_ = 0.0F;
} else {
@@ -442,25 +493,25 @@ void Player::updateVelocity(float delta_time) {
switch (wanna_go_) {
case Direction::LEFT:
movement_time_ += delta_time;
if (movement_time_ < TIME_TO_RUN) {
if (movement_time_ < physics_.time_to_run) {
// Caminando: velocidad fija inmediata
vx_ = -WALK_VELOCITY;
vx_ = -physics_.walk_velocity;
} else {
// Corriendo: acelerar hacia RUN_VELOCITY
vx_ -= RUN_ACCELERATION * delta_time;
vx_ = std::max(vx_, -RUN_VELOCITY);
vx_ -= physics_.run_acceleration * delta_time;
vx_ = std::max(vx_, -physics_.run_velocity);
}
sprite_->setFlip(Flip::LEFT);
break;
case Direction::RIGHT:
movement_time_ += delta_time;
if (movement_time_ < TIME_TO_RUN) {
if (movement_time_ < physics_.time_to_run) {
// Caminando: velocidad fija inmediata
vx_ = WALK_VELOCITY;
vx_ = physics_.walk_velocity;
} else {
// Corriendo: acelerar hacia RUN_VELOCITY
vx_ += RUN_ACCELERATION * delta_time;
vx_ = std::min(vx_, RUN_VELOCITY);
vx_ += physics_.run_acceleration * delta_time;
vx_ = std::min(vx_, physics_.run_velocity);
}
sprite_->setFlip(Flip::RIGHT);
break;
@@ -468,10 +519,10 @@ void Player::updateVelocity(float delta_time) {
movement_time_ = 0.0F;
// Desacelerar gradualmente (momentum)
if (vx_ > 0.0F) {
vx_ -= HORIZONTAL_DECELERATION * delta_time;
vx_ -= physics_.horizontal_deceleration * delta_time;
vx_ = std::max(vx_, 0.0F);
} else if (vx_ < 0.0F) {
vx_ += HORIZONTAL_DECELERATION * delta_time;
vx_ += physics_.horizontal_deceleration * delta_time;
vx_ = std::min(vx_, 0.0F);
}
break;