[gnome-games/gnibbles-clutter] Worms are now moving nicely, but need some more tweak..



commit b6b2e8435662545ff8138c51536fe50a2df1aa9b
Author: Guillaume Beland <guillaume beland gmail com>
Date:   Wed Jun 24 15:13:11 2009 -0400

    Worms are now moving nicely, but need some more tweak..
    
    Worm grow by themselve for an unknow reason to date and they have no knowledge
    of each other so they basicaly run through each other.
    Also added a funciton to get the length (the number of dot) of a worm and some
    other code improvement

 gnibbles/main.c         |   15 +++---
 gnibbles/worm-clutter.c |  126 +++++++++++++++++++++++++++--------------------
 gnibbles/worm-clutter.h |    2 +-
 3 files changed, 81 insertions(+), 62 deletions(-)
---
diff --git a/gnibbles/main.c b/gnibbles/main.c
index 716f7f7..1c14e79 100644
--- a/gnibbles/main.c
+++ b/gnibbles/main.c
@@ -1262,7 +1262,7 @@ move_worm_cb (ClutterTimeline *timeline, gint msecs, gpointer data)
   if (!(elapsed_time == duration))
     return;
 
-  gint i, olddir, length;
+  gint i, olddir, length, nbr_actor;
 
   for (i = 0; i < properties->numworms; i++) {
     // get the current direction of the worm
@@ -1273,9 +1273,10 @@ move_worm_cb (ClutterTimeline *timeline, gint msecs, gpointer data)
     if (olddir != cworms[i]->direction)
       gnibbles_cworm_add_actor (cworms[i]);
 
-    length = g_list_length (cworms[i]->list);
-    printf ("\nWorm ID: %d, Length: %d, xhead: %d, yhead:%d",
-            i, length, cworms[i]->xhead, cworms[i]->yhead);
+    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;
@@ -1299,12 +1300,12 @@ move_worm_cb (ClutterTimeline *timeline, gint msecs, gpointer data)
                                               cworms[i]->yhead);
     }
     //if there's only one actor in the list, just move the actor
-    if (length == 1) {
+    if (nbr_actor == 1) {
       gnibbles_cworm_move_straight_worm (cworms[i]);
-    } else if (length >= 2) {
+    } else if (nbr_actor >= 2) {
       gnibbles_cworm_move_tail (cworms[i]);
       gnibbles_cworm_move_head (cworms[i]);
-    } else if (length < 1) {
+    } else if (nbr_actor < 1) {
       //worm's dead
       return;
     }
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index 0272f4e..bb8da19 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -25,6 +25,7 @@
 #include <glib/gi18n.h>
 #include <gdk/gdk.h>
 #include <stdlib.h>
+#include <math.h>
 #include <libgames-support/games-runtime.h>
 #include <clutter-gtk/clutter-gtk.h>
 #include "main.h"
@@ -89,13 +90,10 @@ gnibbles_cworm_add_actor (GnibblesCWorm *worm)
   ClutterActor *tmp = NULL;
 
   if (worm->list) {
-    gfloat w,h;
     tmp = gnibbles_cworm_get_head_actor (worm);
-    clutter_actor_get_size (CLUTTER_ACTOR (tmp), &w, &h);
-    size = w < h ? h : w;
-    size = size / properties->tilesize;
   } else {
-    size = SLENGTH; 
+    size = SLENGTH;
+    worm->length = size;
   }
   
   if (worm->direction == WORMRIGHT || worm->direction == WORMLEFT) {
@@ -216,9 +214,8 @@ gnibbles_cworm_resize (GnibblesCWorm *worm, gint newtile)
     return;
 
   int i;
-  gfloat x_pos;
-  gfloat y_pos;
-  int count;    
+  gfloat x_pos, y_pos;
+  gint count;    
   gfloat w,h;
   guint size;
   gboolean direction;
@@ -242,15 +239,16 @@ gnibbles_cworm_resize (GnibblesCWorm *worm, gint newtile)
     direction = g_value_get_boolean (&val);
 
     clutter_actor_get_size (CLUTTER_ACTOR (tmp), &w, &h);
-    size = w < h ? h : w;
-    size = size / properties->tilesize;
+    size = w < h ? roundf(h) : roundf(w);
+    size = roundf (size / properties->tilesize);
 
     if (direction)
       clutter_actor_set_size (tmp, newtile * size, newtile);
     else
       clutter_actor_set_size (tmp, newtile, newtile * size);
 
-    gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (tmp), worm_pixmaps[worm->number]);
+    gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (tmp), 
+                                         worm_pixmaps[worm->number]);
   }
 }
 
@@ -299,7 +297,7 @@ gnibbles_cworm_move_head (GnibblesCWorm *worm)
 
   gfloat w,h;
   gfloat x,y;
-  guint size;
+  gfloat size;
 
   ClutterActor *head = gnibbles_cworm_get_head_actor (worm);
 
@@ -307,25 +305,24 @@ gnibbles_cworm_move_head (GnibblesCWorm *worm)
   clutter_actor_get_position (CLUTTER_ACTOR (head), &x, &y);
   size = w < h ? h : w;
   size = size + properties->tilesize;
