[gnome-games] [same-gnome-clutter] Reorganize score-management code



commit d061e87f31d0d8a4871ff0abc6df62e61897149b
Author: Tim Horton <hortont424 gmail com>
Date:   Sun Jul 19 23:19:13 2009 -0400

    [same-gnome-clutter] Reorganize score-management code

 same-gnome-clutter/src/Board.js |   43 +++++++-------------------------------
 same-gnome-clutter/src/Score.js |   43 ++++++++++++++++++++++++++++++++++++++-
 same-gnome-clutter/src/main.js  |    2 -
 3 files changed, 50 insertions(+), 38 deletions(-)
---
diff --git a/same-gnome-clutter/src/Board.js b/same-gnome-clutter/src/Board.js
index a36c8c0..7a4ee2f 100644
--- a/same-gnome-clutter/src/Board.js
+++ b/same-gnome-clutter/src/Board.js
@@ -73,14 +73,6 @@ Board = new GType({
 			return _connected_lights(li);
 		}
 		
-		function calculate_score(n_lights)
-		{
-			if (n_lights < 3)
-				return 0;
-
-			return (n_lights - 2) * (n_lights - 2);
-		}
-		
 		function light_lights_from(li)
 		{
 			var i;
@@ -98,30 +90,6 @@ Board = new GType({
 			return cl;
 		}
 		
-		function update_score(tiles)
-		{
-			var points_awarded = calculate_score(tiles);
-			
-			if(Settings.zealous)
-			{
-				var score_text = new Score.Score();
-				score_text.animate_score(points_awarded);
-			}
-			
-			main.current_score += points_awarded;
-			
-			if(self.has_completed())
-			{
-				if(self.has_won())
-					main.current_score += 1000;
-				
-				final_score = new Score.Score();
-				final_score.animate_final_score(main.current_score);
-			}
-			
-			main.score_label.label = Seed.sprintf(_("Score: %d"), main.current_score);
-		}
-		
 		function light_entered(actor, event)
 		{
 			if(actor === last_light)
@@ -130,7 +98,7 @@ Board = new GType({
 			last_light = actor;
 			
 			var lights_lit = light_lights_from(actor).length;
-			var new_score = calculate_score(lights_lit);
+			var new_score = Score.calculate_score(lights_lit);
 			var score_string = "No points";
 			
 			if(new_score > 0)
@@ -200,6 +168,8 @@ Board = new GType({
 			
 			if(cl.length < 2)
 				return false;
+				
+			main.message_label.label = "";
 			
 			var close_timeline = new Clutter.Timeline({duration: 500});
 			
@@ -268,7 +238,10 @@ Board = new GType({
 			for(; real_x < main.size_o.columns; real_x++)
 				lights[real_x] = null;
 			
-			update_score(cl.length);
+			Score.increment_score(cl.length);
+			
+			if(self.has_completed())
+				Score.game_completed(self.has_won())
 			
 			cl = last_light = null;
 			
@@ -285,7 +258,7 @@ Board = new GType({
 			if(final_score)
 				final_score.hide_score();
 			
-			main.current_score = 0;
+			Score.set_score(0);
 			
 			all_lights = [];
 			
diff --git a/same-gnome-clutter/src/Score.js b/same-gnome-clutter/src/Score.js
index 3983d16..9a29718 100644
--- a/same-gnome-clutter/src/Score.js
+++ b/same-gnome-clutter/src/Score.js
@@ -2,8 +2,49 @@ Clutter = imports.gi.Clutter;
 Pango = imports.gi.Pango;
 main = imports.main;
 Settings = imports.Settings;
+_ = imports.gettext.gettext;
 
-Score = new GType({
+var current_score = 0;
+
+function set_score(score)
+{
+	current_score = score;
+	main.score_label.label = Seed.sprintf(_("Score: %d"), current_score);
+}
+
+function calculate_score(n_lights)
+{
+	if (n_lights < 3)
+		return 0;
+
+	return (n_lights - 2) * (n_lights - 2);
+}
+
+function increment_score(tiles)
+{
+	var points_awarded = calculate_score(tiles);
+	var new_score = current_score;
+	
+	if(Settings.zealous)
+	{
+		var score_text = new ScoreView();
+		score_text.animate_score(points_awarded);
+	}
+	
+	new_score += points_awarded;
+	
+	set_score(new_score);
+}
+
+function game_completed(won)
+{
+	set_score(current_score + 1000);
+	
+	final_score = new ScoreView();
+	final_score.animate_final_score(current_score);
+}
+
+ScoreView = new GType({
 	parent: Clutter.Group.type,
 	name: "Score",
 	init: function()
diff --git a/same-gnome-clutter/src/main.js b/same-gnome-clutter/src/main.js
index 3f1557e..bf31c2f 100755
--- a/same-gnome-clutter/src/main.js
+++ b/same-gnome-clutter/src/main.js
@@ -66,8 +66,6 @@ var score_label = b.get_object("score_label");
 
 var stage = clutter_embed.get_stage();
 
-var current_score = 0;
-
 stage.signal.hide.connect(Gtk.main_quit);
 stage.set_use_fog(false);
 



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