~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
Settings = imports.Settings;
GLib = imports.gi.GLib;
Clutter = imports.gi.Clutter;
Light = imports.Light;
Puzzle = imports.Puzzle;

var tiles = 5;
var puzzle_gen = new Puzzle.Gen(tiles);

// All lights need to be shifted down and right by half a light,
// as lights have center gravity.
function position_for_light(x, y)
{
	var p_l = {x: (x + 0.5) * Settings.theme.light[0].width + 2,
	           y: (y + 0.5) * Settings.theme.light[0].height + 2};
	
	return p_l;
}

BoardView = new GType({
	parent: Clutter.Group.type,
	name: "BoardView",
	signals: [{name: "game_won"}],
	class_init: function(klass, prototype)
	{
		function fade(out)
		{
			return function(timeline)
			{
				this.animate_with_timeline(Clutter.AnimationMode.EASE_OUT_SINE, 
			                               timeline,
				{
					opacity: out ? 0 : 255
				});
			}
		}
	
		prototype.fade_in = fade(false);
		prototype.fade_out = fade(true);
		
		function slide(out)
		{
			return function(direction, sign, timeline)
			{
				this.x = out ? 0 : (-sign) * direction * this.width;
				this.y = out ? 0 : (-sign) * !direction * this.height;
				
				this.animate_with_timeline(Clutter.AnimationMode.EASE_OUT_BOUNCE, 
					                       timeline,
				{
					x: out ? sign * direction * this.width : 0,
					y: out ? sign * !direction * this.height : 0
				});
			}
		}
		
		prototype.slide_in = slide(false);
		prototype.slide_out = slide(true);
		
		function swap(out)
		{
			return function(direction, timeline)
			{
				this.animate_with_timeline(Clutter.AnimationMode.EASE_IN_SINE, 
					                       timeline,
				{
					depth: out ? (250 * direction) : 0,
					x: 0,
					y: 0,
					opacity: out ? 0 : 255
				});
			}
		}
		
		prototype.swap_in = swap(false);
		prototype.swap_out = swap(true);
	},
	init: function(self)
	{
		// Private
		var playable = true;
		var lights = [];
		var loading_level = false;
		
		// Create a two-dimensional array of 'tiles*tiles' lights,
		// connect to their clicked signals, and display them.
		var create_lights = function()
		{
			for(var x = 0; x < tiles; x++)
			{
				lights[x] = [];
				
				for(var y = 0; y < tiles; y++)
				{
					var l = new Light.LightView();
					var loc = position_for_light(x, y);
					l.set_position(loc.x, loc.y);
					l.signal.button_press_event.connect(light_clicked, {"x":x, "y":y});
					
					lights[x][y] = l;
					self.add_actor(l);
					
					// GLib.main_context_iteration(null, false);
				}
			}
		}
		
		// Check if the game was won; if so, emit the game_won signal
		// in order to notify the Game controller.
		var check_won = function()
		{
			if(cleared())
				self.signal.game_won.emit();
		}
		
		// Callback for button_press_event from each light; user_data
		// is an object containing the coordinates of the clicked light.
		var light_clicked = function(light, event, coords)
		{
			self.light_toggle(coords.x, coords.y);
			
			return false;
		}
		
		// Returns whether or not the board is entirely 'off' (i.e. game is won)
		var cleared = function()
		{
			for(var x = 0; x < tiles; x++)
				for(var y = 0; y < tiles; y++)
					if(lights[x][y].get_state())
						return false;
			
			return true;
		}
		
		// Public
		
		// Toggle a light and those in each cardinal direction around it.
		this.light_toggle = function(x, y)
		{
			if(!playable)
				return;
			
			var timeline = null;
			
			if(!loading_level)
			{
				timeline = new Clutter.Timeline({duration: 300});
				timeline.signal.completed.connect(check_won);
			}
			
			if(x + 1 < tiles)
				lights[x + 1][y].toggle(timeline);
			if(x - 1 >= 0)
				lights[x - 1][y].toggle(timeline);
			if(y + 1 < tiles)
				lights[x][y + 1].toggle(timeline);
			if(y - 1 >= 0)
				lights[x][y - 1].toggle(timeline);

			lights[x][y].toggle(timeline);
			
			if(!loading_level)
				timeline.start();
		}
		
		// Pseudorandomly generates and sets the state of each light based on
		// a level number; hopefully this is stable between machines, but that
		// depends on GLib's PRNG stability. Also, provides some semblance of 
		// symmetry for some levels.
		this.load_level = function(level)
		{
			loading_level = true;
			
			// We *must* not have level < 1, as the following assumes
			// a nonzero, nonnegative number
			if(level < 1)
			{
				level = 1;
			}
			
			for(var x = 0; x < tiles; x++)
				for(var y = 0; y < tiles; y++)
					lights[x][y].set_state(0, 0);
			
			GLib.random_set_seed(level);
			
			// Determine the solution length (number of clicks required)
			// based on the level number.
			var solution_length = Math.floor(2 * Math.log(level) + 1);
			var sol = puzzle_gen.minimal_solution(solution_length);

			for(var x = 0; x < tiles; x++)
				for(var y = 0; y < tiles; y++)
					if(sol[tiles * y + x])
						self.light_toggle(x, y);
			
			loading_level = false;
		}
		
		// Set whether or not clicks on the gameboard should respond;
		// this is used to prevent clicks from registering while the board is
		// animating.
		this.set_playable = function(p)
		{
			playable = p;
		}
		
		// Implementation
		
		create_lights();
	}
});