[gnome-nibbles/wip/vala: 4/16] Display walls current level based on level file



commit 0ad88ecb284d1ee69dbbd5b13f5eecc12143e241
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Tue May 19 23:01:33 2015 +0300

    Display walls current level based on level file

 src/gnome-nibbles.vala |   14 +++--
 src/nibbles-game.vala  |    5 ++
 src/nibbles-view.vala  |  180 +++++++++++++++++++++++++++++++++++++++++++++++-
 src/properties.vala    |    5 ++
 4 files changed, 196 insertions(+), 8 deletions(-)
---
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 13259ca..c5d5644 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -116,7 +116,6 @@ public class Nibbles : Gtk.Application
     public bool configure_event_cb (Gdk.EventConfigure event)
     {
         int tile_size, ts_x, ts_y;
-        int board_width, board_height;
 
         /* Compute the new tile size based on the size of the
          * drawing area, rounded down.
@@ -131,11 +130,10 @@ public class Nibbles : Gtk.Application
 
         if (game.properties.tile_size != tile_size)
         {
-            board_width = tile_size * game.width;
-            board_height = tile_size * game.height;
 
-            view.stage.set_size (board_width, board_height);
-            view.surface.set_size (board_width, board_height);
+            view.stage.set_size (tile_size * game.width, tile_size * game.height);
+
+            view.board_rescale (tile_size);
 
             game.properties.tile_size = tile_size;
         }
@@ -177,6 +175,12 @@ public class Nibbles : Gtk.Application
         frame.add (view);
         frame.show_all ();
 
+        /* TODO Fix problem and remove this call
+         * For some reason tile_size gets set to 0 after calling
+         * frame.add (view). start_level stays the same
+         */
+        game.properties.update_properties (settings);
+
         game.current_level = game.properties.start_level;
         view.new_level (game.current_level);
         show_game_view ();
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 1d13ac2..a180a44 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -5,10 +5,15 @@ public class NibblesGame : Object
     public int width = 92;
     public int height = 66;
 
+    public char EMPTYCHAR = 'a';
+    public char WORMCHAR = 'w';
+
     public int current_level;
+    public int[,] walls;
 
     public NibblesGame ()
     {
         properties = new Properties ();
+        walls = new int[width, height];
     }
 }
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 74c1639..fd35534 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -1,10 +1,11 @@
 public class NibblesView : GtkClutter.Embed
 {
     /* Game being played */
-    public NibblesGame game;
+    public NibblesGame game { get; private set; }
 
     public Clutter.Actor surface;
     public Clutter.Stage stage;
+    private Clutter.Actor level;
 
     Gdk.Pixbuf[] wall_pixmaps = { null, null, null, null, null,
                                   null, null, null, null, null,
@@ -25,7 +26,7 @@ public class NibblesView : GtkClutter.Embed
         Clutter.Color stage_color = { 0x00, 0x00, 0x00, 0xff };
         stage.set_background_color (stage_color);
 
-        set_size_request (game.properties.tile_size * game.width, game.properties.tile_size * game.height);
+        set_size_request (7 * game.width, 7 * game.height);
 
         try
         {
@@ -61,6 +62,7 @@ public class NibblesView : GtkClutter.Embed
     {
         string level_name;
         string filename;
+        string tmpboard;
 
         level_name = "level%03d.gnl".printf (level);
         filename = Path.build_filename (DATADIR, "levels", level_name, null);
@@ -77,8 +79,49 @@ public class NibblesView : GtkClutter.Embed
                                                 message);
             dialog.run ();
             dialog.destroy ();
-            Posix.exit (Posix.EXIT_FAILURE);
         }
+
+        for (int i = 0; i < game.height; i++)
+        {
+            if ((tmpboard = file.read_line ()) == null)
+            {
+                string message =
+                    (_("Level file appears to be damaged:\n%s\n\n" +
+                       "Please check your Nibbles installation")).printf (filename);
+                var dialog = new Gtk.MessageDialog (null,
+                                                    Gtk.DialogFlags.MODAL,
+                                                    Gtk.MessageType.ERROR,
+                                                    Gtk.ButtonsType.OK,
+                                                    message);
+                dialog.run ();
+                dialog.destroy ();
+                break;
+            }
+
+            for (int j = 0; j < game.width; j++)
+            {
+                game.walls[j, i] = tmpboard  get(j);
+                switch (game.walls[j, i])
+                {
+                    case 'm':
+                        game.walls[j, i] = game.EMPTYCHAR;
+                        break;
+                    case 'n':
+                        game.walls[j, i] = game.EMPTYCHAR;
+                        break;
+                    case 'o':
+                        game.walls[j, i] = game.EMPTYCHAR;
+                        break;
+                    case 'p':
+                        game.walls[j, i] = game.EMPTYCHAR;
+                        break;
+                    default:
+                        break;
+                }
+            }
+        }
+
+        load_level ();
     }
 
     private Gdk.Pixbuf load_pixmap_file (string pixmap, int xsize, int ysize)
@@ -165,4 +208,135 @@ public class NibblesView : GtkClutter.Embed
                                                 tile_size, tile_size);
         }
     }
