Game-Pexeso

 view release on metacpan or  search on metacpan

bin/pexeso  view on Meta::CPAN

#
# The animation to run when a matching pair is found.
#
sub matching_pair_animation {
	my $pexeso = shift;
	my $timeline = Clutter::Timeline->new(300);

	# Hide the cards
	$pexeso->card_1->fade($timeline);
	$pexeso->card_1(undef);
	$pexeso->card_2->fade($timeline);
	$pexeso->card_2(undef);
	$pexeso->disable_selection(0);

	# Check if the game is won (no more cards left)
	if ($pexeso->number_pairs == 0) {
		$timeline->signal_connect(completed => sub {
			$pexeso->winning_screen();
		});
	}

	# Start the timeline and keep a reference otherwise the signal connected
	# here will not run
	$timeline->start();
	$pexeso->timeline($timeline);
}


#
# Called to display a winning screen.
#
sub winning_screen {
	my $pexeso = shift;
	my $stage = $pexeso->stage;

	my $label = Clutter::Text->new(
		"Purisa Bold Italic 20",
		"You won!",
		Clutter::Color->new(0xFF, 0x80, 0x40, 0xFF),
	);
	$label->set_anchor_point_from_gravity('center');
	$label->set_position(
		($stage->get_width/2),
		($stage->get_height/2),
	);
	$label->show();
	$stage->add($label);


	# Animate the victory text
	my $timeline = Clutter::Timeline->new(300);
	my $alpha = Clutter::Alpha->new($timeline, 'linear');

	# Expand the card
	my $zoom = Clutter::Behaviour::Scale->new($alpha, 1.0, 1.0, 1.5, 1.5);
	$zoom->apply($label);

	# Keep a handle to the behaviours otherwise they wont be applied
	$pexeso->{zoom} = $zoom;

	# Start an infinite loop that will end after two iterations. In the first
	# iteration the text is zoomed in a linear way and at the second iteration
	# the text is zoomed out with a bouncy effect. Afterwards the animation is
	# stopped.
	$timeline->set_loop(TRUE);
	my $count = 0;
	$timeline->signal_connect(completed => sub {
		++$count;
		if ($count == 2) {
			$timeline->stop();
			return;
		}

		# Reverse the animation once completed
		$alpha->set_mode('ease-in-bounce');
		my $reverse = $timeline->get_direction eq 'forward' ? 'backward' : 'forward';
		$timeline->set_direction($reverse);
	});
	$timeline->start()
}


#
# Called to quit the game. If an optional error messages is passed it will be
# also printed.
#
sub quit {
	my $pexeso = shift;
	carp @_ if @_;
	Clutter->main_quit();
}



( run in 0.595 second using v1.01-cache-2.11-cpan-71847e10f99 )