[gnome-games] Code refactoring & clean up & some more tweak on worm



commit 18a8f48b14e52bc00913edd728fac3aa9e9531ad
Author: Guillaume Beland <guillaume beland gmail com>
Date:   Wed Jun 24 22:24:41 2009 -0400

    Code refactoring & clean up & some more tweak on worm
    
    -Removed gnibbles_cworm_add_actor_with_position as it is useless,
    -Making some utility function private
    -Removed unused code and general clean up of the code
    -Keeping track of Head and Tail position,
    -...and various code improvement

 gnibbles/level.c        |    4 +-
 gnibbles/main.c         |   16 +---
 gnibbles/worm-clutter.c |  186 ++++++++++++++++++++++------------------------
 gnibbles/worm-clutter.h |   17 +---
 4 files changed, 100 insertions(+), 123 deletions(-)
---
diff --git a/gnibbles/level.c b/gnibbles/level.c
index f5f3d3d..9926801 100644
--- a/gnibbles/level.c
+++ b/gnibbles/level.c
@@ -86,12 +86,12 @@ gnibbles_level_new (gint level)
         case 'n':
           lvl->walls[j][i] = EMPTYCHAR;
           if (count < properties->numworms)
-            cworms[count] = gnibbles_cworm_new (count++, j, i, WORMDOWN);
+            cworms[count] = gnibbles_cworm_new (count++, j, i, WORMLEFT);
           break;
         case 'o':
           lvl->walls[j][i] = EMPTYCHAR;
           if (count < properties->numworms)
-            cworms[count] = gnibbles_cworm_new (count++, j, i, WORMLEFT);
+            cworms[count] = gnibbles_cworm_new (count++, j, i, WORMDOWN);
           break;
         case 'p':
           lvl->walls[j][i] = EMPTYCHAR;
