#include "character/states/common/FallState.h"
#include "character/CharacterObject.h"

namespace frostbite2D {

FallState::FallState()
    : CharacterStateBase(CharacterStateId::Fall) {
}

void FallState::OnEnter(CharacterStateMachine& machine,
                        CharacterStateContext& context,
                        CharacterStateId previousState) {
  (void)previousState;
  context.owner.PlayAnimationTag("fall");
  InvokeHook(machine, context.owner, context.owner.FindAction("fall"), "on_enter");
}

void FallState::OnUpdate(CharacterStateMachine& machine,
                         CharacterStateContext& context) {
  auto& owner = context.owner;
  auto& motor = owner.GetMotorMutable();
  motor.ApplyGroundInput(context.intent.moveX, context.intent.moveY, 0.75f);
  owner.SetFacing(motor.facing);

  if (motor.grounded) {
    ChangeState(machine, context, CharacterStateId::Landing);
    return;
  }

  if (motor.verticalVelocity > 0.0f) {
    ChangeState(machine, context, CharacterStateId::Jump);
    return;
  }

  context.commandBuffer.ClearBufferedActions();
}

} // namespace frostbite2D
