[gnome-games] Bunch of small fixes related to colision detection, positioning and such



commit 8d62510d54ef489a1b7d1957be0b6916c80d403a
Author: Guillaume Beland <guillaume beland gmail com>
Date:   Sat Jul 18 19:07:46 2009 -0400

    Bunch of small fixes related to colision detection, positioning and such

 gnibbles/board.h        |    3 +-
 gnibbles/boni.c         |    5 ++-
 gnibbles/bonus.c        |    5 ++-
 gnibbles/bonus.h        |    3 +-
 gnibbles/gnibbles.c     |    6 ++-
 gnibbles/level.c        |   23 +++++++++++--
 gnibbles/level.h        |    5 ++-
 gnibbles/main.c         |    5 ++-
 gnibbles/main.h         |    3 +-
 gnibbles/warp.c         |    5 ++-
 gnibbles/warpmanager.c  |    3 +-
 gnibbles/worm-clutter.c |   84 ++++++++++++++++++++++++-----------------------
 gnibbles/worm-clutter.h |   11 +++---
 13 files changed, 96 insertions(+), 65 deletions(-)
---
diff --git a/gnibbles/board.h b/gnibbles/board.h
index defc148..3907244 100644
--- a/gnibbles/board.h
+++ b/gnibbles/board.h
@@ -2,8 +2,9 @@
 
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
  *                 Guillaume Beland <guillaume beland gmail com>
+ *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  *   the Free Software Foundation; either version 2 of the License, or
diff --git a/gnibbles/boni.c b/gnibbles/boni.c
index bcacf9a..b8139b0 100644
--- a/gnibbles/boni.c
+++ b/gnibbles/boni.c
@@ -1,7 +1,8 @@
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
- * 
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
+ *              Guillaume Beland <guillaume beland gmail com>
+ *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  *   the Free Software Foundation; either version 2 of the License, or
diff --git a/gnibbles/bonus.c b/gnibbles/bonus.c
index f64f3bf..de83c64 100644
--- a/gnibbles/bonus.c
+++ b/gnibbles/bonus.c
@@ -1,7 +1,8 @@
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
- * 
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
+ *              Guillaume Beland <guillaume beland gmail com>
+ *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  *   the Free Software Foundation; either version 2 of the License, or
diff --git a/gnibbles/bonus.h b/gnibbles/bonus.h
index ab79d86..6571a08 100644
--- a/gnibbles/bonus.h
+++ b/gnibbles/bonus.h
@@ -1,6 +1,7 @@
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
+ *              Guillaume Beland <guillaume beland gmail com>
  * 
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
diff --git a/gnibbles/gnibbles.c b/gnibbles/gnibbles.c
index 719c1a5..e013a11 100644
--- a/gnibbles/gnibbles.c
+++ b/gnibbles/gnibbles.c
@@ -2,8 +2,9 @@
 
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
- * 
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
+ *              Guillaume Beland <guillaume beland gmail com>
+ *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  *   the Free Software Foundation; either version 2 of the License, or
@@ -208,6 +209,7 @@ gnibbles_init ()
     if (worms[i]) {
       clutter_container_add_actor (CLUTTER_CONTAINER (stage), worms[i]->actors);
       clutter_actor_raise_top (worms[i]->actors);
+      gnibbles_worm_show (worms[i]);
     }
   }
 
diff --git a/gnibbles/level.c b/gnibbles/level.c
index 4d36788..703de3a 100644
--- a/gnibbles/level.c
+++ b/gnibbles/level.c
@@ -2,8 +2,9 @@
 
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
-                Guillaume Beland <guillaume beland gmail com>
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
+ *              Guillaume Beland <guillaume beland gmail com>
+ *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  *   the Free Software Foundation; either version 2 of the License, or
@@ -152,7 +153,23 @@ gnibbles_level_new (gint level)
   g_free (filename);
   fclose (in);
 