diff --git a/gnibbles/main.c b/gnibbles/main.c
index 39ec3bd..2c646eb 100644
--- a/gnibbles/main.c
+++ b/gnibbles/main.c
@@ -1287,24 +1287,16 @@ move_worm_cb (ClutterTimeline *timeline, gint msecs, gpointer data)
 
     if (cworms[i]->xhead >= BOARDWIDTH) {
       cworms[i]->xhead = 0;
-      gnibbles_cworm_add_actor_with_position (cworms[i],
-                                              cworms[i]->xhead,
-                                              cworms[i]->yhead);
+      gnibbles_cworm_add_actor(cworms[i]);
     } else if (cworms[i]->xhead < 0) {
       cworms[i]->xhead = BOARDWIDTH;
-      gnibbles_cworm_add_actor_with_position (cworms[i],
-                                              cworms[i]->xhead,
-                                              cworms[i]->yhead);
+      gnibbles_cworm_add_actor (cworms[i]);
     } else if (cworms[i]->yhead >= BOARDHEIGHT) {
       cworms[i]->yhead = 0;
-      gnibbles_cworm_add_actor_with_position (cworms[i],
-                                              cworms[i]->xhead,
-                                              cworms[i]->yhead);
+      gnibbles_cworm_add_actor (cworms[i]);
     } else if (cworms[i]->xhead < 0) {
       cworms[i]->yhead = BOARDHEIGHT;
-      gnibbles_cworm_add_actor_with_position (cworms[i],
-                                              cworms[i]->xhead,
-                                              cworms[i]->yhead);
+      gnibbles_cworm_add_actor (cworms[i]);
     }
     //if there's only one actor in the list, just move the actor
     if (nbr_actor == 1) {
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index 32cd85d..ff117cd 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -45,6 +45,52 @@ extern GnibblesBoni *boni;
 extern GnibblesWarpManager *warpmanager;
 extern GnibblesCWorm *cworms[NUMWORMS];
 
+static ClutterActor*
+gnibbles_cworm_get_head_actor (GnibblesCWorm *worm)
+{
+  return CLUTTER_ACTOR (g_list_first (worm->list)->data);
+}
+
+static ClutterActor*
+gnibbles_cworm_get_tail_actor (GnibblesCWorm *worm)
+{
+  return CLUTTER_ACTOR (g_list_last (worm->list)->data);
+}
+
+static gint
+gnibbles_cworm_get_tail_direction (GnibblesCWorm *worm)
+{
+  gfloat w,h;
+  gfloat x1,y1,x2,y2;
+  gint dir = -1;
+  gboolean is_horizontal;
+  GValue val = {0,};
+  g_value_init (&val, G_TYPE_BOOLEAN);
+
+  ClutterActor *tail = gnibbles_cworm_get_tail_actor (worm);
+  ClutterActor *next = g_list_previous (g_list_last (worm->list))->data;
+  
+  g_object_get_property (G_OBJECT (tail), "repeat-x", &val);
+  is_horizontal = g_value_get_boolean (&val);
+
+  clutter_actor_get_position (CLUTTER_ACTOR (next), &x2, &y2);
+  clutter_actor_get_size (CLUTTER_ACTOR (next), &w, &h);
+  clutter_actor_get_position (CLUTTER_ACTOR (tail), &x1, &y1);
+  
+  if (is_horizontal) {
+    if (x2 > x1)
+      dir = WORMRIGHT;
+    else if (x2 == x1)
+      dir = WORMLEFT;
+  } else {
+    if (y2 > y1)
+      dir = WORMDOWN;
+    else if (y2 == y1)
+      dir = WORMUP;
+  }
+
+  return dir;
+}
 
 GnibblesCWorm*
 gnibbles_cworm_new (guint number, guint t_xhead,
@@ -81,10 +127,6 @@ gnibbles_cworm_add_actor (GnibblesCWorm *worm)
   g_value_init (&val, G_TYPE_BOOLEAN);
   g_value_set_boolean (&val, TRUE);
 
-  clutter_actor_set_position (CLUTTER_ACTOR (actor),
-                              worm->xhead * properties->tilesize,
-                              worm->yhead * properties->tilesize);
-
   g_object_set_property (G_OBJECT (actor), "keep-aspect-ratio", &val);
 
   ClutterActor *tmp = NULL;
@@ -95,18 +137,21 @@ gnibbles_cworm_add_actor (GnibblesCWorm *worm)
     size = SLENGTH;
     worm->length = size;
   }
-  
+
+  gfloat x,y;
+  clutter_actor_get_position (CLUTTER_ACTOR (actor), &x, &y);
+
   if (worm->direction == WORMRIGHT || worm->direction == WORMLEFT) {
-    // if it's the worm's head, set its position    
+    // if it's the worm's head, set its size    
     if (!tmp) {
       clutter_actor_set_size (CLUTTER_ACTOR (actor),
                               properties->tilesize * size,
                               properties->tilesize);
 
       if (worm->direction == WORMRIGHT)
-        worm->xhead += size * properties->tilesize;
+        worm->xhead += size;
       else 
-        worm->xhead -= size * properties->tilesize;
+        worm->xhead -= size;
 
     } else {
       clutter_actor_set_size (CLUTTER_ACTOR (actor), 0, properties->tilesize);
@@ -114,56 +159,42 @@ gnibbles_cworm_add_actor (GnibblesCWorm *worm)
 
     g_object_set_property (G_OBJECT (actor), "repeat-x", &val);
   } else if (worm->direction == WORMDOWN || worm->direction == WORMUP) {
-    // if it's the worm's head, set its position
+    // if it's the worm's head, set its size
     if (!tmp) {
       clutter_actor_set_size (CLUTTER_ACTOR (actor),
-                          properties->tilesize,
-                          properties->tilesize * size);
+                              properties->tilesize,
+                              properties->tilesize * size);
 
       if (worm->direction == WORMDOWN)
-        worm->yhead += size * properties->tilesize;
+        worm->yhead += size;
       else 
-        worm->yhead -= size * properties->tilesize;
+        worm->yhead -= size;
 
     } else {
-      clutter_actor_set_size (CLUTTER_ACTOR (actor), properties->tilesize, 0);
+      clutter_actor_set_size (CLUTTER_ACTOR (actor), properties->tilesize, 0);                  
     }
     
     g_object_set_property (G_OBJECT (actor), "repeat-y", &val);
   }
 
+  clutter_actor_set_position (CLUTTER_ACTOR (actor),
+                              worm->xhead * properties->tilesize,
+                              worm->yhead * properties->tilesize);
+
+
   clutter_container_add_actor (CLUTTER_CONTAINER (worm->actors), actor);  
   worm->list = g_list_prepend (worm->list, actor);
 }
 
 void
