Commit 81c7a09b authored by Gabriel Ivascu's avatar Gabriel Ivascu Committed by Iulian Radu

Prevent last level repeating forever

Given the current implementation, level 26 (the final one) keeps replaying
itself forever until you eventually run out of lives. So there is no way
to end a game unless you've lost all your lives.

By this commit, the game will finish after completing the final level
displaying a congratulations message along with the 'Play Again' button.

https://bugzilla.gnome.org/show_bug.cgi?id=762105
parent ef70798c
......@@ -775,7 +775,7 @@ public class Nibbles : Gtk.Application
return scorecats.first ();
}
private void log_score_cb (int score)
private void log_score_cb (int score, int level_reached)
{
/* Disable these here to prevent the user clicking the buttons before the score is saved */
new_game_action.set_enabled (false);
......@@ -786,13 +786,13 @@ public class Nibbles : Gtk.Application
if (game.numhumans != 1)
{
game_over (score, lowest_high_score);
game_over (score, lowest_high_score, level_reached);
return;
}
if (game.start_level != 1)
{
game_over (score, lowest_high_score);
game_over (score, lowest_high_score, level_reached);
return;
}
......@@ -809,7 +809,7 @@ public class Nibbles : Gtk.Application
warning ("Failed to add score: %s", e.message);
}
game_over (score, lowest_high_score);
game_over (score, lowest_high_score, level_reached);
});
}
......@@ -831,6 +831,9 @@ public class Nibbles : Gtk.Application
private void level_completed_cb ()
{
if (game.current_level == NibblesGame.MAX_LEVEL)
return;
new_game_action.set_enabled (false);
pause_action.set_enabled (false);
......@@ -904,17 +907,25 @@ public class Nibbles : Gtk.Application
preferences_dialog.run ();
}
private void game_over (int score, long lowest_high_score)
private void game_over (int score, long lowest_high_score, int level_reached)
{
var is_high_score = (score > lowest_high_score);
var is_game_won = (level_reached == NibblesGame.MAX_LEVEL + 1);
var game_over_label = new Gtk.Label (_(@"Game Over!"));
var game_over_label = new Gtk.Label (is_game_won ? _("Congratulations!") : _("Game Over!"));
game_over_label.halign = Gtk.Align.CENTER;
game_over_label.valign = Gtk.Align.START;
game_over_label.set_margin_top (window_height / 3);
game_over_label.set_margin_top (150);
game_over_label.get_style_context ().add_class ("menu-title");
game_over_label.show ();
var msg_label = new Gtk.Label (_("You have completed the game."));
msg_label.halign = Gtk.Align.CENTER;
msg_label.valign = Gtk.Align.START;
msg_label.set_margin_top (window_height / 3);
msg_label.get_style_context ().add_class ("menu-title");
msg_label.show ();
var score_string = ngettext ("%d Point".printf (score), "%d Points".printf (score), score);
var score_label = new Gtk.Label (@"<b>$(score_string)</b>");
score_label.set_use_markup (true);
......@@ -941,6 +952,7 @@ public class Nibbles : Gtk.Application
score_label.destroy ();
points_left_label.destroy ();
button.destroy ();
msg_label.destroy ();
new_game_action.set_enabled (true);
pause_action.set_enabled (true);
......@@ -950,6 +962,8 @@ public class Nibbles : Gtk.Application
button.show ();
overlay.add_overlay (game_over_label);
if (is_game_won)
overlay.add_overlay (msg_label);
if (game.numhumans == 1)
overlay.add_overlay (score_label);
if (game.numhumans == 1 && !is_high_score)
......
......@@ -47,7 +47,7 @@ public class NibblesGame : Object
public const char WORMCHAR = 'w';
public const char WARPCHAR = 'W';
private const int MAX_LEVEL = 26;
public const int MAX_LEVEL = 26;
public int start_level { get; private set; }
public int current_level { get; private set; }
......@@ -79,7 +79,7 @@ public class NibblesGame : Object
public signal void worm_moved (Worm worm);
public signal void bonus_applied (Worm worm);
public signal void log_score (int score);
public signal void log_score (int score, int level_reached);
public signal void animate_end_game ();
public signal void level_completed ();
......@@ -161,7 +161,7 @@ public class NibblesGame : Object
{
end ();
log_score (worms.first ().score);
log_score (worms.first ().score, current_level);
return Source.REMOVE;
}
......@@ -173,7 +173,7 @@ public class NibblesGame : Object
if (winner == null)
return Source.REMOVE;
log_score (winner.score);
log_score (winner.score, current_level);
return Source.REMOVE;
}
......@@ -184,8 +184,10 @@ public class NibblesGame : Object
animate_end_game ();
level_completed ();
if (current_level < MAX_LEVEL)
current_level++;
current_level++;
if (current_level == MAX_LEVEL + 1)
log_score (worms.first ().score, current_level);
return Source.REMOVE;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment