seed r828 - trunk/examples/same-seed



Author: hortont
Date: Mon Feb  2 06:48:03 2009
New Revision: 828
URL: http://svn.gnome.org/viewvc/seed?rev=828&view=rev

Log:
Same Seed. It works, but Clutter is broken. Or something. So uh. No animations at the moment.


Modified:
   trunk/examples/same-seed/board.js
   trunk/examples/same-seed/main.js

Modified: trunk/examples/same-seed/board.js
==============================================================================
--- trunk/examples/same-seed/board.js	(original)
+++ trunk/examples/same-seed/board.js	Mon Feb  2 06:48:03 2009
@@ -35,40 +35,45 @@
 		// Private
 		var lights = new Array();
 		var all_lights = new Array();
+		var cl, oldpicked;
 		
-		function _connected_lights(li)
+		function _light_connected_lights(li)
 		{
-			if(!li)
+			// Anything anyone can do to make this function faster
+			// will make the game snappier.
+			
+			if(!li || li.visited || li.get_closed())
 				return [ ];
 			
 			var x = li.get_light_x();
 			var y = li.get_light_y();
 			
-			if(li.visited || li.get_closed())
-				return [ ];
-			
 			li.visited = true;
 			
 			var con = [li];
 			
+			// Do all of the concatenation together for performance
+			
+			var a = [], b = [], c = [], d = [];
+			
 			if(lights[x][y+1] && (li.get_state() == lights[x][y+1].get_state()))
-				con = con.concat(_connected_lights(lights[x][y+1]));
+				a = _light_connected_lights(lights[x][y+1]);
 			
 			if(lights[x][y-1] && (li.get_state() == lights[x][y-1].get_state()))
-				con = con.concat(_connected_lights(lights[x][y-1]));
+				b = _light_connected_lights(lights[x][y-1]);
 			
 			if(lights[x+1] && (li.get_state() == lights[x+1][y].get_state()))
-				con = con.concat(_connected_lights(lights[x+1][y]));
+				c = _light_connected_lights(lights[x+1][y]);
 			
 			if(lights[x-1] && (li.get_state() == lights[x-1][y].get_state()))
-				con = con.concat(_connected_lights(lights[x-1][y]));
+				d = _light_connected_lights(lights[x-1][y]);
 			
-			return con;
+			return con.concat(a,b,c,d);
 		};
 		
-		function connected_lights(li)
+		function light_connected_lights(li)
 		{
-			for(i in all_lights)
+			for(var i in all_lights)
 			{
 				all_lights[i].visited = false;
 			}
@@ -76,7 +81,41 @@
 			if(!li.get_light_x) // We're picking something other than a light!
 				return [ li ];
 			
-			return _connected_lights(li);
+			return _light_connected_lights(li);
+		};
+		
+		function any_connected_lights(li)
+		{
+			if(!li)
+				return false;
+			
+			var x = li.get_light_x();
+			var y = li.get_light_y();
+			
+			if(li.get_closed())
+				return false;
+			
+			if(lights[x][y+1] && (li.get_state() == lights[x][y+1].get_state()))
+				return true;
+			
+			if(lights[x][y-1] && (li.get_state() == lights[x][y-1].get_state()))
+				return true;
+			
+			if(lights[x+1] && (li.get_state() == lights[x+1][y].get_state()))
+				return true;
+			
+			if(lights[x-1] && (li.get_state() == lights[x-1][y].get_state()))
+				return true;
+			
+			return false;
+		};
+		
+		function calculate_score(n_lights)
+		{
+			if (n_lights < 3)
+				return 0;
+
+			return (n_lights - 2) * (n_lights - 2);
 		};
 		
 		var mouse_moved = function (actor, event)
@@ -84,17 +123,20 @@
 			var picked = stage.get_actor_at_pos(event.motion.x,
 												event.motion.y).get_parent();
 			
-			for(i in all_lights)
+			if(picked == oldpicked)
+				return;
+			
+			for(var i in all_lights)
 			{
 				all_lights[i].opacity = 180;
 			}
 			
-			var cl = connected_lights(picked);
+			cl = light_connected_lights(picked);
 			
 			if(cl.length < 2)
 				return false;
 			
-			for(i in cl)
+			for(var i in cl)
 			{
 				cl[i].opacity = 255;
 			}
@@ -103,31 +145,55 @@
 		};
 		
 		// Public
