[gnome-games/gnibbles-clutter] Factorized a bunch of code out of main into 3 functions to move worm



commit 890daaae114f4f5200d264a07e8a64b161914e39
Author: Guillaume Beland <guillaume beland gmail com>
Date:   Fri Jun 19 22:07:43 2009 -0400

    Factorized a bunch of code out of main into 3 functions to move worm
    
    Still need to figure out what's wrong with actor's manipulation

 gnibbles/gnibbles.c     |   25 ++++-----
 gnibbles/main.c         |   90 +------------------------------
 gnibbles/worm-clutter.c |  135 ++++++++++++++++++++++++++++++++++++++++++++++-
 gnibbles/worm-clutter.h |    4 ++
 4 files changed, 153 insertions(+), 101 deletions(-)
---
diff --git a/gnibbles/gnibbles.c b/gnibbles/gnibbles.c
index d2b2d1c..c9b4df7 100644
--- a/gnibbles/gnibbles.c
+++ b/gnibbles/gnibbles.c
@@ -488,17 +488,17 @@ gnibbles_move_worms (void)
   if (boni->missed > MAXMISSED)
     for (i = 0; i < properties->numworms; i++)
       if (worms[i]->score)
-	worms[i]->score--;
+	      worms[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_add_bonus (1);
+	      gnibbles_boni_remove_bonus (boni,
+				          boni->bonuses[i]->x, boni->bonuses[i]->y);
+	      boni->missed++;
+	      gnibbles_add_bonus (1);
       } else {
-	gnibbles_boni_remove_bonus (boni,
+	      gnibbles_boni_remove_bonus (boni,
 				    boni->bonuses[i]->x, boni->bonuses[i]->y);
       }
     }