-  return lvl;
+  for (i = 0; i < count; i++) {
+    if (worms[i]->direction == WORMRIGHT) {
+      for (j = 0; j < worms[i]->length; j++)
+        lvl->walls[worms[i]->xhead++][worms[i]->yhead] = WORMCHAR;
+    } else if ( worms[i]->direction == WORMLEFT) {
+      for (j = 0; j < worms[i]->length; j++)
+        lvl->walls[worms[i]->xhead--][worms[i]->yhead] = WORMCHAR;
+    } else if (worms[i]->direction == WORMDOWN) {
+      for (j = 0; j < worms[i]->length; j++)
+        lvl->walls[worms[i]->xhead][worms[i]->yhead++] = WORMCHAR;
+    } else if (worms[i]->direction == WORMUP) {
+      for (j = 0; j < worms[i]->length; j++)
+        lvl->walls[worms[i]->xhead][worms[i]->yhead--] = WORMCHAR;
+    }
+  }
+
+return lvl;
 }
 
 void
diff --git a/gnibbles/level.h b/gnibbles/level.h
index 56f2c99..4134d3b 100644
--- a/gnibbles/level.h
+++ b/gnibbles/level.h
@@ -2,8 +2,9 @@
 
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
- *              Guillaume Beland <guillaume beland gmail com> 
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
+ *              Guillaume Beland <guillaume beland gmail com>
+ *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  *   the Free Software Foundation; either version 2 of the License, or
diff --git a/gnibbles/main.c b/gnibbles/main.c
index 3ba8579..454924c 100644
--- a/gnibbles/main.c
+++ b/gnibbles/main.c
@@ -2,8 +2,9 @@
 
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
- * 
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
+ *              Guillaume Beland <guillaume beland gmail com>
+ *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  *   the Free Software Foundation; either version 2 of the License, or
diff --git a/gnibbles/main.h b/gnibbles/main.h
index 5ede05f..66a2076 100644
--- a/gnibbles/main.h
+++ b/gnibbles/main.h
@@ -1,6 +1,7 @@
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
+ *              Guillaume Beland <guillaume beland gmail com>
  * 
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
diff --git a/gnibbles/warp.c b/gnibbles/warp.c
index b16cf9e..daeedac 100644
--- a/gnibbles/warp.c
+++ b/gnibbles/warp.c
@@ -1,7 +1,8 @@
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
- * 
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
+ *              Guillaume Beland <guillaume beland gmail com>
+ *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  *   the Free Software Foundation; either version 2 of the License, or
diff --git a/gnibbles/warpmanager.c b/gnibbles/warpmanager.c
index 26f992c..fd51a4b 100644
--- a/gnibbles/warpmanager.c
+++ b/gnibbles/warpmanager.c
@@ -1,6 +1,7 @@
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
+ *              Guillaume Beland <guillaume beland gmail com>
  * 
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index ee85019..1e6f57f 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -2,7 +2,7 @@
 
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
  *              Guillaume Béland <guillaume beland gmail com>
  *
  *   This program is free software; you can redistribute it and/or modify
@@ -122,7 +122,6 @@ worm_handle_direction (int worm, int dir)
   }
 }
 
-
 static void
 gnibbles_worm_queue_empty (GnibblesWorm * worm)
 {
@@ -163,9 +162,12 @@ gnibbles_worm_get_tail_actor (GnibblesWorm *worm)
   return CLUTTER_ACTOR (g_list_last (worm->list)->data);
 }
 
