Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Spectators::draw( ) {
- if ( !g_game.engine( )->is_in_game( ) || !g_eva.view_entity( ) )
- return;
- // view entity's name
- std::string view_name( g_eva.view_info( ).m_name );
- if ( view_name.empty( ) )
- return;
- // time to reach min / max alpha
- constexpr float opacity_fade_time = 255.f / 0.3f;
- // update observed state
- m_observed = ( m_spectator_count > 0 );
- // fade in/out
- if ( m_observed && m_spectator_text_opacity < 255 )
- m_spectator_text_opacity += opacity_fade_time * std::max( g_game.globals( )->m_frametime, 0.01f );
- else if ( !m_observed && m_spectator_text_opacity > 0 )
- m_spectator_text_opacity -= opacity_fade_time * std::max( g_game.globals( )->m_frametime, 0.01f );
- // need to clamp to be sure value is within range
- math::clamp( m_spectator_text_opacity, 0, 255 );
- // draw the "spectators" text if the alpha is greater than 0
- if ( m_spectator_text_opacity )
- g_draw.m_fonts->console( ).draw( "spectators", 8, g_draw.height( ) / 2, Color::white( m_spectator_text_opacity ) );
- // iterate through all player entities
- auto y = g_draw.height( ) / 2 + g_draw.m_fonts->console( ).height( ) + 2;
- for ( int i = 1; i < g_game.globals( )->m_max_clients; ++i ) {
- auto player = ( csgo::C_BasePlayer * )g_game.entity_list( )->get_client_entity( i );
- if ( !player || player->alive( ) )
- continue;
- // ref to this entity's alpha in speclist
- auto &opacity = m_opacities[ i ];
- // get this player's observe target - make sure they're spectating our view entity
- auto observed = ( csgo::C_BasePlayer * )g_game.entity_list( )->get_client_entity_from_handle( player->m_hObserverTarget( ) );
- // is the observe target our view entity ?
- bool observing_view_entity = observed ? ( observed == g_eva.view_entity( ) ) : false;
- // fade in / out
- if ( observing_view_entity && opacity < 255 ) {
- opacity += opacity_fade_time * std::max( g_game.globals( )->m_frametime, 0.01f );
- m_spectator_count++;
- } else if ( !observing_view_entity && opacity > 0 ) {
- opacity -= opacity_fade_time * std::max( g_game.globals( )->m_frametime, 0.01f );
- m_spectator_count--;
- }
- // clamp opacity to make sure alpha is within range
- math::clamp( opacity, 0, 255 );
- // opacity at 0, don't draw
- if ( !opacity )
- continue;
- // get this observing player's name
- csgo::IVEngineClient::player_info_t spectator_info;
- if ( !g_game.engine( )->get_player_info( i, &spectator_info ) )
- continue;
- std::string spec_name( spectator_info.m_name );
- spec_name.append( " -> " );
- // get the size of the line we're going to render
- auto size = g_draw.m_fonts->console( ).size( spec_name );
- // render the text - choose colour based on team
- auto team = player->m_iTeamNum( );
- g_draw.m_fonts->console( ).draw( spec_name, 16, y, ( team == csgo::TEAM_COUNTER_TERRORIST ) ? Color::light_blue( opacity ) : ( team == csgo::TEAM_TERRORIST ) ? Color::orange( opacity ) : Color::white( opacity ) );
- // draw our name
- g_draw.m_fonts->console( ).draw( view_name, 16 + size.m_width, y, Color::[censored]_green( opacity ) );
- // increment value of y by the height of the drawn string
- y += size.m_height + 2;
- }
- }
- void Spectators::register_listeners( ) {
- m_round_start = std::make_shared<csgo::IStandardEventListener>( [&]( csgo::IGameEvent *event ) {
- // reset spectator count
- m_spectator_count = 0;
- m_spectator_text_opacity = 0;
- } );
- m_player_death = std::make_shared<csgo::IStandardEventListener>( [&]( csgo::IGameEvent *event ) {
- if ( !g_eva.view_entity( ) )
- return;
- // make sure the view entity is the dying client
- auto userid = g_game.engine( )->get_player_for_user_id( event->get_int( "userid" ) );
- if ( userid != g_game.client_state( )->m_view_entity )
- return;
- // reset spectator count
- m_spectator_count = 0;
- m_spectator_text_opacity = 0;
- } );
- g_eva.register_listener( m_round_start, "round_start" );
- g_eva.register_listener( m_player_death, "player_death" );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement