[gnome-games] Some minor work on worm-clutter



commit be2990becaf603ecc04c64df522bc2a5fe13f326
Author: Guillaume Beland <guillaume beland gmail com>
Date:   Wed May 27 21:22:06 2009 -0400

    Some minor work on worm-clutter

 gnibbles/gnibbles.c     |    4 +++
 gnibbles/level.c        |   25 +++++++++++++-------
 gnibbles/main.c         |   24 ++++++++++++++++----
 gnibbles/worm-clutter.c |   56 +++++++++++++++++++++++++++++++++++++---------
 gnibbles/worm-clutter.h |    9 +++----
 5 files changed, 88 insertions(+), 30 deletions(-)
---
diff --git a/gnibbles/gnibbles.c b/gnibbles/gnibbles.c
index 5546ca6..6cb65ca 100644
--- a/gnibbles/gnibbles.c
+++ b/gnibbles/gnibbles.c
@@ -43,10 +43,14 @@
 #include "properties.h"
 #include "scoreboard.h"
 
+#include "worm-clutter.h"
+
 #ifdef GGZ_CLIENT
 #include "ggz-network.h"
 #endif
 
+GnibblesCWorm *cworms[NUMWORMS];
+
 GnibblesWorm *worms[NUMWORMS];
 GnibblesBoni *boni = NULL;
 GnibblesWarpManager *warpmanager;
diff --git a/gnibbles/level.c b/gnibbles/level.c
index 01bdfee..f7b7cb2 100644
--- a/gnibbles/level.c
+++ b/gnibbles/level.c
@@ -22,7 +22,13 @@
 #include <stdlib.h>
 #include <glib/gi18n.h>
 #include "level.h"
+#include "gnibbles.h"
+#include "worm-clutter.h"
+#include "main.h"
+#include "properties.h"
 
+extern GnibblesCWorm *cworms[];
+extern GnibblesProperties *properties;
 
 GnibblesLevel *
 gnibbles_level_new (gint level)
@@ -35,7 +41,8 @@ gnibbles_level_new (gint level)
   FILE *in;
   gchar tmpboard [BOARDWIDTH +2];
   gint i,j;
-  
+  gint count = 0;
+
   tmp = g_strdup_printf("level%03d.gnl", level);
   
   dirname = games_runtime_get_directory (GAMES_RUNTIME_GAME_GAMES_DIRECTORY);
@@ -68,23 +75,23 @@ gnibbles_level_new (gint level)
       switch (lvl->walls[j][i]) {
         case 'm':
           lvl->walls[j][i] = EMPTYCHAR;
-          //if (count < properties->numworms)
-          //  gnibbles_worm_set_start (worms[count++], j, i, WORMUP);
+          if (count < properties->numworms)
+            gnibbles_cworm_set_start (cworms[count++], j, i, WORMUP);
           break;
         case 'n':
           lvl->walls[j][i] = EMPTYCHAR;
-          //if (count < properties->numworms)
-          //  gnibbles_worm_set_start (worms[count++], j, i, WORMLEFT);
+          if (count < properties->numworms)
+            gnibbles_cworm_set_start (cworms[count++], j, i, WORMLEFT);
           break;
         case 'o':
           lvl->walls[j][i] = EMPTYCHAR;
-          //if (count < properties->numworms)
-          //  gnibbles_worm_set_start (worms[count++], j, i, WORMDOWN);
+          if (count < properties->numworms)
+            gnibbles_cworm_set_start (cworms[count++], j, i, WORMDOWN);
           break;
         case 'p':
           lvl->walls[j][i] = EMPTYCHAR;
-          //if (count < properties->numworms)
-          //  gnibbles_worm_set_start (worms[count++], j, i, WORMRIGHT);
+          if (count < properties->numworms)
+            gnibbles_cworm_set_start (cworms[count++], j, i, WORMRIGHT);
           break;
         case 'Q':
           //gnibbles_warpmanager_add_warp (warpmanager, j - 1, i - 1, -1, -1);
diff --git a/gnibbles/main.c b/gnibbles/main.c
index 8ed67e4..2aae522 100644
--- a/gnibbles/main.c
+++ b/gnibbles/main.c
@@ -101,6 +101,7 @@ GdkPixbuf *boni_pixmaps[9] = { NULL, NULL, NULL, NULL, NULL,
   NULL, NULL, NULL, NULL
 };
 
+extern GnibblesCWorm *cworms[];
 extern GnibblesBoni *boni;
 
 gchar board[BOARDWIDTH][BOARDHEIGHT];
@@ -1331,12 +1332,25 @@ main (int argc, char **argv)
   GnibblesBoard *board = gnibbles_board_new (BOARDWIDTH, BOARDHEIGHT);
   setup_window_clutter (board);
   
-  gnibbles_board_load_level (board, gnibbles_level_new (16));
-
-  GnibblesCWorm *cworm = gnibbles_cworm_new (1,15,15);
-  
   ClutterActor *stage = gnibbles_board_get_stage (board);
-  clutter_container_add_actor (CLUTTER_CONTAINER (stage), cworm->actors);
+
+  int i;
+
+  for (i = 0; i < properties->numworms; i++)
+    if (cworms[i])
+      gnibbles_cworm_destroy (cworms[i]);
+
+  for (i = 0; i < properties->numworms; i++) {
+    cworms[i] = gnibbles_cworm_new (i);
+  }
+
+  gnibbles_board_load_level (board, gnibbles_level_new (1));
+ 
+  for (i = 0; i < properties->numworms; i++) {
+   clutter_container_add_actor (CLUTTER_CONTAINER (stage), cworms[i]->actors);
+   clutter_actor_raise_top (cworms[i]->actors);
+  }
+
   //render_logo_clutter (board);
 
   gtk_main ();
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index 80425ee..dead4fd 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -36,7 +36,7 @@ extern GnibblesProperties *properties;
 extern GdkPixbuf *worm_pixmaps[];
 
 GnibblesCWorm*
-gnibbles_cworm_new (guint number, gint x_s, gint y_s)
+gnibbles_cworm_new (guint number)
 {
   GnibblesCWorm *worm = g_new (GnibblesCWorm, 1);
   
@@ -46,15 +46,10 @@ gnibbles_cworm_new (guint number, gint x_s, gint y_s)
   worm->lives = SLIVES;
   worm->direction = 1;
   worm->inverse = FALSE;
-  worm->xstart = x_s;
-  worm->ystart = y_s;
-  worm->xhead = x_s;
-  worm->yhead = y_s;
 
   worm->direction = WORMDOWN;
   gnibbles_cworm_add_straight_actor (worm);
 
-
   return worm;
 }
 
@@ -64,7 +59,7 @@ gnibbles_cworm_add_straight_actor (GnibblesCWorm *worm)
   ClutterActor *actor = NULL;
   GValue val = {0,};
   gint size;
-  actor = gtk_clutter_texture_new_from_pixbuf (worm_pixmaps[0]);
+  actor = gtk_clutter_texture_new_from_pixbuf (worm_pixmaps[worm->number]);
 
   g_value_init (&val, G_TYPE_BOOLEAN);
   g_value_set_boolean ( &val, TRUE);
@@ -141,12 +136,51 @@ gnibbles_cworm_add_straight_actor (GnibblesCWorm *worm)
 }
 
 void
