[gnome-games] Partially solved the collision bug, added utility method to lighten the code



commit 724b4acbc44c9fcf5aa964fb0af45ae660193207
Author: Guillaume Beland <guillaume beland gmail com>
Date:   Wed Jul 29 14:16:33 2009 -0400

    Partially solved the collision bug, added utility method to lighten the code

 gnibbles/board.c        |    1 +
 gnibbles/gnibbles.c     |  104 ----------------------------------
 gnibbles/worm-clutter.c |  143 +++++++++++++++++++++++------------------------
 3 files changed, 72 insertions(+), 176 deletions(-)
---
diff --git a/gnibbles/board.c b/gnibbles/board.c
index fe54012..2dba4d5 100644
--- a/gnibbles/board.c
+++ b/gnibbles/board.c
@@ -347,6 +347,7 @@ gnibbles_board_level_new (GnibblesBoard *board, gint level)
         worms[i]->yhead--;
       }
     }
+    board->walls[worms[i]->xtail][worms[i]->ytail] = EMPTYCHAR;
   }
   gnibbles_board_load_level (board); 
 }
diff --git a/gnibbles/gnibbles.c b/gnibbles/gnibbles.c
index ad15b7e..c2e3eae 100644
--- a/gnibbles/gnibbles.c
+++ b/gnibbles/gnibbles.c
@@ -350,110 +350,6 @@ gnibbles_move_worms (void)
   g_free (dead);
   return (CONTINUE);
 }
-/*
-gint
-gnibbles_move_worms (void)
-{
-  gint i, j, status = 1, nlives = 0;
-  gint *dead;
-
-  dead = g_new (gint, properties->numworms);
-
-  for (i = 0; i < properties->ai; i++) {
-    gnibbles_worm_ai_move (worms[properties->human + i]);
-  }
-
-
-  if (boni->missed > MAXMISSED)
-    for (i = 0; i < properties->numworms; i++)
-      if (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);
-      } else {
-	      gnibbles_boni_remove_bonus (boni,
-				    boni->bonuses[i]->x, boni->bonuses[i]->y);
-      }
-    }
-  }
-
-  for (i = 0; i < properties->numworms; i++) {
-    gnibbles_worm_erase_tail (worms[i]);
-  }
-
-  for (i = 0; i < properties->numworms; i++) {
-    dead[i] = !gnibbles_worm_test_move_head (worms[i]);
-    status &= !dead[i];
-  }
-
-  for (i = 0; i < properties->numworms; i++)
-    if (!dead[i] && worms[i]->lives > 0)
-      gnibbles_worm_move_tail (worms[i]);
-
-  for (i = 0; i < properties->numworms; i++)
-    if (!dead[i] && worms[i]->lives > 0)
-      gnibbles_worm_draw_head (worms[i]);
-
-
-  // If one worm has died, me must make sure that an earlier worm was not
-  // supposed to die as well. 
-
-  for (i = 0; i < properties->numworms; i++)
-    for (j = 0; j < properties->numworms; j++) {
-      if (i != j
-          && worms[i]->xhead == worms[j]->xhead
-	  && worms[i]->yhead == worms[j]->yhead
-	  && worms[i]->lives > 0
-	  && worms[j]->lives > 0)
-	dead[i] = TRUE;
-    }
-
-  for (i = 0; i < properties->numworms; i++)
-    if (dead[i]) {
-      if (properties->numworms > 1)
-	      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");
-	// Don't return here.  May need to reset more worms. 
-	    }
-    }
-
-  if (status & GAMEOVER) {
-    games_sound_play ("crash");
-    games_sound_play ("gameover");
-    return (GAMEOVER);
-  }
-
-  for (i = 0; i < properties->numworms; i++) {
-    if (worms[i]->lives > 0)
-      nlives += 1;
-  }
-  if (nlives == 1 && (properties->ai + properties->human > 1)) {
-    // There is one player left, the other AI players are dead, and that player has won! 
-    return (VICTORY);
-  } else if (nlives == 0) {
-    // There was only one worm, and it died. 
-    return (GAMEOVER);
-  }
-
-   // Noone died, so the round can continue. 
-
-  g_free (dead);
-  return (CONTINUE);
-}
-*/
 
 gint
 gnibbles_get_winner (void)
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index f500117..e1c3cce 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -353,8 +353,6 @@ gnibbles_worm_reset (ClutterAnimation *animation, gpointer data)
     gnibbles_worm_remove_actor (worm);
   }
 
-  //printf ("\n\n %d)XHEAD %d, YHEAD %d",worm->number, worm->xhead, worm->yhead);
-  //printf ("\n\n %d)XTAIL %d, YTAIL %d",worm->number, worm->xtail, worm->ytail);
   worm->xhead = worm->xstart;
   worm->yhead = worm->ystart;
   worm->xtail = worm->xhead;
@@ -385,8 +383,9 @@ gnibbles_worm_reset (ClutterAnimation *animation, gpointer data)
         worm->yhead--;
       }
     }
+    board->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
   }