-  size = size / properties->tilesize;
 
   // set the size of the head actor 
   switch (worm->direction) {
     case WORMRIGHT:
       clutter_actor_set_size (CLUTTER_ACTOR (head), 
-                              size * properties->tilesize, 
+                              size, 
                               properties->tilesize);
       worm->xhead++;
       break;
     case WORMDOWN:
       clutter_actor_set_size (CLUTTER_ACTOR (head), 
                               properties->tilesize, 
-                              size * properties->tilesize);
+                              size);
       worm->yhead++;
       break;
     case WORMLEFT:
       clutter_actor_set_size (CLUTTER_ACTOR (head), 
-                              size * properties->tilesize, 
+                              size, 
                               properties->tilesize);
       clutter_actor_set_position (CLUTTER_ACTOR (head), 
                                   x - properties->tilesize, y);
@@ -334,7 +331,7 @@ gnibbles_cworm_move_head (GnibblesCWorm *worm)
     case WORMUP:
       clutter_actor_set_size (CLUTTER_ACTOR (head), 
                               properties->tilesize, 
-                              size * properties->tilesize);
+                              size);
       clutter_actor_set_position (CLUTTER_ACTOR (head), 
                                   x, y - properties->tilesize);
       worm->yhead--;
@@ -352,7 +349,7 @@ gnibbles_cworm_move_tail (GnibblesCWorm *worm)
 
   gfloat w,h;
   gfloat x,y;
-  guint size;
+  gfloat size;
   gint tmp_dir;
 
   ClutterActor *tail = gnibbles_cworm_get_tail_actor (worm);
@@ -361,44 +358,44 @@ gnibbles_cworm_move_tail (GnibblesCWorm *worm)
   clutter_actor_get_position (CLUTTER_ACTOR (tail), &x, &y);
   size = w < h ? h : w;
   size = size - properties->tilesize;
-  size = size / properties->tilesize;
-
-  tmp_dir = gnibbles_cworm_get_tail_direction (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++;
-      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++;
-      break;
-    case WORMLEFT:
-      clutter_actor_set_size (CLUTTER_ACTOR (tail), 
-                              properties->tilesize * size, 
-                              properties->tilesize);
-      worm->xtail--;
-      break;
-    case WORMUP:
-      clutter_actor_set_size (CLUTTER_ACTOR (tail), 
-                              properties->tilesize, 
-                              properties->tilesize * size);
-      worm->ytail--;
-      break;
-    default:
-      break;
-  }
 
-  if (size <= 0)
+  if (size <= 0) {
      gnibbles_cworm_remove_actor (worm);
+  } else {
+    tmp_dir = gnibbles_cworm_get_tail_direction (worm);
+    switch (tmp_dir) {
+      case WORMRIGHT:
+        clutter_actor_set_size (CLUTTER_ACTOR (tail), 
+                                size, 
+                                properties->tilesize);
+        clutter_actor_set_position (CLUTTER_ACTOR (tail), 
+                                    x + properties->tilesize, y);
+        worm->xtail++;
+        break;
+      case WORMDOWN:
+        clutter_actor_set_size (CLUTTER_ACTOR (tail), 
+                                properties->tilesize, 
+                                size);
+        clutter_actor_set_position (CLUTTER_ACTOR (tail), 
+                                    x, y + properties->tilesize);
+        worm->ytail++;
+        break;
+      case WORMLEFT:
+        clutter_actor_set_size (CLUTTER_ACTOR (tail), 
+                                size, 
+                                properties->tilesize);
+        worm->xtail--;
+        break;
+      case WORMUP:
+        clutter_actor_set_size (CLUTTER_ACTOR (tail), 
+                                properties->tilesize, 
+                                size);
+        worm->ytail--;
+        break;
+      default:
+        break;
+    }
+  }
 }
 
 gint
@@ -437,6 +434,27 @@ gnibbles_cworm_get_tail_direction (GnibblesCWorm *worm)
 }
 
 gint
+gnibbles_cworm_get_length (GnibblesCWorm *worm)
+{
+  ClutterActor *tmp = NULL;
+  gint nbr_actor;
+  int i;
+  gfloat w,h;
+  gfloat tmp_size = 0;
+  gint size = 0;
+
+  nbr_actor = clutter_group_get_n_children (CLUTTER_GROUP (worm->actors));
+  for (i = 0; i < nbr_actor; i++) {
+    tmp = clutter_group_get_nth_child (CLUTTER_GROUP (worm->actors), i);
+    clutter_actor_get_size (CLUTTER_ACTOR (tmp), &w, &h);
+    tmp_size = w > h ? roundf(w) : roundf(h);
+    size += roundf (tmp_size / properties->tilesize);
+  }
+
+  return size;
+}
+
+gint
 gnibbles_cworm_lose_life (GnibblesCWorm * worm)
 {
   worm->lives--;
diff --git a/gnibbles/worm-clutter.h b/gnibbles/worm-clutter.h
index efd68f5..b9b2c00 100644
--- a/gnibbles/worm-clutter.h
+++ b/gnibbles/worm-clutter.h
@@ -80,7 +80,7 @@ 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);



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