+gnibbles_cworm_destroy (GnibblesCWorm *worm)
+{
+  while (worm->list)
+    gnibbles_cworm_remove_actor (worm);
+
+  g_list_free (worm->list);
+  g_free (worm->actors);
+}
+
+void
 gnibbles_cworm_remove_actor (GnibblesCWorm *worm)
 {
-  if (!worm->inverse)
-    worm->list = g_list_remove_link (worm->list, g_list_first (worm->list));
-  else 
-    worm->list = g_list_remove_link (worm->list, g_list_last (worm->list));
+  g_return_if_fail (g_list_first (worm->list)->data);
+
+  ClutterActor *tmp = NULL;
+
+  if (!worm->inverse) {
+    tmp = CLUTTER_ACTOR ((g_list_first (worm->list))->data);
+    worm->list = g_list_delete_link (worm->list, g_list_first (worm->list));
+  } else {
+    tmp = CLUTTER_ACTOR ((g_list_last (worm->list))->data);
+    worm->list = g_list_delete_link (worm->list, g_list_last (worm->list));
+  }
+
+  clutter_container_remove_actor (CLUTTER_CONTAINER (worm->actors), tmp);
 }
 
+void 
+gnibbles_cworm_set_start (GnibblesCWorm * worm, guint t_xhead,
+			                    guint t_yhead, gint t_direction)
+{
+  worm->xhead = t_xhead;
+  worm->xstart = t_xhead;
+  worm->yhead = t_yhead;
+  worm->ystart = t_yhead;
+  worm->direction = t_direction;
+  worm->direction_start = t_direction;
+}
+
+gint
+gnibbles_cworm_lose_life (GnibblesCWorm * worm)
+{
+  worm->lives--;
+  if (worm->lives < 0)
+    return 1;
 
+  return 0;
+}
diff --git a/gnibbles/worm-clutter.h b/gnibbles/worm-clutter.h
index 38c232f..3e459c3 100644
--- a/gnibbles/worm-clutter.h
+++ b/gnibbles/worm-clutter.h
@@ -42,7 +42,7 @@ typedef struct {
   ClutterActor *actors;
   GList *list;
   gint xstart, ystart;
-  gint xhead, yhead;
+  guint xhead, yhead;
   gint direction;
   gint direction_start;
   gint length;
@@ -62,18 +62,17 @@ typedef struct {
   gint direction;
 } WormCorner;
 
-GnibblesCWorm * gnibbles_cworm_new (guint number, gint x_s, gint y_s);
+GnibblesCWorm * gnibbles_cworm_new (guint number);
 void gnibbles_cworm_add_straight_actor (GnibblesCWorm *worm);
 void gnibbles_cworm_remove_actor (GnibblesCWorm *worm);
 void gnibbles_cworm_destroy (GnibblesCWorm * worm);
 void gnibbles_cworm_set_start (GnibblesCWorm * worm, guint t_xhead,
 			      guint t_yhead, gint t_direction);
+gint gnibbles_cworm_lose_life (GnibblesCWorm * worm);
+
 gint gnibbles_cworm_handle_keypress (GnibblesCWorm * worm, guint keyval);
-void gnibbles_cworm_position_move_head (GnibblesCWorm * worm, gint *x, gint *y);
-gint gnibbles_cworm_test_move_head (GnibblesCWorm * worm);
 gint gnibbles_cworm_can_move_to (GnibblesCWorm * worm, gint x, gint y);
 gint gnibbles_cworm_is_move_safe (GnibblesCWorm * worm);
-gint gnibbles_cworm_lose_life (GnibblesCWorm * worm);
 void gnibbles_cworm_reset (GnibblesCWorm * worm);
 void gnibbles_cworm_ai_move (GnibblesCWorm * worm);
 



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