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



commit 1febf282a52c2807baae9a71456fef241cde8999
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 |  128 +++++++++++++++++++++++++++--------------------
 gnibbles/worm-clutter.h |    2 +-
 3 files changed, 82 insertions(+), 63 deletions(-)
---
diff --git a/gnibbles/main.c b/gnibbles/main.c
index 9b6d333..39ec3bd 100644
--- a/gnibbles/main.c
+++ b/gnibbles/main.c
@@ -1269,7 +1269,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
@@ -1280,9 +1280,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;
@@ -1306,12 +1307,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 53b234d..32cd85d 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,10 +214,9 @@ gnibbles_cworm_resize (GnibblesCWorm *worm, gint newtile)
     return;
 
   int i;
-  int x_pos;
-  int y_pos;
-  int count;    
-  guint w,h;
+  gfloat x_pos, y_pos;
+  gint count;    
+  gfloat w,h;
   guint size;
   gboolean direction;
   GValue val = {0,};
@@ -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]);
   }
 
 }
@@ -357,7 +355,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);
 
@@ -365,25 +363,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);
@@ -392,7 +389,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--;
@@ -410,7 +407,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);
@@ -419,44 +416,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
@@ -495,6 +492,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]