[gnome-games/gnibbles-clutter] Completely changing the way worms are moving.



commit 069d79134c17ed84282316630dc443e450687211
Author: Guillaume Beland <guillaume beland gmail com>
Date:   Wed Aug 5 14:57:45 2009 -0400

    Completely changing the way worms are moving.
    
    Making it less complex to manage and also giving way more posibility regarding
    animation and effects.
    
    This also solve a longstanding bug where worm were growing when they weren't
    supposed to and I hope it will also solve the collision detection bug i'm
    constantly complaining about.
    
    Some great animation & effect are coming!

 gnibbles/board.c        |    4 ++
 gnibbles/gnibbles.c     |   11 +---
 gnibbles/worm-clutter.c |  131 +++++++++++++++++++++++++++++++----------------
 gnibbles/worm-clutter.h |    1 +
 4 files changed, 93 insertions(+), 54 deletions(-)
---
diff --git a/gnibbles/board.c b/gnibbles/board.c
index 9e1a766..2a2630a 100644
--- a/gnibbles/board.c
+++ b/gnibbles/board.c
@@ -326,21 +326,25 @@ gnibbles_board_level_new (GnibblesBoard *board, gint level)
       for (j = 0; j < worms[i]->length; j++) {
         board->walls[worms[i]->xhead][worms[i]->yhead] = WORMCHAR + worms[i]->number;
         worms[i]->xhead++;
+        gnibbles_worm_add_single_actor (worms[i]);
       }
     } else if ( worms[i]->direction == WORMLEFT) {
       for (j = 0; j < worms[i]->length; j++){
         board->walls[worms[i]->xhead][worms[i]->yhead] = WORMCHAR + worms[i]->number;
         worms[i]->xhead--;
+        gnibbles_worm_add_single_actor (worms[i]);
       }
     } else if (worms[i]->direction == WORMDOWN) {
       for (j = 0; j < worms[i]->length; j++) {
         board->walls[worms[i]->xhead][worms[i]->yhead] = WORMCHAR + worms[i]->number;
         worms[i]->yhead++;
+        gnibbles_worm_add_single_actor (worms[i]);
       }
     } else if (worms[i]->direction == WORMUP) {
       for (j = 0; j < worms[i]->length; j++) {
         board->walls[worms[i]->xhead][worms[i]->yhead] = WORMCHAR + worms[i]->number;
         worms[i]->yhead--;
+        gnibbles_worm_add_single_actor (worms[i]);
       }
     }
     board->walls[worms[i]->xtail][worms[i]->ytail] = EMPTYCHAR;
diff --git a/gnibbles/gnibbles.c b/gnibbles/gnibbles.c
index aa42793..865dcb7 100644
--- a/gnibbles/gnibbles.c
+++ b/gnibbles/gnibbles.c
@@ -240,9 +240,6 @@ gnibbles_move_worms (void)
     olddir = worms[i]->direction;
     if (!worms[i]->human) {
       gnibbles_worm_ai_move (worms[i]);
-
-      if (olddir != worms[i]->direction)
-        gnibbles_worm_add_actor (worms[i]);
     }
   }
 
@@ -283,25 +280,21 @@ gnibbles_move_worms (void)
     if (g_list_length (worms[i]->list) > 1 && !dead[i] && worms[i]->lives > 0)
       gnibbles_worm_move_head (worms[i]);
   }
