Meta-Widget-Gtk-Sprite

 view release on metacpan or  search on metacpan

Sprite.pm  view on Meta::CPAN

	{
		my ($self, $filename, $x, $y) = @_;
		my $img = Gtk::Gdk::ImlibImage->load_image($filename) || die "Could not load requested tile, $filename.  $!";
		my ( $cg, $cg_index ) = $self->_get_new_cgroup();
		$cg->hide;
		my $imgitem = $cg->new($cg, "Gnome::CanvasImage",
			'image' => $img,
			'x' => $x,
			'y' => $y,
			width => $img->rgb_width,
			height => $img->rgb_height,
		);
		$cg->{x} = $x;
		$cg->{y} = $y;
		$cg->{width} = $img->rgb_width;
		$cg->{height} = $img->rgb_height;
		#$cg->{radius} = sqrt($cg->{width}**2 + $cg->{height}**2)/2;
		$cg->{radius} = ($cg->{width} + $cg->{height})/4;
		$cg->{cx} = $cg->{x} + $cg->{width}/2;
		$cg->{cy} = $cg->{y} + $cg->{height}/2;
		my $index = $self->_add_sprite($cg);
		$cg->{index} = $index;
		return $index;
	}

=item $sprites->show( $sprite_number );

Makes the sprite appear on the canvas

=cut

sub show
	{
		my ($self, $item) = @_;
		$self->{sprite}->{$item}->show;
	}

=item $sprites->hide( $sprite_number );

Make the sprite picture disappear from the canvas.  Note that it can still collide with other sprites.  If you don't want it to hit anything, move it out of the way or ignore it in your own collision handler.

=cut

sub hide
	{
		my ($self, $item) = @_;
		$self->{sprite}->{$item}->hide;
	}


=item $sprites->destroy( $sprite_number );

Completely destroys a sprite.

=cut

sub destroy
	{
	}

sub update_sprite
	{
		my ($self, $item) = @_;
		my $cg = $self->{sprite}->{$item};
		$cg->{cx} = $cg->{x} + $cg->{width}/2;
		$cg->{cy} = $cg->{y} + $cg->{height}/2;
	}
		
		

=item $sprites->move_to( $sprite_number, 10, 20 );

Teleports the sprite named in $sprite_number to the position given immediately.  Contrast slide_to_xxx functions.

=cut

sub move_to
	{
		my ( $self, $index, $x, $y) = @_;
		#_debug "Moving sprite number $index";
		#_debug "Moving sprite with index $index and reef  ", ref( $self->{sprite}->{$index}), "\n";
		return unless (ref( $self->{sprite}->{$index}) =~ /CanvasGroup/i);
		my $deltax = $x-$self->{sprite}->{$index}->{x};
		my $deltay = $y-$self->{sprite}->{$index}->{y};
		$self->{sprite}->{$index}->{x} = $x;
		$self->{sprite}->{$index}->{y} = $y;
		_debug "time: ", time(), " index: $index x: $x, y: $y\n";
		$self->{sprite}->{$index}->move($deltax, $deltay);
		
	}

=item $sprites->slide_to_time( $sprite_number, $time, 10, 20 );

Will make the sprite $sprite_number 'slide' across the canvas to the position 10, 20.  It will take $time seconds to do so.  Slow speeds will appear jerky.

=cut

sub slide_to_time
	{
		my ( $self, $index, $time, $x, $y) = @_;
		if ( $time ==0 )
			{
				#The user really wanted move_to
				$self->move_to($index, $x, $y);
				#Aren't I a nice guy?
				return;
			}
		#$self->velocity($index, 1, 1);
		my $deltax = $x-$self->{sprite}->{$index}->{x};
		my $deltay = $y-$self->{sprite}->{$index}->{y};
		my $distance = sqrt($deltax**2 + $deltay**2);
		my $speed = $distance / $time;
		my $vx  = $deltax / $time*1000;
		my $vy = $deltay / $time*1000;
		$self->velocity($index, $vx, $vy);
		my $larger = (abs($deltax)>abs($deltay)) ? $deltax : $deltay;
		$self->{sprite}->{$index}->{timeout} = $time;
		_debug "Moving sprite $index to $x, $y (distance $distance) at speed $vx, $vy for $time milliseconds\n";
	}
sub _delta
	{



( run in 1.297 second using v1.01-cache-2.11-cpan-bbcb1afb8fc )