-gnibbles_cworm_add_actor_with_position (GnibblesCWorm *worm, 
-                                        gint t_xhead, gint t_yhead)
+gnibbles_cworm_remove_actor (GnibblesCWorm *worm)
 {
-  ClutterActor *actor = NULL;
-  GValue val = {0,};
-  printf ("\nCALLED\n");
-  actor = gtk_clutter_texture_new_from_pixbuf (worm_pixmaps[worm->number]);
-
-  g_value_init (&val, G_TYPE_BOOLEAN);
-  g_value_set_boolean (&val, TRUE);
-
-  clutter_actor_set_position (CLUTTER_ACTOR (actor),
-                              t_xhead * properties->tilesize,
-                              t_yhead * properties->tilesize);
+  g_return_if_fail (worm->list);
 
-  g_object_set_property (G_OBJECT (actor), "keep-aspect-ratio", &val);
-  
-  if (worm->direction == WORMRIGHT || worm->direction == WORMLEFT) {    
-    clutter_actor_set_size (CLUTTER_ACTOR (actor), 0, properties->tilesize);
-    g_object_set_property (G_OBJECT (actor), "repeat-x", &val);
-  } else if (worm->direction == WORMDOWN || worm->direction == WORMUP) {
-    clutter_actor_set_size (CLUTTER_ACTOR (actor), properties->tilesize, 0);
-    g_object_set_property (G_OBJECT (actor), "repeat-y", &val);
-  }
+  ClutterActor *tmp = gnibbles_cworm_get_tail_actor (worm);
+  worm->list = g_list_delete_link (worm->list, g_list_last (worm->list));
 
-  clutter_container_add_actor (CLUTTER_CONTAINER (worm->actors), actor);  
-  worm->list = g_list_prepend (worm->list, actor);
+  clutter_container_remove_actor (CLUTTER_CONTAINER (worm->actors), tmp);
 }
 
 void
@@ -182,29 +213,6 @@ gnibbles_cworm_inverse (GnibblesCWorm *worm)
   worm->list = g_list_reverse (worm->list);
 }
 
-ClutterActor*
-gnibbles_cworm_get_head_actor (GnibblesCWorm *worm)
-{
-  return CLUTTER_ACTOR (g_list_first (worm->list)->data);
-}
-
-ClutterActor*
-gnibbles_cworm_get_tail_actor (GnibblesCWorm *worm)
-{
-  return CLUTTER_ACTOR (g_list_last (worm->list)->data);
-}
-
-void
-gnibbles_cworm_remove_actor (GnibblesCWorm *worm)
-{
-  g_return_if_fail (worm->list);
-
-  ClutterActor *tmp = gnibbles_cworm_get_tail_actor (worm);
-  worm->list = g_list_delete_link (worm->list, g_list_last (worm->list));
-
-  clutter_container_remove_actor (CLUTTER_CONTAINER (worm->actors), tmp);
-}
-
 void 
 gnibbles_cworm_resize (GnibblesCWorm *worm, gint newtile)
 {
@@ -324,27 +332,38 @@ gnibbles_cworm_move_straight_worm (GnibblesCWorm *worm)
     case WORMRIGHT:
       clutter_actor_set_position (CLUTTER_ACTOR (head), 
                                  x + properties->tilesize, y);
+      //level->walls[worm->xhead][worm->yhead] = WORMCHAR;
       worm->xhead++;
+      //level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
+      worm->xtail++;
       break;
     case WORMDOWN:
       clutter_actor_set_position (CLUTTER_ACTOR (head), 
                                   x, y + properties->tilesize);
+      //level->walls[worm->xhead][worm->yhead] = WORMCHAR;
       worm->yhead++;
+      //level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
+      worm->ytail++;
       break;
     case WORMLEFT:
       clutter_actor_set_position (CLUTTER_ACTOR (head), 
                                  x - properties->tilesize, y);
+      //level->walls[worm->xhead][worm->yhead] = WORMCHAR;
       worm->xhead--;
+      //level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
+      worm->xtail--;
       break;
     case WORMUP:
       clutter_actor_set_position (CLUTTER_ACTOR (head), 
                                   x, y - properties->tilesize);
+      //level->walls[worm->xhead][worm->yhead] = WORMCHAR;
       worm->yhead--;
+      //level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
+      worm->ytail--;
       break;
     default:
       break;
   }
-
 }
 
 void
@@ -370,12 +389,14 @@ gnibbles_cworm_move_head (GnibblesCWorm *worm)
       clutter_actor_set_size (CLUTTER_ACTOR (head), 
                               size, 
                               properties->tilesize);
+      //level->walls[worm->xhead][worm->yhead] = WORMCHAR;
       worm->xhead++;
       break;
     case WORMDOWN:
       clutter_actor_set_size (CLUTTER_ACTOR (head), 
                               properties->tilesize, 
                               size);
+      //level->walls[worm->xhead][worm->yhead] = WORMCHAR;
       worm->yhead++;
       break;
     case WORMLEFT:
@@ -384,6 +405,7 @@ gnibbles_cworm_move_head (GnibblesCWorm *worm)
                               properties->tilesize);
       clutter_actor_set_position (CLUTTER_ACTOR (head), 
                                   x - properties->tilesize, y);
+      //level->walls[worm->xhead][worm->yhead] = WORMCHAR;
       worm->xhead--;
       break;
     case WORMUP:
@@ -392,6 +414,7 @@ gnibbles_cworm_move_head (GnibblesCWorm *worm)
                               size);
       clutter_actor_set_position (CLUTTER_ACTOR (head), 
                                   x, y - properties->tilesize);