-  
+/*
   for (i = 0; i < properties->numworms; i++) {
     if (g_list_length (worms[i]->list) == 1 && !dead[i] && worms[i]->lives > 0)
       gnibbles_worm_move_straight_worm (worms[i]);
   }
-
+*/
   for (i = 0; i < properties->numworms; i++) { 
     if (worms[i]->xhead >= BOARDWIDTH) {
       worms[i]->xhead = 0;
-      gnibbles_worm_add_actor(worms[i]);
     } else if (worms[i]->xhead <= 0) {
       worms[i]->xhead = BOARDWIDTH;
-      gnibbles_worm_add_actor (worms[i]);
     } else if (worms[i]->yhead >= BOARDHEIGHT) {
       worms[i]->yhead = 0;
-      gnibbles_worm_add_actor (worms[i]);
     } else if (worms[i]->xhead <= 0) {
       worms[i]->yhead = BOARDHEIGHT;
-      gnibbles_worm_add_actor (worms[i]);
     }
   }
 
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index e576d02..68fe2c6 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -61,6 +61,21 @@ typedef struct _key_queue_entry {
 
 static GQueue *key_queue[NUMWORMS] = { NULL, NULL, NULL, NULL };
 
+void
+gnibbles_worm_add_single_actor (GnibblesWorm *worm)
+{
+  ClutterActor *actor;
+
+  actor = gtk_clutter_texture_new_from_pixbuf (worm_pixmaps[worm->number]);
+  clutter_actor_set_size (actor, properties->tilesize, properties->tilesize);
+  clutter_actor_set_position (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);  
+}
+
 static void
 gnibbles_worm_queue_keypress (GnibblesWorm * worm, guint dir)
 {
@@ -147,19 +162,28 @@ gnibbles_worm_dequeue_keypress (GnibblesWorm * worm)
 
   g_free (entry);
 }
-
+/*
 static ClutterActor*
 gnibbles_worm_get_head_actor (GnibblesWorm *worm)
 {
   return CLUTTER_ACTOR (g_list_first (worm->list)->data);
 }
-
+*/
 static ClutterActor*
 gnibbles_worm_get_tail_actor (GnibblesWorm *worm)
 {
   return CLUTTER_ACTOR (g_list_last (worm->list)->data);
 }
 
+static void
+gnibbles_worm_remove_trailing_actor (GnibblesWorm *worm)
+{
+  ClutterActor *actor = gnibbles_worm_get_tail_actor (worm);
+  clutter_actor_hide (actor);
+  worm->list = g_list_delete_link (worm->list, g_list_last (worm->list));
+  clutter_container_remove_actor (CLUTTER_CONTAINER (worm->actors), actor);
+}
+
 gboolean
 gnibbles_worm_handle_keypress (GnibblesWorm * worm, guint keyval)
 {
@@ -179,10 +203,8 @@ gnibbles_worm_handle_keypress (GnibblesWorm * worm, guint keyval)
   if (properties->wormprops[worm->number]->relmove) {
     if (keyvalUpper == propsLeft) {
       worm_handle_direction (worm->number, worm->direction - 1);
-      gnibbles_worm_add_actor (worm);
     } else if (keyvalUpper == propsRight) {
       worm_handle_direction (worm->number, worm->direction + 1);
-      gnibbles_worm_add_actor (worm);
     } else {
       return FALSE;
     }
@@ -190,22 +212,18 @@ gnibbles_worm_handle_keypress (GnibblesWorm * worm, guint keyval)
   } else {
     if ((keyvalUpper == propsUp) && (worm->direction != WORMDOWN)) {
       worm_handle_direction (worm->number, WORMUP);
-      gnibbles_worm_add_actor (worm);
       return TRUE;
     }
     if ((keyvalUpper == propsRight) && (worm->direction != WORMLEFT)) {
       worm_handle_direction (worm->number, WORMRIGHT);
-      gnibbles_worm_add_actor (worm);
       return TRUE;
     }
     if ((keyvalUpper == propsDown) && (worm->direction != WORMUP)) {
       worm_handle_direction (worm->number, WORMDOWN);
-      gnibbles_worm_add_actor (worm);
       return TRUE;
     }
     if ((keyvalUpper == propsLeft) && (worm->direction != WORMRIGHT)) {
       worm_handle_direction (worm->number, WORMLEFT);
-      gnibbles_worm_add_actor (worm);
       return TRUE;
     }
   }
@@ -300,7 +318,7 @@ gnibbles_worm_get_tail_direction (GnibblesWorm *worm)
 
   return dir;
 }
-
+/*
 static gint
 gnibbles_worm_get_actor_length (ClutterActor *actor) {
   gint size;
@@ -312,45 +330,19 @@ gnibbles_worm_get_actor_length (ClutterActor *actor) {
 
   return size;
 }
-
+*/
 static void
 gnibbles_worm_reset (ClutterAnimation *animation, gpointer data)
 {
   GnibblesWorm *worm = (GnibblesWorm *)data;
 
-  ClutterActor *tail_actor = NULL;
-  gint tail_length;
-  gint tail_dir;
-  gint i,j;
+  gint j;
   gint nbr_actor = g_list_length (worm->list);
 
   for (j = 0; j < nbr_actor; j++) {
-    tail_dir = gnibbles_worm_get_tail_direction (worm);
-    tail_actor = gnibbles_worm_get_tail_actor (worm);
-    tail_length = gnibbles_worm_get_actor_length (tail_actor); 
-    
-    switch (tail_dir) {
-      case WORMUP:
-        for (i = 0; i < tail_length; i++)
-          board->walls[worm->xtail][worm->ytail--] = EMPTYCHAR;
-        break;
-      case WORMDOWN:
-        for (i = 0; i < tail_length; i++)
-          board->walls[worm->xtail][worm->ytail++] = EMPTYCHAR;
-        break;
-      case WORMLEFT:
-        for (i = 0; i < tail_length; i++)
-          board->walls[worm->xtail--][worm->ytail] = EMPTYCHAR;
-        break;
-      case WORMRIGHT:
-        for (i = 0; i < tail_length; i++)
-          board->walls[worm->xtail++][worm->ytail] = EMPTYCHAR;
-        break;
-      default:
-        break;
-    }
+    board->walls[worm->xtail][worm->ytail--] = EMPTYCHAR;
+    gnibbles_worm_remove_trailing_actor (worm);
     board->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
-    gnibbles_worm_remove_actor (worm);
   }
 
   worm->xhead = worm->xstart;
@@ -366,21 +358,25 @@ gnibbles_worm_reset (ClutterAnimation *animation, gpointer data)
       for (j = 0; j < worm->length; j++) {
         board->walls[worm->xhead][worm->yhead] = WORMCHAR + worm->number;
         worm->xhead++;
+        gnibbles_worm_add_single_actor (worm);
       }
     } else if ( worm->direction == WORMLEFT) {
       for (j = 0; j < worm->length; j++) {
         board->walls[worm->xhead][worm->yhead] = WORMCHAR + worm->number;
         worm->yhead--;
+        gnibbles_worm_add_single_actor (worm);
       }
     } else if (worm->direction == WORMDOWN) {
       for (j = 0; j < worm->length; j++) {
         board->walls[worm->xhead][worm->yhead] = WORMCHAR + worm->number;
         worm->yhead++;
+        gnibbles_worm_add_single_actor (worm);
       }
     } else if (worm->direction == WORMUP) {
       for (j = 0; j < worm->length; j++) {
         board->walls[worm->xhead][worm->yhead] = WORMCHAR + worm->number;
         worm->yhead--;
+        gnibbles_worm_add_single_actor (worm);
       }
     }
     board->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
@@ -484,7 +480,6 @@ gnibbles_worm_move_tail_pointer (GnibblesWorm *worm)
 static void
 gnibbles_worm_handle_bonus (GnibblesWorm *worm)
 {
-  ClutterActor *actor = NULL;
   if ((board->walls[worm->xhead][worm->yhead] != EMPTYCHAR) &&
     (board->walls[worm->xhead][worm->yhead] != WARPLETTER)) {
     gnibbles_worm_grok_bonus (worm);
@@ -531,7 +526,7 @@ gnibbles_worm_new (guint number, guint t_xhead,
   worm->direction = t_direction;
   worm->direction_start = t_direction;
   worm->length = SLENGTH;
-  worm->change = 1; 
+  worm->change = 0; 
 
   gnibbles_worm_queue_empty (worm);
 
@@ -541,7 +536,7 @@ gnibbles_worm_new (guint number, guint t_xhead,
 void
 gnibbles_worm_show (GnibblesWorm *worm)
 {
-  gnibbles_worm_add_actor (worm);
+  gnibbles_worm_add_single_actor (worm);
   clutter_actor_set_opacity (worm->actors, 0);
   clutter_actor_set_scale (worm->actors, 2.0, 2.0);
   clutter_actor_animate (worm->actors, CLUTTER_EASE_OUT_CIRC, 510,
@@ -551,7 +546,7 @@ gnibbles_worm_show (GnibblesWorm *worm)
                          "opacity", 0xff,
                          NULL);
 }
-
+/*
 void
 gnibbles_worm_add_actor (GnibblesWorm *worm)
 {
@@ -616,7 +611,7 @@ gnibbles_worm_add_actor (GnibblesWorm *worm)
   clutter_container_add_actor (CLUTTER_CONTAINER (worm->actors), actor);  
   worm->list = g_list_prepend (worm->list, actor);
 }
-
+*/
 void
 gnibbles_worm_remove_actor (GnibblesWorm *worm)
 {
@@ -729,7 +724,53 @@ gnibbles_worm_resize (GnibblesWorm *worm, gint newtile)
       gnibbles_error (err->message);
   }
 }
+void
+gnibbles_worm_move_head (GnibblesWorm *worm)
+{
+  if (g_list_length (worm->list) <= 1)
+    return;
+
+  if (worm->human)
+    worm->keypress = 0;
+  
+  gnibbles_worm_move_head_pointer (worm);
+  gnibbles_worm_add_single_actor (worm);
+  gnibbles_worm_handle_bonus (worm);
+
+  board->walls[worm->xhead][worm->yhead] = WORMCHAR + worm->number;
+  worm->length++;
+
+  if (key_queue[worm->number] && !g_queue_is_empty (key_queue[worm->number])) {
+    gnibbles_worm_dequeue_keypress (worm);
+  }
+}
+
+void
+gnibbles_worm_move_tail (GnibblesWorm *worm)
+{
+  if (g_list_length (worm->list) <= 1)
+    return;
+
+  if (worm->change <= 0) {
+    gnibbles_worm_move_tail_pointer (worm);
+    gnibbles_worm_remove_trailing_actor (worm);
+    
+    if (worm->change) {
 
+      gnibbles_worm_move_tail_pointer (worm);
+      gnibbles_worm_remove_trailing_actor (worm);
+      board->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
+      worm->change++;
+      worm->length--;
+    }
+    board->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
+  } else {
+    worm->change--;
+    worm->length++;
+  }
+}
+
+/*
 void
 gnibbles_worm_move_straight_worm (GnibblesWorm *worm)
 {
@@ -953,7 +994,7 @@ gnibbles_worm_move_tail (GnibblesWorm *worm)
     worm->length++;
   }
 }
-
+*/
 void 
 gnibbles_worm_shrink (GnibblesWorm *worm, gint shrinksize)
 {
diff --git a/gnibbles/worm-clutter.h b/gnibbles/worm-clutter.h
index cb8cbc7..a8c251b 100644
--- a/gnibbles/worm-clutter.h
+++ b/gnibbles/worm-clutter.h
@@ -62,6 +62,7 @@ GnibblesWorm* gnibbles_worm_new (guint number, guint t_xhead,
 			                             guint t_yhead, gint t_direction);
 void gnibbles_worm_show (GnibblesWorm *worm);
 gboolean gnibbles_worm_handle_keypress (GnibblesWorm * worm, guint keyval);
+void gnibbles_worm_add_single_actor (GnibblesWorm *worm);
 void gnibbles_worm_add_actor (GnibblesWorm *worm);
 void gnibbles_worm_remove_actor (GnibblesWorm *worm);
 void gnibbles_worm_destroy (GnibblesWorm * worm);



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