[gnome-games/gnibbles-clutter] Now handling worm's status and moved move_worm_cb to gnibbles.c and renabed it



commit a55bb26b5f4b88b421bc21b509b31d183fc7a38f
Author: Guillaume Beland <guillaubel svn gnome org>
Date:   Tue Jul 7 11:44:31 2009 -0400

    Now handling worm's status and moved move_worm_cb to gnibbles.c and renabed it
    gnibbles_move_worms_clutter ();

 gnibbles/gnibbles.c |  132 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 gnibbles/gnibbles.h |    1 +
 gnibbles/main.c     |   58 ++--------------------
 3 files changed, 137 insertions(+), 54 deletions(-)
---
diff --git a/gnibbles/gnibbles.c b/gnibbles/gnibbles.c
index 05203f4..d90270d 100644
--- a/gnibbles/gnibbles.c
+++ b/gnibbles/gnibbles.c
@@ -708,6 +708,135 @@ gnibbles_add_bonus (gint regular)
 }
 
 gint
+gnibbles_move_worms_clutter (void)
+{
+  gint i, j, olddir, length, nbr_actor;
+  gint status = 1, nlives = 1;
+  gint *dead;
+
+  dead = g_new (gint, properties->numworms);
+
+  for (i = 0; i < properties->ai; i++) {
+    olddir = cworms[i]->direction;
+    gnibbles_cworm_ai_move (cworms[properties->human + i]);
+
+    if (olddir != cworms[i]->direction)
+      gnibbles_cworm_add_actor (cworms[i]);
+  }
+
+ if (boni->missed > MAXMISSED)
+   for (i = 0; i < properties->numworms; i++)
+     if (cworms[i]->score)
+       cworms[i]->score--;
+
+  for (i = 0; i < boni->numbonuses; i++) {
+    if (!(boni->bonuses[i]->countdown--)) {
+      if (boni->bonuses[i]->type == BONUSREGULAR && !boni->bonuses[i]->fake) {
+	      gnibbles_boni_remove_bonus (boni, 
+                                    boni->bonuses[i]->x, 
+                                    boni->bonuses[i]->y);
+	      boni->missed++;
+	      gnibbles_clutter_add_bonus (1);
+      } else {
+	      gnibbles_boni_remove_bonus (boni, 
+                                    boni->bonuses[i]->x, 
+                                    boni->bonuses[i]->y);
+      }
+    }
+  }
+
+  for (i = 0; i < properties->numworms; i++) {
+    dead[i] = !gnibbles_cworm_test_move_head (cworms[i]);
+    status &= !dead[i];
+  }
+
+  for (i = 0; i < properties->numworms; i++) {
+
+    nbr_actor = g_list_length (cworms[i]->list);
+    length = gnibbles_cworm_get_length (cworms[i]);
+    printf ("\nWorm ID: %d, Actors: %d, Length: %d,  xhead: %d, yhead:%d",
+            i, nbr_actor, length, cworms[i]->xhead, cworms[i]->yhead);
+
+    if (cworms[i]->xhead >= BOARDWIDTH) {
+      cworms[i]->xhead = 0;
+      gnibbles_cworm_add_actor(cworms[i]);
+    } else if (cworms[i]->xhead < 0) {
+      cworms[i]->xhead = BOARDWIDTH;
+      gnibbles_cworm_add_actor (cworms[i]);
+    } else if (cworms[i]->yhead >= BOARDHEIGHT) {
+      cworms[i]->yhead = 0;
+      gnibbles_cworm_add_actor (cworms[i]);
+    } else if (cworms[i]->xhead < 0) {
+      cworms[i]->yhead = BOARDHEIGHT;
+      gnibbles_cworm_add_actor (cworms[i]);
+    }
+
+    //if there's only one actor in the list, just move the actor
+    if (nbr_actor == 1 && !dead[i] && cworms[i]->lives > 0) {
+      gnibbles_cworm_move_straight_worm (cworms[i]);
+    } else if (nbr_actor >= 2 && !dead[i] && worms[i]->lives > 0) {
+      gnibbles_cworm_move_tail (cworms[i]);
+      if (g_list_length (cworms[i]->list) == 1)
+        gnibbles_cworm_move_straight_worm (cworms[i]);
+      else 
+        gnibbles_cworm_move_head (cworms[i]);
+    } else if (dead[i]) {
+      //worm's dead, do something clever about it...
+    }
+  }
+
+  for (i = 0; i < properties->numworms; i++) {
+    for (j = 0; j < properties->numworms; j++) {
+      if (i != j && cworms[i]->xhead == cworms[j]->xhead
+	        && cworms[i]->yhead == cworms[j]->yhead
+	        && cworms[i]->lives > 0
+	        && cworms[j]->lives > 0)
+	        dead[i] = TRUE;
+    }
+  }
+
+  for (i = 0; i < properties->numworms; i++) {
+    if (dead[i]) {
+      if (properties->numworms > 1)
+	      cworms[i]->score *= .7;
+      if (!gnibbles_cworm_lose_life (cworms[i])) {
+        /* One of the worms lost one life, but the round continues. */
+        // TODO: reset worm state
+        //gnibbles_worm_reset (cworms[i]);
+        //gnibbles_worm_set_start (cworms[i],
+				//                         cworms[i]->xstart,
+        //                				 cworms[i]->ystart,
+				//                         cworms[i]->direction_start);
+	      games_sound_play ("crash");
+	    }
+    }
+  }
+
+  if (status & GAMEOVER) {
+    games_sound_play ("crash");
+    games_sound_play ("gameover");
+    return (GAMEOVER);
+  }
+
+  for (i = 0; i < properties->numworms; i++) {
+    if (cworms[i]->lives > 0)
+      nlives += 1;
+  }
+  if (nlives == 1 && (properties->ai + properties->human > 1)) {
+    /* There is one player left, the other AI players are dead, and that player has won! */
+    return (VICTORY);
+  } else if (nlives == 0) {
+    /* There was only one worm, and it died. */
+    return (GAMEOVER);
+  }
+
+   /* Noone died, so the round can continue. */
+
+  g_free (dead);
+  return (CONTINUE);
+}
+
+gint
 gnibbles_move_worms (void)
 {
   gint i, j, status = 1, nlives = 0;
@@ -844,7 +973,8 @@ gnibbles_undraw_worms (gint data)
   gint i;
 
   for (i = 0; i < properties->numworms; i++)
-    gnibbles_worm_undraw_nth (worms[i], data);
+    gnibbles_cworm_shrink (cworms[i], data);
+    //gnibbles_worm_undraw_nth (worms[i], data);
 }
 
 void