@@ -538,18 +538,17 @@ gnibbles_move_worms (void)
   for (i = 0; i < properties->numworms; i++)
     if (dead[i]) {
       if (properties->numworms > 1)
-	worms[i]->score *= .7;
+	      worms[i]->score *= .7;
       if (!gnibbles_worm_lose_life (worms[i])) {
         /* One of the worms lost one life, but the round continues. */
         gnibbles_worm_reset (worms[i]);
         gnibbles_worm_set_start (worms[i],
-				 worms[i]->xstart,
-				 worms[i]->ystart,
-				 worms[i]->direction_start);
-	games_sound_play ("crash");
+				  worms[i]->xstart,
+				  worms[i]->ystart,
+				  worms[i]->direction_start);
+	      games_sound_play ("crash");
 	/* Don't return here.  May need to reset more worms. */
-	}
-
+	    }
     }
 
   if (status & GAMEOVER) {
diff --git a/gnibbles/main.c b/gnibbles/main.c
index 368ab89..164c257 100644
--- a/gnibbles/main.c
+++ b/gnibbles/main.c
@@ -1269,13 +1269,8 @@ move_worm_cb (ClutterTimeline *timeline, gint msecs, gpointer data)
   gint i, olddir, length, tmp_dir;
 
   for (i = 0; i < 4 /*numworms*/; i++) {
-    
-    ClutterActor *head = g_list_first (cworms[i]->list)->data;
-    ClutterActor *tail = g_list_last (cworms[i]->list)->data;
-
     // 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
@@ -1285,89 +1280,11 @@ move_worm_cb (ClutterTimeline *timeline, gint msecs, gpointer data)
     length = g_list_length (cworms[i]->list);
     //if there's only one actor in the list, just move the actor
     if (length == 1) {
-      clutter_actor_get_position (CLUTTER_ACTOR (head), &x, &y);
-      switch (cworms[i]->direction) {
-        case WORMRIGHT:
-          clutter_actor_set_position (CLUTTER_ACTOR (head), x + properties->tilesize, y);
-          cworms[i]->xhead += properties->tilesize;
-          break;
-        case WORMDOWN:
-          clutter_actor_set_position (CLUTTER_ACTOR (head), x, y + properties->tilesize);
-          cworms[i]->yhead += properties->tilesize;
-          break;
-        case WORMLEFT:
-          clutter_actor_set_position (CLUTTER_ACTOR (head), x - properties->tilesize, y);
-          cworms[i]->xhead -= properties->tilesize;
-          break;
-        case WORMUP:
-          clutter_actor_set_position (CLUTTER_ACTOR (head), x, y - properties->tilesize);
-          cworms[i]->yhead -= properties->tilesize;
-          break;
-        default:
-          break;
-      }
-    //if there's 2 actors or more, increase the size of the head actor, and
-    //decrease the size of the tail actor
+      gnibbles_cworm_move_straight_worm (cworms[i]);
     } else if (length >= 2) {
+      gnibbles_cworm_move_head (cworms[i]);
+      gnibbles_cworm_move_tail (cworms[i]);
 
-      clutter_actor_get_size (CLUTTER_ACTOR (head), &w, &h);
-      clutter_actor_get_position (CLUTTER_ACTOR (head), &x, &y);
-      size = w < h ? h : w;
-      // set the size of the head actor 
-      switch (cworms[i]->direction) {
-        case WORMRIGHT:
-          clutter_actor_set_size (CLUTTER_ACTOR (head), size + properties->tilesize, properties->tilesize);
-          cworms[i]->xhead += properties->tilesize;
-          break;
-        case WORMDOWN:
-          clutter_actor_set_size (CLUTTER_ACTOR (head), properties->tilesize, size + properties->tilesize);
-          cworms[i]->yhead += properties->tilesize;
-          break;
-        case WORMLEFT:
-          clutter_actor_set_size (CLUTTER_ACTOR (head), size + (2 * properties->tilesize), properties->tilesize);
-          clutter_actor_set_position (CLUTTER_ACTOR (head), x - properties->tilesize, y);
-          cworms[i]->xhead -= properties->tilesize;
-          break;
-        case WORMUP:
-          clutter_actor_set_size (CLUTTER_ACTOR (head), properties->tilesize, size + (2 * properties->tilesize));
-          clutter_actor_set_position (CLUTTER_ACTOR (head), x, y - properties->tilesize);
-          cworms[i]->yhead -= properties->tilesize;
-          break;
-        default:
-          break;
-      }
-
-      clutter_actor_get_size (CLUTTER_ACTOR (tail), &w, &h);
-      clutter_actor_get_position (CLUTTER_ACTOR (tail), &x, &y);
-      size = w < h ? h : w;
-      // decrease and move the tail actor if needed
-      // here the tmp_dir and WORMRIGHT & cie represent the position of the next
-      // actor
-      tmp_dir = gnibbles_cworm_get_next_actor_position (cworms[i]);
-      switch (tmp_dir) {
-        case WORMRIGHT:
-          clutter_actor_set_size (CLUTTER_ACTOR (tail), size - properties->tilesize, properties->tilesize);
-          clutter_actor_set_position (CLUTTER_ACTOR (tail), x + properties->tilesize, y);
-          cworms[i]->xtail -= properties->tilesize;
-          break;
-        case WORMDOWN:
-          clutter_actor_set_size (CLUTTER_ACTOR (tail), properties->tilesize, size - properties->tilesize);
-          clutter_actor_set_position (CLUTTER_ACTOR (tail), x, y + properties->tilesize);
-          cworms[i]->ytail -= properties->tilesize;
-          break;
-        case WORMLEFT:
-          clutter_actor_set_size (CLUTTER_ACTOR (tail), properties->tilesize * size, properties->tilesize);
-          clutter_actor_set_position (CLUTTER_ACTOR (tail), x - properties->tilesize, y);
-          cworms[i]->xtail += properties->tilesize;
-          break;
-        case WORMUP:
-          clutter_actor_set_size (CLUTTER_ACTOR (tail), properties->tilesize, properties->tilesize * size);
-          clutter_actor_set_position (CLUTTER_ACTOR (tail), x, y - properties->tilesize);
-          cworms[i]->ytail += properties->tilesize;
-          break;
-        default:
-          break;
-      } 
       if (size <= 0)
         gnibbles_cworm_remove_actor (cworms[i]);
     } else {
@@ -1377,7 +1294,6 @@ move_worm_cb (ClutterTimeline *timeline, gint msecs, gpointer data)
   }
 }
 
-
 int
 main (int argc, char **argv)
 {
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index 7561b2c..3ca9800 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -157,7 +157,7 @@ gnibbles_cworm_get_next_actor_position (GnibblesCWorm *worm)
   if (is_horizontal)
     dir = x2 > x1 ? WORMRIGHT : WORMLEFT;
   else
-   dir = y2 > y1 ? WORMDOWN : WORMUP;
+    dir = y2 > y1 ? WORMDOWN : WORMUP;
 
   return dir;
 }
@@ -246,6 +246,139 @@ gnibbles_cworm_resize (GnibblesCWorm *worm, gint newtile)
   }
 }
 
