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

namespace frostbite2D {

JumpState::JumpState()
    : CharacterStateBase(CharacterStateId::Jump) {
}

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

void JumpState::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::Fall);
    return;
  }

  context.commandBuffer.ClearBufferedActions();
}

} // namespace frostbite2D
