[gnome-nibbles] Switch to next level when all bonuses are consumed. Refactor move ()
- From: Iulian Radu <iulianradu src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-nibbles] Switch to next level when all bonuses are consumed. Refactor move ()
- Date: Mon, 26 Oct 2015 00:18:19 +0000 (UTC)
commit 34ae74e90040818e343b5907e4d6311f27ed94b7
Author: Iulian Radu <iulian radu67 gmail com>
Date: Sat Aug 1 20:04:39 2015 +0300
Switch to next level when all bonuses are consumed. Refactor move ()
data/nibbles.ui | 5 +++-
src/boni.vala | 9 +++++++
src/gnome-nibbles.vala | 34 ++++++++++++++++++++++++-----
src/nibbles-game.vala | 40 ++++++++++++++++++++++++++-------
src/nibbles-view.vala | 56 ++++++++++++++++++++---------------------------
src/worm.vala | 24 +++++++++++++------
6 files changed, 112 insertions(+), 56 deletions(-)
---
diff --git a/data/nibbles.ui b/data/nibbles.ui
index 56bf3eb..9f1b062 100644
--- a/data/nibbles.ui
+++ b/data/nibbles.ui
@@ -248,10 +248,13 @@
<child>
<object class="GtkLabel" id="countdown">
<property name="visible">True</property>
- <property name="label">5</property>
+ <property name="label">3</property>
<property name="halign">center</property>
<style><class name="countdown"/></style>
</object>
+ <packing>
+ <property name="name">countdown</property>
+ </packing>
</child>
</object>
</child>
diff --git a/src/boni.vala b/src/boni.vala
index c387f4a..d83ff42 100644
--- a/src/boni.vala
+++ b/src/boni.vala
@@ -71,6 +71,15 @@ public class Boni : Object
bonus_removed (bonus);
}
+ public void reset (int numworms)
+ {
+ bonuses.clear ();
+ missed = 0;
+ numboni = 8 + numworms;
+ numbonuses = 0;
+ numleft = numboni;
+ }
+
public Bonus? get_bonus (int[,] walls, int x, int y)
{
foreach (var bonus in bonuses)
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 29aae3e..fd79eda 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -45,7 +45,7 @@ public class Nibbles : Gtk.Application
private NibblesView? view;
private NibblesGame? game = null;
- private const int COUNTDOWN_TIME = 0;
+ private const int COUNTDOWN_TIME = 3;
private const ActionEntry action_entries[] =
{
@@ -140,6 +140,7 @@ public class Nibbles : Gtk.Application
/* Load game */
game = new NibblesGame (settings);
game.log_score.connect (log_score_cb);
+ game.restart_game.connect (restart_game_cb);
view = new NibblesView (game);
view.show ();
@@ -160,8 +161,7 @@ public class Nibbles : Gtk.Application
if (first_run)
show_first_run_screen ();
else
- // show_new_game_screen_cb ();
- start_game_cb ();
+ show_new_game_screen_cb ();
window.show_all ();
@@ -242,8 +242,6 @@ public class Nibbles : Gtk.Application
private void start_game_cb ()
{
settings.set_boolean ("first-run", false);
- stderr.printf("[Debug] cl %d\n", game.current_level);
- stderr.printf("[Debug] %d\n", game.numworms);
/* TODO Fix problem and remove this call
* For some reason tile_size gets set to 0 after calling
@@ -270,9 +268,16 @@ public class Nibbles : Gtk.Application
game.add_worms ();
show_game_view ();
+ start_game_with_countdown ();
+ }
+
+ public void start_game_with_countdown ()
+ {
+ statusbar_stack.set_visible_child_name ("countdown");
+
var seconds = COUNTDOWN_TIME;
Timeout.add (1000, () => {
- countdown.set_label ("%d".printf (seconds));
+ countdown.set_label (seconds.to_string ());
if (seconds == 0)
{
statusbar_stack.set_visible_child_name ("scoreboard");
@@ -284,6 +289,23 @@ public class Nibbles : Gtk.Application
});
}
+ private void restart_game_cb ()
+ {
+ view.new_level (game.current_level);
+
+ foreach (var worm in game.worms)
+ {
+ var actors = view.worm_actors.get (worm);
+ if (actors.get_stage () == null) {
+ view.stage.add_child (actors);
+ }
+ actors.show ();
+ }
+
+ game.add_worms ();
+ start_game_with_countdown ();
+ }
+
private void show_first_run_screen ()
{
main_stack.set_visible_child_name ("first_run");
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 214fbf2..8552ba4 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -52,6 +52,8 @@ public class NibblesGame : Object
public const char EMPTYCHAR = 'a';
public const char WORMCHAR = 'w';
+ public const int MAX_LEVEL = 26;
+
public int current_level;
public int[,] walls;
@@ -73,6 +75,7 @@ public class NibblesGame : Object
public signal void loop_started ();
public signal void log_score (int score);
public signal void animate_end_game ();
+ public signal void restart_game ();
public Gee.HashMap<Worm, WormProperties?> worm_props;
@@ -81,6 +84,11 @@ public class NibblesGame : Object
boni = new Boni (numworms);
walls = new int[WIDTH, HEIGHT];
worms = new Gee.LinkedList<Worm> ();
+ for (int i = 0; i < numworms; i++)
+ {
+ var worm = new Worm (i);
+ worms.add (worm);
+ }
worm_props = new Gee.HashMap<Worm, WormProperties?> ();
Random.set_seed ((uint32) time_t ());
@@ -89,6 +97,7 @@ public class NibblesGame : Object
public void start ()
{
+ stderr.printf("[Debug] Game started\n");
add_bonus (true);
main_id = Timeout.add (GAMEDELAY * speed, main_loop_cb);
@@ -287,13 +296,7 @@ public class NibblesGame : Object
continue;
}
- if (worm.change > 0)
- {
- worm.move (walls, false);
- worm.change--;
- }
- else
- worm.move (walls, true);
+ worm.move (walls);
}
foreach (var worm in dead_worms)
@@ -393,6 +396,19 @@ public class NibblesGame : Object
return Source.REMOVE;
}
+ else if (status == GameStatus.NEWROUND)
+ {
+ stop ();
+
+ if (current_level < MAX_LEVEL)
+ current_level++;
+
+ animate_end_game ();
+
+ restart_game ();
+
+ return Source.REMOVE;
+ }
move_worms ();
return Source.CONTINUE;
@@ -410,13 +426,19 @@ public class NibblesGame : Object
}
if (worms_left == 1 && numworms > 1)
+ {
/* There were multiple worms but only one is still alive */
return GameStatus.VICTORY;
- else if (worms_left == 0) {
+ }
+ else if (worms_left == 0)
+ {
/* There was only one worm and it died */
return GameStatus.GAMEOVER;
}
+ if (boni.numleft == 0)
+ return GameStatus.NEWROUND;
+
return null;
}
@@ -497,5 +519,5 @@ public enum GameStatus
{
GAMEOVER,
VICTORY,
- NEXTROUND
+ NEWROUND
}
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 1a523c5..8d3a3ba 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -35,6 +35,15 @@ public class NibblesView : GtkClutter.Embed
_game.bonus_applied.connect (bonus_applied_cb);
_game.animate_end_game.connect (animate_end_game_cb);
+
+ foreach (var worm in _game.worms)
+ {
+ worm.added.connect (worm_added_cb);
+ worm.moved.connect (worm_moved_cb);
+ worm.rescaled.connect (worm_rescaled_cb);
+ worm.died.connect (worm_died_cb);
+ worm.tail_reduced.connect (worm_tail_reduced_cb);
+ }
}
}
@@ -99,8 +108,10 @@ public class NibblesView : GtkClutter.Embed
error (_("Nibbles couldn't find pixmap file: %s"), filename);
}
+ worm_actors.clear ();
+ bonus_actors.clear ();
+ game.boni.reset (game.numworms);
- stderr.printf("[Debug] %d\n", game.tile_size);
for (int i = 0; i < NibblesGame.HEIGHT; i++)
{
if ((tmpboard = file.read_line ()) == null)
@@ -118,52 +129,44 @@ public class NibblesView : GtkClutter.Embed
game.walls[j, i] = NibblesGame.EMPTYCHAR;
if (count < game.numworms)
{
- var worm = new Worm (count++, WormDirection.UP);
- connect_signals (worm);
- worm.set_start (j, i);
- game.worms.add (worm);
+ game.worms[count].set_start (j, i, WormDirection.UP);
var actors = new WormActor ();
- worm_actors.set (worm, actors);
+ worm_actors.set (game.worms[count], actors);
+ count++;
}
break;
case 'n':
game.walls[j, i] = NibblesGame.EMPTYCHAR;
if (count < game.numworms)
{
- var worm = new Worm (count++, WormDirection.LEFT);
- connect_signals (worm);
- worm.set_start (j, i);
- game.worms.add (worm);
+ game.worms[count].set_start (j, i, WormDirection.LEFT);
var actors = new WormActor ();
- worm_actors.set (worm, actors);
+ worm_actors.set (game.worms[count], actors);
+ count++;
}
break;
case 'o':
game.walls[j, i] = NibblesGame.EMPTYCHAR;
if (count < game.numworms)
{
- var worm = new Worm (count++, WormDirection.DOWN);
- connect_signals (worm);
- worm.set_start (j, i);
- game.worms.add (worm);
+ game.worms[count].set_start (j, i, WormDirection.DOWN);
var actors = new WormActor ();
- worm_actors.set (worm, actors);
+ worm_actors.set (game.worms[count], actors);
+ count++;
}
break;
case 'p':
game.walls[j, i] = NibblesGame.EMPTYCHAR;
if (count < game.numworms)
{
- var worm = new Worm (count++, WormDirection.RIGHT);
- connect_signals (worm);
- worm.set_start (j, i);
- game.worms.add (worm);
+ game.worms[count].set_start (j, i, WormDirection.RIGHT);
var actors = new WormActor ();
- worm_actors.set (worm, actors);
+ worm_actors.set (game.worms[count], actors);
+ count++;
}
break;
default:
@@ -172,8 +175,6 @@ public class NibblesView : GtkClutter.Embed
}
}
- stderr.printf("[Debug] %d\n", game.tile_size);
- stderr.printf("[Debug] Loading level\n");
load_level ();
}
@@ -358,15 +359,6 @@ public class NibblesView : GtkClutter.Embed
level.restore_easing_state ();
}
- private void connect_signals (Worm worm)
- {
- worm.added.connect (worm_added_cb);
- worm.moved.connect (worm_moved_cb);
- worm.rescaled.connect (worm_rescaled_cb);
- worm.died.connect (worm_died_cb);
- worm.tail_reduced.connect (worm_tail_reduced_cb);
- }
-
public void board_rescale (int tile_size)
{
int board_width, board_height;
diff --git a/src/worm.vala b/src/worm.vala
index 62ce5f6..2de6349 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -74,11 +74,10 @@ public class Worm : Object
public signal void bonus_found ();
- public Worm (int id, WormDirection direction)
+ public Worm (int id)
{
this.id = id;
is_human = true;
- starting_direction = direction;
lives = STARTING_LIVES;
score = 0;
change = 0;
@@ -91,8 +90,10 @@ public class Worm : Object
return list.first ();
}
- public void set_start (int xhead, int yhead)
+ public void set_start (int xhead, int yhead, WormDirection direction)
{
+ list.clear ();
+
starting_position = Position () {
x = xhead,
y = yhead
@@ -100,10 +101,13 @@ public class Worm : Object
list.add (starting_position);
+ starting_direction = direction;
this.direction = starting_direction;
+ change = 0;
+ key_queue.clear ();
}
- public void move (int[,] walls, bool remove)
+ public void move (int[,] walls)
{
if (is_human)
keypress = false;
@@ -138,14 +142,17 @@ public class Worm : Object
/* Add a new body piece */
list.offer_head (position);
- if (remove)
+ if (change > 0)
+ {
+ change--;
+ added ();
+ }
+ else
{
walls[list.last ().x, list.last ().y] = NibblesGame.EMPTYCHAR;
list.poll_tail ();
moved ();
}
- else
- added ();
/* Check for bonus before changing tile */
if (walls[head ().x, head ().y] != NibblesGame.EMPTYCHAR)
@@ -203,8 +210,9 @@ public class Worm : Object
public void spawn (int[,] walls)
{
+ change = STARTING_LENGTH;
for (int i = 0; i < STARTING_LENGTH; i++)
- move (walls, false);
+ move (walls);
}
public void lose_life ()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]