-/*
+
   FILE *fo;
   fo = fopen ("output.txt", "w" );
   for (i = 0; i < BOARDHEIGHT; i++) {
@@ -399,7 +398,7 @@ gnibbles_worm_reset (ClutterAnimation *animation, gpointer data)
     fprintf (fo, "\n");
   }
   fclose (fo);
-*/
+
 }
 
 static void *
@@ -428,6 +427,59 @@ gnibbles_worm_animate (GnibblesWorm *worm)
   return animation;
 }
 
+static void
+gnibbles_worm_move_head_pointer (GnibblesWorm *worm)
+{
+  switch (worm->direction) {
+    case WORMRIGHT:
+      worm->xhead++;
+      break;
+    case WORMDOWN:
+      worm->yhead++;
+      break;
+    case WORMLEFT:
+      worm->xhead--;
+      break;
+    case WORMUP:
+      worm->yhead--;
+      break;
+    default:
+      break;
+  }
+
+  if (worm->xhead == BOARDWIDTH) 
+    worm->xhead = 0;
+  if (worm->xhead < 0) 
+    worm->xhead = BOARDWIDTH - 1;
+  if (worm->yhead == BOARDHEIGHT) 
+    worm->yhead = 0;
+  if (worm->yhead < 0) 
+    worm->yhead = BOARDHEIGHT - 1;
+}
+
+static void
+gnibbles_worm_move_tail_pointer (GnibblesWorm *worm)
+{
+  gint tail_dir = gnibbles_worm_get_tail_direction (worm);
+
+  switch (tail_dir) {
+    case WORMRIGHT:
+      worm->xtail++;
+      break;
+    case WORMDOWN:
+      worm->ytail++;
+      break;
+    case WORMLEFT:
+      worm->xtail--;
+      break;
+    case WORMUP:
+      worm->ytail--;
+      break;
+    default:
+      break;
+  }
+}
+
 GnibblesWorm*
 gnibbles_worm_new (guint number, guint t_xhead,
 			                    guint t_yhead, gint t_direction)
@@ -719,42 +771,25 @@ gnibbles_worm_move_straight_worm (GnibblesWorm *worm)
       case WORMRIGHT:
         clutter_actor_set_x (CLUTTER_ACTOR (head), 
                              x + properties->tilesize);
-        worm->xhead++;
-        worm->xtail++;
         break;
       case WORMDOWN:
         clutter_actor_set_y (CLUTTER_ACTOR (head), 
                              y + properties->tilesize);
-        worm->yhead++;
-        worm->ytail++;
         break;
       case WORMLEFT:
         clutter_actor_set_x (CLUTTER_ACTOR (head), 
                              x - properties->tilesize);
-        worm->xhead--;
-        worm->xtail--;
         break;
       case WORMUP:
         clutter_actor_set_y (CLUTTER_ACTOR (head), 
                              y - properties->tilesize);
-        worm->yhead--;
-        worm->ytail--;
         break;
       default:
         break;
     }
-    if (worm->xhead == BOARDWIDTH) {
-      worm->xhead = 0;
-    }
-    if (worm->xhead < 0) {
-      worm->xhead = BOARDWIDTH - 1;
-    }
-    if (worm->yhead == BOARDHEIGHT) {
-      worm->yhead = 0;
-    }
-    if (worm->yhead < 0) {
-      worm->yhead = BOARDHEIGHT - 1;
-    }
+
+    gnibbles_worm_move_head_pointer (worm);
+    gnibbles_worm_move_tail_pointer (worm);
 
     if ((board->walls[worm->xhead][worm->yhead] != EMPTYCHAR) &&
         (board->walls[worm->xhead][worm->yhead] != WARPLETTER)) {
@@ -772,7 +807,6 @@ gnibbles_worm_move_straight_worm (GnibblesWorm *worm)
       } else
         gnibbles_boni_remove_bonus_final (boni, worm->xhead, worm->yhead);
     }
-
     board->walls[worm->xhead][worm->yhead] = WORMCHAR + worm->number;
 
     if (worm->change) {
@@ -780,43 +814,25 @@ gnibbles_worm_move_straight_worm (GnibblesWorm *worm)
         case WORMRIGHT:
           clutter_actor_set_x (CLUTTER_ACTOR (head), 
                                x + properties->tilesize);
-          worm->xhead++;
-          worm->xtail++;
           break;
         case WORMDOWN:
           clutter_actor_set_y (CLUTTER_ACTOR (head), 
                                y + properties->tilesize);
-          worm->yhead++;
-          worm->ytail++;
           break;
         case WORMLEFT:
           clutter_actor_set_x (CLUTTER_ACTOR (head), 
                                x - properties->tilesize);
-          worm->xhead--;
-          worm->xtail--;
           break;
         case WORMUP:
           clutter_actor_set_y (CLUTTER_ACTOR (head), 
                                y - properties->tilesize);
-          worm->yhead--;
-          worm->ytail--;
           break;
         default:
           break;
       }