-gint
+gboolean
 gnibbles_worm_handle_keypress (GnibblesWorm * worm, guint keyval)
 {
+  if (worm->lives <= 0)
+    return FALSE;
+
   GnibblesWormProps *props;
   guint propsUp, propsLeft, propsDown, propsRight, keyvalUpper;
 
@@ -190,25 +192,21 @@ gnibbles_worm_handle_keypress (GnibblesWorm * worm, guint keyval)
   } else {
     if ((keyvalUpper == propsUp) && (worm->direction != WORMDOWN)) {
       worm_handle_direction (worm->number, WORMUP);
-      //worm->direction = WORMUP;
       gnibbles_worm_add_actor (worm);
       return TRUE;
     }
     if ((keyvalUpper == propsRight) && (worm->direction != WORMLEFT)) {
       worm_handle_direction (worm->number, WORMRIGHT);
-      //worm->direction = WORMRIGHT;
       gnibbles_worm_add_actor (worm);
       return TRUE;
     }
     if ((keyvalUpper == propsDown) && (worm->direction != WORMUP)) {
       worm_handle_direction (worm->number, WORMDOWN);
-      //worm->direction = WORMDOWN;
       gnibbles_worm_add_actor (worm);
       return TRUE;
     }
     if ((keyvalUpper == propsLeft) && (worm->direction != WORMRIGHT)) {
       worm_handle_direction (worm->number, WORMLEFT);
-      //worm->direction = WORMLEFT;
       gnibbles_worm_add_actor (worm);
       return TRUE;
     }
@@ -336,16 +334,21 @@ gnibbles_worm_new (guint number, guint t_xhead,
   worm->ytail = t_yhead;
   worm->direction = t_direction;
   worm->direction_start = t_direction;
+  worm->length = SLENGTH;
 
-  gnibbles_worm_add_actor (worm);
   gnibbles_worm_queue_empty (worm);
 
+  return worm;
+}
+
+void
+gnibbles_worm_show (GnibblesWorm *worm)
+{
+  gnibbles_worm_add_actor (worm);
   clutter_actor_set_opacity (worm->actors, 0);
   clutter_actor_animate (worm->actors, CLUTTER_EASE_IN_QUAD, 410,
                          "opacity", 0xff,
                          NULL);
-
-  return worm;
 }
 
 void
@@ -353,7 +356,6 @@ gnibbles_worm_add_actor (GnibblesWorm *worm)
 {
   ClutterActor *actor = NULL;
   GValue val = {0,};
-  gint size;
 
   actor = gtk_clutter_texture_new_from_pixbuf (worm_pixmaps[worm->number]);
 
@@ -364,12 +366,8 @@ gnibbles_worm_add_actor (GnibblesWorm *worm)
 
   ClutterActor *tmp = NULL;
 
-  if (worm->list) {
+  if (worm->list) 
     tmp = gnibbles_worm_get_head_actor (worm);
-  } else {
-    size = SLENGTH;
-    worm->length = size;
-  }
 
   gfloat x,y;
   clutter_actor_get_position (CLUTTER_ACTOR (actor), &x, &y);
@@ -378,14 +376,9 @@ gnibbles_worm_add_actor (GnibblesWorm *worm)
     // if it's the worm's head, set its size    
     if (!tmp) {
       clutter_actor_set_size (CLUTTER_ACTOR (actor),
-                              properties->tilesize * size,
+                              properties->tilesize * worm->length,
                               properties->tilesize);
 
-      if (worm->direction == WORMRIGHT)
-        worm->xhead += size;
-      else 
-        worm->xhead -= size;
-
     } else {
       clutter_actor_set_size (CLUTTER_ACTOR (actor), 0, properties->tilesize);
     }
@@ -396,13 +389,7 @@ gnibbles_worm_add_actor (GnibblesWorm *worm)
     if (!tmp) {
       clutter_actor_set_size (CLUTTER_ACTOR (actor),
                               properties->tilesize,
-                              properties->tilesize * size);
-
-      if (worm->direction == WORMDOWN)
-        worm->yhead += size;
-      else 
-        worm->yhead -= size;
-
+                              properties->tilesize * worm->length);
     } else {
       clutter_actor_set_size (CLUTTER_ACTOR (actor), properties->tilesize, 0);
     }
@@ -410,9 +397,23 @@ gnibbles_worm_add_actor (GnibblesWorm *worm)
     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);
+  //if it's the head, set its position according to its direction
+  if (!tmp) {
+    if (worm->direction == WORMLEFT || worm->direction == WORMUP) {
+      clutter_actor_set_position (CLUTTER_ACTOR (actor),
+                                  worm->xhead * properties->tilesize,
+                                  worm->yhead * properties->tilesize);
+    } else if (worm->direction == WORMRIGHT || worm->direction == WORMDOWN) {
+      clutter_actor_set_position (CLUTTER_ACTOR (actor),
+                                  worm->xtail * properties->tilesize,
+                                  worm->ytail * properties->tilesize);
+    }
+  } else {
+    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);
@@ -435,10 +436,10 @@ gnibbles_worm_destroy (GnibblesWorm *worm)
   while (worm->list)
     gnibbles_worm_remove_actor (worm);
 