diff --git a/gnibbles/gnibbles.h b/gnibbles/gnibbles.h
index d69402c..67a13f5 100644
--- a/gnibbles/gnibbles.h
+++ b/gnibbles/gnibbles.h
@@ -69,6 +69,7 @@ void gnibbles_clutter_init (void);
 void gnibbles_init (void);
 void gnibbles_clutter_add_bonus (gint regular);
 void gnibbles_add_bonus (gint regular);
+gint gnibbles_move_worms_clutter (void);
 gint gnibbles_move_worms (void);
 gint gnibbles_get_winner (void);
 gint gnibbles_keypress_worms (guint keyval);
diff --git a/gnibbles/main.c b/gnibbles/main.c
index defe013..f1ad486 100644
--- a/gnibbles/main.c
+++ b/gnibbles/main.c
@@ -515,54 +515,6 @@ new_game_2_cb (GtkWidget * widget, gpointer data)
   return (FALSE);
 }
 
-static void
-move_worm_cb (void)
-{
-  gint i, olddir, length, nbr_actor;
-
-  for (i = 0; i < properties->numworms; i++) {
-    // get the current direction of the worm
-    olddir = cworms[i]->direction;
-    // determine the new direction the worm will take
-    gnibbles_cworm_ai_move (cworms[i]);
-    // Add an actor when we change direction
-    if (olddir != cworms[i]->direction)
-      gnibbles_cworm_add_actor (cworms[i]);
-
-    nbr_actor = g_list_length (cworms[i]->list);
-    length = gnibbles_cworm_get_length (cworms[i]);
-    printf ("\nWorm ID: %d, Actors: %d, Length: %d,  xhead: %d, yhead:%d",
-            i, nbr_actor, length, cworms[i]->xhead, cworms[i]->yhead);
-
-    if (cworms[i]->xhead >= BOARDWIDTH) {
-      cworms[i]->xhead = 0;
-      gnibbles_cworm_add_actor(cworms[i]);
-    } else if (cworms[i]->xhead < 0) {
-      cworms[i]->xhead = BOARDWIDTH;
-      gnibbles_cworm_add_actor (cworms[i]);
-    } else if (cworms[i]->yhead >= BOARDHEIGHT) {
-      cworms[i]->yhead = 0;
-      gnibbles_cworm_add_actor (cworms[i]);
-    } else if (cworms[i]->xhead < 0) {
-      cworms[i]->yhead = BOARDHEIGHT;
-      gnibbles_cworm_add_actor (cworms[i]);
-    }
-    //if there's only one actor in the list, just move the actor
-    if (nbr_actor == 1) {
-      gnibbles_cworm_move_straight_worm (cworms[i]);
-    } else if (nbr_actor >= 2) {
-      gnibbles_cworm_move_tail (cworms[i]);
-      if (g_list_length (cworms[i]->list) == 1)
-        gnibbles_cworm_move_straight_worm (cworms[i]);
-      else 
-        gnibbles_cworm_move_head (cworms[i]);
-    } else if (nbr_actor < 1) {
-      //worm's dead, do something clever about it...
-      return;
-    }
-  }
-}
-
 gint
 new_game_clutter (void)
 {
@@ -793,7 +745,7 @@ end_game_cb (GtkAction * action, gpointer data)
 static gint
 add_bonus_cb (gpointer data)
 {
-  gnibbles_add_bonus (0);
+  //gnibbles_add_bonus (0);
   gnibbles_clutter_add_bonus (0);
   return (TRUE);
 }
@@ -804,7 +756,7 @@ restart_game_clutter (gpointer data)
   level = gnibbles_level_new (current_level);
   gnibbles_board_load_level (clutter_board, level);
   gnibbles_clutter_add_bonus (1);
-  dummy_id = g_timeout_add_seconds (1, (GSourceFunc) new_game_2_cb, NULL);
+  dummy_id = g_timeout_add_seconds (1, (GSourceFunc) new_game_clutter_2_cb, NULL);
   restart_id = 0;
   
   return FALSE;
@@ -852,7 +804,7 @@ main_loop (gpointer data)
   gchar *str = NULL;
 
   //status = gnibbles_move_worms ();
-  move_worm_cb ();
+  status = gnibbles_move_worms_clutter ();
   gnibbles_scoreboard_update (scoreboard);
 
   if (status == VICTORY) {
@@ -930,7 +882,7 @@ main_loop (gpointer data)
     erase_id = g_timeout_add (ERASETIME / ERASESIZE,
 			      (GSourceFunc) erase_worms_cb,
 			      (gpointer) ERASESIZE);
-    restart_id = g_timeout_add_seconds (1, (GSourceFunc) restart_game, NULL);
+    restart_id = g_timeout_add_seconds (1, (GSourceFunc) restart_game_clutter, NULL);
     return (FALSE);
   }
 
@@ -959,7 +911,7 @@ main_loop (gpointer data)
 	      tmp = rand () % MAXLEVEL + 1;
       current_level = tmp;
     }
-    restart_id = g_timeout_add_seconds (1, (GSourceFunc) restart_game, NULL);
+    restart_id = g_timeout_add_seconds (1, (GSourceFunc) restart_game_clutter, NULL);
     return (FALSE);
   }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]