+		this.has_completed = function ()
+		{
+			for(var i in all_lights)
+			{
+				li = all_lights[i];
+				
+				if(!li.get_closed() && any_connected_lights(li))
+					return false;
+			}
+			
+			return true;
+		};
+		
+		this.has_won = function ()
+		{
+			for(var i in all_lights)
+			{
+				li = all_lights[i];
+				
+				if(!li.get_closed())
+					return false;
+			}
+			
+			return true;
+		};
+		
 		this.get_lights = function ()
 		{
 			return lights;
-		}
+		};
 		
 		this.remove_region = function (actor, event, light)
 		{
-			var cl = connected_lights(light);
-			
 			if(cl.length < 2)
 				return false;
 			
-			for(i in cl)
+			for(var i in cl)
 			{
 				cl[i].close_tile();
 			}
 			
 			var real_x = 0;
 			
-			for(x in lights)
+			for(var x in lights)
 			{
 				var good_lights = [];
 				var bad_lights = [];
 				
-				for(y in lights[x])
+				for(var y in lights[x])
 				{
 					var li = lights[x][y];
 					
@@ -141,15 +207,44 @@
 				
 				var empty_col = true;
 				
-				for(y in lights[real_x])
+				for(var y in lights[real_x])
 				{
-					lights[real_x][y].set_light_x(real_x);
-					lights[real_x][y].set_light_y(parseInt(y,10)); //wtfstring?
-					lights[real_x][y].set_position(real_x * tile_size + offset,
-												   (tiles_h - y - 1) * tile_size + offset);
+					var li = lights[real_x][y];
+					
+					var timeline = new Clutter.Timeline.for_duration(1000);
+					var anim = new Clutter.Animation();
+					
+					anim.set_timeline(null);
+					anim.set_alpha(null);
+					anim.object = li;
+					anim.set_duration(1000);
+					anim.set_mode(Clutter.AnimationMode.EASE_IN_BOUNCE);
+					
+					var y_interval = new
+						Clutter.Interval.with_values(GObject.TYPE_INT,
+							[GObject.TYPE_INT, li.y],
+							[GObject.TYPE_INT, ((tiles_h - y - 1) * tile_size + offset)]);
+					
+					var height_interval = new
+						Clutter.Interval.with_values(GObject.TYPE_INT,
+							[GObject.TYPE_INT, li.height],
+							[GObject.TYPE_INT, 0]);
+					
+					//anim.bind_property("y", y_interval);
+					anim.bind_property("y", height_interval);
+					
+					li.set_light_x(real_x);
+					li.set_light_y(parseInt(y,10));
+					li.set_position(real_x * tile_size + offset,
+									(tiles_h - y - 1) * tile_size + offset);
 					
-					if(!lights[real_x][y].get_closed())
+					if(!li.get_closed())
 						empty_col = false;
+
+					//timeline.start();
+					//timelines.push(anim);
+					//timelines.push(timeline);
+					//timelines.push(height_interval);
 				}
 				
 				if(!empty_col)
@@ -159,6 +254,18 @@
 			for(;real_x < tiles_w; real_x++)
 				lights[real_x] = null;
 			
+			score += calculate_score(cl.length);
+			
+			Seed.print(score);
+			
+			if(board.has_completed())
+			{
+				if(board.has_won())
+					score += 1000;
+				
+				Seed.print("Done with: " + score + " points!");
+			}
+			
 			return false;
 		}
 		

Modified: trunk/examples/same-seed/main.js
==============================================================================
--- trunk/examples/same-seed/main.js	(original)
+++ trunk/examples/same-seed/main.js	Mon Feb  2 06:48:03 2009
@@ -18,6 +18,9 @@
 Seed.include("light.js");
 Seed.include("board.js");
 
+var score = 0;
+var timelines = [];
+
 var black = new Clutter.Color();
 Clutter.color_parse("Black", black);
 
@@ -28,8 +31,8 @@
 stage.color = black;
 stage.set_size((tiles_w * tile_size),(tiles_h * tile_size));
 
-colors = [load_svg("blue.svg"), load_svg("green.svg"),
-		  load_svg("red.svg"), load_svg("yellow.svg")]
+colors = [load_svg("blue.svg"), load_svg("green.svg")]//,
+//		  load_svg("red.svg"), load_svg("yellow.svg")]
 
 board = new Board();
 



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