+void
+gnibbles_cworm_move_straight_worm (GnibblesCWorm *worm)
+{
+  gfloat x,y;
+  ClutterActor *head = g_list_first (worm->list)->data;
+
+  clutter_actor_get_position (CLUTTER_ACTOR (head), &x, &y);
+  switch (worm->direction) {
+    case WORMRIGHT:
+      clutter_actor_set_position (CLUTTER_ACTOR (head), 
+                                  x + properties->tilesize, y);
+      worm->xhead += properties->tilesize;
+      break;
+    case WORMDOWN:
+      clutter_actor_set_position (CLUTTER_ACTOR (head), 
+                                  x, y + properties->tilesize);
+      worm->yhead += properties->tilesize;
+      break;
+    case WORMLEFT:
+      clutter_actor_set_position (CLUTTER_ACTOR (head), 
+                                  x - properties->tilesize, y);
+      worm->xhead -= properties->tilesize;
+      break;
+    case WORMUP:
+      clutter_actor_set_position (CLUTTER_ACTOR (head), 
+                                  x, y - properties->tilesize);
+      worm->yhead -= properties->tilesize;
+      break;
+    default:
+      break;
+  }
+}
+
+void
+gnibbles_cworm_move_head (GnibblesCWorm *worm)
+{
+  gfloat w,h;
+  gfloat x,y;
+  guint size;
+
+  ClutterActor *head = g_list_first (worm->list)->data;
+
+  clutter_actor_get_size (CLUTTER_ACTOR (head), &w, &h);
+  clutter_actor_get_position (CLUTTER_ACTOR (head), &x, &y);
+  size = w < h ? h : w;
+  // set the size of the head actor 
+  switch (worm->direction) {
+    case WORMRIGHT:
+      clutter_actor_set_size (CLUTTER_ACTOR (head), 
+                              size + properties->tilesize, 
+                              properties->tilesize);
+      worm->xhead += properties->tilesize;
+      break;
+    case WORMDOWN:
+      clutter_actor_set_size (CLUTTER_ACTOR (head), 
+                              properties->tilesize, 
+                              size + properties->tilesize);
+      worm->yhead += properties->tilesize;
+      break;
+    case WORMLEFT:
+      clutter_actor_set_size (CLUTTER_ACTOR (head), 
+                              size + (2 * properties->tilesize), 
+                              properties->tilesize);
+      clutter_actor_set_position (CLUTTER_ACTOR (head), 
+                                  x - properties->tilesize, y);
+      worm->xhead -= properties->tilesize;
+      break;
+    case WORMUP:
+      clutter_actor_set_size (CLUTTER_ACTOR (head), 
+                              properties->tilesize, 
+                              size + (2 * properties->tilesize));
+      clutter_actor_set_position (CLUTTER_ACTOR (head), 
+                                  x, y - properties->tilesize);
+      worm->yhead -= properties->tilesize;
+      break;
+    default:
+      break;
+  }
+}
+
+void
+gnibbles_cworm_move_tail (GnibblesCWorm *worm)
+{
+  gfloat w,h;
+  gfloat x,y;
+  guint size;
+  gint tmp_dir;
+
+  ClutterActor *tail = g_list_last (worm->list)->data;
+
+  clutter_actor_get_size (CLUTTER_ACTOR (tail), &w, &h);
+  clutter_actor_get_position (CLUTTER_ACTOR (tail), &x, &y);
+  size = w < h ? h : w;
+
+  tmp_dir = gnibbles_cworm_get_next_actor_position (worm);
+  switch (tmp_dir) {
+   case WORMRIGHT:
+     clutter_actor_set_size (CLUTTER_ACTOR (tail), 
+                              size - properties->tilesize, 
+                              properties->tilesize);
+     clutter_actor_set_position (CLUTTER_ACTOR (tail), 
+                                 x + properties->tilesize, y);
+     worm->xtail -= properties->tilesize;
+     break;
+   case WORMDOWN:
+     clutter_actor_set_size (CLUTTER_ACTOR (tail), 
+                             properties->tilesize, 
+                             size - properties->tilesize);
+     clutter_actor_set_position (CLUTTER_ACTOR (tail), 
+                                 x, y + properties->tilesize);
+     worm->ytail -= properties->tilesize;
+     break;
+   case WORMLEFT:
+     clutter_actor_set_size (CLUTTER_ACTOR (tail), 
+                             properties->tilesize * size, 
+                             properties->tilesize);
+     clutter_actor_set_position (CLUTTER_ACTOR (tail), 
+                                 x - properties->tilesize, y);
+     worm->xtail += properties->tilesize;
+     break;
+   case WORMUP:
+     clutter_actor_set_size (CLUTTER_ACTOR (tail), 
+                             properties->tilesize, 
+                             properties->tilesize * size);
+     clutter_actor_set_position (CLUTTER_ACTOR (tail), 
+                                 x, y - properties->tilesize);
+     worm->ytail += properties->tilesize;
+     break;
+   default:
+     break;
+   } 
+}
+
 gint
 gnibbles_cworm_can_move_to (GnibblesCWorm * worm, gint x, gint y)
 {
diff --git a/gnibbles/worm-clutter.h b/gnibbles/worm-clutter.h
index e809750..9335d6d 100644
--- a/gnibbles/worm-clutter.h
+++ b/gnibbles/worm-clutter.h
@@ -77,6 +77,10 @@ void gnibbles_cworm_resize (GnibblesCWorm *worm, gint newtile);
 void gnibbles_cworm_move (ClutterTimeline *timeline, gint msecs, gpointer data);
 gint gnibbles_cworm_get_next_actor_position (GnibblesCWorm *worm);
 
+void gnibbles_cworm_move_straight_worm (GnibblesCWorm *worm);
+void gnibbles_cworm_move_head (GnibblesCWorm *worm);
+void gnibbles_cworm_move_tail (GnibblesCWorm *worm);
+
 gint gnibbles_cworm_handle_keypress (GnibblesCWorm * worm, guint keyval);
 void gnibbles_cworm_draw_head (GnibblesCWorm * worm);
 gint gnibbles_cworm_can_move_to (GnibblesCWorm * worm, gint x, gint y);



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