alai/src/player/states/PlayerFall.cpp

76 lines
1.7 KiB
C++
Raw Normal View History

#include "player/Player.h"
2022-10-10 21:54:12 -03:00
#include "player/states/PlayerFall.h"
2022-08-26 19:27:57 -04:00
void alai::player::PlayerFall::_register_methods()
{
godot::register_method("_state_enter", &PlayerFall::_state_enter);
godot::register_method("_state_exit", &PlayerFall::_state_exit);
godot::register_method("_physics_process", &PlayerFall::_physics_process);
}
2022-08-26 19:27:57 -04:00
alai::player::PlayerFall::PlayerFall()
{
}
2022-08-26 19:27:57 -04:00
alai::player::PlayerFall::~PlayerFall()
{
}
2022-08-26 19:27:57 -04:00
void alai::player::PlayerFall::_init()
{
2022-08-26 19:27:57 -04:00
_input = godot::Input::get_singleton();
}
2022-08-26 19:27:57 -04:00
void alai::player::PlayerFall::_state_enter()
{
2022-08-26 19:27:57 -04:00
animated_sprite = get_parent()->get_node<godot::AnimatedSprite>("AnimatedSprite");
animated_sprite->stop();
animated_sprite->set_animation("air");
}
2022-08-26 19:27:57 -04:00
void alai::player::PlayerFall::_state_exit()
{
animated_sprite->set_animation("move");
animated_sprite->set_frame(1);
}
2022-08-26 19:27:57 -04:00
void alai::player::PlayerFall::_physics_process(float delta)
{
2022-08-26 19:27:57 -04:00
auto parent = Object::cast_to<Player>(get_parent());
if (parent->is_on_floor())
{
get_state_machine()->change("Move");
return;
}
auto current_speed = parent->get_speed();
2022-10-10 21:54:12 -03:00
auto velocity = parent->get_velocity();
velocity.x = 0;
if (_input->is_action_pressed("run"))
{
current_speed *= parent->get_run_speed();
}
if (_input->is_action_pressed("right"))
{
velocity.x += current_speed;
}
if (_input->is_action_pressed("left"))
{
velocity.x += -current_speed;
}
if (velocity.x > 0)
{
animated_sprite->set_flip_h(true);
}
else if (velocity.x < 0)
{
animated_sprite->set_flip_h(false);
}
parent->set_velocity(velocity);
}