All pastes #886590 Raw Edit

Untitled

public text v1 · immutable
#886590 ·published 2008-01-31 19:56 UTC
rendered paste body
package ProneClient;




#the game event.
sub event_game {
	#the event
    	my $event = new SDL::Event;

	#network events
	my $netevent;

	#we do no read from keyboard.
	$readinput = 0;
	$line ="";
	#load the objects
	our $TILES;
	$TILES = objects_load();

	#load the map
	our @map =  Prone_load_map( "village" );

	our $EDITOR ;
	#the screen rect.
 	my $screen_rect = SDL::Rect->new(
                -height => 480,
                -width  => 640,
                -x      => 0,
                -y      => 0,
        );      
        my $black_color = SDL::Color->new(
                -r => 0x00,
                -g => 0x00,
                -b => 0x00,
        );

	#the loop timer
	my $timer = [gettimeofday()];
	
	#the animation timer
        my $anim_timer = [gettimeofday()];
	
	#the local player name
	$lpn = $local_player->name ;

	while( !$STOP ) {
		#poll network event
	        while ($netevent = $ProneClient::EventQueue->dequeue_nb) {
	                convert_net_event( $netevent ) ;
	        }
	
		#poll local events
	        while( $event->poll ) {
	                my $type = $event->type();

			#keyboard
			#key pressed
	                if( $type == SDL_KEYDOWN ) {
	                        my $key = $event->key_name() ;
	                        $key =~ s/\[|\]//g;
				#read string from input
	                        if( $readinput == 1 ) {
	                                $key = convert_input( $key );
	                                if ( $key =~ /return/ ) {
	                                        $ProneClient::CONSOLE->console_read( $line );
	                                        $line="";
						$readinput=0;
	                                }
	                                else {
	                                        $line .= $key;
	                                }
	
	                        }
				#read character from input
				else {
					$local_player->action($key);
				}
	                }
			#key released
			if( $type == SDL_KEYUP ) {
	                        my $key = $event->key_name() ;
				$local_player->stopaction($key);
					
			}
			#mouse
	                if( $type == 5 ) {
				#depending on actives windows
				#editor get priority
				if( defined( $EDITOR ) ) {
	                        	$EDITOR->action( $event->motion_x  ,$event->motion_y  );
				}
				if( defined( $CONSOLE ) ) {
	                        	$CONSOLE->action( $event->motion_x  ,$event->motion_y  );
				}
					
	                }
	
	        }
		

		#calculate tiles area to show.
	        my $min_x =  int( $local_player->x /32 ) - 10 ;
		my $max_x =  int( $local_player->x /32) + 10 ;
		my $min_y =  int( $local_player->y /32 ) - 8;
		my $max_y =  int( $local_player->y /32 ) + 8;
		my $xoff = $local_player->xoffset;	
		my $yoff = $local_player->yoffset;	
#		print "********************\n";
#		print "min x $min_x\n";
#		print "min y $min_y\n";
#		print "max x $max_x\n";
#		print "max y $max_y\n";
#		print "***********END********\n";

		#animate timer && animate stuff ( to be improved )
 		my $update = tv_interval( $anim_timer );
		#clear the screen
		$screen->fill( $screen_rect,$black_color );   	
                if(  $update   > 0.1) {
			#animate players !
			foreach my $p ( @players ) {
				$p->animate();
			}
			if( defined $EDITOR ) {
				my $content = $EDITOR->content;
				foreach my $dget ( @$content ) {
					if( $dget->animation > 1 ) {
						$dget->anim();
					}
				}
			}
			
                }

		#copy the map
		#640  20
		#320  15
		#performance hunting !
		#$min_x--;
		$max_x++;
		#$min_y--;
		$max_y++;
		my $i;
		my $j;
		for(  $i = $min_x ;$i< $max_x   ;$i++ ) {
			for (  $j = $min_y  ;$j< $max_y   ;$j++ ) {
				#std maps are 30x30 .
                	        if( $i <= 29 and $i >= 0  and $j >= 0 and $j <=29 ) {
					#animate stuff
					if(  $update   > 0.1 ) {
						$map[$i][$j]->anim;	
					}

                        	        $imgname = $map[$i][$j]->img ;
					#copy a tile
					prone_image_copy( $screen,$imgs->{$imgname},$i*32,$j*32,$xoff,$yoff );

                        	}
			
                	}
		}
		#done with anim, reset the timer.
		if(  $update   > 0.1 ) {
			$anim_timer = [gettimeofday()];
		}


		#players's movement and drawing.
		foreach my $p (  @players ){
			if( $p->state =~ /MOVE/ ) {
				$p->move() ;
			}
			if( $p->name =~ /$lpn/ ) { 
				#$screen = 
				prone_image_copy( $screen,$imgs->{$p->img()},320,240 );
			}       
			else {
				#$screen = 
				prone_image_copy( $screen,$imgs->{$p->img()},$p->x,$p->y,$xoff,$yoff );
			}
		}       
		
		#show standard windows if defined
	        $CONSOLE->show() if defined ( $CONSOLE );
	        $EDITOR->show() if defined ( $EDITOR );
	        $screen->sync();

		#the loop timer
		# it should be the same duration as in the server game loop.( stupid ? );
		my $slep = tv_interval( $timer );
		#print "SLEP $slep\n";
                if(  $slep   < 0.066) {
                        sleep( 0.066 - $slep );
                        $timer = [gettimeofday()];
                }
                else {
                        $timer = [gettimeofday()];
                }
	
	}
}
return 1;