-  if (worm->list)
-    g_list_free (worm->list);
+  //if (worm->list)
+  //  g_list_free (worm->list);
 
-  g_free (worm);
+  //g_free (worm);
 }
 
 void
@@ -506,7 +507,8 @@ gnibbles_worm_reset (GnibblesWorm * worm)
   worm->ytail = worm->yhead;
   worm->direction = worm->direction_start;
 
-  gnibbles_worm_add_actor (worm);
+  if (!(worm->lives <= 0))
+    gnibbles_worm_add_actor (worm);
 }
 
 void 
@@ -914,12 +916,12 @@ gnibbles_worm_lose_life (GnibblesWorm * worm)
 {
   worm->lives--;
   if (worm->lives < 0)
-    return 1;
+    return TRUE;
 
-  return 0;
+  return FALSE;
 }
 
-gint
+gboolean
 gnibbles_worm_can_move_to (GnibblesWorm * worm, gint x, gint y)
 {
   if (worm->xhead == x)
@@ -960,7 +962,7 @@ gnibbles_worm_position_move_head (GnibblesWorm * worm, gint *x, gint *y)
     *y = BOARDHEIGHT - 1;
 }
 
-gint
+gboolean
 gnibbles_worm_test_move_head (GnibblesWorm * worm)
 {
   int x, y;
@@ -973,7 +975,7 @@ gnibbles_worm_test_move_head (GnibblesWorm * worm)
   return TRUE;
 }
 
-gint
+gboolean
 gnibbles_worm_is_move_safe (GnibblesWorm * worm)
 {
   int x, y, i;
diff --git a/gnibbles/worm-clutter.h b/gnibbles/worm-clutter.h
index fba2498..a251621 100644
--- a/gnibbles/worm-clutter.h
+++ b/gnibbles/worm-clutter.h
@@ -2,7 +2,7 @@
 
 /* 
  *   Gnome Nibbles: Gnome Worm Game
- *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>
+ *   Written by Sean MacIsaac <sjm acm org>, Ian Peters <itp gnu org>,
  *              Guillaume Béland <guillaume beland gmail com>
  *
  *   This program is free software; you can redistribute it and/or modify
@@ -62,7 +62,8 @@ void worm_handle_direction (int worm, int dir);
 
 GnibblesWorm* gnibbles_worm_new (guint number, guint t_xhead,
 			                             guint t_yhead, gint t_direction);
-gint gnibbles_worm_handle_keypress (GnibblesWorm * worm, guint keyval);
+void gnibbles_worm_show (GnibblesWorm *worm);
+gboolean gnibbles_worm_handle_keypress (GnibblesWorm * worm, guint keyval);
 void gnibbles_worm_add_actor (GnibblesWorm *worm);
 void gnibbles_worm_remove_actor (GnibblesWorm *worm);
 void gnibbles_worm_destroy (GnibblesWorm * worm);
@@ -79,10 +80,10 @@ gint gnibbles_worm_get_length (GnibblesWorm *worm);
 
 gint gnibbles_worm_lose_life (GnibblesWorm * worm);
 
-gint gnibbles_worm_can_move_to (GnibblesWorm * worm, gint x, gint y);
+gboolean gnibbles_worm_can_move_to (GnibblesWorm * worm, gint x, gint y);
 void gnibbles_worm_position_move_head (GnibblesWorm * worm, gint *x, gint *y);
-gint gnibbles_worm_test_move_head (GnibblesWorm * worm);
-gint gnibbles_worm_is_move_safe (GnibblesWorm * worm);
+gboolean gnibbles_worm_test_move_head (GnibblesWorm * worm);
+gboolean gnibbles_worm_is_move_safe (GnibblesWorm * worm);
 
 void gnibbles_worm_ai_move (GnibblesWorm * worm);
 



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