+      //level->walls[worm->xhead][worm->yhead] = WORMCHAR;
       worm->yhead--;
       break;
     default:
@@ -428,6 +451,7 @@ gnibbles_cworm_move_tail (GnibblesCWorm *worm)
                                 properties->tilesize);
         clutter_actor_set_position (CLUTTER_ACTOR (tail), 
                                     x + properties->tilesize, y);
+        //level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
         worm->xtail++;
         break;
       case WORMDOWN:
@@ -436,18 +460,21 @@ gnibbles_cworm_move_tail (GnibblesCWorm *worm)
                                 size);
         clutter_actor_set_position (CLUTTER_ACTOR (tail), 
                                     x, y + properties->tilesize);
+        //level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
         worm->ytail++;
         break;
       case WORMLEFT:
         clutter_actor_set_size (CLUTTER_ACTOR (tail), 
                                 size, 
                                 properties->tilesize);
+        //level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
         worm->xtail--;
         break;
       case WORMUP:
         clutter_actor_set_size (CLUTTER_ACTOR (tail), 
                                 properties->tilesize, 
                                 size);
+        //level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
         worm->ytail--;
         break;
       default:
@@ -457,41 +484,6 @@ gnibbles_cworm_move_tail (GnibblesCWorm *worm)
 }
 
 gint
-gnibbles_cworm_get_tail_direction (GnibblesCWorm *worm)
-{
-  gfloat w,h;
-  gfloat x1,y1,x2,y2;
-  gint dir = -1;
-  gboolean is_horizontal;
-  GValue val = {0,};
-  g_value_init (&val, G_TYPE_BOOLEAN);
-
-  ClutterActor *tail = gnibbles_cworm_get_tail_actor (worm);
-  ClutterActor *next = g_list_previous (g_list_last (worm->list))->data;
-  
-  g_object_get_property (G_OBJECT (tail), "repeat-x", &val);
-  is_horizontal = g_value_get_boolean (&val);
-
-  clutter_actor_get_position (CLUTTER_ACTOR (next), &x2, &y2);
-  clutter_actor_get_size (CLUTTER_ACTOR (next), &w, &h);
-  clutter_actor_get_position (CLUTTER_ACTOR (tail), &x1, &y1);
-  
-  if (is_horizontal) {
-    if (x2 > x1)
-      dir = WORMRIGHT;
-    else if (x2 == x1)
-      dir = WORMLEFT;
-  } else {
-    if (y2 > y1)
-      dir = WORMDOWN;
-    else if (y2 == y1)
-      dir = WORMUP;
-  }
-
-  return dir;
-}
-
-gint
 gnibbles_cworm_get_length (GnibblesCWorm *worm)
 {
   ClutterActor *tmp = NULL;
diff --git a/gnibbles/worm-clutter.h b/gnibbles/worm-clutter.h
index b9b2c00..697dec2 100644
--- a/gnibbles/worm-clutter.h
+++ b/gnibbles/worm-clutter.h
@@ -66,27 +66,20 @@ typedef struct {
 } WormCorner;
 
 GnibblesCWorm * gnibbles_cworm_new (guint number, guint t_xhead,
-			                    guint t_yhead, gint t_direction);
-                          
+			                    guint t_yhead, gint t_direction);       
 void gnibbles_cworm_add_actor (GnibblesCWorm *worm);
-void gnibbles_cworm_add_actor_with_position (GnibblesCWorm *worm, 
-                                        gint t_xhead, gint t_yhead);
 void gnibbles_cworm_remove_actor (GnibblesCWorm *worm);
 void gnibbles_cworm_destroy (GnibblesCWorm * worm);
 void gnibbles_cworm_inverse (GnibblesCWorm *worm);
-ClutterActor* gnibbles_cworm_get_head_actor (GnibblesCWorm *worm);
-ClutterActor* gnibbles_cworm_get_tail_actor (GnibblesCWorm *worm);
-gint gnibbles_cworm_lose_life (GnibblesCWorm * worm);
 void gnibbles_cworm_resize (GnibblesCWorm *worm, gint newtile);
-void gnibbles_cworm_move (ClutterTimeline *timeline, gint msecs, gpointer data);
-gint gnibbles_cworm_get_tail_direction (GnibblesCWorm *worm);
-gint gnibbles_cworm_get_length (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_get_length (GnibblesCWorm *worm);
+gint gnibbles_cworm_lose_life (GnibblesCWorm * worm);
+
 gint gnibbles_cworm_can_move_to (GnibblesCWorm * worm, gint x, gint y);
 void gnibbles_cworm_position_move_head (GnibblesCWorm * worm, gint *x, gint *y);
 gint gnibbles_cworm_test_move_head (GnibblesCWorm * worm);



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