void Player::land_at_y(float y)
{
    if (state_ != PlayerState::Falling)
    {
        return;
    }

    snap_to_position(sf::Vector2f(position().x,y));
    switch_to_state(PlayerState::Landing);
}
void controls_handle_input( game* _pGame, int _pressedKey )
{
    switch ( _pressedKey )
    {
        // KEY_RETURN
        case '\r':
        {
            switch_to_state( _pGame, MAIN_MENU );

            break;
        }
    }
}
void Player::launch()
{
    if (state_ != PlayerState::Launching)
    {
        return;
    }

    float angle_with_direction =
        direction_ == Left ?
        180 - aim_angle_ : aim_angle_;

    float launch_charge_modified = std::sqrt(launch_charge_);

    sf::Vector2f launch_vector = unit_vector_from_angle(angle_with_direction);
    launch_vector *= launch_charge_modified * launch_impulse_speed_;
    launch_vector.y *= -1;

    velocity_ += launch_vector;

    jumpsnd_.Play();

    switch_to_state(PlayerState::Flying);
}
void in_game_handle_input( game* _pGame, int _pressedKey )
{
    if ( ((in_game_state*)_pGame->p_current_state)->bool_menu_is_active )
    {
        switch ( _pressedKey )
        {
            case KEY_UP:
            case 'W':
            case 'w':
            {
                // switch selected button( or not if continue button is the one which is selected ):
                if ( ((in_game_state*)_pGame->p_current_state)->selected_button == CONTINUE_BUTTON )
                    ; // do nothing
                else if (  ((in_game_state*)_pGame->p_current_state)->selected_button == MAIN_MENU_BUTTON )
                {
                    ((in_game_state*)_pGame->p_current_state)->selected_button = CONTINUE_BUTTON;
                    ((in_game_state*)_pGame->p_current_state)->p_continue->bool_is_selected  = 1;
                    ((in_game_state*)_pGame->p_current_state)->p_main_menu->bool_is_selected = 0;
                }
                else // EXIT_BUTTON
                {
                    ((in_game_state*)_pGame->p_current_state)->selected_button = MAIN_MENU_BUTTON;
                    ((in_game_state*)_pGame->p_current_state)->p_main_menu->bool_is_selected = 1;
                    ((in_game_state*)_pGame->p_current_state)->p_exit->bool_is_selected      = 0;
                }

                break;
            }

            // player walk down
            case KEY_DOWN:
            case 'S':
            case 's':
            {
                // switch selected button( or not if  exit button is the one which is selected ):
                if ( ((in_game_state*)_pGame->p_current_state)->selected_button == CONTINUE_BUTTON )
                {
                    ((in_game_state*)_pGame->p_current_state)->selected_button = MAIN_MENU_BUTTON;
                    ((in_game_state*)_pGame->p_current_state)->p_continue->bool_is_selected  = 0;
                    ((in_game_state*)_pGame->p_current_state)->p_main_menu->bool_is_selected = 1;
                }
                else if (  ((in_game_state*)_pGame->p_current_state)->selected_button == MAIN_MENU_BUTTON )
                {
                    ((in_game_state*)_pGame->p_current_state)->selected_button = EXIT_BUTTON;
                    ((in_game_state*)_pGame->p_current_state)->p_main_menu->bool_is_selected = 0;
                    ((in_game_state*)_pGame->p_current_state)->p_exit->bool_is_selected      = 1;
                }
                else // EXIT_BUTTON
                    ; // do nothing

                break;
            }

            // KEY_RETURN
            case '\r':
            {
                // continue game, switch current state( to main menu ) or exit game based on which button is selected
                if ( ((in_game_state*)_pGame->p_current_state)->selected_button == CONTINUE_BUTTON )
                {
                    // unpause game if player is alive and deactivate escape menu
                    if ( ((in_game_state*)_pGame->p_current_state)->p_player->alive )
                        ((in_game_state*)_pGame->p_current_state)->bool_paused = !((in_game_state*)_pGame->p_current_state)->bool_paused;
                    ((in_game_state*)_pGame->p_current_state)->bool_menu_is_active = 0;
                }
                else if (  ((in_game_state*)_pGame->p_current_state)->selected_button == MAIN_MENU_BUTTON )
                {
                    switch_to_state( _pGame, MAIN_MENU );
                }
                else // EXIT_BUTTON
                {
                    _pGame->bool_exit = 1; // exit request
                }

                break;
            }

            // Esc menu
            case '\e':
            {
                // unpause game if game is not over == player is alive
                if ( ((in_game_state*)_pGame->p_current_state)->p_player->alive )
                    ((in_game_state*)_pGame->p_current_state)->bool_paused = !((in_game_state*)_pGame->p_current_state)->bool_paused;

                // deactivate escape menu
                ((in_game_state*)_pGame->p_current_state)->bool_menu_is_active = 0;

                break;
            }
        }   // switch
    }
    else
    {
        if ( !((in_game_state*)_pGame->p_current_state)->p_player->alive )
void Player::update(float dt)
{
    if (state_ == PlayerState::Launching)
    {
        launch_charge_ += launch_charge_speed_ * dt;
        launch_charge_ = clamp(launch_charge_,0.0f,1.0f);
    }

    aimer_->hold(
        clamp(aimer_->sequence_frame("charge") +
         int(aimer_->sequence_duration("charge") * launch_charge_),
         0, aimer_->sequence_duration("charge") - 1));

    sf::Vector2f friction(0.0f,0.0f);
    if (!state_machine_.state_in_the_air(state_))
    {
        friction.x = -signum(velocity_.x);
        friction *= friction_constant_;
    }
    else
    {
        friction.x = -signum(velocity_.x);
        friction *= air_friction_constant_;
    }


    sf::Vector2f gravity_effect = current_gravity_;
    if (state_ == PlayerState::Winning)
    {
        gravity_effect = sf::Vector2f(0.0f,0.0f);
    }

    velocity_ += dt * (acceleration_ + gravity_effect);
    
    float velocity_after_friction = velocity_.x + dt * friction.x;
    // apply friction
    if (signum(velocity_.x) !=
        signum(velocity_after_friction))
    {
        float dv = velocity_after_friction - velocity_.x;
        float ratio = (0 - velocity_.x) / dv;

        move(sf::Vector2f(dt * (velocity_.x + ratio * dv),0.0f));
        velocity_.x = 0.0f;
    }
    else
    {
        velocity_.x = velocity_after_friction;
    }

    if (state_ == PlayerState::Winning)
    {
        if (dt > 0.0f)
        {
            sf::Vector2f dampening =  velocity_ * dt;
            velocity_ -= dampening;
        }
    }

    move(dt * (velocity_ + movement_velocity_));
    rotate_aim(dt * current_aim_speed_);

    if (state_ == PlayerState::Flying)
    {
        if (velocity_.y >= 0.0f)
        {
            switch_to_state(PlayerState::Falling);
        }
    }

    if (state_ == PlayerState::Landing)
    {
        if (!animation().playing())
        {
            switch_to_state(PlayerState::Idle);
        }
    }

    animation().update(dt);
    aimer_->update(dt);
}
void Player::init()
{
    jumpsndbuf_.LoadFromFile("sound/jump.wav");
    jumpsnd_.SetBuffer(jumpsndbuf_);

    reload_entity_data();

    const float* launch_speed = animation().maybe_constant("launch_speed");
    if (launch_speed)
    {
        launch_impulse_speed_ = *launch_speed;
    }
    else
    {
        launch_impulse_speed_ = 150.0f;
    }

    aimer_data_ = new AnimData("assets/aimer");
    aimer_ = new Animation(*aimer_data_);

    aimer_->set_alpha(128);

    sf::Vector2f aimer_origin = aimer_->point("origin");
    aimer_->set_origin(aimer_origin);

    const sf::Vector2f* aimer_scale = aimer_->maybe_point("scale");
    if (aimer_scale)
    {
        aimer_->set_scale(*aimer_scale);
    }

    aimer_->set_hidden(true);

    // set default values
    state_ = PlayerState::Idle;
    direction_ = Right;

    movement_speed_ = 200.0f;
    movement_velocity_ = sf::Vector2f(0.0f,0.0f);

    aim_angle_ = 45.0f;
    aim_movement_ = Up;
    aim_rotation_speed_ = 90.0f;
    current_aim_speed_ = 0.0f;

    const float* charge_time = animation().maybe_constant("charge_time");
    if (charge_time)
    {
        if (std::fabs(*charge_time) < 0.001)
        {
            launch_charge_speed_ = 1000;
        }
        else
        {
            launch_charge_speed_ = 1/(*charge_time);
        }
    }
    else
    {
        launch_charge_speed_ = 1.3f;
    }
    launch_charge_ = 0.0f;

    gravity_ = sf::Vector2f(0.0f,200.0f);
    current_gravity_ = sf::Vector2f(0.0f,0.0f);

    friction_constant_ = 200.0f;
    air_friction_constant_ = 30.0f;

    snap_to_position(sf::Vector2f(0.0f,0.0f));
    velocity_ = sf::Vector2f(0.0f,0.0f);
    acceleration_ = sf::Vector2f(0.0f,0.0f);

    // allow mechanism to properly switch state
    switch_to_state(PlayerState::Idle);
    switch_direction(Right);
}
Beispiel #7
0
int AddIn_main(int isAppli, unsigned short OptionNum)
{
    SaveData dataToSave;
    int i;
    Game_Data data;
	// variables for fps calculation
    unsigned int fps = 0, frame = 0, tempsOrigine = RTC_GetTicks();
	// char string to display the fps
    unsigned char fps_text[8] = {0};
	// rand initialisation
    srand(RTC_GetTicks());

    // Key init
    data.shift_latch_value = 0;
    data.alpha_latch_value = 0;

    data.entry_highscores = NULL;
    data.entry_highscores = malloc(sizeof(float) * 6);
    if(data.entry_highscores == NULL)
        switch_to_state(OUT_OF_MEMORY, &data);


    data.entry_difficulties = NULL;
    data.entry_difficulties = malloc(sizeof(char*) * 6);
    if(data.entry_difficulties == NULL)
        switch_to_state(OUT_OF_MEMORY, &data);
    load_difficulty_names(data.entry_difficulties);


    loadDataFromSave(&data);
    switch_to_state(TITLE, &data);

    {
        char machin[] = "0";
        machin[0] = GetMPU() + '0';
        locate(1,1);
        Print(machin);
        GetKey(&i);

    }

    while(KeyUp(K_EXIT)){ // main loop
        // fps
        if(RTC_GetTicks() - tempsOrigine >= 32 )// if 1/4 seconds elapsed
        {
            fps = frame*4;
            frame = 0;
            tempsOrigine = RTC_GetTicks();
        }
        frame++;


        update(&data);
        draw(&data);

	// printing debug information

	// updating the screen
        ML_display_vram();
        ML_clear_vram();

        Sleep(1/0.06);
    }

    for(i = 0; i < 6; ++i)
        dataToSave.highscores[i] = data.entry_highscores[i];
    Bfile_CreateFile(filename, filesize);
    data.fileHandle = Bfile_OpenFile(filename, _OPENMODE_WRITE);
    if(data.fileHandle >= 0) {
        Bfile_WriteFile(data.fileHandle, &dataToSave, filesize);
        Bfile_CloseFile(data.fileHandle);
    }

    free(data.entry_highscores);

    return 1;
}