+
+    void load_level ()
+    {
+        int x_pos, y_pos;
+        Clutter.Actor tmp = null;
+        bool is_wall = true;
+        level = new Clutter.Actor ();
+
+        /* Load wall_pixmaps onto the surface */
+        for (int i = 0; i < game.height; i++)
+        {
+            y_pos = i * game.properties.tile_size;
+            for (int j = 0; j < game.width; j++)
+            {
+                is_wall = true;
+                try
+                {
+                    switch (game.walls[j, i])
+                    {
+                        case 'a': // empty space
+                            is_wall = false;
+                            break;
+                        case 'b': // straight up
+                            tmp = new GtkClutter.Texture ();
+                            ((GtkClutter.Texture) tmp).set_from_pixbuf (wall_pixmaps[0]);
+                            break;
+                        case 'c': // straight side
+                            tmp = new GtkClutter.Texture ();
+                            ((GtkClutter.Texture) tmp).set_from_pixbuf (wall_pixmaps[1]);
+                            break;
+                        case 'd': // corner bottom left
+                            tmp = new GtkClutter.Texture ();
+                            ((GtkClutter.Texture) tmp).set_from_pixbuf (wall_pixmaps[2]);
+                            break;
+                        case 'e': // corner bottom right
+                            tmp = new GtkClutter.Texture ();
+                            ((GtkClutter.Texture) tmp).set_from_pixbuf (wall_pixmaps[3]);
+                            break;
+                        case 'f': // corner up left
+                            tmp = new GtkClutter.Texture ();
+                            ((GtkClutter.Texture) tmp).set_from_pixbuf (wall_pixmaps[4]);
+                            break;
+                        case 'g': // corner up right
+                            tmp = new GtkClutter.Texture ();
+                            ((GtkClutter.Texture) tmp).set_from_pixbuf (wall_pixmaps[5]);
+                            break;
+                        case 'h': // tee up
+                            tmp = new GtkClutter.Texture ();
+                            ((GtkClutter.Texture) tmp).set_from_pixbuf (wall_pixmaps[6]);
+                            break;
+                        case 'i': // tee right
+                            tmp = new GtkClutter.Texture ();
+                            ((GtkClutter.Texture) tmp).set_from_pixbuf (wall_pixmaps[7]);
+                            break;
+                        case 'j': // tee left
+                            tmp = new GtkClutter.Texture ();
+                            ((GtkClutter.Texture) tmp).set_from_pixbuf (wall_pixmaps[8]);
+                            break;
+                        case 'k': // tee down
+                            tmp = new GtkClutter.Texture ();
+                            ((GtkClutter.Texture) tmp).set_from_pixbuf (wall_pixmaps[9]);
+                            break;
+                        case 'l': // tee cross
+                            tmp = new GtkClutter.Texture ();
+                            ((GtkClutter.Texture) tmp).set_from_pixbuf (wall_pixmaps[10]);
+                            break;
+                        default:
+                            is_wall = false;
+                            break;
+                    }
+                }
+                catch (GLib.Error e)
+                {
+
+                }
+
+                if (is_wall)
+                {
+                    x_pos = j * game.properties.tile_size;
+
+                    ((Clutter.Actor) tmp).set_size (game.properties.tile_size,
+                                                    game.properties.tile_size);
+                    ((Clutter.Actor) tmp).set_position (x_pos, y_pos);
+                    ((Clutter.Actor) tmp).show ();
+                    level.add_child ((Clutter.Actor) tmp);
+                }
+            }
+        }
+
+        stage.add_child (level);
+
+        level.set_opacity (0);
+        ((Clutter.Actor) level).set_scale (0.2, 0.2);
+
+        level.save_easing_state ();
+        level.set_easing_mode (Clutter.AnimationMode.EASE_OUT_BOUNCE);
+        level.set_easing_duration (game.properties.GAMEDELAY * game.properties.GAMEDELAY);
+        level.set_scale (1.0, 1.0);
+        level.set_pivot_point (0.5f, 0.5f);
+        level.set_opacity (0xff);
+        level.restore_easing_state ();
+    }
+
+    public void board_rescale (int tile_size)
+    {
+        int count;
+        int board_width, board_height;
+        float x_pos, y_pos;
+        Clutter.Actor tmp;
+
+        if (level == null)
+            return;
+        if (surface == null)
+            return;
+
+        board_width = game.width * tile_size;
+        board_height = game.height * tile_size;
+
+        surface.set_size (board_width, board_height);
+
+        count = level.get_n_children ();
+
+        for (int i = 0; i < count; i++)
+        {
+            tmp = level.get_child_at_index (i);
+            ((Clutter.Actor) tmp).get_position (out x_pos, out y_pos);
+            ((Clutter.Actor) tmp).set_position ((x_pos / game.properties.tile_size) * tile_size,
+                                                (y_pos / game.properties.tile_size) * tile_size);
+            ((Clutter.Actor) tmp).set_size (tile_size, tile_size);
+        }
+    }
 }
diff --git a/src/properties.vala b/src/properties.vala
index 8b32249..b230893 100644
--- a/src/properties.vala
+++ b/src/properties.vala
@@ -3,6 +3,11 @@ public class Properties : Object
     public int tile_size;
     public int start_level;
 
+    public int DEFAULTGAMEDELAY = 35;
+    public int GAMEDELAY = 35;
+    public int NETDELAY = 2;
+    public int BONUSDELAY = 100;
+
     public Properties ()
     {
 


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