-
-      if (worm->xhead == BOARDWIDTH) {
-        worm->xhead = 0;
-      }
-      if (worm->xhead < 0) {
-        worm->xhead = BOARDWIDTH - 1;
-      }
-      if (worm->yhead == BOARDHEIGHT) {
-        worm->yhead = 0;
-      }
-      if (worm->yhead < 0) {
-        worm->yhead = BOARDHEIGHT - 1;
-      }
+      
+      gnibbles_worm_move_head_pointer (worm);
+      gnibbles_worm_move_tail_pointer (worm);
       
       if ((board->walls[worm->xhead][worm->yhead] != EMPTYCHAR) &&
           (board->walls[worm->xhead][worm->yhead] != WARPLETTER)) {
@@ -842,6 +858,7 @@ gnibbles_worm_move_straight_worm (GnibblesWorm *worm)
       worm->change++;
       worm->length--; 
     }
+      board->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
   } else {
     worm->change--;
     worm->length++;
@@ -876,40 +893,25 @@ gnibbles_worm_move_head (GnibblesWorm *worm)
   switch (worm->direction) {
     case WORMRIGHT:
       clutter_actor_set_width (CLUTTER_ACTOR (head), size);
-      worm->xhead++;
       break;
     case WORMDOWN:
       clutter_actor_set_height (CLUTTER_ACTOR (head), size);
-      worm->yhead++;
       break;
     case WORMLEFT:
       clutter_actor_set_width (CLUTTER_ACTOR (head), size);
       clutter_actor_set_x (CLUTTER_ACTOR (head), 
                            x - properties->tilesize);
-      worm->xhead--;
       break;
     case WORMUP:
       clutter_actor_set_height (CLUTTER_ACTOR (head), size);
       clutter_actor_set_y (CLUTTER_ACTOR (head), 
                            y - properties->tilesize);
-      worm->yhead--;
       break;
     default:
       break;
   }
 
-  if (worm->xhead == BOARDWIDTH) {
-    worm->xhead = 0;
-  }
-  if (worm->xhead < 0) {
-    worm->xhead = BOARDWIDTH - 1;
-  }
-  if (worm->yhead == BOARDHEIGHT) {
-    worm->yhead = 0;
-  }
-  if (worm->yhead < 0) {
-    worm->yhead = BOARDHEIGHT - 1;
-  }
+  gnibbles_worm_move_head_pointer (worm);
 
   if ((board->walls[worm->xhead][worm->yhead] != EMPTYCHAR) &&
       (board->walls[worm->xhead][worm->yhead] != WARPLETTER)) {
@@ -954,7 +956,8 @@ gnibbles_worm_move_tail (GnibblesWorm *worm)
   
   if (worm->change <= 0) {
     if (size <= 0) {
-       gnibbles_worm_remove_actor (worm);
+      gnibbles_worm_move_tail_pointer (worm);
+      gnibbles_worm_remove_actor (worm);      
     } else {
       tail_dir = gnibbles_worm_get_tail_direction (worm);
       switch (tail_dir) {
@@ -962,28 +965,26 @@ gnibbles_worm_move_tail (GnibblesWorm *worm)
           clutter_actor_set_width (CLUTTER_ACTOR (tail), size);
           clutter_actor_set_x (CLUTTER_ACTOR (tail), 
                                x + properties->tilesize);
-          worm->xtail++;
        case WORMDOWN:
           clutter_actor_set_height (CLUTTER_ACTOR (tail), size);
           clutter_actor_set_y (CLUTTER_ACTOR (tail), 
                                y + properties->tilesize);
-          worm->ytail++;
           break;
         case WORMLEFT:
           clutter_actor_set_width (CLUTTER_ACTOR (tail), size);
-          worm->xtail--;
           break;
         case WORMUP:
           clutter_actor_set_height (CLUTTER_ACTOR (tail), size);
-          worm->ytail--;
           break;
         default:
           break;
       }
+      gnibbles_worm_move_tail_pointer (worm);
     }
-    
+ 
     if (worm->change) {
       if (size <= 0) {
+        gnibbles_worm_move_tail_pointer (worm);  
         gnibbles_worm_remove_actor (worm);
       } else {
         tail_dir = gnibbles_worm_get_tail_direction (worm);
@@ -992,29 +993,27 @@ gnibbles_worm_move_tail (GnibblesWorm *worm)
             clutter_actor_set_width (CLUTTER_ACTOR (tail), size);
             clutter_actor_set_x (CLUTTER_ACTOR (tail), 
                                  x + properties->tilesize);
-            worm->xtail++;
           case WORMDOWN:
             clutter_actor_set_height (CLUTTER_ACTOR (tail), size);
             clutter_actor_set_y (CLUTTER_ACTOR (tail), 
                                  y + properties->tilesize);
-            worm->ytail++;
             break;
          case WORMLEFT:
             clutter_actor_set_width (CLUTTER_ACTOR (tail), size);
-            worm->ytail--;
             break;
           case WORMUP:
             clutter_actor_set_height (CLUTTER_ACTOR (tail), size);
-            worm->ytail--;
             break;
           default:
             break;
         }
+        gnibbles_worm_move_tail_pointer (worm);
       }
       board->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
       worm->change++;
       worm->length--;
     }
+    board->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
   } else {
     worm->change--;
     worm->length++;



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